diff --git a/src/com/owncloud/android/lib/common/methods/HttpBaseMethod.java b/src/com/owncloud/android/lib/common/methods/HttpBaseMethod.java index c153d086..cac3c62c 100644 --- a/src/com/owncloud/android/lib/common/methods/HttpBaseMethod.java +++ b/src/com/owncloud/android/lib/common/methods/HttpBaseMethod.java @@ -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; + } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java b/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java index 21dd6911..11fc49c8 100644 --- a/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java +++ b/src/com/owncloud/android/lib/common/methods/nonwebdav/GetMethod.java @@ -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(); } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/methods/nonwebdav/HttpMethod.java b/src/com/owncloud/android/lib/common/methods/nonwebdav/HttpMethod.java index 3a70e6a9..4c6f5e2c 100644 --- a/src/com/owncloud/android/lib/common/methods/nonwebdav/HttpMethod.java +++ b/src/com/owncloud/android/lib/common/methods/nonwebdav/HttpMethod.java @@ -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; } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/methods/webdav/DavMethod.java b/src/com/owncloud/android/lib/common/methods/webdav/DavMethod.java index 5c95f441..195222fc 100644 --- a/src/com/owncloud/android/lib/common/methods/webdav/DavMethod.java +++ b/src/com/owncloud/android/lib/common/methods/webdav/DavMethod.java @@ -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); } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/methods/webdav/PropfindMethod.java b/src/com/owncloud/android/lib/common/methods/webdav/PropfindMethod.java index 36a7bb2f..a21cc494 100644 --- a/src/com/owncloud/android/lib/common/methods/webdav/PropfindMethod.java +++ b/src/com/owncloud/android/lib/common/methods/webdav/PropfindMethod.java @@ -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 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 getMembers() { + return mMembers; } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java index acf436bd..1dd82b22 100644 --- a/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java +++ b/src/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java @@ -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 " + diff --git a/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java index 12e0486b..3639d5fa 100644 --- a/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java +++ b/src/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.java @@ -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 data = new ArrayList(); - 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 data = new ArrayList(); +// 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;