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