1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Move OkHttp and dav4android dependencies to the new wrapper

This commit is contained in:
davigonz 2018-06-08 14:43:04 +02:00
parent 08777c0265
commit bcc8a78bcf
23 changed files with 291 additions and 293 deletions

View File

@ -310,21 +310,21 @@ public class OwnCloudClient extends HttpClient {
return status; return status;
} }
public Response executeHttpMethod (HttpBaseMethod method) throws Exception { public int executeHttpMethod (HttpBaseMethod method) throws Exception {
boolean repeatWithFreshCredentials; boolean repeatWithFreshCredentials;
int repeatCounter = 0; int repeatCounter = 0;
Response response; int status;
do { do {
response = method.execute(); status = method.execute();
repeatWithFreshCredentials = checkUnauthorizedAccess(response.code(), repeatCounter); repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
if (repeatWithFreshCredentials) { if (repeatWithFreshCredentials) {
repeatCounter++; repeatCounter++;
} }
} while (repeatWithFreshCredentials); } while (repeatWithFreshCredentials);
return response; return status;
} }
private void checkFirstRedirection(HttpMethod method) { private void checkFirstRedirection(HttpMethod method) {

View File

@ -24,8 +24,15 @@
package com.owncloud.android.lib.common.http; package com.owncloud.android.lib.common.http;
import com.owncloud.android.lib.common.utils.Log_OC;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Headers;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.internal.http2.Header;
/** /**
* Wrapper to perform http calls transparently by using: * Wrapper to perform http calls transparently by using:
@ -35,10 +42,33 @@ import okhttp3.Response;
* @author David González Verdugo * @author David González Verdugo
*/ */
public abstract class HttpBaseMethod { public abstract class HttpBaseMethod {
public abstract Response execute() throws Exception; public abstract int execute() throws Exception;
protected Request mRequest; protected Response mResponse;
private static final String TAG = HttpBaseMethod.class.getSimpleName();
public Request getRequest() { // Status
return mRequest; public int getStatusCode() {
return mResponse.code();
}
public String getStatusMessage() {
return mResponse.message();
}
// Response
public String getResponseBodyAsString() throws IOException {
return mResponse.body().string();
}
public InputStream getResponseAsStream() {
return mResponse.body().byteStream();
}
public Headers getResponseHeaders() {
return mResponse.headers();
}
public String getResponseHeader(String headerName) {
return mResponse.header(headerName);
} }
} }

View File

@ -34,17 +34,19 @@ import okhttp3.Response;
*/ */
public class DeleteMethod extends HttpMethod{ public class DeleteMethod extends HttpMethod{
public DeleteMethod(OkHttpClient okHttpClient, Request baseRequest) { public DeleteMethod(OkHttpClient okHttpClient, String httpUrl) {
super(okHttpClient, baseRequest); super(okHttpClient, httpUrl);
} }
@Override @Override
public Response execute() throws Exception { public int execute() throws Exception {
mRequest = mBaseRequest Request request = mRequest
.newBuilder() .newBuilder()
.delete() .delete()
.build(); .build();
return mOkHttpClient.newCall(mRequest).execute(); mResponse = mOkHttpClient.newCall(request).execute();
return mResponse.code();
} }
} }

View File

@ -26,9 +26,9 @@ package com.owncloud.android.lib.common.http.nonwebdav;
import java.io.IOException; import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
/** /**
* OkHttp get calls wrapper * OkHttp get calls wrapper
@ -36,17 +36,23 @@ import okhttp3.Response;
*/ */
public class GetMethod extends HttpMethod { public class GetMethod extends HttpMethod {
public GetMethod(OkHttpClient okHttpClient, Request baseRequest) { public GetMethod(OkHttpClient okHttpClient, String httpUrl) {
super(okHttpClient, baseRequest); super(okHttpClient, httpUrl);
}
public GetMethod(OkHttpClient okHttpClient, HttpUrl httpUrl) {
super(okHttpClient, httpUrl);
} }
@Override @Override
public Response execute() throws IOException { public int execute() throws IOException {
mRequest = mBaseRequest Request request = mRequest
.newBuilder() .newBuilder()
.get() .get()
.build(); .build();
return mOkHttpClient.newCall(mRequest).execute(); mResponse = mOkHttpClient.newCall(request).execute();
return mResponse.code();
} }
} }

View File

@ -26,20 +26,42 @@ package com.owncloud.android.lib.common.http.nonwebdav;
import com.owncloud.android.lib.common.http.HttpBaseMethod; import com.owncloud.android.lib.common.http.HttpBaseMethod;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
/** /**
* Wrapper to perform OkHttp calls * Wrapper to perform OkHttp calls
*
* @author David González Verdugo * @author David González Verdugo
*/ */
public abstract class HttpMethod extends HttpBaseMethod { public abstract class HttpMethod extends HttpBaseMethod {
protected OkHttpClient mOkHttpClient; protected OkHttpClient mOkHttpClient;
protected Request mBaseRequest; protected Request mRequest;
public HttpMethod (OkHttpClient okHttpClient, Request baseRequest) { public HttpMethod(OkHttpClient okHttpClient, String httpUrl) {
mOkHttpClient = okHttpClient; mOkHttpClient = okHttpClient;
mBaseRequest = baseRequest; mRequest = new Request.Builder()
.url(httpUrl)
.build();
}
public HttpMethod(OkHttpClient okHttpClient, HttpUrl httpUrl) {
mOkHttpClient = okHttpClient;
mRequest = new Request.Builder()
.url(httpUrl)
.build();
}
// Request headers
public void addRequestHeader(String name, String value) {
mRequest.newBuilder()
.addHeader(name, value)
.build();
}
public void setRequestHeader(String name, String value){
mRequest.newBuilder()
.header(name, value);
} }
} }

View File

@ -37,18 +37,20 @@ public class PostMethod extends HttpMethod {
private RequestBody mRequestBody; private RequestBody mRequestBody;
public PostMethod(OkHttpClient okHttpClient, Request baseRequest, RequestBody requestBody){ public PostMethod(OkHttpClient okHttpClient, String httpUrl, RequestBody requestBody){
super(okHttpClient, baseRequest); super(okHttpClient, httpUrl);
mRequestBody = requestBody; mRequestBody = requestBody;
} }
@Override @Override
public Response execute() throws Exception { public int execute() throws Exception {
mRequest = mBaseRequest Request request = mRequest
.newBuilder() .newBuilder()
.post(mRequestBody) .post(mRequestBody)
.build(); .build();
return mOkHttpClient.newCall(mRequest).execute(); mResponse = mOkHttpClient.newCall(request).execute();
return mResponse.code();
} }
} }

View File

@ -33,18 +33,20 @@ public class PutMethod extends HttpMethod{
private RequestBody mRequestBody; private RequestBody mRequestBody;
public PutMethod(OkHttpClient okHttpClient, Request baseRequest, RequestBody requestBody){ public PutMethod(OkHttpClient okHttpClient, String httpUrl, RequestBody requestBody){
super(okHttpClient, baseRequest); super(okHttpClient, httpUrl);
mRequestBody = requestBody; mRequestBody = requestBody;
} }
@Override @Override
public Response execute() throws Exception { public int execute() throws Exception {
mRequest = mBaseRequest Request request = mRequest
.newBuilder() .newBuilder()
.put(mRequestBody) .put(mRequestBody)
.build(); .build();
return mOkHttpClient.newCall(mRequest).execute(); mResponse = mOkHttpClient.newCall(request).execute();
return mResponse.code();
} }
} }

View File

@ -51,7 +51,7 @@ public class PropfindMethod extends DavMethod {
}; };
@Override @Override
public Response 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(); mMembers = mDavResource.getMembers();
@ -59,8 +59,9 @@ public class PropfindMethod extends DavMethod {
// Do nothing, we will use the 401 code to handle the situation // Do nothing, we will use the 401 code to handle the situation
} }
mRequest = mDavResource.getRequest(); mResponse = mDavResource.getResponse();
return mDavResource.getResponse();
return mResponse.code();
} }
public int getDepth() { public int getDepth() {

View File

@ -32,6 +32,8 @@ import java.util.Locale;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.http.HttpBaseMethod;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.jackrabbit.webdav.property.DavPropertyName; import org.apache.jackrabbit.webdav.property.DavPropertyName;
@ -173,19 +175,19 @@ public class WebdavUtils {
/** /**
* *
* @param response from which to get the etag * @param httpBaseMethod from which to get the etag
* @return etag from response * @return etag from response
*/ */
public static String getEtagFromResponse(Response response) { public static String getEtagFromResponse(HttpBaseMethod httpBaseMethod) {
String eTag = response.header("OC-ETag"); String eTag = httpBaseMethod.getResponseHeader("OC-ETag");
if (eTag == null) { if (eTag == null) {
eTag = response.header("oc-etag"); eTag = httpBaseMethod.getResponseHeader("oc-etag");
} }
if (eTag == null) { if (eTag == null) {
eTag = response.header("ETag"); eTag = httpBaseMethod.getResponseHeader("ETag");
} }
if (eTag == null) { if (eTag == null) {
eTag = response.header("etag"); eTag = httpBaseMethod.getResponseHeader("etag");
} }
String result = ""; String result = "";
if (eTag != null) { if (eTag != null) {

View File

@ -28,6 +28,7 @@ import android.accounts.Account;
import android.accounts.AccountsException; import android.accounts.AccountsException;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.http.HttpBaseMethod;
import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
@ -54,8 +55,6 @@ import java.util.Map;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import okhttp3.Headers; import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.Response;
/** /**
@ -226,6 +225,7 @@ public class RemoteOperationResult implements Serializable {
* @param httpMethod HTTP/DAV method already executed which response will be examined to interpret the * @param httpMethod HTTP/DAV method already executed which response will be examined to interpret the
* result. * result.
*/ */
// TODO Delete this
public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException { public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException {
// this( // this(
// success, // success,
@ -279,20 +279,19 @@ public class RemoteOperationResult implements Serializable {
* will depend on the HTTP code and HTTP response headers received. In some cases will inspect also the * will depend on the HTTP code and HTTP response headers received. In some cases will inspect also the
* response body * response body
* *
* @param success * @param httpMethod
* @param request
* @param response
* @throws IOException * @throws IOException
*/ */
public RemoteOperationResult(boolean success, Request request, Response response) throws IOException { public RemoteOperationResult(HttpBaseMethod httpMethod) throws IOException {
this(success, response.code(), HttpStatus.getStatusText(response.code()), response.headers()); this(httpMethod.getStatusCode(),
httpMethod.getStatusMessage(),
httpMethod.getResponseHeaders()
);
// TODO success parameter might not be needed
if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400 if (mHttpCode == HttpStatus.SC_BAD_REQUEST) { // 400
String bodyResponse = httpMethod.getResponseBodyAsString();
String bodyResponse = response.body().string();
// do not get for other HTTP codes!; could not be available // do not get for other HTTP codes!; could not be available
if (bodyResponse != null && bodyResponse.length() > 0) { if (bodyResponse != null && bodyResponse.length() > 0) {
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser(); InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser();
@ -311,16 +310,22 @@ public class RemoteOperationResult implements Serializable {
// before // before
switch (mHttpCode) { switch (mHttpCode) {
case HttpStatus.SC_FORBIDDEN: case HttpStatus.SC_FORBIDDEN:
// TODO parseErrorMessageAndSetCode(
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_FORBIDDEN); httpMethod.getResponseBodyAsString(),
ResultCode.SPECIFIC_FORBIDDEN
);
break; break;
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
// TODO parseErrorMessageAndSetCode(
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE); httpMethod.getResponseBodyAsString(),
ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE
);
break; break;
case HttpStatus.SC_SERVICE_UNAVAILABLE: case HttpStatus.SC_SERVICE_UNAVAILABLE:
// TODO parseErrorMessageAndSetCode(
// parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE); httpMethod.getResponseBodyAsString(),
ResultCode.SPECIFIC_SERVICE_UNAVAILABLE
);
break; break;
default: default:
break; break;
@ -356,6 +361,33 @@ public class RemoteOperationResult implements Serializable {
} }
} }
/**
* Parse the error message included in the body response, if any, and set the specific result
* code
*
* @param bodyResponse okHttp response body
* @param resultCode our own custom result code
* @throws IOException
*/
private void parseErrorMessageAndSetCode(String bodyResponse, ResultCode resultCode) {
if (bodyResponse != null && bodyResponse.length() > 0) {
InputStream is = new ByteArrayInputStream(bodyResponse.getBytes());
ErrorMessageParser xmlParser = new ErrorMessageParser();
try {
String errorMessage = xmlParser.parseXMLResponse(is);
if (errorMessage != "" && errorMessage != null) {
mCode = resultCode;
mHttpPhrase = errorMessage;
}
} catch (Exception e) {
Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage());
// mCode stays as set in this(success, httpCode, headers)
}
}
}
/** /**
* Public constructor from separate elements of an HTTP or DAV response. * Public constructor from separate elements of an HTTP or DAV response.
* *
@ -367,13 +399,12 @@ public class RemoteOperationResult implements Serializable {
* *
* Determines a {@link ResultCode} depending on the HTTP code and HTTP response headers received. * Determines a {@link ResultCode} depending on the HTTP code and HTTP response headers received.
* *
* @param success The operation was considered successful or not.
* @param httpCode HTTP status code returned by an HTTP/DAV method. * @param httpCode HTTP status code returned by an HTTP/DAV method.
* @param httpPhrase HTTP status line phrase returned by an HTTP/DAV method * @param httpPhrase HTTP status line phrase returned by an HTTP/DAV method
* @param headers HTTP response header returned by an HTTP/DAV method * @param headers HTTP response header returned by an HTTP/DAV method
*/ */
public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Headers headers) { public RemoteOperationResult(int httpCode, String httpPhrase, Headers headers) {
this(success, httpCode, httpPhrase); this(httpCode, httpPhrase);
if (headers != null) { if (headers != null) {
for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) { for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) {
if ("location".equals(header.getKey().toLowerCase())) { if ("location".equals(header.getKey().toLowerCase())) {
@ -390,60 +421,20 @@ public class RemoteOperationResult implements Serializable {
mCode = com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.UNAUTHORIZED; mCode = com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.UNAUTHORIZED;
} }
} }
//
// /**
// * Public constructor from separate elements of an HTTP or DAV response.
// *
// * To be used when the result needs to be interpreted from HTTP response elements that could come from
// * different requests (WARNING: black magic, try to avoid).
// *
// * If all the fields come from the same HTTP/DAV response, {@link #RemoteOperationResult(boolean, HttpMethod)}
// * should be used instead.
// *
// * Determines a {@link ResultCode} depending on the HTTP code and HTTP response headers received.
// *
// * @param success The operation was considered successful or not.
// * @param httpCode HTTP status code returned by an HTTP/DAV method.
// * @param httpPhrase HTTP status line phrase returned by an HTTP/DAV method
// * @param httpHeaders HTTP response header returned by an HTTP/DAV method
// */
// public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Header[] httpHeaders) {
// this(success, httpCode, httpPhrase);
// if (httpHeaders != null) {
// for (Header httpHeader : httpHeaders) {
// if ("location".equals(httpHeader.getName().toLowerCase())) {
// mRedirectedLocation = httpHeader.getValue();
// continue;
// }
// if ("www-authenticate".equals(httpHeader.getName().toLowerCase())) {
// mAuthenticate.add(httpHeader.getValue().toLowerCase());
// }
// }
// }
// if (isIdPRedirection()) {
// // overrides default ResultCode.UNKNOWN
// mCode = ResultCode.UNAUTHORIZED; // overrides default ResultCode.UNKNOWN
// }
// }
/** /**
* Private constructor for results built interpreting a HTTP or DAV response. * Private constructor for results built interpreting a HTTP or DAV response.
* *
* Determines a {@link ResultCode} depending of the type of the exception. * Determines a {@link ResultCode} depending of the type of the exception.
* *
* @param success Operation was successful or not.
* @param httpCode HTTP status code returned by the HTTP/DAV method. * @param httpCode HTTP status code returned by the HTTP/DAV method.
* @param httpPhrase HTTP status line phrase returned by the HTTP/DAV method * @param httpPhrase HTTP status line phrase returned by the HTTP/DAV method
*/ */
private RemoteOperationResult(boolean success, int httpCode, String httpPhrase) { private RemoteOperationResult(int httpCode, String httpPhrase) {
mSuccess = success;
mHttpCode = httpCode; mHttpCode = httpCode;
mHttpPhrase = httpPhrase; mHttpPhrase = httpPhrase;
if (success) { if (httpCode > 0) {
mCode = ResultCode.OK;
} else if (httpCode > 0) {
switch (httpCode) { switch (httpCode) {
case HttpStatus.SC_UNAUTHORIZED: // 401 case HttpStatus.SC_UNAUTHORIZED: // 401
mCode = ResultCode.UNAUTHORIZED; mCode = ResultCode.UNAUTHORIZED;
@ -668,5 +659,4 @@ public class RemoteOperationResult implements Serializable {
public void setLastPermanentLocation(String lastPermanentLocation) { public void setLastPermanentLocation(String lastPermanentLocation) {
mLastPermanentLocation = lastPermanentLocation; mLastPermanentLocation = lastPermanentLocation;
} }
}
}

View File

@ -25,6 +25,7 @@
package com.owncloud.android.lib.resources.files; package com.owncloud.android.lib.resources.files;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.webdav.PropfindMethod; import com.owncloud.android.lib.common.http.webdav.PropfindMethod;
import com.owncloud.android.lib.common.network.RedirectionPath; import com.owncloud.android.lib.common.network.RedirectionPath;
import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.network.WebdavUtils;
@ -32,10 +33,7 @@ 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 org.apache.commons.httpclient.HttpStatus;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
@ -43,6 +41,7 @@ import static com.owncloud.android.lib.common.operations.RemoteOperationResult.R
* Operation to check the existence or absence of a path in a remote server. * Operation to check the existence or absence of a path in a remote server.
* *
* @author David A. Velasco * @author David A. Velasco
* @author David GonzálezVerdugo
*/ */
public class ExistenceCheckRemoteOperation extends RemoteOperation { public class ExistenceCheckRemoteOperation extends RemoteOperation {
@ -81,13 +80,12 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
try { try {
// client.setFollowRedirects(false); // client.setFollowRedirects(false);
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
client.getOkHttpClient(), client.getOkHttpClient(),
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)), HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)),
0); 0);
Response response = client.executeHttpMethod(propfindMethod); int status = client.executeHttpMethod(propfindMethod);
// if (previousFollowRedirects) { // if (previousFollowRedirects) {
// mRedirectionPath = client.followRedirection(propfind); // mRedirectionPath = client.followRedirection(propfind);
@ -102,22 +100,15 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
* 404 NOT FOUND: path doesn't exist, * 404 NOT FOUND: path doesn't exist,
* 207 MULTI_STATUS: path exists. * 207 MULTI_STATUS: path exists.
*/ */
boolean isSuccess = ((response.code() == HttpStatus.SC_OK
|| response.code() == HttpStatus.SC_MULTI_STATUS)
&& !mSuccessIfAbsent)
|| (response.code() == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent)
|| (response.code() == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
result = isSuccess result = isSuccess(status) ?
? new RemoteOperationResult(OK) new RemoteOperationResult(OK) :
: new RemoteOperationResult( new RemoteOperationResult(propfindMethod);
false, propfindMethod.getRequest(), response
);
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 " +
(mSuccessIfAbsent ? " absence " : " existence ") + (mSuccessIfAbsent ? " absence " : " existence ") +
"finished with HTTP status " + response.code() + (!isSuccess?"(FAIL)":"")); "finished with HTTP status " + status + (!isSuccess(status)?"(FAIL)":""));
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);
@ -147,4 +138,10 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
public boolean wasRedirected() { public boolean wasRedirected() {
return (mRedirectionPath != null && mRedirectionPath.getRedirectionsCount() > 0); return (mRedirectionPath != null && mRedirectionPath.getRedirectionsCount() > 0);
} }
private boolean isSuccess(int status) {
return ((status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS) && !mSuccessIfAbsent)
|| (status == HttpConstants.HTTP_MULTI_STATUS && !mSuccessIfAbsent)
|| (status == HttpConstants.HTTP_NOT_FOUND && mSuccessIfAbsent);
}
} }

View File

@ -36,7 +36,6 @@ import java.util.ArrayList;
import at.bitfire.dav4android.DavResource; import at.bitfire.dav4android.DavResource;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
@ -79,12 +78,9 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath)), HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
1); 1);
Response response = client.executeHttpMethod(propfindMethod); int status = client.executeHttpMethod(propfindMethod);
boolean isSuccess = (response.code() == HttpConstants.HTTP_MULTI_STATUS if (isSuccess(status)) {
|| response.code() == HttpConstants.HTTP_OK);
if (isSuccess) {
ArrayList<Object> mFolderAndFiles = new ArrayList<>(); ArrayList<Object> mFolderAndFiles = new ArrayList<>();
@ -104,13 +100,8 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
result.setData(mFolderAndFiles); result.setData(mFolderAndFiles);
} else { } else {
// synchronization failed // synchronization failed
result = new RemoteOperationResult( result = new RemoteOperationResult(propfindMethod);
false,
propfindMethod.getRequest(),
response
);
} }
} catch (Exception e) { } catch (Exception e) {
@ -129,4 +120,9 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
} }
return result; return result;
} }
private boolean isSuccess(int status) {
return status == HttpConstants.HTTP_MULTI_STATUS ||
status == HttpConstants.HTTP_OK;
}
} }

View File

@ -42,8 +42,6 @@ import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import okhttp3.FormBody; import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
/** /**
* Creates a new share. This allows sharing with a user or group or as a link. * Creates a new share. This allows sharing with a user or group or as a link.
@ -200,17 +198,6 @@ public class CreateRemoteShareOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.header("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8")
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
FormBody.Builder formBodyBuilder = new FormBody.Builder() FormBody.Builder formBodyBuilder = new FormBody.Builder()
.add(PARAM_PATH, mRemoteFilePath) .add(PARAM_PATH, mRemoteFilePath)
.add(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())) .add(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue()))
@ -240,11 +227,18 @@ public class CreateRemoteShareOperation extends RemoteOperation {
FormBody formBody = formBodyBuilder.build(); FormBody formBody = formBodyBuilder.build();
PostMethod postMethod = new PostMethod(client.getOkHttpClient(), request, formBody); Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
Response response = client.executeHttpMethod(postMethod); PostMethod postMethod = new PostMethod(client.getOkHttpClient(),
uriBuilder.build().toString(), formBody);
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
if (isSuccess(response.code())) { int status = client.executeHttpMethod(postMethod);
if (isSuccess(status)) {
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
@ -252,7 +246,7 @@ public class CreateRemoteShareOperation extends RemoteOperation {
parser.setOneOrMoreSharesRequired(true); parser.setOneOrMoreSharesRequired(true);
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response.body().string()); result = parser.parse(postMethod.getResponseBodyAsString());
if (result.isSuccess() && mGetShareDetails) { if (result.isSuccess() && mGetShareDetails) {
@ -266,7 +260,7 @@ public class CreateRemoteShareOperation extends RemoteOperation {
} }
} else { } else {
result = new RemoteOperationResult(false, postMethod.getRequest(), response); result = new RemoteOperationResult(postMethod);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -35,9 +35,6 @@ 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 okhttp3.Request;
import okhttp3.Response;
/** /**
* Get the data about a Share resource, known its remote ID. * Get the data about a Share resource, known its remote ID.
* *
@ -66,15 +63,12 @@ public class GetRemoteShareOperation extends RemoteOperation {
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
final Request request = new Request.Builder() GetMethod getMethod = new GetMethod(client.getOkHttpClient(),
.url(uriBuilder.build().toString()) uriBuilder.build().toString());
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.build(); int status = client.executeHttpMethod(getMethod);
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); if (isSuccess(status)) {
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
@ -82,10 +76,10 @@ public class GetRemoteShareOperation extends RemoteOperation {
parser.setOneOrMoreSharesRequired(true); parser.setOneOrMoreSharesRequired(true);
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response.body().string()); result = parser.parse(getMethod.getResponseBodyAsString());
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -42,9 +42,6 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/** /**
@ -133,19 +130,17 @@ public class GetRemoteShareesOperation extends RemoteOperation{
uriBuilder.appendQueryParameter(PARAM_PAGE, String.valueOf(mPage)); uriBuilder.appendQueryParameter(PARAM_PAGE, String.valueOf(mPage));
uriBuilder.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(mPerPage)); uriBuilder.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(mPerPage));
Request request = new Request.Builder() GetMethod getMethod = new GetMethod(client.getOkHttpClient(), uriBuilder.build().toString());
.url(uriBuilder.build().toString()) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); int status = client.executeHttpMethod(getMethod);
Response response = client.executeHttpMethod(getMethod); String response = getMethod.getResponseBodyAsString();
if(isSuccess(response.code())) { if(isSuccess(status)) {
Log_OC.d(TAG, "Successful response: " + response); Log_OC.d(TAG, "Successful response: " + response);
// Parse the response // Parse the response
JSONObject respJSON = new JSONObject(response.body().string()); JSONObject respJSON = new JSONObject(response);
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
JSONObject respData = respOCS.getJSONObject(NODE_DATA); JSONObject respData = respOCS.getJSONObject(NODE_DATA);
JSONObject respExact = respData.getJSONObject(NODE_EXACT); JSONObject respExact = respData.getJSONObject(NODE_EXACT);
@ -179,12 +174,12 @@ public class GetRemoteShareesOperation extends RemoteOperation{
Log_OC.d(TAG, "*** Get Users or groups completed " ); Log_OC.d(TAG, "*** Get Users or groups completed " );
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
Log_OC.e(TAG, "Failed response while getting users/groups from the server "); Log_OC.e(TAG, "Failed response while getting users/groups from the server ");
if (response != null) { if (response != null) {
Log_OC.e(TAG, "*** status code: " + response.code() + "; response message: " + response); Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response);
} else { } else {
Log_OC.e(TAG, "*** status code: " + response.code()); Log_OC.e(TAG, "*** status code: " + status);
} }
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -29,16 +29,14 @@ package com.owncloud.android.lib.resources.shares;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.HttpConstants;
import com.owncloud.android.lib.common.http.nonwebdav.GetMethod; import com.owncloud.android.lib.common.http.nonwebdav.GetMethod;
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 okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
/** /**
* Provide a list shares for a specific file. * Provide a list shares for a specific file.
@ -95,29 +93,25 @@ public class GetRemoteSharesForFileOperation extends RemoteOperation {
httpBuilder.addQueryParameter(PARAM_RESHARES, String.valueOf(mReshares)); httpBuilder.addQueryParameter(PARAM_RESHARES, String.valueOf(mReshares));
httpBuilder.addQueryParameter(PARAM_SUBFILES, String.valueOf(mSubfiles)); httpBuilder.addQueryParameter(PARAM_SUBFILES, String.valueOf(mSubfiles));
Request request = new Request.Builder() GetMethod getMethod = new GetMethod(client.getOkHttpClient(), httpBuilder.build());
.url(httpBuilder.build()) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); int status = client.executeHttpMethod(getMethod);
Response response = client.executeHttpMethod(getMethod); if (isSuccess(status)) {
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response.body().string()); result = parser.parse(getMethod.getResponseBodyAsString());
if (result.isSuccess()) { if (result.isSuccess()) {
Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); Log_OC.d(TAG, "Got " + result.getData().size() + " shares");
} }
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
} }
} catch (Exception e) { } catch (Exception e) {
result = new RemoteOperationResult(e); result = new RemoteOperationResult(e);

View File

@ -35,7 +35,6 @@ 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 okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
/** /**
* Get the data from the server about ALL the known shares owned by the requester. * Get the data from the server about ALL the known shares owned by the requester.
@ -58,16 +57,13 @@ public class GetRemoteSharesOperation extends RemoteOperation {
Uri.Builder uriBuilder = requestUri.buildUpon(); Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
Request request = new Request.Builder() GetMethod getMethod = new GetMethod(client.getOkHttpClient(),
.url(uriBuilder.build().toString()) uriBuilder.build().toString());
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); int status = client.executeHttpMethod(getMethod);
Response response = client.executeHttpMethod(getMethod); if (isSuccess(status)) {
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
@ -75,9 +71,9 @@ public class GetRemoteSharesOperation extends RemoteOperation {
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response.body().string()); result = parser.parse(getMethod.getResponseBodyAsString());
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -37,7 +37,6 @@ 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 okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
/** /**
* Remove a share * Remove a share
@ -68,34 +67,29 @@ public class RemoveRemoteShareOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
String id = "/" + String.valueOf(mRemoteShareId);
Uri requestUri = client.getBaseUri(); Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon(); Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH);
uriBuilder.appendEncodedPath(String.valueOf(id)); uriBuilder.appendEncodedPath(String.valueOf(mRemoteShareId));
Request request = new Request.Builder() DeleteMethod deleteMethod = new DeleteMethod(client.getOkHttpClient(),
.url(uriBuilder.build().toString()) uriBuilder.build().toString());
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.build();
DeleteMethod deleteMethod = new DeleteMethod(client.getOkHttpClient(), request); int status = client.executeHttpMethod(deleteMethod);
Response response = client.executeHttpMethod(deleteMethod); if (isSuccess(status)) {
if (isSuccess(response.code())) {
// Parse xml response and obtain the list of shares // Parse xml response and obtain the list of shares
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
); );
result = parser.parse(response.body().string()); result = parser.parse(deleteMethod.getResponseBodyAsString());
Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.getLogMessage()); Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.getLogMessage());
} else { } else {
result = new RemoteOperationResult(false, deleteMethod.getRequest(), response); result = new RemoteOperationResult(deleteMethod);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -41,7 +41,6 @@ import java.util.Locale;
import okhttp3.FormBody; import okhttp3.FormBody;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
/** /**
@ -169,18 +168,6 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
Request request = new Request.Builder()
.url(uriBuilder.build().toString())
.header("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8")
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
FormBody.Builder formBodyBuilder = new FormBody.Builder(); FormBody.Builder formBodyBuilder = new FormBody.Builder();
// Parameters to update // Parameters to update
@ -212,23 +199,32 @@ public class UpdateRemoteShareOperation extends RemoteOperation {
formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions));
} }
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon();
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1));
uriBuilder.appendEncodedPath(Long.toString(mRemoteId));
FormBody formBody = formBodyBuilder.build(); FormBody formBody = formBodyBuilder.build();
PutMethod putMethod = new PutMethod(client.getOkHttpClient(), request, formBody); PutMethod putMethod = new PutMethod(client.getOkHttpClient(),
uriBuilder.build().toString(), formBody);
putMethod.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8");
putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
Response response = client.executeHttpMethod(putMethod); int status = client.executeHttpMethod(putMethod);
if (isSuccess(response.code())) { if (isSuccess(status)) {
// Parse xml response // Parse xml response
ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
new ShareXMLParser() new ShareXMLParser()
); );
parser.setOwnCloudVersion(client.getOwnCloudVersion()); parser.setOwnCloudVersion(client.getOwnCloudVersion());
parser.setServerBaseUri(client.getBaseUri()); parser.setServerBaseUri(client.getBaseUri());
result = parser.parse(response.body().string()); result = parser.parse(putMethod.getResponseBodyAsString());
} else { } else {
result = new RemoteOperationResult(false, putMethod.getRequest(), response); result = new RemoteOperationResult(putMethod);
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -40,9 +40,6 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/** /**
@ -133,20 +130,17 @@ public class GetRemoteCapabilitiesOperation extends RemoteOperation {
String url = uriBuilder.build().toString(); String url = uriBuilder.build().toString();
final Request request = new Request.Builder() GetMethod getMethod = new GetMethod(client.getOkHttpClient(), url);
.url(url) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); int status = client.executeHttpMethod(getMethod);
final Response response = client.executeHttpMethod(getMethod); String response = getMethod.getResponseBodyAsString();
if(isSuccess(status)) {
if(isSuccess(response.code())) {
Log_OC.d(TAG, "Successful response: " + response); Log_OC.d(TAG, "Successful response: " + response);
// Parse the response // Parse the response
JSONObject respJSON = new JSONObject(response.body().string()); JSONObject respJSON = new JSONObject(response);
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
JSONObject respMeta = respOCS.getJSONObject(NODE_META); JSONObject respMeta = respOCS.getJSONObject(NODE_META);
JSONObject respData = respOCS.getJSONObject(NODE_DATA); JSONObject respData = respOCS.getJSONObject(NODE_DATA);
@ -269,19 +263,18 @@ public class GetRemoteCapabilitiesOperation extends RemoteOperation {
Log_OC.d(TAG, "*** Get Capabilities completed "); Log_OC.d(TAG, "*** Get Capabilities completed ");
} else { } else {
result = new RemoteOperationResult(statusProp, statuscode, null, null); result = new RemoteOperationResult(statuscode, message, null);
Log_OC.e(TAG, "Failed response while getting capabilities from the server "); Log_OC.e(TAG, "Failed response while getting capabilities from the server ");
Log_OC.e(TAG, "*** status: " + statusProp + "; message: " + message); Log_OC.e(TAG, "*** status: " + statusProp + "; message: " + message);
} }
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
Log_OC.e(TAG, "Failed response while getting capabilities from the server "); Log_OC.e(TAG, "Failed response while getting capabilities from the server ");
if (response.body().string() != null) { if (response != null) {
Log_OC.e(TAG, "*** status code: " + response.code() + "; response message: " + Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response);
response.body().string());
} else { } else {
Log_OC.e(TAG, "*** status code: " + response.code()); Log_OC.e(TAG, "*** status code: " + status);
} }
} }

View File

@ -40,9 +40,6 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/** /**
@ -79,17 +76,12 @@ public class GetRemoteStatusOperation extends RemoteOperation {
boolean retval = false; boolean retval = false;
String baseUrlSt = client.getBaseUri().toString(); String baseUrlSt = client.getBaseUri().toString();
try { try {
String url = baseUrlSt + OwnCloudClient.STATUS_PATH; String url = baseUrlSt + OwnCloudClient.STATUS_PATH;
final Request request = new Request.Builder()
.url(url)
.build();
client.setFollowRedirects(false); client.setFollowRedirects(false);
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); GetMethod getMethod = new GetMethod(client.getOkHttpClient(), url);
Response response = client.executeHttpMethod(getMethod); int status = client.executeHttpMethod(getMethod);
mLatestResult = new RemoteOperationResult(OK); mLatestResult = new RemoteOperationResult(OK);
@ -115,9 +107,9 @@ public class GetRemoteStatusOperation extends RemoteOperation {
// redirectedLocation = mLatestResult.getRedirectedLocation(); // redirectedLocation = mLatestResult.getRedirectedLocation();
// } // }
if (response.code() == HttpConstants.HTTP_OK) { if (isSuccess(status)) {
JSONObject respJSON = new JSONObject(response.body().string()); JSONObject respJSON = new JSONObject(getMethod.getResponseBodyAsString());
if (!respJSON.getBoolean(NODE_INSTALLED)) { if (!respJSON.getBoolean(NODE_INSTALLED)) {
mLatestResult = new RemoteOperationResult( mLatestResult = new RemoteOperationResult(
RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
@ -147,7 +139,7 @@ public class GetRemoteStatusOperation extends RemoteOperation {
} }
} else { } else {
mLatestResult = new RemoteOperationResult(false, getMethod.getRequest(), response); mLatestResult = new RemoteOperationResult(getMethod);
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -199,4 +191,8 @@ public class GetRemoteStatusOperation extends RemoteOperation {
} }
return mLatestResult; return mLatestResult;
} }
private boolean isSuccess(int status) {
return (status == HttpConstants.HTTP_OK);
}
} }

View File

@ -40,9 +40,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import okhttp3.Request;
import okhttp3.Response;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
@ -76,11 +73,11 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
GetMethod getMethod = null;
RemoteOperationResult result; RemoteOperationResult result;
InputStream inputStream = null; InputStream inputStream = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
ByteArrayOutputStream bos = null; ByteArrayOutputStream bos = null;
Response response = null;
try { try {
String url = String url =
@ -89,17 +86,13 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
; ;
Log_OC.d(TAG, "avatar URI: " + url); Log_OC.d(TAG, "avatar URI: " + url);
final Request request = new Request.Builder() getMethod = new GetMethod(client.getOkHttpClient(), url);
.url(url) int status = client.executeHttpMethod(getMethod);
.build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); if (isSuccess(status)) {
response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) {
// find out size of file to read // find out size of file to read
int totalToTransfer = 0; int totalToTransfer = 0;
String contentLength = response.header("Content-Length"); String contentLength = getMethod.getResponseHeader("Content-Length");
if (contentLength != null && contentLength.length() > 0) { if (contentLength != null && contentLength.length() > 0) {
totalToTransfer = Integer.parseInt(contentLength); totalToTransfer = Integer.parseInt(contentLength);
@ -107,7 +100,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
// find out MIME-type! // find out MIME-type!
String mimeType; String mimeType;
String contentType =response.header("Content-Type"); String contentType = getMethod.getResponseHeader("Content-Type");
if (contentType == null || !contentType.startsWith("image")) { if (contentType == null || !contentType.startsWith("image")) {
Log_OC.e( Log_OC.e(
@ -122,7 +115,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
mimeType = contentType; mimeType = contentType;
/// download will be performed to a buffer /// download will be performed to a buffer
inputStream = response.body().byteStream(); inputStream = getMethod.getResponseAsStream();
bis = new BufferedInputStream(inputStream); bis = new BufferedInputStream(inputStream);
bos = new ByteArrayOutputStream(totalToTransfer); bos = new ByteArrayOutputStream(totalToTransfer);
@ -136,7 +129,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
// TODO check total bytes transferred? // TODO check total bytes transferred?
// find out etag // find out etag
String etag = WebdavUtils.getEtagFromResponse(response); String etag = WebdavUtils.getEtagFromResponse(getMethod);
if (etag.length() == 0) { if (etag.length() == 0) {
Log_OC.w(TAG, "Could not read Etag from avatar"); Log_OC.w(TAG, "Could not read Etag from avatar");
} }
@ -149,8 +142,8 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
result.setData(data); result.setData(data);
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
client.exhaustResponse(response.body().byteStream()); client.exhaustResponse(getMethod.getResponseAsStream());
} }
} catch (Exception e) { } catch (Exception e) {
@ -158,7 +151,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
Log_OC.e(TAG, "Exception while getting OC user avatar", e); Log_OC.e(TAG, "Exception while getting OC user avatar", e);
} finally { } finally {
if (response != null) { if (getMethod != null) {
try { try {
if (inputStream != null) { if (inputStream != null) {
client.exhaustResponse(inputStream); client.exhaustResponse(inputStream);
@ -178,7 +171,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation {
} catch (IOException o) { } catch (IOException o) {
Log_OC.e(TAG, "Unexpected exception closing output stream ", o); Log_OC.e(TAG, "Unexpected exception closing output stream ", o);
} }
response.body().close(); client.exhaustResponse(getMethod.getResponseAsStream());
} }
} }

View File

@ -75,13 +75,15 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
.addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
.build(); .build();
GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); String url = client.getBaseUri() + OCS_ROUTE;
Response response = client.executeHttpMethod(getMethod);
if (isSuccess(response.code())) { GetMethod getMethod = new GetMethod(client.getOkHttpClient(), url);
int status = client.executeHttpMethod(getMethod);
if (isSuccess(status)) {
Log_OC.d(TAG, "Successful response"); Log_OC.d(TAG, "Successful response");
JSONObject respJSON = new JSONObject(response.body().string()); JSONObject respJSON = new JSONObject(getMethod.getResponseBodyAsString());
JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
JSONObject respData = respOCS.getJSONObject(NODE_DATA); JSONObject respData = respOCS.getJSONObject(NODE_DATA);
@ -97,12 +99,13 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
result.setData(data); result.setData(data);
} else { } else {
result = new RemoteOperationResult(false, getMethod.getRequest(), response); result = new RemoteOperationResult(getMethod);
String response = getMethod.getResponseBodyAsString();
Log_OC.e(TAG, "Failed response while getting user information "); Log_OC.e(TAG, "Failed response while getting user information ");
if (response != null) { if (getMethod != null) {
Log_OC.e(TAG, "*** status code: " + response.code() + " ; response message: " + response); Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
} else { } else {
Log_OC.e(TAG, "*** status code: " + response.code()); Log_OC.e(TAG, "*** status code: " + status);
} }
} }
} catch (Exception e) { } catch (Exception e) {