mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 10:27:45 +00:00 
			
		
		
		
	Move OkHttp and dav4android dependencies to the new wrapper
This commit is contained in:
		
							parent
							
								
									08777c0265
								
							
						
					
					
						commit
						bcc8a78bcf
					
				| @ -310,21 +310,21 @@ public class OwnCloudClient extends HttpClient { | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     public Response executeHttpMethod (HttpBaseMethod method) throws Exception { | ||||
|     public int executeHttpMethod (HttpBaseMethod method) throws Exception { | ||||
| 
 | ||||
|         boolean repeatWithFreshCredentials; | ||||
|         int repeatCounter = 0; | ||||
|         Response response; | ||||
|         int status; | ||||
| 
 | ||||
|         do { | ||||
|             response = method.execute(); | ||||
|             repeatWithFreshCredentials = checkUnauthorizedAccess(response.code(), repeatCounter); | ||||
|             status = method.execute(); | ||||
|             repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter); | ||||
|             if (repeatWithFreshCredentials) { | ||||
|                 repeatCounter++; | ||||
|             } | ||||
|         } while (repeatWithFreshCredentials); | ||||
| 
 | ||||
|         return response; | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     private void checkFirstRedirection(HttpMethod method) { | ||||
|  | ||||
| @ -24,8 +24,15 @@ | ||||
| 
 | ||||
| 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.Response; | ||||
| import okhttp3.internal.http2.Header; | ||||
| 
 | ||||
| /** | ||||
|  * Wrapper to perform http calls transparently by using: | ||||
| @ -35,10 +42,33 @@ import okhttp3.Response; | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| public abstract class HttpBaseMethod { | ||||
|     public abstract Response execute() throws Exception; | ||||
|     protected Request mRequest; | ||||
|     public abstract int execute() throws Exception; | ||||
|     protected Response mResponse; | ||||
|     private static final String TAG = HttpBaseMethod.class.getSimpleName(); | ||||
| 
 | ||||
|     public Request getRequest() { | ||||
|         return mRequest; | ||||
|     // Status | ||||
|     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); | ||||
|     } | ||||
| } | ||||
| @ -34,17 +34,19 @@ import okhttp3.Response; | ||||
|  */ | ||||
| public class DeleteMethod extends HttpMethod{ | ||||
| 
 | ||||
|     public DeleteMethod(OkHttpClient okHttpClient, Request baseRequest) { | ||||
|         super(okHttpClient, baseRequest); | ||||
|     public DeleteMethod(OkHttpClient okHttpClient, String httpUrl) { | ||||
|         super(okHttpClient, httpUrl); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Response execute() throws Exception { | ||||
|         mRequest = mBaseRequest | ||||
|     public int execute() throws Exception { | ||||
|         Request request = mRequest | ||||
|                 .newBuilder() | ||||
|                 .delete() | ||||
|                 .build(); | ||||
| 
 | ||||
|         return mOkHttpClient.newCall(mRequest).execute(); | ||||
|         mResponse = mOkHttpClient.newCall(request).execute(); | ||||
| 
 | ||||
|         return mResponse.code(); | ||||
|     } | ||||
| } | ||||
| @ -26,9 +26,9 @@ package com.owncloud.android.lib.common.http.nonwebdav; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
|  * OkHttp get calls wrapper | ||||
| @ -36,17 +36,23 @@ import okhttp3.Response; | ||||
|  */ | ||||
| public class GetMethod extends HttpMethod { | ||||
| 
 | ||||
|     public GetMethod(OkHttpClient okHttpClient, Request baseRequest) { | ||||
|         super(okHttpClient, baseRequest); | ||||
|     public GetMethod(OkHttpClient okHttpClient, String httpUrl) { | ||||
|         super(okHttpClient, httpUrl); | ||||
|     } | ||||
| 
 | ||||
|     public GetMethod(OkHttpClient okHttpClient, HttpUrl httpUrl) { | ||||
|         super(okHttpClient, httpUrl); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Response execute() throws IOException { | ||||
|         mRequest = mBaseRequest | ||||
|     public int execute() throws IOException { | ||||
|         Request request = mRequest | ||||
|                 .newBuilder() | ||||
|                 .get() | ||||
|                 .build(); | ||||
| 
 | ||||
|         return mOkHttpClient.newCall(mRequest).execute(); | ||||
|         mResponse = mOkHttpClient.newCall(request).execute(); | ||||
| 
 | ||||
|         return mResponse.code(); | ||||
|     } | ||||
| } | ||||
| @ -26,20 +26,42 @@ package com.owncloud.android.lib.common.http.nonwebdav; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.http.HttpBaseMethod; | ||||
| 
 | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| 
 | ||||
| /** | ||||
|  * Wrapper to perform OkHttp calls | ||||
|  * | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| public abstract class HttpMethod extends HttpBaseMethod { | ||||
| 
 | ||||
|     protected OkHttpClient mOkHttpClient; | ||||
|     protected Request mBaseRequest; | ||||
|     protected Request mRequest; | ||||
| 
 | ||||
|     public HttpMethod (OkHttpClient okHttpClient, Request baseRequest) { | ||||
|     public HttpMethod(OkHttpClient okHttpClient, String httpUrl) { | ||||
|         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); | ||||
|     } | ||||
| } | ||||
| @ -37,18 +37,20 @@ public class PostMethod extends HttpMethod { | ||||
| 
 | ||||
|     private RequestBody mRequestBody; | ||||
| 
 | ||||
|     public PostMethod(OkHttpClient okHttpClient, Request baseRequest, RequestBody requestBody){ | ||||
|         super(okHttpClient, baseRequest); | ||||
|     public PostMethod(OkHttpClient okHttpClient, String httpUrl, RequestBody requestBody){ | ||||
|         super(okHttpClient, httpUrl); | ||||
|         mRequestBody = requestBody; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Response execute() throws Exception { | ||||
|         mRequest = mBaseRequest | ||||
|     public int execute() throws Exception { | ||||
|         Request request = mRequest | ||||
|                 .newBuilder() | ||||
|                 .post(mRequestBody) | ||||
|                 .build(); | ||||
| 
 | ||||
|         return mOkHttpClient.newCall(mRequest).execute(); | ||||
|         mResponse = mOkHttpClient.newCall(request).execute(); | ||||
| 
 | ||||
|         return mResponse.code(); | ||||
|     } | ||||
| } | ||||
| @ -33,18 +33,20 @@ public class PutMethod extends HttpMethod{ | ||||
| 
 | ||||
|     private RequestBody mRequestBody; | ||||
| 
 | ||||
|     public PutMethod(OkHttpClient okHttpClient, Request baseRequest, RequestBody requestBody){ | ||||
|         super(okHttpClient, baseRequest); | ||||
|     public PutMethod(OkHttpClient okHttpClient, String httpUrl, RequestBody requestBody){ | ||||
|         super(okHttpClient, httpUrl); | ||||
|         mRequestBody = requestBody; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Response execute() throws Exception { | ||||
|         mRequest = mBaseRequest | ||||
|     public int execute() throws Exception { | ||||
|         Request request = mRequest | ||||
|                 .newBuilder() | ||||
|                 .put(mRequestBody) | ||||
|                 .build(); | ||||
| 
 | ||||
|         return mOkHttpClient.newCall(mRequest).execute(); | ||||
|         mResponse = mOkHttpClient.newCall(request).execute(); | ||||
| 
 | ||||
|         return mResponse.code(); | ||||
|     } | ||||
| } | ||||
| @ -51,7 +51,7 @@ public class PropfindMethod extends DavMethod { | ||||
|     }; | ||||
| 
 | ||||
|     @Override | ||||
|     public Response execute() throws IOException, HttpException, DavException { | ||||
|     public int execute() throws IOException, HttpException, DavException { | ||||
|         try { | ||||
|             mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet()); | ||||
|             mMembers = mDavResource.getMembers(); | ||||
| @ -59,8 +59,9 @@ public class PropfindMethod extends DavMethod { | ||||
|             // Do nothing, we will use the 401 code to handle the situation | ||||
|         } | ||||
| 
 | ||||
|         mRequest = mDavResource.getRequest(); | ||||
|         return mDavResource.getResponse(); | ||||
|         mResponse = mDavResource.getResponse(); | ||||
| 
 | ||||
|         return mResponse.code(); | ||||
|     } | ||||
| 
 | ||||
|     public int getDepth() { | ||||
|  | ||||
| @ -32,6 +32,8 @@ import java.util.Locale; | ||||
| 
 | ||||
| import android.net.Uri; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.http.HttpBaseMethod; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.Header; | ||||
| import org.apache.commons.httpclient.HttpMethod; | ||||
| 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 | ||||
|      */ | ||||
|     public static String getEtagFromResponse(Response response) { | ||||
|         String eTag = response.header("OC-ETag"); | ||||
|     public static String getEtagFromResponse(HttpBaseMethod httpBaseMethod) { | ||||
|         String eTag = httpBaseMethod.getResponseHeader("OC-ETag"); | ||||
|         if (eTag == null) { | ||||
|             eTag = response.header("oc-etag"); | ||||
|             eTag = httpBaseMethod.getResponseHeader("oc-etag"); | ||||
|         } | ||||
|         if (eTag == null) { | ||||
|             eTag = response.header("ETag"); | ||||
|             eTag = httpBaseMethod.getResponseHeader("ETag"); | ||||
|         } | ||||
|         if (eTag == null) { | ||||
|             eTag = response.header("etag"); | ||||
|             eTag = httpBaseMethod.getResponseHeader("etag"); | ||||
|         } | ||||
|         String result = ""; | ||||
|         if (eTag != null) { | ||||
|  | ||||
| @ -28,6 +28,7 @@ import android.accounts.Account; | ||||
| import android.accounts.AccountsException; | ||||
| 
 | ||||
| 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.utils.Log_OC; | ||||
| 
 | ||||
| @ -54,8 +55,6 @@ import java.util.Map; | ||||
| import javax.net.ssl.SSLException; | ||||
| 
 | ||||
| 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 | ||||
|      *                   result. | ||||
|      */ | ||||
|     // TODO Delete this | ||||
|     public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException { | ||||
| //        this( | ||||
| //                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 | ||||
|      * response body | ||||
|      * | ||||
|      * @param success | ||||
|      * @param request | ||||
|      * @param response | ||||
|      * @param httpMethod | ||||
|      * @throws IOException | ||||
|      */ | ||||
|     public RemoteOperationResult(boolean success, Request request, Response response) throws IOException { | ||||
|         this(success, response.code(), HttpStatus.getStatusText(response.code()), response.headers()); | ||||
|     public RemoteOperationResult(HttpBaseMethod httpMethod) throws IOException { | ||||
|         this(httpMethod.getStatusCode(), | ||||
|                 httpMethod.getStatusMessage(), | ||||
|                 httpMethod.getResponseHeaders() | ||||
|         ); | ||||
| 
 | ||||
|         // TODO success parameter might not be needed | ||||
|         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 | ||||
| 
 | ||||
|             if (bodyResponse != null && bodyResponse.length() > 0) { | ||||
|                 InputStream is = new ByteArrayInputStream(bodyResponse.getBytes()); | ||||
|                 InvalidCharacterExceptionParser xmlParser = new InvalidCharacterExceptionParser(); | ||||
| @ -311,16 +310,22 @@ public class RemoteOperationResult implements Serializable { | ||||
|         // before | ||||
|         switch (mHttpCode) { | ||||
|             case HttpStatus.SC_FORBIDDEN: | ||||
|                 // TODO | ||||
| //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_FORBIDDEN); | ||||
|                 parseErrorMessageAndSetCode( | ||||
|                         httpMethod.getResponseBodyAsString(), | ||||
|                         ResultCode.SPECIFIC_FORBIDDEN | ||||
|                 ); | ||||
|                 break; | ||||
|             case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: | ||||
|                 // TODO | ||||
| //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE); | ||||
|                 parseErrorMessageAndSetCode( | ||||
|                         httpMethod.getResponseBodyAsString(), | ||||
|                         ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE | ||||
|                 ); | ||||
|                 break; | ||||
|             case HttpStatus.SC_SERVICE_UNAVAILABLE: | ||||
|                 // TODO | ||||
| //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE); | ||||
|                 parseErrorMessageAndSetCode( | ||||
|                         httpMethod.getResponseBodyAsString(), | ||||
|                         ResultCode.SPECIFIC_SERVICE_UNAVAILABLE | ||||
|                 ); | ||||
|                 break; | ||||
|             default: | ||||
|                 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. | ||||
|      * | ||||
| @ -367,13 +399,12 @@ public class RemoteOperationResult implements Serializable { | ||||
|      * | ||||
|      * 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 headers HTTP response header returned by an HTTP/DAV method | ||||
|      */ | ||||
|     public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Headers headers) { | ||||
|         this(success, httpCode, httpPhrase); | ||||
|     public RemoteOperationResult(int httpCode, String httpPhrase, Headers headers) { | ||||
|         this(httpCode, httpPhrase); | ||||
|         if (headers != null) { | ||||
|             for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) { | ||||
|                 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; | ||||
|         } | ||||
|     } | ||||
| // | ||||
| //    /** | ||||
| //     * 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. | ||||
|      * | ||||
|      * 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 httpPhrase HTTP status line phrase returned by the HTTP/DAV method | ||||
|      */ | ||||
|     private RemoteOperationResult(boolean success, int httpCode, String httpPhrase) { | ||||
|         mSuccess = success; | ||||
|     private RemoteOperationResult(int httpCode, String httpPhrase) { | ||||
|         mHttpCode = httpCode; | ||||
|         mHttpPhrase = httpPhrase; | ||||
| 
 | ||||
|         if (success) { | ||||
|             mCode = ResultCode.OK; | ||||
| 
 | ||||
|         } else if (httpCode > 0) { | ||||
|         if (httpCode > 0) { | ||||
|             switch (httpCode) { | ||||
|                 case HttpStatus.SC_UNAUTHORIZED:                    // 401 | ||||
|                     mCode = ResultCode.UNAUTHORIZED; | ||||
| @ -668,5 +659,4 @@ public class RemoteOperationResult implements Serializable { | ||||
|     public void setLastPermanentLocation(String lastPermanentLocation) { | ||||
|         mLastPermanentLocation = lastPermanentLocation; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| } | ||||
| @ -25,6 +25,7 @@ | ||||
| package com.owncloud.android.lib.resources.files; | ||||
| 
 | ||||
| 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.network.RedirectionPath; | ||||
| 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.utils.Log_OC; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.HttpStatus; | ||||
| 
 | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| 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. | ||||
|  *  | ||||
|  * @author David A. Velasco | ||||
|  * @author David GonzálezVerdugo | ||||
|  */ | ||||
| public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|      | ||||
| @ -81,13 +80,12 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|         try { | ||||
| 
 | ||||
| //            client.setFollowRedirects(false); | ||||
| 
 | ||||
|             PropfindMethod propfindMethod = new PropfindMethod( | ||||
|                     client.getOkHttpClient(), | ||||
|                     HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)), | ||||
|                     0); | ||||
| 
 | ||||
|             Response response = client.executeHttpMethod(propfindMethod); | ||||
|             int status = client.executeHttpMethod(propfindMethod); | ||||
| 
 | ||||
| //            if (previousFollowRedirects) { | ||||
| //                mRedirectionPath = client.followRedirection(propfind); | ||||
| @ -102,22 +100,15 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|              *  404 NOT FOUND: path doesn't exist, | ||||
|              *  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 | ||||
|                     ? new RemoteOperationResult(OK) | ||||
|                     : new RemoteOperationResult( | ||||
|                             false, propfindMethod.getRequest(), response | ||||
|             ); | ||||
|             result = isSuccess(status) ? | ||||
|                     new RemoteOperationResult(OK) : | ||||
|                     new RemoteOperationResult(propfindMethod); | ||||
| 
 | ||||
|             Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() + | ||||
|                     WebdavUtils.encodePath(mPath) + " targeting for " + | ||||
|                     (mSuccessIfAbsent ? " absence " : " existence ") + | ||||
|                     "finished with HTTP status " + response.code() + (!isSuccess?"(FAIL)":"")); | ||||
|                     "finished with HTTP status " + status + (!isSuccess(status)?"(FAIL)":"")); | ||||
|              | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
| @ -147,4 +138,10 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | ||||
|     public boolean wasRedirected() { | ||||
|         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); | ||||
|     } | ||||
| } | ||||
| @ -36,7 +36,6 @@ import java.util.ArrayList; | ||||
| 
 | ||||
| import at.bitfire.dav4android.DavResource; | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| 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)), | ||||
|                     1); | ||||
| 
 | ||||
|             Response response = client.executeHttpMethod(propfindMethod); | ||||
|             int status = client.executeHttpMethod(propfindMethod); | ||||
| 
 | ||||
|             boolean isSuccess = (response.code() == HttpConstants.HTTP_MULTI_STATUS | ||||
|                     || response.code() == HttpConstants.HTTP_OK); | ||||
| 
 | ||||
|             if (isSuccess) { | ||||
|             if (isSuccess(status)) { | ||||
| 
 | ||||
|                 ArrayList<Object> mFolderAndFiles = new ArrayList<>(); | ||||
| 
 | ||||
| @ -104,13 +100,8 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | ||||
|                 result.setData(mFolderAndFiles); | ||||
| 
 | ||||
|             } else { | ||||
| 
 | ||||
|                 // synchronization failed | ||||
|                 result = new RemoteOperationResult( | ||||
|                         false, | ||||
|                         propfindMethod.getRequest(), | ||||
|                         response | ||||
|                 ); | ||||
|                 result = new RemoteOperationResult(propfindMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
| @ -129,4 +120,9 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     private boolean isSuccess(int status) { | ||||
|         return status == HttpConstants.HTTP_MULTI_STATUS || | ||||
|                 status == HttpConstants.HTTP_OK; | ||||
|     } | ||||
| } | ||||
| @ -42,8 +42,6 @@ import java.util.Calendar; | ||||
| import java.util.Locale; | ||||
| 
 | ||||
| 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. | ||||
| @ -200,17 +198,6 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
|         RemoteOperationResult result; | ||||
| 
 | ||||
|         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() | ||||
|                     .add(PARAM_PATH, mRemoteFilePath) | ||||
|                     .add(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue())) | ||||
| @ -240,11 +227,18 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
| 
 | ||||
|             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( | ||||
|                         new ShareXMLParser() | ||||
| @ -252,7 +246,7 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
|                 parser.setOneOrMoreSharesRequired(true); | ||||
|                 parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
|                 parser.setServerBaseUri(client.getBaseUri()); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(postMethod.getResponseBodyAsString()); | ||||
| 
 | ||||
|                 if (result.isSuccess() && mGetShareDetails) { | ||||
| 
 | ||||
| @ -266,7 +260,7 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
|                 } | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, postMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(postMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -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.utils.Log_OC; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
|  * 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(Long.toString(mRemoteId)); | ||||
| 
 | ||||
|             final Request request = new Request.Builder() | ||||
|                     .url(uriBuilder.build().toString()) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), | ||||
|                     uriBuilder.build().toString()); | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             Response response = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             if (isSuccess(response.code())) { | ||||
|             if (isSuccess(status)) { | ||||
|                 // Parse xml response and obtain the list of shares | ||||
|                 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
|                         new ShareXMLParser() | ||||
| @ -82,10 +76,10 @@ public class GetRemoteShareOperation extends RemoteOperation { | ||||
|                 parser.setOneOrMoreSharesRequired(true); | ||||
|                 parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
|                 parser.setServerBaseUri(client.getBaseUri()); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(getMethod.getResponseBodyAsString()); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(getMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -42,9 +42,6 @@ import org.json.JSONObject; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| 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_PER_PAGE, String.valueOf(mPerPage)); | ||||
| 
 | ||||
|             Request request = new Request.Builder() | ||||
|                     .url(uriBuilder.build().toString()) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), uriBuilder.build().toString()); | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             Response response = client.executeHttpMethod(getMethod); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
|             String response = getMethod.getResponseBodyAsString(); | ||||
| 
 | ||||
|             if(isSuccess(response.code())) { | ||||
|             if(isSuccess(status)) { | ||||
|                 Log_OC.d(TAG, "Successful response: " + response); | ||||
| 
 | ||||
|                 // Parse the response | ||||
|                 JSONObject respJSON = new JSONObject(response.body().string()); | ||||
|                 JSONObject respJSON = new JSONObject(response); | ||||
|                 JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); | ||||
|                 JSONObject respData = respOCS.getJSONObject(NODE_DATA); | ||||
|                 JSONObject respExact = respData.getJSONObject(NODE_EXACT); | ||||
| @ -179,12 +174,12 @@ public class GetRemoteShareesOperation extends RemoteOperation{ | ||||
|                 Log_OC.d(TAG, "*** Get Users or groups completed " ); | ||||
| 
 | ||||
|             } 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 "); | ||||
|                 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 { | ||||
|                     Log_OC.e(TAG, "*** status code: " + response.code()); | ||||
|                     Log_OC.e(TAG, "*** status code: " + status); | ||||
|                 } | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -29,16 +29,14 @@ package com.owncloud.android.lib.resources.shares; | ||||
| 
 | ||||
| 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.nonwebdav.GetMethod; | ||||
| 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 okhttp3.HttpUrl; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
|  * 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_SUBFILES, String.valueOf(mSubfiles)); | ||||
| 
 | ||||
|             Request request = new Request.Builder() | ||||
|                     .url(httpBuilder.build()) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), httpBuilder.build()); | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             Response response = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             if (isSuccess(response.code())) { | ||||
|             if (isSuccess(status)) { | ||||
|                 // Parse xml response and obtain the list of shares | ||||
|                 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
|                     new ShareXMLParser() | ||||
|                 ); | ||||
|                 parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
|                 parser.setServerBaseUri(client.getBaseUri()); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(getMethod.getResponseBodyAsString()); | ||||
| 
 | ||||
|                 if (result.isSuccess()) { | ||||
|                     Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); | ||||
|                 } | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(getMethod); | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
|  | ||||
| @ -35,7 +35,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
|  * 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(); | ||||
|             uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); | ||||
| 
 | ||||
|             Request request = new Request.Builder() | ||||
|                     .url(uriBuilder.build().toString()) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), | ||||
|                     uriBuilder.build().toString()); | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             Response response = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             if (isSuccess(response.code())) { | ||||
|             if (isSuccess(status)) { | ||||
| 
 | ||||
|                 // Parse xml response and obtain the list of shares | ||||
|                 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
| @ -75,9 +71,9 @@ public class GetRemoteSharesOperation extends RemoteOperation { | ||||
|                 ); | ||||
|                 parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
|                 parser.setServerBaseUri(client.getBaseUri()); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(getMethod.getResponseBodyAsString()); | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(getMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -37,7 +37,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
|  * Remove a share | ||||
| @ -68,34 +67,29 @@ public class RemoveRemoteShareOperation extends RemoteOperation { | ||||
|         RemoteOperationResult result; | ||||
| 
 | ||||
|         try { | ||||
|             String id = "/" + String.valueOf(mRemoteShareId); | ||||
| 
 | ||||
|             Uri requestUri = client.getBaseUri(); | ||||
|             Uri.Builder uriBuilder = requestUri.buildUpon(); | ||||
|             uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); | ||||
|             uriBuilder.appendEncodedPath(String.valueOf(id)); | ||||
|             uriBuilder.appendEncodedPath(String.valueOf(mRemoteShareId)); | ||||
| 
 | ||||
|             Request request = new Request.Builder() | ||||
|                     .url(uriBuilder.build().toString()) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             DeleteMethod deleteMethod = new DeleteMethod(client.getOkHttpClient(), | ||||
|                     uriBuilder.build().toString()); | ||||
|             deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|             DeleteMethod deleteMethod = new DeleteMethod(client.getOkHttpClient(), request); | ||||
|             int status = client.executeHttpMethod(deleteMethod); | ||||
| 
 | ||||
|             Response response = client.executeHttpMethod(deleteMethod); | ||||
| 
 | ||||
|             if (isSuccess(response.code())) { | ||||
|             if (isSuccess(status)) { | ||||
| 
 | ||||
|                 // Parse xml response and obtain the list of shares | ||||
|                 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
|                         new ShareXMLParser() | ||||
|                 ); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(deleteMethod.getResponseBodyAsString()); | ||||
| 
 | ||||
|                 Log_OC.d(TAG, "Unshare " + mRemoteShareId + ": " + result.getLogMessage()); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, deleteMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(deleteMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -41,7 +41,6 @@ import java.util.Locale; | ||||
| 
 | ||||
| import okhttp3.FormBody; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
| @ -169,18 +168,6 @@ public class UpdateRemoteShareOperation extends RemoteOperation { | ||||
|         RemoteOperationResult result; | ||||
| 
 | ||||
|         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(); | ||||
| 
 | ||||
|             // Parameters to update | ||||
| @ -212,23 +199,32 @@ public class UpdateRemoteShareOperation extends RemoteOperation { | ||||
|                 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(); | ||||
| 
 | ||||
|             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 | ||||
|                 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( | ||||
|                         new ShareXMLParser() | ||||
|                 ); | ||||
|                 parser.setOwnCloudVersion(client.getOwnCloudVersion()); | ||||
|                 parser.setServerBaseUri(client.getBaseUri()); | ||||
|                 result = parser.parse(response.body().string()); | ||||
|                 result = parser.parse(putMethod.getResponseBodyAsString()); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, putMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(putMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|  | ||||
| @ -40,9 +40,6 @@ import org.json.JSONObject; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| 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(); | ||||
| 
 | ||||
|             final Request request = new Request.Builder() | ||||
|                     .url(url) | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), url); | ||||
|             getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             final Response response = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             if(isSuccess(response.code())) { | ||||
|             String response = getMethod.getResponseBodyAsString(); | ||||
|             if(isSuccess(status)) { | ||||
|                 Log_OC.d(TAG, "Successful response: " + response); | ||||
| 
 | ||||
|                 // Parse the response | ||||
|                 JSONObject respJSON = new JSONObject(response.body().string()); | ||||
|                 JSONObject respJSON = new JSONObject(response); | ||||
|                 JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); | ||||
|                 JSONObject respMeta = respOCS.getJSONObject(NODE_META); | ||||
|                 JSONObject respData = respOCS.getJSONObject(NODE_DATA); | ||||
| @ -269,19 +263,18 @@ public class GetRemoteCapabilitiesOperation extends RemoteOperation { | ||||
| 
 | ||||
|                     Log_OC.d(TAG, "*** Get Capabilities completed "); | ||||
|                 } 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, "*** status: " + statusProp + "; message: " + message); | ||||
|                 } | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 result = new RemoteOperationResult(getMethod); | ||||
|                 Log_OC.e(TAG, "Failed response while getting capabilities from the server "); | ||||
|                 if (response.body().string() != null) { | ||||
|                     Log_OC.e(TAG, "*** status code: " + response.code() + "; response message: " + | ||||
|                             response.body().string()); | ||||
|                 if (response != null) { | ||||
|                     Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); | ||||
|                 } else { | ||||
|                     Log_OC.e(TAG, "*** status code: " + response.code()); | ||||
|                     Log_OC.e(TAG, "*** status code: " + status); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|  | ||||
| @ -40,9 +40,6 @@ import org.json.JSONObject; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||
| 
 | ||||
| /** | ||||
| @ -79,17 +76,12 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|         boolean retval = false; | ||||
|         String baseUrlSt = client.getBaseUri().toString(); | ||||
|         try { | ||||
| 
 | ||||
|             String url = baseUrlSt + OwnCloudClient.STATUS_PATH; | ||||
| 
 | ||||
|             final Request request = new Request.Builder() | ||||
|                     .url(url) | ||||
|                     .build(); | ||||
| 
 | ||||
|             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); | ||||
| 
 | ||||
| @ -115,9 +107,9 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
| //                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)) { | ||||
|                     mLatestResult = new RemoteOperationResult( | ||||
|                         RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); | ||||
| @ -147,7 +139,7 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|                 } | ||||
| 
 | ||||
|             } else { | ||||
|                 mLatestResult = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 mLatestResult = new RemoteOperationResult(getMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (JSONException e) { | ||||
| @ -199,4 +191,8 @@ public class GetRemoteStatusOperation extends RemoteOperation { | ||||
|         } | ||||
|         return mLatestResult; | ||||
|     } | ||||
| 
 | ||||
|     private boolean isSuccess(int status) { | ||||
|         return (status == HttpConstants.HTTP_OK); | ||||
|     } | ||||
| } | ||||
| @ -40,9 +40,6 @@ import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||
| 
 | ||||
| 
 | ||||
| @ -76,11 +73,11 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
| 
 | ||||
|     @Override | ||||
|     protected RemoteOperationResult run(OwnCloudClient client) { | ||||
|         GetMethod getMethod = null; | ||||
|         RemoteOperationResult result; | ||||
|         InputStream inputStream = null; | ||||
|         BufferedInputStream bis = null; | ||||
|         ByteArrayOutputStream bos = null; | ||||
|         Response response = null; | ||||
| 
 | ||||
|         try { | ||||
|             String url = | ||||
| @ -89,17 +86,13 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|             ; | ||||
|             Log_OC.d(TAG, "avatar URI: " + url); | ||||
| 
 | ||||
|             final Request request = new Request.Builder() | ||||
|                     .url(url) | ||||
|                     .build(); | ||||
|             getMethod = new GetMethod(client.getOkHttpClient(), url); | ||||
|             int status = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             response = client.executeHttpMethod(getMethod); | ||||
| 
 | ||||
|             if (isSuccess(response.code())) { | ||||
|             if (isSuccess(status)) { | ||||
|                 // find out size of file to read | ||||
|                 int totalToTransfer = 0; | ||||
|                 String contentLength = response.header("Content-Length"); | ||||
|                 String contentLength = getMethod.getResponseHeader("Content-Length"); | ||||
| 
 | ||||
|                 if (contentLength != null && contentLength.length() > 0) { | ||||
|                     totalToTransfer = Integer.parseInt(contentLength); | ||||
| @ -107,7 +100,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
| 
 | ||||
|                 // find out MIME-type! | ||||
|                 String mimeType; | ||||
|                 String contentType =response.header("Content-Type"); | ||||
|                 String contentType = getMethod.getResponseHeader("Content-Type"); | ||||
| 
 | ||||
|                 if (contentType == null || !contentType.startsWith("image")) { | ||||
|                     Log_OC.e( | ||||
| @ -122,7 +115,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|                 mimeType = contentType; | ||||
| 
 | ||||
|                 /// download will be performed to a buffer | ||||
|                 inputStream = response.body().byteStream(); | ||||
|                 inputStream = getMethod.getResponseAsStream(); | ||||
|                 bis = new BufferedInputStream(inputStream); | ||||
|                 bos = new ByteArrayOutputStream(totalToTransfer); | ||||
| 
 | ||||
| @ -136,7 +129,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|                 // TODO check total bytes transferred? | ||||
| 
 | ||||
|                 // find out etag | ||||
|                 String etag = WebdavUtils.getEtagFromResponse(response); | ||||
|                 String etag = WebdavUtils.getEtagFromResponse(getMethod); | ||||
|                 if (etag.length() == 0) { | ||||
|                     Log_OC.w(TAG, "Could not read Etag from avatar"); | ||||
|                 } | ||||
| @ -149,8 +142,8 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|                 result.setData(data); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, getMethod.getRequest(), response); | ||||
|                 client.exhaustResponse(response.body().byteStream()); | ||||
|                 result = new RemoteOperationResult(getMethod); | ||||
|                 client.exhaustResponse(getMethod.getResponseAsStream()); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
| @ -158,7 +151,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|             Log_OC.e(TAG, "Exception while getting OC user avatar", e); | ||||
| 
 | ||||
|         } finally { | ||||
|             if (response != null) { | ||||
|             if (getMethod != null) { | ||||
|                 try { | ||||
|                     if (inputStream != null) { | ||||
|                         client.exhaustResponse(inputStream); | ||||
| @ -178,7 +171,7 @@ public class GetRemoteUserAvatarOperation extends RemoteOperation { | ||||
|                 } catch (IOException o) { | ||||
|                     Log_OC.e(TAG, "Unexpected exception closing output stream ", o); | ||||
|                 } | ||||
|                 response.body().close(); | ||||
|                 client.exhaustResponse(getMethod.getResponseAsStream()); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|  | ||||
| @ -75,13 +75,15 @@ public class GetRemoteUserInfoOperation extends RemoteOperation { | ||||
|                     .addHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) | ||||
|                     .build(); | ||||
| 
 | ||||
|             GetMethod getMethod = new GetMethod(client.getOkHttpClient(), request); | ||||
|             Response response = client.executeHttpMethod(getMethod); | ||||
|             String url = client.getBaseUri() + OCS_ROUTE; | ||||
| 
 | ||||
|             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"); | ||||
| 
 | ||||
|                 JSONObject respJSON = new JSONObject(response.body().string()); | ||||
|                 JSONObject respJSON = new JSONObject(getMethod.getResponseBodyAsString()); | ||||
|                 JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); | ||||
|                 JSONObject respData = respOCS.getJSONObject(NODE_DATA); | ||||
| 
 | ||||
| @ -97,12 +99,13 @@ public class GetRemoteUserInfoOperation extends RemoteOperation { | ||||
|                 result.setData(data); | ||||
| 
 | ||||
|             } 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 "); | ||||
|                 if (response != null) { | ||||
|                     Log_OC.e(TAG, "*** status code: " + response.code() + " ; response message: " + response); | ||||
|                 if (getMethod != null) { | ||||
|                     Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response); | ||||
|                 } else { | ||||
|                     Log_OC.e(TAG, "*** status code: " + response.code()); | ||||
|                     Log_OC.e(TAG, "*** status code: " + status); | ||||
|                 } | ||||
|             } | ||||
|         } catch (Exception e) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user