mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +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 String mRedirectedLocation;
|
||||
private boolean mFollowRedirects;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -282,6 +283,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
int status;
|
||||
|
||||
do {
|
||||
method.setFollowRedirects(mFollowRedirects);
|
||||
status = method.execute();
|
||||
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
|
||||
if (repeatWithFreshCredentials) {
|
||||
@ -668,4 +670,12 @@ public class OwnCloudClient extends HttpClient {
|
||||
(mRedirectedLocation.toUpperCase().contains("SAML") ||
|
||||
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.
|
||||
* @return A OwnCloudClient object ready to be used
|
||||
*/
|
||||
public static OwnCloudClient createOwnCloudClient(Uri uri, Context context,
|
||||
boolean followRedirects) {
|
||||
public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
|
||||
try {
|
||||
NetworkUtils.registerAdvancedSslContext(true, context);
|
||||
} catch (GeneralSecurityException e) {
|
||||
@ -175,11 +174,10 @@ public class OwnCloudClientFactory {
|
||||
}
|
||||
|
||||
OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager());
|
||||
|
||||
client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
|
||||
|
||||
HttpClient.getOkHttpClient()
|
||||
.newBuilder()
|
||||
.followRedirects(followRedirects);
|
||||
client.setFollowRedirects(followRedirects);
|
||||
|
||||
client.setContext(context);
|
||||
|
||||
|
@ -58,6 +58,7 @@ public class HttpClient {
|
||||
.followRedirects(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
return mOkHttpClient;
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ import com.owncloud.android.lib.common.http.HttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.HttpUrl;
|
||||
@ -57,6 +58,24 @@ public abstract class HttpBaseMethod {
|
||||
.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
|
||||
public String getRequestHeader(String 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.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
|
||||
* in the same account.
|
||||
* <p/>
|
||||
*
|
||||
* Allows renaming the moving file/folder at the same time.
|
||||
*
|
||||
* @author David A. Velasco
|
||||
@ -58,7 +59,6 @@ public class CopyRemoteFileOperation extends RemoteOperation {
|
||||
|
||||
private boolean mOverwrite;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* <p/>
|
||||
@ -74,7 +74,6 @@ public class CopyRemoteFileOperation extends RemoteOperation {
|
||||
mOverwrite = overwrite;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs the rename operation.
|
||||
*
|
||||
@ -108,7 +107,10 @@ public class CopyRemoteFileOperation extends RemoteOperation {
|
||||
HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath)),
|
||||
client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath),
|
||||
mOverwrite);
|
||||
//TODO: apply timeout
|
||||
|
||||
copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||
copyMethod.setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
|
||||
|
||||
final int status = client.executeHttpMethod(copyMethod);
|
||||
|
||||
if(status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) {
|
||||
@ -134,7 +136,6 @@ public class CopyRemoteFileOperation extends RemoteOperation {
|
||||
result = new RemoteOperationResult(e);
|
||||
Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " +
|
||||
result.getLogMessage(), e);
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -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.HttpClient;
|
||||
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||
import com.owncloud.android.lib.common.http.HttpUtils;
|
||||
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.utils.Log_OC;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||
|
||||
/**
|
||||
@ -90,6 +94,9 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
|
||||
0,
|
||||
DavUtils.getAllPropset());
|
||||
|
||||
propfindMethod.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
|
||||
propfindMethod.setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS);
|
||||
|
||||
int status = client.executeHttpMethod(propfindMethod);
|
||||
|
||||
// if (previousFollowRedirects) {
|
||||
|
@ -79,6 +79,8 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
|
||||
DavConstants.DEPTH_1,
|
||||
DavUtils.getAllPropset());
|
||||
|
||||
client.setFollowRedirects(true);
|
||||
|
||||
int status = client.executeHttpMethod(propfindMethod);
|
||||
|
||||
if (isSuccess(status)) {
|
||||
|
@ -40,7 +40,9 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
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,
|
||||
* 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();
|
||||
|
||||
@ -78,14 +80,13 @@ public class GetRemoteStatusOperation extends RemoteOperation {
|
||||
boolean retval = false;
|
||||
String baseUrlSt = client.getBaseUri().toString();
|
||||
try {
|
||||
HttpClient.getOkHttpClient()
|
||||
.newBuilder()
|
||||
.followRedirects(false);
|
||||
|
||||
GetMethod getMethod = new GetMethod(
|
||||
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);
|
||||
|
||||
mLatestResult = new RemoteOperationResult(OK);
|
||||
|
Loading…
x
Reference in New Issue
Block a user