mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Include timeouts and short refactoring
This commit is contained in:
		
							parent
							
								
									73b4ffe0b9
								
							
						
					
					
						commit
						6a375dcb93
					
				| @ -96,6 +96,7 @@ public class OwnCloudClient extends HttpClient { | |||||||
|     private boolean mSilentRefreshOfAccountCredentials = true; |     private boolean mSilentRefreshOfAccountCredentials = true; | ||||||
| 
 | 
 | ||||||
|     private String mRedirectedLocation; |     private String mRedirectedLocation; | ||||||
|  |     private boolean mFollowRedirects; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Constructor |      * Constructor | ||||||
| @ -282,6 +283,7 @@ public class OwnCloudClient extends HttpClient { | |||||||
|         int status; |         int status; | ||||||
| 
 | 
 | ||||||
|         do { |         do { | ||||||
|  |             method.setFollowRedirects(mFollowRedirects); | ||||||
|             status = method.execute(); |             status = method.execute(); | ||||||
|             repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter); |             repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter); | ||||||
|             if (repeatWithFreshCredentials) { |             if (repeatWithFreshCredentials) { | ||||||
| @ -668,4 +670,12 @@ public class OwnCloudClient extends HttpClient { | |||||||
|                 (mRedirectedLocation.toUpperCase().contains("SAML") || |                 (mRedirectedLocation.toUpperCase().contains("SAML") || | ||||||
|                         mRedirectedLocation.toLowerCase().contains("wayf"))); |                         mRedirectedLocation.toLowerCase().contains("wayf"))); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isFollowRedirects() { | ||||||
|  |         return mFollowRedirects; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setFollowRedirects(boolean followRedirects) { | ||||||
|  |         this.mFollowRedirects = followRedirects; | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -161,8 +161,7 @@ public class OwnCloudClientFactory { | |||||||
|      * @param context   Android context where the OwnCloudClient is being created. |      * @param context   Android context where the OwnCloudClient is being created. | ||||||
|      * @return          A OwnCloudClient object ready to be used |      * @return          A OwnCloudClient object ready to be used | ||||||
|      */ |      */ | ||||||
|     public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, |     public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) { | ||||||
|                                                       boolean followRedirects) { |  | ||||||
|         try { |         try { | ||||||
|             NetworkUtils.registerAdvancedSslContext(true, context); |             NetworkUtils.registerAdvancedSslContext(true, context); | ||||||
|         }  catch (GeneralSecurityException e) { |         }  catch (GeneralSecurityException e) { | ||||||
| @ -175,11 +174,10 @@ public class OwnCloudClientFactory { | |||||||
|         } |         } | ||||||
|          |          | ||||||
|         OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); |         OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); | ||||||
|  | 
 | ||||||
|         client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); |         client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); | ||||||
| 
 | 
 | ||||||
|         HttpClient.getOkHttpClient() |         client.setFollowRedirects(followRedirects); | ||||||
|                 .newBuilder() |  | ||||||
|                 .followRedirects(followRedirects); |  | ||||||
| 
 | 
 | ||||||
|         client.setContext(context); |         client.setContext(context); | ||||||
|          |          | ||||||
|  | |||||||
| @ -58,6 +58,7 @@ public class HttpClient { | |||||||
|                     .followRedirects(false) |                     .followRedirects(false) | ||||||
|                     .build(); |                     .build(); | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|         return mOkHttpClient; |         return mOkHttpClient; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -28,6 +28,7 @@ import com.owncloud.android.lib.common.http.HttpClient; | |||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.io.InputStream; | import java.io.InputStream; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
| import okhttp3.Headers; | import okhttp3.Headers; | ||||||
| import okhttp3.HttpUrl; | import okhttp3.HttpUrl; | ||||||
| @ -57,6 +58,24 @@ public abstract class HttpBaseMethod { | |||||||
|                 .build(); |                 .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public void setReadTimeout(long readTimeout, TimeUnit timeUnit) { | ||||||
|  |         mOkHttpClient = mOkHttpClient.newBuilder() | ||||||
|  |                 .readTimeout(readTimeout, timeUnit) | ||||||
|  |                 .build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setConnectionTimeout(long connectionTimeout, TimeUnit timeUnit) { | ||||||
|  |         mOkHttpClient = mOkHttpClient.newBuilder() | ||||||
|  |                 .readTimeout(connectionTimeout, timeUnit) | ||||||
|  |                 .build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setFollowRedirects(boolean followRedirects) { | ||||||
|  |         mOkHttpClient = mOkHttpClient.newBuilder() | ||||||
|  |                 .followRedirects(followRedirects) | ||||||
|  |                 .build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     // Request |     // Request | ||||||
|     public String getRequestHeader(String name) { |     public String getRequestHeader(String name) { | ||||||
|         return mRequest.header(name); |         return mRequest.header(name); | ||||||
|  | |||||||
| @ -35,13 +35,14 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; | |||||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||||
| import com.owncloud.android.lib.resources.status.OwnCloudVersion; | import com.owncloud.android.lib.resources.status.OwnCloudVersion; | ||||||
| 
 | 
 | ||||||
| import okhttp3.HttpUrl; | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
|  | import okhttp3.HttpUrl; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Remote operation moving a remote file or folder in the ownCloud server to a different folder |  * Remote operation moving a remote file or folder in the ownCloud server to a different folder | ||||||
|  * in the same account. |  * in the same account. | ||||||
|  * <p/> |  * | ||||||
|  * Allows renaming the moving file/folder at the same time. |  * Allows renaming the moving file/folder at the same time. | ||||||
|  * |  * | ||||||
|  * @author David A. Velasco |  * @author David A. Velasco | ||||||
| @ -58,7 +59,6 @@ public class CopyRemoteFileOperation extends RemoteOperation { | |||||||
| 
 | 
 | ||||||
|     private boolean mOverwrite; |     private boolean mOverwrite; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * Constructor. |      * Constructor. | ||||||
|      * <p/> |      * <p/> | ||||||
| @ -74,7 +74,6 @@ public class CopyRemoteFileOperation extends RemoteOperation { | |||||||
|         mOverwrite = overwrite; |         mOverwrite = overwrite; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * Performs the rename operation. |      * Performs the rename operation. | ||||||
|      * |      * | ||||||
| @ -108,7 +107,10 @@ public class CopyRemoteFileOperation extends RemoteOperation { | |||||||
|                     HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath)), |                     HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath)), | ||||||
|                     client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), |                     client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), | ||||||
|                     mOverwrite); |                     mOverwrite); | ||||||
|             //TODO: apply timeout | 
 | ||||||
|  |             copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS); | ||||||
|  |             copyMethod.setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
|  | 
 | ||||||
|             final int status = client.executeHttpMethod(copyMethod); |             final int status = client.executeHttpMethod(copyMethod); | ||||||
| 
 | 
 | ||||||
|             if(status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) { |             if(status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) { | ||||||
| @ -134,7 +136,6 @@ public class CopyRemoteFileOperation extends RemoteOperation { | |||||||
|             result = new RemoteOperationResult(e); |             result = new RemoteOperationResult(e); | ||||||
|             Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + |             Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + | ||||||
|                     result.getLogMessage(), e); |                     result.getLogMessage(), e); | ||||||
| 
 |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return result; |         return result; | ||||||
|  | |||||||
| @ -25,6 +25,7 @@ | |||||||
| package com.owncloud.android.lib.resources.files; | package com.owncloud.android.lib.resources.files; | ||||||
| 
 | 
 | ||||||
| import com.owncloud.android.lib.common.OwnCloudClient; | import com.owncloud.android.lib.common.OwnCloudClient; | ||||||
|  | import com.owncloud.android.lib.common.http.HttpClient; | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants; | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
| import com.owncloud.android.lib.common.http.HttpUtils; | import com.owncloud.android.lib.common.http.HttpUtils; | ||||||
| import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; | import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; | ||||||
| @ -35,6 +36,9 @@ 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 java.sql.Time; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | 
 | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -90,6 +94,9 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation { | |||||||
|                     0, |                     0, | ||||||
|                     DavUtils.getAllPropset()); |                     DavUtils.getAllPropset()); | ||||||
| 
 | 
 | ||||||
|  |             propfindMethod.setReadTimeout(TIMEOUT, TimeUnit.SECONDS); | ||||||
|  |             propfindMethod.setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS); | ||||||
|  | 
 | ||||||
|             int status = client.executeHttpMethod(propfindMethod); |             int status = client.executeHttpMethod(propfindMethod); | ||||||
| 
 | 
 | ||||||
| //            if (previousFollowRedirects) { | //            if (previousFollowRedirects) { | ||||||
|  | |||||||
| @ -79,6 +79,8 @@ public class ReadRemoteFolderOperation extends RemoteOperation { | |||||||
|                     DavConstants.DEPTH_1, |                     DavConstants.DEPTH_1, | ||||||
|                     DavUtils.getAllPropset()); |                     DavUtils.getAllPropset()); | ||||||
| 
 | 
 | ||||||
|  |             client.setFollowRedirects(true); | ||||||
|  | 
 | ||||||
|             int status = client.executeHttpMethod(propfindMethod); |             int status = client.executeHttpMethod(propfindMethod); | ||||||
| 
 | 
 | ||||||
|             if (isSuccess(status)) { |             if (isSuccess(status)) { | ||||||
|  | |||||||
| @ -40,7 +40,9 @@ import com.owncloud.android.lib.common.utils.Log_OC; | |||||||
| import org.json.JSONException; | import org.json.JSONException; | ||||||
| import org.json.JSONObject; | import org.json.JSONObject; | ||||||
| 
 | 
 | ||||||
|  | import java.sql.Time; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||||
| 
 | 
 | ||||||
| @ -58,7 +60,7 @@ public class GetRemoteStatusOperation extends RemoteOperation { | |||||||
|      * Maximum time to wait for a response from the server when the connection is being tested, |      * Maximum time to wait for a response from the server when the connection is being tested, | ||||||
|      * in MILLISECONDs. |      * in MILLISECONDs. | ||||||
|      */ |      */ | ||||||
|     public static final int TRY_CONNECTION_TIMEOUT = 5000; |     public static final long TRY_CONNECTION_TIMEOUT = 5000; | ||||||
| 
 | 
 | ||||||
|     private static final String TAG = GetRemoteStatusOperation.class.getSimpleName(); |     private static final String TAG = GetRemoteStatusOperation.class.getSimpleName(); | ||||||
| 
 | 
 | ||||||
| @ -78,14 +80,13 @@ public class GetRemoteStatusOperation extends RemoteOperation { | |||||||
|         boolean retval = false; |         boolean retval = false; | ||||||
|         String baseUrlSt = client.getBaseUri().toString(); |         String baseUrlSt = client.getBaseUri().toString(); | ||||||
|         try { |         try { | ||||||
|             HttpClient.getOkHttpClient() |  | ||||||
|                     .newBuilder() |  | ||||||
|                     .followRedirects(false); |  | ||||||
| 
 |  | ||||||
|             GetMethod getMethod = new GetMethod( |             GetMethod getMethod = new GetMethod( | ||||||
|                     HttpUtils.stringUrlToHttpUrl(baseUrlSt + OwnCloudClient.STATUS_PATH) |                     HttpUtils.stringUrlToHttpUrl(baseUrlSt + OwnCloudClient.STATUS_PATH) | ||||||
|             ); |             ); | ||||||
| 
 | 
 | ||||||
|  |             getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
|  |             getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); | ||||||
|  | 
 | ||||||
|             int status = client.executeHttpMethod(getMethod); |             int status = client.executeHttpMethod(getMethod); | ||||||
| 
 | 
 | ||||||
|             mLatestResult = new RemoteOperationResult(OK); |             mLatestResult = new RemoteOperationResult(OK); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user