From: David A. Velasco Date: Thu, 3 Oct 2013 10:38:09 +0000 (+0200) Subject: Merge branch 'fixed_permanent_redirections' into refresh_folder_contents_when_browsed... X-Git-Tag: oc-android-1.5.5~155^2~26 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/8a27bf186a3985571447d364cc5989e2b8428a2c?hp=74370d5dee77282a8f71f787890b625a9f6a3aa0 Merge branch 'fixed_permanent_redirections' into refresh_folder_contents_when_browsed_into --- diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index cedf9267..2ab5a69c 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -24,12 +24,14 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpVersion; +import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthPolicy; import org.apache.commons.httpclient.auth.AuthScope; @@ -48,6 +50,8 @@ import com.owncloud.android.network.BearerCredentials; import android.net.Uri; public class WebdavClient extends HttpClient { + private static final int MAX_REDIRECTIONS_COUNT = 3; + private Uri mUri; private Credentials mCredentials; private boolean mFollowRedirects; @@ -160,15 +164,39 @@ public class WebdavClient extends HttpClient { @Override public int executeMethod(HttpMethod method) throws IOException, HttpException { + boolean customRedirectionNeeded = false; try { method.setFollowRedirects(mFollowRedirects); } catch (Exception e) { - + Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() + " method, custom redirection will be used"); + customRedirectionNeeded = true; } if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) { method.setRequestHeader("Cookie", mSsoSessionCookie); } - return super.executeMethod(method); + int status = super.executeMethod(method); + int redirectionsCount = 0; + while (mFollowRedirects && + redirectionsCount < MAX_REDIRECTIONS_COUNT && + customRedirectionNeeded && + ( status == HttpStatus.SC_MOVED_PERMANENTLY || + status == HttpStatus.SC_MOVED_TEMPORARILY || + status == HttpStatus.SC_TEMPORARY_REDIRECT) + ) { + Header location = method.getResponseHeader("Location"); + if (location != null) { + Log_OC.d(TAG, "Location to redirect: " + location.getValue()); + method.setURI(new URI(location.getValue(), true)); + status = super.executeMethod(method); + redirectionsCount++; + + } else { + Log_OC.d(TAG, "No location to redirect!"); + status = HttpStatus.SC_NOT_FOUND; + } + } + + return status; }