X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/a6a56bae2acae4f69c421fa5b8166a3cd5ebaf9f..db1ea015619a1cc5a3886ea90dc20f910fc6dd9e:/src/eu/alefzero/webdav/WebdavClient.java diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 570ebb85..f25e3906 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; @@ -41,16 +43,20 @@ import org.apache.http.params.CoreProtocolPNames; import com.owncloud.android.Log_OC; +import com.owncloud.android.authentication.AccountAuthenticator; import com.owncloud.android.network.BearerAuthScheme; 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; private String mSsoSessionCookie; + private String mAuthTokenType; final private static String TAG = "WebdavClient"; public static final String USER_AGENT = "Android-ownCloud"; @@ -66,6 +72,7 @@ public class WebdavClient extends HttpClient { getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); mFollowRedirects = true; mSsoSessionCookie = null; + mAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD; } public void setBearerCredentials(String accessToken) { @@ -78,6 +85,7 @@ public class WebdavClient extends HttpClient { mCredentials = new BearerCredentials(accessToken); getState().setCredentials(AuthScope.ANY, mCredentials); mSsoSessionCookie = null; + mAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN; } public void setBasicCredentials(String username, String password) { @@ -89,6 +97,7 @@ public class WebdavClient extends HttpClient { mCredentials = new UsernamePasswordCredentials(username, password); getState().setCredentials(AuthScope.ANY, mCredentials); mSsoSessionCookie = null; + mAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD; } public void setSsoSessionCookie(String accessToken) { @@ -96,6 +105,7 @@ public class WebdavClient extends HttpClient { getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); mSsoSessionCookie = accessToken; mCredentials = null; + mAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE; } @@ -154,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) { - + if (mFollowRedirects) Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() + " method, custom redirection will be used"); + customRedirectionNeeded = mFollowRedirects; } if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) { method.setRequestHeader("Cookie", mSsoSessionCookie); } - return super.executeMethod(method); + int status = super.executeMethod(method); + int redirectionsCount = 0; + while (customRedirectionNeeded && + redirectionsCount < MAX_REDIRECTIONS_COUNT && + ( 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; } @@ -206,9 +240,17 @@ public class WebdavClient extends HttpClient { public final Credentials getCredentials() { return mCredentials; } + + public final String getSsoSessionCookie() { + return mSsoSessionCookie; + } public void setFollowRedirects(boolean followRedirects) { mFollowRedirects = followRedirects; } + public String getAuthTokenType() { + return mAuthTokenType; + } + }