mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Code refactoring
This commit is contained in:
parent
dfab453e6b
commit
1cf44a9fff
@ -1,5 +1,18 @@
|
||||
package com.owncloud.android.lib.common.methods;
|
||||
|
||||
public interface HttpBaseMethod {
|
||||
int execute() throws Exception;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public abstract class HttpBaseMethod {
|
||||
public abstract int execute() throws Exception;
|
||||
protected Request mRequest;
|
||||
protected Response mResponse;
|
||||
|
||||
public Request getRequest() {
|
||||
return mRequest;
|
||||
}
|
||||
|
||||
public Response getResponse() {
|
||||
return mResponse;
|
||||
}
|
||||
}
|
@ -8,19 +8,18 @@ import okhttp3.Response;
|
||||
|
||||
public class GetMethod extends HttpMethod {
|
||||
|
||||
public GetMethod(OkHttpClient okHttpClient, String url) {
|
||||
super(okHttpClient, url);
|
||||
public GetMethod(OkHttpClient okHttpClient, Request requestBase) {
|
||||
super(okHttpClient, requestBase);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int execute() throws IOException {
|
||||
final Request request =
|
||||
new Request.Builder()
|
||||
.url(mUrl)
|
||||
final Request request = mBaseRequest
|
||||
.newBuilder()
|
||||
.get()
|
||||
.build();
|
||||
|
||||
Response response = mOkHttpClient.newCall(request).execute();
|
||||
return response.code();
|
||||
mResponse = mOkHttpClient.newCall(request).execute();
|
||||
return mResponse.code();
|
||||
}
|
||||
}
|
@ -3,14 +3,15 @@ package com.owncloud.android.lib.common.methods.nonwebdav;
|
||||
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
|
||||
public abstract class HttpMethod implements HttpBaseMethod {
|
||||
public abstract class HttpMethod extends HttpBaseMethod {
|
||||
|
||||
protected OkHttpClient mOkHttpClient;
|
||||
protected String mUrl;
|
||||
protected Request mBaseRequest;
|
||||
|
||||
public HttpMethod (OkHttpClient okHttpClient, String url) {
|
||||
public HttpMethod (OkHttpClient okHttpClient, Request baseRequest) {
|
||||
mOkHttpClient = okHttpClient;
|
||||
mUrl = url;
|
||||
mBaseRequest = baseRequest;
|
||||
}
|
||||
}
|
@ -2,13 +2,16 @@ package com.owncloud.android.lib.common.methods.webdav;
|
||||
|
||||
import com.owncloud.android.lib.common.methods.HttpBaseMethod;
|
||||
|
||||
import at.bitfire.dav4android.DavOCResource;
|
||||
import at.bitfire.dav4android.DavResource;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public abstract class DavMethod implements HttpBaseMethod {
|
||||
public abstract class DavMethod extends HttpBaseMethod {
|
||||
|
||||
protected DavResource mDavResource;
|
||||
|
||||
public DavMethod(DavResource davResource) {
|
||||
mDavResource = davResource;
|
||||
public DavMethod(OkHttpClient okHttpClient, HttpUrl httpUrl) {
|
||||
mDavResource = new DavOCResource(okHttpClient, httpUrl);
|
||||
}
|
||||
}
|
@ -1,28 +1,51 @@
|
||||
package com.owncloud.android.lib.common.methods.webdav;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
import at.bitfire.dav4android.DavOCResource;
|
||||
import at.bitfire.dav4android.DavResource;
|
||||
import at.bitfire.dav4android.PropertyUtils;
|
||||
import at.bitfire.dav4android.exception.DavException;
|
||||
import at.bitfire.dav4android.exception.HttpException;
|
||||
import at.bitfire.dav4android.exception.UnauthorizedException;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public class PropfindMethod extends DavMethod {
|
||||
|
||||
private int mDepth;
|
||||
private Set<DavResource> mMembers;
|
||||
|
||||
public PropfindMethod(DavResource davResource, int depth) {
|
||||
super(davResource);
|
||||
public PropfindMethod(OkHttpClient okHttpClient, HttpUrl httpUrl, int depth) {
|
||||
super(okHttpClient, httpUrl);
|
||||
mDepth = depth;
|
||||
};
|
||||
|
||||
@Override
|
||||
public int execute() throws IOException, HttpException, DavException {
|
||||
|
||||
try {
|
||||
mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet());
|
||||
mMembers = mDavResource.getMembers();
|
||||
} catch (UnauthorizedException davException) {
|
||||
return 401;
|
||||
// Do nothing, we will use the 401 code to handle the situation
|
||||
}
|
||||
return mDavResource.getResponse().code();
|
||||
|
||||
mRequest = mDavResource.getRequest();
|
||||
mResponse = mDavResource.getResponse();
|
||||
|
||||
return mResponse.code();
|
||||
}
|
||||
|
||||
public int getDepth() {
|
||||
return mDepth;
|
||||
}
|
||||
|
||||
public Set<DavResource> getMembers() {
|
||||
return mMembers;
|
||||
}
|
||||
}
|
@ -24,8 +24,6 @@
|
||||
|
||||
package com.owncloud.android.lib.resources.files;
|
||||
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.methods.webdav.PropfindMethod;
|
||||
import com.owncloud.android.lib.common.network.RedirectionPath;
|
||||
@ -34,7 +32,8 @@ import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
import at.bitfire.dav4android.DavOCResource;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
|
||||
import okhttp3.HttpUrl;
|
||||
|
||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||
@ -80,14 +79,13 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
|
||||
try {
|
||||
|
||||
DavOCResource davOCResource = new DavOCResource(
|
||||
client.getOkHttpClient(),
|
||||
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath))
|
||||
);
|
||||
|
||||
// client.setFollowRedirects(false);
|
||||
|
||||
PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 0);
|
||||
PropfindMethod propfindMethod = new PropfindMethod(
|
||||
client.getOkHttpClient(),
|
||||
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)),
|
||||
0);
|
||||
|
||||
int status = client.executeHttpMethod(propfindMethod);
|
||||
|
||||
// if (previousFollowRedirects) {
|
||||
@ -110,8 +108,9 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
|
||||
result = isSuccess
|
||||
? new RemoteOperationResult(OK)
|
||||
: new RemoteOperationResult(false, davOCResource.getRequest(), davOCResource.getResponse());
|
||||
|
||||
: new RemoteOperationResult(
|
||||
false, propfindMethod.getRequest(), propfindMethod.getResponse()
|
||||
);
|
||||
|
||||
Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() +
|
||||
WebdavUtils.encodePath(mPath) + " targeting for " +
|
||||
|
@ -27,13 +27,15 @@ package com.owncloud.android.lib.resources.users;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
import com.owncloud.android.lib.common.methods.nonwebdav.GetMethod;
|
||||
|
||||
import okhttp3.Request;
|
||||
|
||||
|
||||
/**
|
||||
@ -63,53 +65,61 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
|
||||
@Override
|
||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||
RemoteOperationResult result = null;
|
||||
int status = -1;
|
||||
GetMethod get = null;
|
||||
|
||||
//Get the user
|
||||
try {
|
||||
get = new GetMethod(client.getBaseUri() + OCS_ROUTE);
|
||||
get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
|
||||
status = client.executeMethod(get);
|
||||
if (isSuccess(status)) {
|
||||
String response = get.getResponseBodyAsString();
|
||||
Log_OC.d(TAG, "Successful response");
|
||||
|
||||
// Parse the response
|
||||
JSONObject respJSON = new JSONObject(response);
|
||||
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
|
||||
JSONObject respData = respOCS.getJSONObject(NODE_DATA);
|
||||
final Request request = new Request.Builder()
|
||||
.url(client.getBaseUri() + OCS_ROUTE)
|
||||
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
|
||||
.build();
|
||||
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.mId = respData.getString(NODE_ID);
|
||||
userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME);
|
||||
userInfo.mEmail = respData.getString(NODE_EMAIL);
|
||||
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request);
|
||||
|
||||
// Result
|
||||
result = new RemoteOperationResult(true, get);
|
||||
// Username in result.data
|
||||
ArrayList<Object> data = new ArrayList<Object>();
|
||||
data.add(userInfo);
|
||||
result.setData(data);
|
||||
|
||||
} else {
|
||||
result = new RemoteOperationResult(false, get);
|
||||
String response = get.getResponseBodyAsString();
|
||||
Log_OC.e(TAG, "Failed response while getting user information ");
|
||||
if (response != null) {
|
||||
Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
|
||||
} else {
|
||||
Log_OC.e(TAG, "*** status code: " + status);
|
||||
}
|
||||
}
|
||||
// client.executeHttpMethod(getMethod);
|
||||
//
|
||||
// get = new GetMethod(client.getBaseUri() + OCS_ROUTE);
|
||||
// get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
|
||||
// status = client.executeMethod(get);
|
||||
// if (isSuccess(status)) {
|
||||
// String response = get.getResponseBodyAsString();
|
||||
// Log_OC.d(TAG, "Successful response");
|
||||
//
|
||||
// // Parse the response
|
||||
// JSONObject respJSON = new JSONObject(response);
|
||||
// JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
|
||||
// JSONObject respData = respOCS.getJSONObject(NODE_DATA);
|
||||
//
|
||||
// UserInfo userInfo = new UserInfo();
|
||||
// userInfo.mId = respData.getString(NODE_ID);
|
||||
// userInfo.mDisplayName = respData.getString(NODE_DISPLAY_NAME);
|
||||
// userInfo.mEmail = respData.getString(NODE_EMAIL);
|
||||
//
|
||||
// // Result
|
||||
// result = new RemoteOperationResult(true, get);
|
||||
// // Username in result.data
|
||||
// ArrayList<Object> data = new ArrayList<Object>();
|
||||
// data.add(userInfo);
|
||||
// result.setData(data);
|
||||
//
|
||||
// } else {
|
||||
// result = new RemoteOperationResult(false, get);
|
||||
// String response = get.getResponseBodyAsString();
|
||||
// Log_OC.e(TAG, "Failed response while getting user information ");
|
||||
// if (response != null) {
|
||||
// Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
|
||||
// } else {
|
||||
// Log_OC.e(TAG, "*** status code: " + status);
|
||||
// }
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
result = new RemoteOperationResult(e);
|
||||
Log_OC.e(TAG, "Exception while getting OC user information", e);
|
||||
|
||||
} finally {
|
||||
if (get != null) {
|
||||
get.releaseConnection();
|
||||
}
|
||||
// if (get != null) {
|
||||
// get.releaseConnection();
|
||||
// }
|
||||
}
|
||||
|
||||
return result;
|
||||
|
Loading…
x
Reference in New Issue
Block a user