mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Working on authentication operations
This commit is contained in:
		
							parent
							
								
									4f0cd82a7d
								
							
						
					
					
						commit
						dfab453e6b
					
				| @ -77,7 +77,6 @@ public class OwnCloudClient extends HttpClient { | |||||||
|     private static byte[] sExhaustBuffer = new byte[1024]; |     private static byte[] sExhaustBuffer = new byte[1024]; | ||||||
| 
 | 
 | ||||||
|     private static int sIntanceCounter = 0; |     private static int sIntanceCounter = 0; | ||||||
|     private boolean mFollowRedirects = true; |  | ||||||
|     private OwnCloudCredentials mCredentials = null; |     private OwnCloudCredentials mCredentials = null; | ||||||
|     private int mInstanceNumber = 0; |     private int mInstanceNumber = 0; | ||||||
| 
 | 
 | ||||||
| @ -292,7 +291,7 @@ public class OwnCloudClient extends HttpClient { | |||||||
| 
 | 
 | ||||||
|             checkFirstRedirection(method); |             checkFirstRedirection(method); | ||||||
| 
 | 
 | ||||||
|             if (mFollowRedirects) { |             if (mOkHttpClient.followRedirects()) { | ||||||
|                 status = followRedirection(method).getLastStatus(); |                 status = followRedirection(method).getLastStatus(); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
| @ -312,7 +311,7 @@ public class OwnCloudClient extends HttpClient { | |||||||
| 
 | 
 | ||||||
|     public int executeHttpMethod (HttpBaseMethod method) throws Exception { |     public int executeHttpMethod (HttpBaseMethod method) throws Exception { | ||||||
| 
 | 
 | ||||||
|         boolean repeatWithFreshCredentials = false; |         boolean repeatWithFreshCredentials; | ||||||
|         int repeatCounter = 0; |         int repeatCounter = 0; | ||||||
|         int status; |         int status; | ||||||
| 
 | 
 | ||||||
| @ -457,7 +456,9 @@ public class OwnCloudClient extends HttpClient { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Uri getNewWebDavUri() { |     public Uri getNewWebDavUri() { | ||||||
|         return Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0 + mCredentials.getUsername()); |         return !(mCredentials instanceof OwnCloudAnonymousCredentials) | ||||||
|  |                 ? Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0) | ||||||
|  |                 : Uri.parse(mBaseUri + NEW_WEBDAV_PATH_4_0 + mCredentials.getUsername()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -483,11 +484,15 @@ public class OwnCloudClient extends HttpClient { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setFollowRedirects(boolean followRedirects) { |     public void setFollowRedirects(boolean followRedirects) { | ||||||
|         mFollowRedirects = followRedirects; |         mOkHttpClient | ||||||
|  |                 .newBuilder() | ||||||
|  |                 .followRedirects(followRedirects) | ||||||
|  |                 .followSslRedirects(followRedirects) | ||||||
|  |                 .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public boolean getFollowRedirects() { |     public boolean getFollowRedirects() { | ||||||
|         return mFollowRedirects; |         return mOkHttpClient.followRedirects(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private void logCookiesAtRequest(Header[] headers, String when) { |     private void logCookiesAtRequest(Header[] headers, String when) { | ||||||
|  | |||||||
| @ -0,0 +1,26 @@ | |||||||
|  | package com.owncloud.android.lib.common.methods.nonwebdav; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | import okhttp3.OkHttpClient; | ||||||
|  | import okhttp3.Request; | ||||||
|  | import okhttp3.Response; | ||||||
|  | 
 | ||||||
|  | public class GetMethod extends HttpMethod { | ||||||
|  | 
 | ||||||
|  |     public GetMethod(OkHttpClient okHttpClient, String url) { | ||||||
|  |         super(okHttpClient, url); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int execute() throws IOException { | ||||||
|  |         final Request request = | ||||||
|  |                 new Request.Builder() | ||||||
|  |                 .url(mUrl) | ||||||
|  |                 .get() | ||||||
|  |                 .build(); | ||||||
|  | 
 | ||||||
|  |         Response response = mOkHttpClient.newCall(request).execute(); | ||||||
|  |         return response.code(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,16 @@ | |||||||
|  | package com.owncloud.android.lib.common.methods.nonwebdav; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.common.methods.HttpBaseMethod; | ||||||
|  | 
 | ||||||
|  | import okhttp3.OkHttpClient; | ||||||
|  | 
 | ||||||
|  | public abstract class HttpMethod implements HttpBaseMethod { | ||||||
|  | 
 | ||||||
|  |     protected OkHttpClient mOkHttpClient; | ||||||
|  |     protected String mUrl; | ||||||
|  | 
 | ||||||
|  |     public HttpMethod (OkHttpClient okHttpClient, String url) { | ||||||
|  |         mOkHttpClient = okHttpClient; | ||||||
|  |         mUrl = url; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,4 +1,6 @@ | |||||||
| package com.owncloud.android.lib.common.methods; | package com.owncloud.android.lib.common.methods.webdav; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.common.methods.HttpBaseMethod; | ||||||
| 
 | 
 | ||||||
| import at.bitfire.dav4android.DavResource; | import at.bitfire.dav4android.DavResource; | ||||||
| 
 | 
 | ||||||
| @ -1,4 +1,4 @@ | |||||||
| package com.owncloud.android.lib.common.methods; | package com.owncloud.android.lib.common.methods.webdav; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| @ -24,6 +24,20 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.common.operations; | package com.owncloud.android.lib.common.operations; | ||||||
| 
 | 
 | ||||||
|  | import android.accounts.Account; | ||||||
|  | import android.accounts.AccountsException; | ||||||
|  | 
 | ||||||
|  | import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; | ||||||
|  | import com.owncloud.android.lib.common.network.CertificateCombinedException; | ||||||
|  | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
|  | 
 | ||||||
|  | import org.apache.commons.httpclient.ConnectTimeoutException; | ||||||
|  | import org.apache.commons.httpclient.HttpException; | ||||||
|  | import org.apache.commons.httpclient.HttpMethod; | ||||||
|  | import org.apache.commons.httpclient.HttpStatus; | ||||||
|  | import org.apache.jackrabbit.webdav.DavException; | ||||||
|  | import org.json.JSONException; | ||||||
|  | 
 | ||||||
| import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| @ -34,24 +48,15 @@ import java.net.SocketException; | |||||||
| import java.net.SocketTimeoutException; | import java.net.SocketTimeoutException; | ||||||
| import java.net.UnknownHostException; | import java.net.UnknownHostException; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| 
 | import java.util.List; | ||||||
| import android.accounts.Account; | import java.util.Map; | ||||||
| import android.accounts.AccountsException; |  | ||||||
| 
 |  | ||||||
| import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; |  | ||||||
| import com.owncloud.android.lib.common.network.CertificateCombinedException; |  | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; |  | ||||||
| 
 |  | ||||||
| import org.apache.commons.httpclient.ConnectTimeoutException; |  | ||||||
| import org.apache.commons.httpclient.Header; |  | ||||||
| import org.apache.commons.httpclient.HttpException; |  | ||||||
| import org.apache.commons.httpclient.HttpMethod; |  | ||||||
| import org.apache.commons.httpclient.HttpStatus; |  | ||||||
| import org.apache.jackrabbit.webdav.DavException; |  | ||||||
| import org.json.JSONException; |  | ||||||
| 
 | 
 | ||||||
| import javax.net.ssl.SSLException; | import javax.net.ssl.SSLException; | ||||||
| 
 | 
 | ||||||
|  | import okhttp3.Headers; | ||||||
|  | import okhttp3.Request; | ||||||
|  | import okhttp3.Response; | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * The result of a remote operation required to an ownCloud server. |  * The result of a remote operation required to an ownCloud server. | ||||||
| @ -222,12 +227,12 @@ public class RemoteOperationResult implements Serializable { | |||||||
|      *                   result. |      *                   result. | ||||||
|      */ |      */ | ||||||
|     public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException { |     public RemoteOperationResult(boolean success, HttpMethod httpMethod) throws IOException { | ||||||
|         this( | //        this( | ||||||
|                 success, | //                success, | ||||||
|                 httpMethod.getStatusCode(), | //                httpMethod.getStatusCode(), | ||||||
|                 httpMethod.getStatusText(), | //                httpMethod.getStatusText(), | ||||||
|                 httpMethod.getResponseHeaders() | //                httpMethod.getResponseHeaders() | ||||||
|         ); | //        ); | ||||||
| 
 | 
 | ||||||
|         if (mHttpCode == HttpStatus.SC_BAD_REQUEST) {   // 400 |         if (mHttpCode == HttpStatus.SC_BAD_REQUEST) {   // 400 | ||||||
| 
 | 
 | ||||||
| @ -265,6 +270,63 @@ public class RemoteOperationResult implements Serializable { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Public constructor from separate elements of an HTTP or DAV response. | ||||||
|  |      * | ||||||
|  |      * To be used when the result needs to be interpreted from the response of an HTTP/DAV method. | ||||||
|  |      * | ||||||
|  |      * Determines a {@link ResultCode} from the already executed method received as a parameter. Generally, | ||||||
|  |      * 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 | ||||||
|  |      * @throws IOException | ||||||
|  |      */ | ||||||
|  |     public RemoteOperationResult(boolean success, Request request, Response response) throws IOException { | ||||||
|  |         this(success, response.code(), HttpStatus.getStatusText(response.code()), response.headers()); | ||||||
|  | 
 | ||||||
|  |         // TODO success parameter might not be needed | ||||||
|  |         if (mHttpCode == HttpStatus.SC_BAD_REQUEST) {   // 400 | ||||||
|  | 
 | ||||||
|  |             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(); | ||||||
|  |                 try { | ||||||
|  |                     if (xmlParser.parseXMLResponse(is)) { | ||||||
|  |                         mCode = ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER; | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|  |                 } catch (Exception e) { | ||||||
|  |                     Log_OC.w(TAG, "Error reading exception from server: " + e.getMessage()); | ||||||
|  |                     // mCode stays as set in this(success, httpCode, headers) | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // before | ||||||
|  |         switch (mHttpCode) { | ||||||
|  |             case HttpStatus.SC_FORBIDDEN: | ||||||
|  |                 // TODO | ||||||
|  | //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_FORBIDDEN); | ||||||
|  |                 break; | ||||||
|  |             case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE: | ||||||
|  |                 // TODO | ||||||
|  | //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_UNSUPPORTED_MEDIA_TYPE); | ||||||
|  |                 break; | ||||||
|  |             case HttpStatus.SC_SERVICE_UNAVAILABLE: | ||||||
|  |                 // TODO | ||||||
|  | //                parseErrorMessageAndSetCode(request, response, ResultCode.SPECIFIC_SERVICE_UNAVAILABLE); | ||||||
|  |                 break; | ||||||
|  |             default: | ||||||
|  |                 break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Parse the error message included in the body response, if any, and set the specific result |      * Parse the error message included in the body response, if any, and set the specific result | ||||||
|      * code |      * code | ||||||
| @ -308,25 +370,61 @@ public class RemoteOperationResult implements Serializable { | |||||||
|      * @param success     The operation was considered successful or not. |      * @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 httpHeaders 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, Header[] httpHeaders) { |     public RemoteOperationResult(boolean success, int httpCode, String httpPhrase, Headers headers) { | ||||||
|         this(success, httpCode, httpPhrase); |         this(success, httpCode, httpPhrase); | ||||||
|         if (httpHeaders != null) { |         if (headers != null) { | ||||||
|             for (Header httpHeader : httpHeaders) { |             for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) { | ||||||
|                 if ("location".equals(httpHeader.getName().toLowerCase())) { |                 if ("location".equals(header.getKey().toLowerCase())) { | ||||||
|                     mRedirectedLocation = httpHeader.getValue(); |                     mRedirectedLocation = header.getValue().get(0); | ||||||
|                     continue; |                     continue; | ||||||
|                 } |                 } | ||||||
|                 if ("www-authenticate".equals(httpHeader.getName().toLowerCase())) { |                 if ("www-authenticate".equals(header.getKey().toLowerCase())) { | ||||||
|                     mAuthenticate.add(httpHeader.getValue().toLowerCase()); |                     mAuthenticate.add(header.getValue().get(0).toLowerCase()); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         if (isIdPRedirection()) { |         if (isIdPRedirection()) { | ||||||
|             mCode = ResultCode.UNAUTHORIZED;    // overrides default ResultCode.UNKNOWN |             // overrides default ResultCode.UNKNOWN | ||||||
|  |             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. | ||||||
|  | |||||||
| @ -25,18 +25,20 @@ | |||||||
| package com.owncloud.android.lib.resources.files; | package com.owncloud.android.lib.resources.files; | ||||||
| 
 | 
 | ||||||
| import org.apache.commons.httpclient.HttpStatus; | import org.apache.commons.httpclient.HttpStatus; | ||||||
| import org.apache.jackrabbit.webdav.DavConstants; |  | ||||||
| import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; |  | ||||||
| 
 |  | ||||||
| import android.content.Context; |  | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
|  | import com.owncloud.android.lib.common.methods.webdav.PropfindMethod; | ||||||
| import com.owncloud.android.lib.common.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; | ||||||
| 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 at.bitfire.dav4android.DavOCResource; | ||||||
|  | import okhttp3.HttpUrl; | ||||||
|  | 
 | ||||||
|  | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * 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. | ||||||
|  *  |  *  | ||||||
| @ -57,8 +59,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|     /** Sequence of redirections followed. Available only after executing the operation */ |     /** Sequence of redirections followed. Available only after executing the operation */ | ||||||
|     private RedirectionPath mRedirectionPath = null; |     private RedirectionPath mRedirectionPath = null; | ||||||
|         // TODO move to {@link RemoteOperation}, that needs a nice refactoring |  | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * Full constructor. Success of the operation will depend upon the value of successIfAbsent. |      * Full constructor. Success of the operation will depend upon the value of successIfAbsent. | ||||||
|      * |      * | ||||||
| @ -73,38 +73,50 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| 	protected RemoteOperationResult run(OwnCloudClient client) { | 	protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|         RemoteOperationResult result = null; |         RemoteOperationResult result; | ||||||
|         PropFindMethod propfind = null; | 
 | ||||||
|         boolean previousFollowRedirects = client.getFollowRedirects(); |         // TODO Complete redirection stuff, although OkHttp should follow redirections by default | ||||||
|  | //        boolean previousFollowRedirects = client.getFollowRedirects(); | ||||||
|  | 
 | ||||||
|         try { |         try { | ||||||
|             propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath), | 
 | ||||||
|                     WebdavUtils.getAllPropSet(), DavConstants.DEPTH_0); |             DavOCResource davOCResource = new DavOCResource( | ||||||
|             client.setFollowRedirects(false); |                     client.getOkHttpClient(), | ||||||
|             int status = client.executeMethod(propfind, TIMEOUT, TIMEOUT); |                     HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mPath)) | ||||||
|             if (previousFollowRedirects) { |             ); | ||||||
|                 mRedirectionPath = client.followRedirection(propfind); | 
 | ||||||
|                 status = mRedirectionPath.getLastStatus(); | //            client.setFollowRedirects(false); | ||||||
|             } | 
 | ||||||
|             if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) { |             PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 0); | ||||||
|                 client.exhaustResponse(propfind.getResponseBodyAsStream()); |             int status = client.executeHttpMethod(propfindMethod); | ||||||
|             } | 
 | ||||||
|  | //            if (previousFollowRedirects) { | ||||||
|  | //                mRedirectionPath = client.followRedirection(propfind); | ||||||
|  | //                status = mRedirectionPath.getLastStatus(); | ||||||
|  | //            } | ||||||
|  | //            if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) { | ||||||
|  | //                client.exhaustResponse(propfind.getResponseBodyAsStream()); | ||||||
|  | //            } | ||||||
|  | 
 | ||||||
|             /** |             /** | ||||||
|              *  PROPFIND method |              *  PROPFIND method | ||||||
|              *  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 success = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) && |             boolean isSuccess = ((status == HttpStatus.SC_OK || status == HttpStatus.SC_MULTI_STATUS) && | ||||||
|                     !mSuccessIfAbsent) || |                     !mSuccessIfAbsent) || | ||||||
|                     (status == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent) || |                     (status == HttpStatus.SC_MULTI_STATUS && !mSuccessIfAbsent) || | ||||||
|                     (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); |                     (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); | ||||||
|             result = new RemoteOperationResult( | 
 | ||||||
|                 success, |             result = isSuccess | ||||||
|                 propfind |                     ? new RemoteOperationResult(OK) | ||||||
|             ); |                     : new RemoteOperationResult(false, davOCResource.getRequest(), davOCResource.getResponse()); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|             Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() + |             Log_OC.d(TAG, "Existence check for " + client.getWebdavUri() + | ||||||
|                     WebdavUtils.encodePath(mPath) + " targeting for " + |                     WebdavUtils.encodePath(mPath) + " targeting for " + | ||||||
|                     (mSuccessIfAbsent ? " absence " : " existence ") + |                     (mSuccessIfAbsent ? " absence " : " existence ") + | ||||||
|                     "finished with HTTP status " + status + (!success?"(FAIL)":"")); |                     "finished with HTTP status " + status + (!isSuccess?"(FAIL)":"")); | ||||||
|              |              | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
| @ -114,9 +126,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | |||||||
|                     result.getLogMessage(), result.getException()); |                     result.getLogMessage(), result.getException()); | ||||||
|              |              | ||||||
|         } finally { |         } finally { | ||||||
|             if (propfind != null) | //            client.setFollowRedirects(previousFollowRedirects); | ||||||
|                 propfind.releaseConnection(); |  | ||||||
|             client.setFollowRedirects(previousFollowRedirects); |  | ||||||
|         } |         } | ||||||
|         return result; |         return result; | ||||||
| 	} | 	} | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| /* ownCloud Android Library is available under MIT license | /* ownCloud Android Library is available under MIT license | ||||||
|  *   Copyright (C) 2016 ownCloud GmbH. |  *   Copyright (C) 2018 ownCloud GmbH. | ||||||
|  * |  * | ||||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy |  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  *   of this software and associated documentation files (the "Software"), to deal |  *   of this software and associated documentation files (the "Software"), to deal | ||||||
| @ -25,27 +25,27 @@ | |||||||
| 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.methods.PropfindMethod; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| import com.owncloud.android.lib.common.network.WebdavEntry; | import com.owncloud.android.lib.common.http.webdav.PropfindMethod; | ||||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | import com.owncloud.android.lib.common.network.WebdavUtils; | ||||||
| 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 org.apache.commons.httpclient.HttpStatus; |  | ||||||
| import org.apache.jackrabbit.webdav.MultiStatus; |  | ||||||
| import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; |  | ||||||
| 
 |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| 
 | 
 | ||||||
| import at.bitfire.dav4android.DavOCResource; | 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; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Remote operation performing the read of remote file or folder in the ownCloud server. |  * Remote operation performing the read of remote file or folder in the ownCloud server. | ||||||
|  * |  * | ||||||
|  * @author David A. Velasco |  * @author David A. Velasco | ||||||
|  * @author masensio |  * @author masensio | ||||||
|  |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| public class ReadRemoteFolderOperation extends RemoteOperation { | public class ReadRemoteFolderOperation extends RemoteOperation { | ||||||
| @ -53,7 +53,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | |||||||
|     private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName(); |     private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
|     private String mRemotePath; |     private String mRemotePath; | ||||||
|     private ArrayList<Object> mFolderAndFiles; |  | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Constructor |      * Constructor | ||||||
| @ -72,55 +71,57 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | |||||||
|     @Override |     @Override | ||||||
|     protected RemoteOperationResult run(OwnCloudClient client) { |     protected RemoteOperationResult run(OwnCloudClient client) { | ||||||
|         RemoteOperationResult result = null; |         RemoteOperationResult result = null; | ||||||
|         PropFindMethod query = null; |  | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
| 
 | 
 | ||||||
|             final HttpUrl location = HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath)); |             PropfindMethod propfindMethod = new PropfindMethod( | ||||||
|             DavOCResource davOCResource = new DavOCResource(client.getOkHttpClient(), location); |                     client.getOkHttpClient(), | ||||||
|             PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 1); |                     HttpUrl.parse(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath)), | ||||||
|  |                     1); | ||||||
| 
 | 
 | ||||||
|             int status = client.executeHttpMethod(propfindMethod); |             Response response = client.executeHttpMethod(propfindMethod); | ||||||
| 
 | 
 | ||||||
|             // TODO Refactor from here down |             boolean isSuccess = (response.code() == HttpConstants.HTTP_MULTI_STATUS | ||||||
|             // remote request |                     || response.code() == HttpConstants.HTTP_OK); | ||||||
| //            query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), | 
 | ||||||
| //                WebdavUtils.getAllPropSet(),    // PropFind Properties |             if (isSuccess) { | ||||||
| //                DavConstants.DEPTH_1); | 
 | ||||||
| //            int status = client.executeMethod(query); |                 ArrayList<Object> mFolderAndFiles = new ArrayList<>(); | ||||||
| // | 
 | ||||||
| //            // check and process response |                 // parse data from remote folder | ||||||
| //            boolean isSuccess = ( |                 mFolderAndFiles.add( | ||||||
| //                status == HttpStatus.SC_MULTI_STATUS || |                         new RemoteFile(propfindMethod.getDavResource(), client.getAccount().getDisplayName()) | ||||||
| //                    status == HttpStatus.SC_OK |                 ); | ||||||
| //            ); | 
 | ||||||
| //            if (isSuccess) { |                 // loop to update every child | ||||||
| //                // get data from remote folder |                 for (DavResource resource : propfindMethod.getMembers()) { | ||||||
| //                MultiStatus dataInServer = query.getResponseBodyAsMultiStatus(); |                     RemoteFile file = new RemoteFile(resource, client.getAccount().getDisplayName()); | ||||||
| //                readData(dataInServer, client); |                     mFolderAndFiles.add(file); | ||||||
| // |                 } | ||||||
| //                // Result of the operation | 
 | ||||||
| //                result = new RemoteOperationResult(true, query); |                 // Result of the operation | ||||||
| //                // Add data to the result |                 result = new RemoteOperationResult(OK); | ||||||
| //                if (result.isSuccess()) { |                 result.setData(mFolderAndFiles); | ||||||
| //                    result.setData(mFolderAndFiles); | 
 | ||||||
| //                } |             } else { | ||||||
| //            } else { | 
 | ||||||
| //                // synchronization failed |                 // synchronization failed | ||||||
| //                result = new RemoteOperationResult(false, query); |                 result = new RemoteOperationResult( | ||||||
| //            } |                         false, | ||||||
|  |                         propfindMethod.getRequest(), | ||||||
|  |                         response | ||||||
|  |                 ); | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
|         } finally { |         } finally { | ||||||
|             if (query != null) |  | ||||||
|                 query.releaseConnection();  // let the connection available for other methods |  | ||||||
|             if (result.isSuccess()) { |             if (result.isSuccess()) { | ||||||
|                 Log_OC.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); |                 Log_OC.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); | ||||||
|             } else { |             } else { | ||||||
|                 if (result.isException()) { |                 if (result.isException()) { | ||||||
|                     Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), |                     Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), | ||||||
|                         result.getException()); |                             result.getException()); | ||||||
|                 } else { |                 } else { | ||||||
|                     Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); |                     Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); | ||||||
|                 } |                 } | ||||||
| @ -128,57 +129,4 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | |||||||
|         } |         } | ||||||
|         return result; |         return result; | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     public boolean isMultiStatus(int status) { |  | ||||||
|         return (status == HttpStatus.SC_MULTI_STATUS); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Read the data retrieved from the server about the contents of the target folder |  | ||||||
|      * |  | ||||||
|      * @param remoteData Full response got from the server with the data of the target |  | ||||||
|      *                   folder and its direct children. |  | ||||||
|      * @param client     Client instance to the remote server where the data were |  | ||||||
|      *                   retrieved. |  | ||||||
|      * @return |  | ||||||
|      */ |  | ||||||
|     private void readData(MultiStatus remoteData, OwnCloudClient client) { |  | ||||||
|         mFolderAndFiles = new ArrayList<Object>(); |  | ||||||
| 
 |  | ||||||
|         // parse data from remote folder  |  | ||||||
|         WebdavEntry we = new WebdavEntry(remoteData.getResponses()[0], |  | ||||||
|             client.getNewWebDavUri().getPath()); |  | ||||||
|         mFolderAndFiles.add(fillOCFile(we)); |  | ||||||
| 
 |  | ||||||
|         // loop to update every child |  | ||||||
|         RemoteFile remoteFile = null; |  | ||||||
|         for (int i = 1; i < remoteData.getResponses().length; ++i) { |  | ||||||
|             /// new OCFile instance with the data from the server |  | ||||||
|             we = new WebdavEntry(remoteData.getResponses()[i], client.getNewWebDavUri().getPath()); |  | ||||||
|             remoteFile = fillOCFile(we); |  | ||||||
|             mFolderAndFiles.add(remoteFile); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Creates and populates a new {@link RemoteFile} object with the data read from the server. |  | ||||||
|      * |  | ||||||
|      * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder). |  | ||||||
|      * @return New OCFile instance representing the remote resource described by we. |  | ||||||
|      */ |  | ||||||
|     private RemoteFile fillOCFile(WebdavEntry we) { |  | ||||||
|         RemoteFile file = new RemoteFile(we.decodedPath()); |  | ||||||
|         file.setCreationTimestamp(we.createTimestamp()); |  | ||||||
|         file.setLength(we.contentLength()); |  | ||||||
|         file.setMimeType(we.contentType()); |  | ||||||
|         file.setModifiedTimestamp(we.modifiedTimestamp()); |  | ||||||
|         file.setEtag(we.etag()); |  | ||||||
|         file.setPermissions(we.permissions()); |  | ||||||
|         file.setRemoteId(we.remoteId()); |  | ||||||
|         file.setSize(we.size()); |  | ||||||
|         file.setQuotaUsedBytes(we.quotaUsedBytes()); |  | ||||||
|         file.setQuotaAvailableBytes(we.quotaAvailableBytes()); |  | ||||||
|         file.setPrivateLink(we.privateLink()); |  | ||||||
|         return file; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @ -31,6 +31,7 @@ import android.net.Uri; | |||||||
| import android.os.Parcel; | import android.os.Parcel; | ||||||
| import android.os.Parcelable; | import android.os.Parcelable; | ||||||
| 
 | 
 | ||||||
|  | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
| import com.owncloud.android.lib.common.network.WebdavEntry; | import com.owncloud.android.lib.common.network.WebdavEntry; | ||||||
| import com.owncloud.android.lib.refactor.OCContext; | import com.owncloud.android.lib.refactor.OCContext; | ||||||
| import com.owncloud.android.lib.refactor.operations.RemoteOperation; | import com.owncloud.android.lib.refactor.operations.RemoteOperation; | ||||||
| @ -173,7 +174,7 @@ public class RemoteFile implements Parcelable, Serializable { | |||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Create new {@link RemoteFile} with given path. |      * Create new {@link RemoteFile} with given path. | ||||||
|      * <p> |      * | ||||||
|      * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'. |      * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'. | ||||||
|      * |      * | ||||||
|      * @param path The remote path of the file. |      * @param path The remote path of the file. | ||||||
| @ -201,8 +202,8 @@ public class RemoteFile implements Parcelable, Serializable { | |||||||
|         this.setPrivateLink(webdavEntry.privateLink()); |         this.setPrivateLink(webdavEntry.privateLink()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public RemoteFile(final DavResource davResource, OCContext context) { |     public RemoteFile(final DavResource davResource, String displayName) { | ||||||
|         this(getRemotePathFromUrl(davResource.getLocation(), context)); |         this(getRemotePathFromUrl(davResource.getLocation(), displayName)); | ||||||
|         final PropertyCollection properties = davResource.getProperties(); |         final PropertyCollection properties = davResource.getProperties(); | ||||||
|         this.setCreationTimestamp(properties.get(CreationDate.class) != null |         this.setCreationTimestamp(properties.get(CreationDate.class) != null | ||||||
|                 ? Long.parseLong(properties.get(CreationDate.class).getCreationDate()) |                 ? Long.parseLong(properties.get(CreationDate.class).getCreationDate()) | ||||||
| @ -227,9 +228,9 @@ public class RemoteFile implements Parcelable, Serializable { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     private static String getRemotePathFromUrl(HttpUrl url, OCContext context) { |     private static String getRemotePathFromUrl(HttpUrl url, String displayName) { | ||||||
|         final String pathToRemove = |         final String pathToRemove = | ||||||
|                 "/" + RemoteOperation.WEBDAV_PATH_4_0 + "/" + context.getOCAccount().getDisplayName(); |                 "/" + RemoteOperation.WEBDAV_PATH_4_0 + "/" + displayName; | ||||||
|         return Uri.decode(url.encodedPath()).replace(pathToRemove, ""); |         return Uri.decode(url.encodedPath()).replace(pathToRemove, ""); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user