mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-28 17:07:49 +00:00 
			
		
		
		
	Create http + webdav methods wrapeer
This commit is contained in:
		
							parent
							
								
									b9ba124541
								
							
						
					
					
						commit
						11938a1fcc
					
				| @ -45,12 +45,11 @@ import org.apache.commons.httpclient.HttpConnectionManager; | ||||
| import org.apache.commons.httpclient.HttpMethod; | ||||
| import org.apache.commons.httpclient.HttpMethodBase; | ||||
| import org.apache.commons.httpclient.HttpStatus; | ||||
| import org.apache.commons.httpclient.HttpVersion; | ||||
| import org.apache.commons.httpclient.URI; | ||||
| import org.apache.commons.httpclient.URIException; | ||||
| import org.apache.commons.httpclient.cookie.CookiePolicy; | ||||
| import org.apache.commons.httpclient.params.HttpMethodParams; | ||||
| import org.apache.commons.httpclient.params.HttpParams; | ||||
| import com.owncloud.android.lib.common.methods.HttpBaseMethod; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| @ -103,47 +102,50 @@ public class OwnCloudClient extends HttpClient { | ||||
| 
 | ||||
|     private String mRedirectedLocation; | ||||
| 
 | ||||
|     private static OkHttpClient mClient = null; | ||||
|     private static OkHttpClient mOkHttpClient = null; | ||||
| 
 | ||||
|     /** | ||||
|      * Constructor | ||||
|      */ | ||||
| 
 | ||||
| //    public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { | ||||
| // | ||||
| //        super(connectionMgr); | ||||
| // | ||||
| //        if (baseUri == null) { | ||||
| //            throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); | ||||
| //        } | ||||
| //        mBaseUri = baseUri; | ||||
| // | ||||
| //        mInstanceNumber = sIntanceCounter++; | ||||
| //        Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); | ||||
| // | ||||
| //        String userAgent = OwnCloudClientManagerFactory.getUserAgent(); | ||||
| //        getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); | ||||
| //        getParams().setParameter( | ||||
| //                PARAM_PROTOCOL_VERSION, | ||||
| //                HttpVersion.HTTP_1_1 | ||||
| //        ); | ||||
| // | ||||
| //        getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); | ||||
| //        getParams().setParameter( | ||||
| //                PARAM_SINGLE_COOKIE_HEADER,             // to avoid problems with some web servers | ||||
| //                PARAM_SINGLE_COOKIE_HEADER_VALUE | ||||
| //        ); | ||||
| // | ||||
| //        applyProxySettings(); | ||||
| // | ||||
| //        clearCredentials(); | ||||
| //    } | ||||
| 
 | ||||
|     public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { | ||||
| 
 | ||||
|         super(connectionMgr); | ||||
| 
 | ||||
|         if (baseUri == null) { | ||||
|             throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); | ||||
|         } | ||||
|         mBaseUri = baseUri; | ||||
| 
 | ||||
|         mInstanceNumber = sIntanceCounter++; | ||||
|         Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); | ||||
| 
 | ||||
|         String userAgent = OwnCloudClientManagerFactory.getUserAgent(); | ||||
|         getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); | ||||
|         getParams().setParameter( | ||||
|                 PARAM_PROTOCOL_VERSION, | ||||
|                 HttpVersion.HTTP_1_1 | ||||
|         ); | ||||
| 
 | ||||
|         getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); | ||||
|         getParams().setParameter( | ||||
|                 PARAM_SINGLE_COOKIE_HEADER,             // to avoid problems with some web servers | ||||
|                 PARAM_SINGLE_COOKIE_HEADER_VALUE | ||||
|         ); | ||||
| 
 | ||||
|         applyProxySettings(); | ||||
| 
 | ||||
|         clearCredentials(); | ||||
|     } | ||||
| 
 | ||||
|     public OwnCloudClient(Uri baseUri) { | ||||
| 
 | ||||
|         String userAgent = OwnCloudClientManagerFactory.getUserAgent(); | ||||
| 
 | ||||
|         if (mClient == null) { | ||||
|             new OkHttpClient.Builder() | ||||
|         if (mOkHttpClient == null) { | ||||
|             mOkHttpClient = new OkHttpClient.Builder() | ||||
|                     .addInterceptor(chain -> | ||||
|                             chain.proceed( | ||||
|                                     chain.request() | ||||
| @ -175,10 +177,8 @@ public class OwnCloudClient extends HttpClient { | ||||
| //        applyProxySettings(); | ||||
| 
 | ||||
|         clearCredentials(); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     private void applyProxySettings() { | ||||
|         String proxyHost = System.getProperty("http.proxyHost"); | ||||
|         String proxyPortSt = System.getProperty("http.proxyPort"); | ||||
| @ -198,7 +198,6 @@ public class OwnCloudClient extends HttpClient { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public void setCredentials(OwnCloudCredentials credentials) { | ||||
|         if (credentials != null) { | ||||
|             mCredentials = credentials; | ||||
| @ -248,7 +247,6 @@ public class OwnCloudClient extends HttpClient { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * Requests the received method. | ||||
|      * | ||||
| @ -300,6 +298,11 @@ public class OwnCloudClient extends HttpClient { | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     public int executeHttpMethod (HttpBaseMethod method) throws Exception { | ||||
|         int status = method.execute(); | ||||
|         return status; | ||||
|     } | ||||
| 
 | ||||
|     private void checkFirstRedirection(HttpMethod method) { | ||||
|         Header[] httpHeaders = method.getResponseHeaders(); | ||||
| 
 | ||||
| @ -338,7 +341,6 @@ public class OwnCloudClient extends HttpClient { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public RedirectionPath followRedirection(HttpMethod method) throws IOException { | ||||
|         int redirectionsCount = 0; | ||||
|         int status = method.getStatusCode(); | ||||
| @ -562,6 +564,10 @@ public class OwnCloudClient extends HttpClient { | ||||
|         return mAccount; | ||||
|     } | ||||
| 
 | ||||
|     public OkHttpClient getOkHttpClient() { | ||||
|         return mOkHttpClient; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Enables or disables silent refresh of credentials, if supported by credentials themselves. | ||||
|      */ | ||||
|  | ||||
| @ -25,15 +25,17 @@ package com.owncloud.android.lib.common.authentication; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.UsernamePasswordCredentials; | ||||
| import org.apache.commons.httpclient.auth.AuthPolicy; | ||||
| import org.apache.commons.httpclient.auth.AuthScope; | ||||
| import org.apache.commons.httpclient.auth.AuthState; | ||||
| import org.apache.commons.httpclient.auth.BasicScheme; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import okhttp3.Credentials; | ||||
| 
 | ||||
| public class OwnCloudBasicCredentials implements OwnCloudCredentials { | ||||
| 
 | ||||
|     private String mUsername; | ||||
| @ -58,14 +60,22 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { | ||||
| 
 | ||||
|         List<String> authPrefs = new ArrayList<String>(1); | ||||
|         authPrefs.add(AuthPolicy.BASIC); | ||||
|         client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); | ||||
| 
 | ||||
|         client.getOkHttpClient().newBuilder() | ||||
|                 .addInterceptor(chain -> | ||||
|                         chain.proceed( | ||||
|                                 chain.request() | ||||
|                                         .newBuilder() | ||||
|                                         .addHeader("Authorization", Credentials.basic(mUsername, mPassword)) | ||||
|                                         .build() | ||||
|                         ) | ||||
|                 ).build(); | ||||
| 
 | ||||
|         //TODO | ||||
|         client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); | ||||
|         client.getParams().setAuthenticationPreemptive(mAuthenticationPreemptive); | ||||
|         client.getParams().setCredentialCharset(OwnCloudCredentialsFactory.CREDENTIAL_CHARSET); | ||||
|         client.getState().setCredentials( | ||||
|             AuthScope.ANY, | ||||
|             new UsernamePasswordCredentials(mUsername, mPassword) | ||||
|         ); | ||||
|         client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(mUsername, mPassword)); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | ||||
							
								
								
									
										14
									
								
								src/com/owncloud/android/lib/common/methods/DavMethod.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/com/owncloud/android/lib/common/methods/DavMethod.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| package com.owncloud.android.lib.common.methods; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import at.bitfire.dav4android.DavResource; | ||||
| 
 | ||||
| public abstract class DavMethod implements HttpBaseMethod { | ||||
| 
 | ||||
|     protected DavResource mDavResource; | ||||
| 
 | ||||
|     public DavMethod(DavResource davResource) { | ||||
|         mDavResource = davResource; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,6 @@ | ||||
| package com.owncloud.android.lib.common.methods; | ||||
| 
 | ||||
| public interface HttpBaseMethod { | ||||
| 
 | ||||
|     int execute() throws Exception; | ||||
| } | ||||
| @ -0,0 +1,23 @@ | ||||
| package com.owncloud.android.lib.common.methods; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import at.bitfire.dav4android.DavResource; | ||||
| import at.bitfire.dav4android.PropertyUtils; | ||||
| import at.bitfire.dav4android.exception.DavException; | ||||
| import at.bitfire.dav4android.exception.HttpException; | ||||
| 
 | ||||
| public class PropfindMethod extends DavMethod { | ||||
| 
 | ||||
|     private int mDepth; | ||||
| 
 | ||||
|     public PropfindMethod(DavResource davResource, int depth) { | ||||
|         super(davResource); | ||||
|         mDepth = depth; | ||||
|     }; | ||||
| 
 | ||||
|     public int execute() throws DavException, IOException, HttpException { | ||||
|         mDavResource.propfind(mDepth, PropertyUtils.INSTANCE.getAllPropSet()); | ||||
|         return mDavResource.getResponse().code(); | ||||
|     } | ||||
| } | ||||
| @ -92,11 +92,12 @@ public class UploadRemoteFileOperation extends RemoteOperation<Void> { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| //    public void cancel() { | ||||
| //        synchronized (mCancellationRequested) { | ||||
| //            mCancellationRequested.set(true); | ||||
|     public void cancel() { | ||||
|         synchronized (mCancellationRequested) { | ||||
|             mCancellationRequested.set(true); | ||||
|             // TODO | ||||
| //            if (mPutMethod != null) | ||||
| //                mPutMethod.abort(); | ||||
| //        } | ||||
| //    } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -24,21 +24,22 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.files; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.HttpStatus; | ||||
| import org.apache.jackrabbit.webdav.DavConstants; | ||||
| import org.apache.jackrabbit.webdav.MultiStatus; | ||||
| import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.methods.PropfindMethod; | ||||
| import com.owncloud.android.lib.common.network.WebdavEntry; | ||||
| import com.owncloud.android.lib.common.network.WebdavUtils; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| import at.bitfire.dav4android.DavResource; | ||||
| 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 at.bitfire.dav4android.DavOCResource; | ||||
| import okhttp3.HttpUrl; | ||||
| 
 | ||||
| /** | ||||
|  * Remote operation performing the read of remote file or folder in the ownCloud server. | ||||
| @ -75,32 +76,40 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | ||||
| 
 | ||||
|         try { | ||||
| 
 | ||||
|             final HttpUrl location = HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); | ||||
|             DavOCResource davOCResource = new DavOCResource(client.getOkHttpClient(), location); | ||||
|             PropfindMethod propfindMethod = new PropfindMethod(davOCResource, 1); | ||||
| 
 | ||||
|             int status = client.executeHttpMethod(propfindMethod); | ||||
| 
 | ||||
|             // TODO Refactor from here down | ||||
| 
 | ||||
|             // remote request | ||||
|             query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), | ||||
|                 WebdavUtils.getAllPropSet(),    // PropFind Properties | ||||
|                 DavConstants.DEPTH_1); | ||||
|             int status = client.executeMethod(query); | ||||
| 
 | ||||
|             // check and process response | ||||
|             boolean isSuccess = ( | ||||
|                 status == HttpStatus.SC_MULTI_STATUS || | ||||
|                     status == HttpStatus.SC_OK | ||||
|             ); | ||||
|             if (isSuccess) { | ||||
|                 // get data from remote folder | ||||
|                 MultiStatus dataInServer = query.getResponseBodyAsMultiStatus(); | ||||
|                 readData(dataInServer, client); | ||||
| 
 | ||||
|                 // Result of the operation | ||||
|                 result = new RemoteOperationResult(true, query); | ||||
|                 // Add data to the result | ||||
|                 if (result.isSuccess()) { | ||||
|                     result.setData(mFolderAndFiles); | ||||
|                 } | ||||
|             } else { | ||||
|                 // synchronization failed | ||||
|                 result = new RemoteOperationResult(false, query); | ||||
|             } | ||||
| //            query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath), | ||||
| //                WebdavUtils.getAllPropSet(),    // PropFind Properties | ||||
| //                DavConstants.DEPTH_1); | ||||
| //            int status = client.executeMethod(query); | ||||
| // | ||||
| //            // check and process response | ||||
| //            boolean isSuccess = ( | ||||
| //                status == HttpStatus.SC_MULTI_STATUS || | ||||
| //                    status == HttpStatus.SC_OK | ||||
| //            ); | ||||
| //            if (isSuccess) { | ||||
| //                // get data from remote folder | ||||
| //                MultiStatus dataInServer = query.getResponseBodyAsMultiStatus(); | ||||
| //                readData(dataInServer, client); | ||||
| // | ||||
| //                // Result of the operation | ||||
| //                result = new RemoteOperationResult(true, query); | ||||
| //                // Add data to the result | ||||
| //                if (result.isSuccess()) { | ||||
| //                    result.setData(mFolderAndFiles); | ||||
| //                } | ||||
| //            } else { | ||||
| //                // synchronization failed | ||||
| //                result = new RemoteOperationResult(false, query); | ||||
| //            } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
| @ -119,7 +128,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | ||||
|                     Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage()); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user