Save target location of server behind a permanent redirection after password validation
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 11 Jun 2015 07:42:21 +0000 (09:42 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 11 Jun 2015 07:42:21 +0000 (09:42 +0200)
owncloud-android-library
src/com/owncloud/android/authentication/AccountUtils.java
src/com/owncloud/android/authentication/AuthenticatorActivity.java
src/com/owncloud/android/authentication/AuthenticatorAsyncTask.java

index e48aa9c..be23f81 160000 (submodule)
@@ -1 +1 @@
-Subproject commit e48aa9c30447d67b0f8ee2054e1fc826108fc5ec
+Subproject commit be23f8118656a84d2303a2317b60aa4fcd8afed5
index 1e67aa6..9afa819 100644 (file)
@@ -250,4 +250,21 @@ public class AccountUtils {
     }\r
 \r
 \r
+    public static String trimWebdavSuffix(String url) {\r
+        while(url.endsWith("/")) {\r
+            url = url.substring(0, url.length() - 1);\r
+        }\r
+        int pos = url.lastIndexOf(WEBDAV_PATH_4_0_AND_LATER);\r
+        if (pos >= 0) {\r
+            url = url.substring(0, pos);\r
+\r
+        } else {\r
+            pos = url.lastIndexOf(ODAV_PATH);\r
+            if (pos >= 0) {\r
+                url = url.substring(0, pos);\r
+            }\r
+        }\r
+        return url;\r
+    }\r
+\r
 }\r
index 713851b..bf1010b 100644 (file)
@@ -785,8 +785,8 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             Intent getServerInfoIntent = new Intent();\r
             getServerInfoIntent.setAction(OperationsService.ACTION_GET_SERVER_INFO);\r
             getServerInfoIntent.putExtra(\r
-                OperationsService.EXTRA_SERVER_URL, \r
-                normalizeUrlSuffix(uri)\r
+                    OperationsService.EXTRA_SERVER_URL,\r
+                    normalizeUrlSuffix(uri)\r
             );\r
             if (mOperationsServiceBinder != null) {\r
                 mWaitingForOpId = mOperationsServiceBinder.queueNewOperation(getServerInfoIntent);\r
@@ -999,7 +999,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
 \r
             if ( mAction == ACTION_CREATE) {\r
                 mUsernameInput.setText(username);\r
-                success = createAccount();\r
+                success = createAccount(result);\r
             } else {\r
 \r
                 if (!mUsernameInput.getText().toString().equals(username)) {\r
@@ -1351,8 +1351,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             Log_OC.d(TAG, "Successful access - time to save the account");\r
 \r
             boolean success = false;\r
+\r
             if (mAction == ACTION_CREATE) {\r
-                success = createAccount();\r
+                success = createAccount(result);\r
 \r
             } else {\r
                 try {\r
@@ -1449,13 +1450,18 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
      * \r
      * TODO Decide how to name the OAuth accounts\r
      */\r
-    private boolean createAccount() {\r
+    private boolean createAccount(RemoteOperationResult authResult) {\r
         /// create and save new ownCloud account\r
         boolean isOAuth = AccountTypeUtils.\r
                 getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType);\r
         boolean isSaml =  AccountTypeUtils.\r
                 getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType);\r
 \r
+        String lastPermanentLocation = authResult.getLastPermanentLocation();\r
+        if (lastPermanentLocation != null) {\r
+            mServerInfo.mBaseUrl = AccountUtils.trimWebdavSuffix(lastPermanentLocation);\r
+        }\r
+\r
         Uri uri = Uri.parse(mServerInfo.mBaseUrl);\r
         String username = mUsernameInput.getText().toString().trim();\r
         if (isOAuth) {\r
@@ -1513,7 +1519,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
             /// add user data to the new account; TODO probably can be done in the last parameter \r
             //      addAccountExplicitly, or in KEY_USERDATA\r
             mAccountMgr.setUserData(\r
-                    mAccount, Constants.KEY_OC_VERSION,    mServerInfo.mVersion.getVersion()\r
+                    mAccount, Constants.KEY_OC_VERSION, mServerInfo.mVersion.getVersion()\r
             );\r
             mAccountMgr.setUserData(\r
                     mAccount, Constants.KEY_OC_BASE_URL,   mServerInfo.mBaseUrl\r
index 4b2aed5..a25ade8 100644 (file)
@@ -24,10 +24,10 @@ import android.content.Context;
 import android.net.Uri;
 import android.os.AsyncTask;
 
-import com.owncloud.android.MainApp;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientFactory;
 import com.owncloud.android.lib.common.OwnCloudCredentials;
+import com.owncloud.android.lib.common.network.RedirectionPath;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
 
@@ -62,7 +62,6 @@ public class AuthenticatorAsyncTask  extends AsyncTask<Object, Void, RemoteOpera
             // Client
             Uri uri = Uri.parse(url);
             OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
-
             client.setCredentials(credentials);
 
             // Operation
@@ -73,6 +72,12 @@ public class AuthenticatorAsyncTask  extends AsyncTask<Object, Void, RemoteOpera
             );
             result = operation.execute(client);
 
+            if (operation.wasRedirected()) {
+                RedirectionPath redirectionPath = operation.getRedirectionPath();
+                String permanentLocation = redirectionPath.getLastPermanentLocation();
+                result.setLastPermanentLocation(permanentLocation);
+            }
+
         } else {
             result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
         }