1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

make redirection actually work ... finaly

This commit is contained in:
theScrabi 2018-07-05 15:25:51 +02:00 committed by davigonz
parent bd5dc20842
commit c33c7c551d
21 changed files with 102 additions and 86 deletions

@ -1 +1 @@
Subproject commit 1108048f1a2ff12103cdf6a95307a314d621c088 Subproject commit c6c06a144a1eaf3a75035dfe8700c42b86b0d223

View File

@ -174,7 +174,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
}); });
}).start(); }).start();
// ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR); // ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(FileUtils.PATH_SEPARATOR);
// refreshOperation.execute(mClient, this, mHandler); // refreshOperation.onExecute(mClient, this, mHandler);
} }
private void startUpload() { private void startUpload() {
@ -210,7 +210,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
// UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp); // UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), remotePath, mimeType, timeStamp);
// uploadOperation.addDatatransferProgressListener(this); // uploadOperation.addDatatransferProgressListener(this);
// uploadOperation.execute(mClient, this, mHandler); // uploadOperation.onExecute(mClient, this, mHandler);
} }
private void startRemoteDeletion() { private void startRemoteDeletion() {
@ -237,7 +237,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
// RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); // RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
// removeOperation.execute(mClient, this, mHandler); // removeOperation.onExecute(mClient, this, mHandler);
} }
private void startDownload() { private void startDownload() {
@ -268,7 +268,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
// DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath()); // DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, downFolder.getAbsolutePath());
// downloadOperation.addDatatransferProgressListener(this); // downloadOperation.addDatatransferProgressListener(this);
// downloadOperation.execute(mClient, this, mHandler); // downloadOperation.onExecute(mClient, this, mHandler);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@ -132,22 +132,13 @@ public class OwnCloudClient extends HttpClient {
boolean repeatWithFreshCredentials; boolean repeatWithFreshCredentials;
int repeatCounter = 0; int repeatCounter = 0;
int status = -1; int status;
do { do {
try { status = method.execute();
status = method.execute(); checkFirstRedirection(method);
checkFirstRedirection(method); if(mFollowRedirects && !isIdPRedirection()) {
if(mFollowRedirects) { status = followRedirection(method).getLastStatus();
status = followRedirection(method).getLastStatus();
}
} catch (RedirectException e) {
// redirect must be handled twice. Once for dav4droid and once okhttp redirect errors
status = e.getStatus();
mRedirectedLocation = e.getRedirectLocation();
if(mFollowRedirects) {
status = followRedirection(method).getLastStatus();
}
} }
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter); repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
@ -160,7 +151,6 @@ public class OwnCloudClient extends HttpClient {
} }
private void checkFirstRedirection(HttpBaseMethod method) { private void checkFirstRedirection(HttpBaseMethod method) {
final String location = method.getResponseHeader("location"); final String location = method.getResponseHeader("location");
if(location != null && !location.isEmpty()) { if(location != null && !location.isEmpty()) {
mRedirectedLocation = location; mRedirectedLocation = location;
@ -414,7 +404,7 @@ public class OwnCloudClient extends HttpClient {
mOwnCloudClientManager.removeClientFor(mAccount); mOwnCloudClientManager.removeClientFor(mAccount);
} }
} }
// else: execute will finish with status 401 // else: onExecute will finish with status 401
} }
return credentialsWereRefreshed; return credentialsWereRefreshed;

View File

@ -45,6 +45,7 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER);
HttpClient.addHeaderForAllRequests(HttpConstants.COOKIE_HEADER, mSessionCookie); HttpClient.addHeaderForAllRequests(HttpConstants.COOKIE_HEADER, mSessionCookie);
client.setFollowRedirects(false);
} }
@Override @Override

View File

@ -107,7 +107,7 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
postMethod.setRequestBody(requestBody); postMethod.setRequestBody(requestBody);
// Do the B***S*** Switch and execute // Do the B***S*** Switch and onExecute
OwnCloudCredentials oauthCredentials = OwnCloudCredentials oauthCredentials =
new OwnCloudBasicCredentials(mClientId, mClientSecret); new OwnCloudBasicCredentials(mClientId, mClientSecret);
OwnCloudCredentials oldCredentials = switchClientCredentials(oauthCredentials); OwnCloudCredentials oldCredentials = switchClientCredentials(oauthCredentials);

View File

@ -46,7 +46,6 @@ import okhttp3.Response;
* @author David González Verdugo * @author David González Verdugo
*/ */
public abstract class HttpBaseMethod { public abstract class HttpBaseMethod {
public abstract int execute() throws Exception;
protected OkHttpClient mOkHttpClient; protected OkHttpClient mOkHttpClient;
protected Request mRequest; protected Request mRequest;
protected RequestBody mRequestBody; protected RequestBody mRequestBody;
@ -60,6 +59,10 @@ public abstract class HttpBaseMethod {
.build(); .build();
} }
public int execute() throws Exception {
return onExecute();
}
public void abort() { public void abort() {
mCall.cancel(); mCall.cancel();
} }
@ -68,6 +71,13 @@ public abstract class HttpBaseMethod {
return mCall.isCanceled(); return mCall.isCanceled();
} }
//////////////////////////////
// For override
//////////////////////////////
protected abstract int onExecute() throws Exception;
////////////////////////////// //////////////////////////////
// Getter // Getter
////////////////////////////// //////////////////////////////

View File

@ -39,11 +39,11 @@ public class DeleteMethod extends HttpMethod{
} }
@Override @Override
public int execute() throws IOException { public int onExecute() throws IOException {
mRequest = mRequest.newBuilder() mRequest = mRequest.newBuilder()
.delete() .delete()
.build(); .build();
return super.execute(); return super.onExecute();
} }
} }

View File

@ -39,11 +39,11 @@ public class GetMethod extends HttpMethod {
} }
@Override @Override
public int execute() throws IOException { public int onExecute() throws IOException {
mRequest = mRequest.newBuilder() mRequest = mRequest.newBuilder()
.get() .get()
.build(); .build();
return super.execute(); return super.onExecute();
} }
} }

View File

@ -43,7 +43,7 @@ public abstract class HttpMethod extends HttpBaseMethod {
} }
@Override @Override
public int execute() throws IOException { public int onExecute() throws IOException {
mCall = mOkHttpClient.newCall(mRequest); mCall = mOkHttpClient.newCall(mRequest);
mResponse = mCall.execute(); mResponse = mCall.execute();
return super.getStatusCode(); return super.getStatusCode();

View File

@ -27,7 +27,6 @@ package com.owncloud.android.lib.common.http.methods.nonwebdav;
import java.io.IOException; import java.io.IOException;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.RequestBody;
/** /**
* OkHttp post calls wrapper * OkHttp post calls wrapper
@ -40,11 +39,11 @@ public class PostMethod extends HttpMethod {
} }
@Override @Override
public int execute() throws IOException { public int onExecute() throws IOException {
mRequest = mRequest.newBuilder() mRequest = mRequest.newBuilder()
.post(mRequestBody) .post(mRequestBody)
.build(); .build();
return super.execute(); return super.onExecute();
} }
} }

View File

@ -27,7 +27,6 @@ package com.owncloud.android.lib.common.http.methods.nonwebdav;
import java.io.IOException; import java.io.IOException;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.RequestBody;
public class PutMethod extends HttpMethod{ public class PutMethod extends HttpMethod{
@ -36,11 +35,11 @@ public class PutMethod extends HttpMethod{
} }
@Override @Override
public int execute() throws IOException { public int onExecute() throws IOException {
mRequest = mRequest.newBuilder() mRequest = mRequest.newBuilder()
.put(mRequestBody) .put(mRequestBody)
.build(); .build();
return super.execute(); return super.onExecute();
} }
} }

View File

@ -15,7 +15,7 @@ public class CopyMethod extends DavMethod {
} }
@Override @Override
public int execute() throws Exception { public int onExecute() throws Exception {
try { try {
mDavResource.copy(destinationUrl, forceOverride); mDavResource.copy(destinationUrl, forceOverride);

View File

@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit;
import at.bitfire.dav4android.DavOCResource; import at.bitfire.dav4android.DavOCResource;
import at.bitfire.dav4android.DavResource; import at.bitfire.dav4android.DavResource;
import at.bitfire.dav4android.exception.RedirectException;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
/** /**
@ -50,9 +51,6 @@ public abstract class DavMethod extends HttpBaseMethod {
mDavResource.setFollowRedirects(false); mDavResource.setFollowRedirects(false);
} }
public DavResource getDavResource() {
return mDavResource;
}
@Override @Override
public void abort() { public void abort() {
@ -60,10 +58,19 @@ public abstract class DavMethod extends HttpBaseMethod {
} }
@Override @Override
public boolean isAborted() { public int execute() throws Exception {
return mDavResource.isCallAborted(); try {
return onExecute();
} catch(RedirectException e) {
mResponse = getDavResource().getResponse();
return getStatusCode();
}
} }
//////////////////////////////
// setter
//////////////////////////////
// Connection parameters // Connection parameters
@Override @Override
public void setReadTimeout(long readTimeout, TimeUnit timeUnit) { public void setReadTimeout(long readTimeout, TimeUnit timeUnit) {
@ -85,8 +92,26 @@ public abstract class DavMethod extends HttpBaseMethod {
mDavResource.setRetryOnConnectionFailure(retryOnConnectionFailure); mDavResource.setRetryOnConnectionFailure(retryOnConnectionFailure);
} }
//////////////////////////////
// getter
//////////////////////////////
@Override @Override
public boolean getRetryOnConnectionFailure() { public boolean getRetryOnConnectionFailure() {
return mDavResource.isRetryOnConnectionFailure(); return mDavResource.isRetryOnConnectionFailure();
} }
@Override
public boolean isAborted() {
return mDavResource.isCallAborted();
}
public DavResource getDavResource() {
return mDavResource;
}
public void setUrl(HttpUrl url) {
mDavResource = new DavOCResource(mOkHttpClient, url);
}
} }

View File

@ -9,7 +9,7 @@ public class MkColMethod extends DavMethod {
} }
@Override @Override
public int execute() throws Exception { public int onExecute() throws Exception {
try { try {
mDavResource.mkCol(null); mDavResource.mkCol(null);

View File

@ -16,7 +16,7 @@ public class MoveMethod extends DavMethod {
} }
@Override @Override
public int execute() throws Exception { public int onExecute() throws Exception {
try { try {
mDavResource.move( mDavResource.move(
destinationUrl, destinationUrl,

View File

@ -51,7 +51,7 @@ public class PropfindMethod extends DavMethod {
}; };
@Override @Override
public int execute() throws IOException, HttpException, DavException { public int onExecute() throws IOException, HttpException, DavException {
try { try {
mDavResource.propfind(mDepth, mProperties); mDavResource.propfind(mDepth, mProperties);
mMembers = mDavResource.getMembers(); mMembers = mDavResource.getMembers();

View File

@ -45,7 +45,7 @@ public class PutMethod extends DavMethod {
}; };
@Override @Override
public int execute() throws IOException, HttpException { public int onExecute() throws IOException, HttpException {
try { try {
mDavResource.put( mDavResource.put(
mRequestBody, mRequestBody,

View File

@ -45,7 +45,7 @@ import okhttp3.OkHttpClient;
/** /**
* Operation which execution involves one or several interactions with an ownCloud server. * Operation which execution involves one or several interactions with an ownCloud server.
* *
* Provides methods to execute the operation both synchronously or asynchronously. * Provides methods to onExecute the operation both synchronously or asynchronously.
* *
* @author David A. Velasco * @author David A. Velasco
* @author David González Verdugo * @author David González Verdugo
@ -115,10 +115,10 @@ public abstract class RemoteOperation implements Runnable {
*/ */
public RemoteOperationResult execute(Account account, Context context) { public RemoteOperationResult execute(Account account, Context context) {
if (account == null) if (account == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + throw new IllegalArgumentException("Trying to onExecute a remote operation with a NULL " +
"Account"); "Account");
if (context == null) if (context == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + throw new IllegalArgumentException("Trying to onExecute a remote operation with a NULL " +
"Context"); "Context");
mAccount = account; mAccount = account;
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
@ -138,7 +138,7 @@ public abstract class RemoteOperation implements Runnable {
*/ */
public RemoteOperationResult execute(OwnCloudClient client) { public RemoteOperationResult execute(OwnCloudClient client) {
if (client == null) if (client == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + throw new IllegalArgumentException("Trying to onExecute a remote operation with a NULL " +
"OwnCloudClient"); "OwnCloudClient");
mClient = client; mClient = client;
if (client.getAccount() != null) { if (client.getAccount() != null) {
@ -160,7 +160,7 @@ public abstract class RemoteOperation implements Runnable {
*/ */
public RemoteOperationResult execute(OkHttpClient client, Context context) { public RemoteOperationResult execute(OkHttpClient client, Context context) {
if (client == null) if (client == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + throw new IllegalArgumentException("Trying to onExecute a remote operation with a NULL " +
"OwnCloudClient"); "OwnCloudClient");
mHttpClient = client; mHttpClient = client;
mContext = context; mContext = context;
@ -188,10 +188,10 @@ public abstract class RemoteOperation implements Runnable {
if (account == null) if (account == null)
throw new IllegalArgumentException throw new IllegalArgumentException
("Trying to execute a remote operation with a NULL Account"); ("Trying to onExecute a remote operation with a NULL Account");
if (context == null) if (context == null)
throw new IllegalArgumentException throw new IllegalArgumentException
("Trying to execute a remote operation with a NULL Context"); ("Trying to onExecute a remote operation with a NULL Context");
// mAccount and mContext in the runnerThread to create below // mAccount and mContext in the runnerThread to create below
mAccount = account; mAccount = account;
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
@ -221,7 +221,7 @@ public abstract class RemoteOperation implements Runnable {
OnRemoteOperationListener listener, Handler listenerHandler) { OnRemoteOperationListener listener, Handler listenerHandler) {
if (client == null) { if (client == null) {
throw new IllegalArgumentException throw new IllegalArgumentException
("Trying to execute a remote operation with a NULL OwnCloudClient"); ("Trying to onExecute a remote operation with a NULL OwnCloudClient");
} }
mClient = client; mClient = client;
if (client.getAccount() != null) { if (client.getAccount() != null) {
@ -231,7 +231,7 @@ public abstract class RemoteOperation implements Runnable {
if (listener == null) { if (listener == null) {
throw new IllegalArgumentException throw new IllegalArgumentException
("Trying to execute a remote operation asynchronously " + ("Trying to onExecute a remote operation asynchronously " +
"without a listener to notiy the result"); "without a listener to notiy the result");
} }
mListener = listener; mListener = listener;
@ -275,7 +275,7 @@ public abstract class RemoteOperation implements Runnable {
} }
/** /**
* Run operation for asynchronous or synchronous 'execute' method. * Run operation for asynchronous or synchronous 'onExecute' method.
* *
* Considers and performs silent refresh of account credentials if possible, and if * Considers and performs silent refresh of account credentials if possible, and if
* {@link RemoteOperation#setSilentRefreshOfAccountCredentials(boolean)} was called with * {@link RemoteOperation#setSilentRefreshOfAccountCredentials(boolean)} was called with

View File

@ -86,7 +86,7 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
try { try {
// client.setFollowRedirects(false); client.setFollowRedirects(true);
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
HttpUtils.stringUrlToHttpUrl(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)), HttpUtils.stringUrlToHttpUrl(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)),
0, 0,
@ -97,14 +97,6 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
int status = client.executeHttpMethod(propfindMethod); 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,
@ -127,9 +119,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
(mSuccessIfAbsent ? " absence " : " existence ") + ": " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " +
result.getLogMessage(), result.getException()); result.getLogMessage(), result.getException());
} finally {
// client.setFollowRedirects(previousFollowRedirects);
} }
return result; return result;
} }

View File

@ -210,7 +210,9 @@ public class RemoteFile implements Parcelable, Serializable {
? BigDecimal.valueOf( ? BigDecimal.valueOf(
properties.get(QuotaAvailableBytes.class).getQuotaAvailableBytes()) properties.get(QuotaAvailableBytes.class).getQuotaAvailableBytes())
: BigDecimal.ZERO); : BigDecimal.ZERO);
this.setPrivateLink(properties.get(OCPrivatelink.class).getLink()); this.setPrivateLink(properties.get(OCPrivatelink.class) != null
? properties.get(OCPrivatelink.class).getLink()
: null);
} }

View File

@ -47,6 +47,8 @@ import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLPeerUnverifiedException;
import okhttp3.HttpUrl;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
@ -100,28 +102,25 @@ public class GetRemoteStatusOperation extends RemoteOperation {
return false; return false;
} }
client.setFollowRedirects(false);
boolean isRedirectToNonSecureConnection = false; boolean isRedirectToNonSecureConnection = false;
String redirectedLocation = mLatestResult.getRedirectedLocation();
while (redirectedLocation != null && redirectedLocation.length() > 0
&& !mLatestResult.isSuccess()) {
// TODO Check this, although OkHttp should take care of the redirections isRedirectToNonSecureConnection |= (
// boolean isRedirectToNonSecureConnection = false; baseUrlSt.startsWith(HTTPS_PREFIX) &&
// String redirectedLocation = mLatestResult.getRedirectedLocation(); redirectedLocation.startsWith(HTTP_PREFIX)
// while (redirectedLocation != null && redirectedLocation.length() > 0 );
// && !mLatestResult.isSuccess()) {
// getMethod = new GetMethod(HttpUrl.parse(redirectedLocation));
// isRedirectToNonSecureConnection |= ( getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
// baseUrlSt.startsWith(HTTPS_PREFIX) && getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
// redirectedLocation.startsWith(HTTP_PREFIX)
// ); status = client.executeHttpMethod(getMethod);
// get.releaseConnection(); mLatestResult = new RemoteOperationResult(getMethod);
// get = new GetMethod(redirectedLocation); redirectedLocation = mLatestResult.getRedirectedLocation();
// status = client.executeRequest(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); }
// mLatestResult = new RemoteOperationResult(
// (status == HttpConstants.HTTP_OK),
// get
// );
// redirectedLocation = mLatestResult.getRedirectedLocation();
// }
if (isSuccess(status)) { if (isSuccess(status)) {