mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Make cookies handling more efficient
This commit is contained in:
		
							parent
							
								
									f2ccc62b88
								
							
						
					
					
						commit
						549cc32703
					
				| @ -316,8 +316,12 @@ public class OwnCloudClient extends HttpClient { | |||||||
| 
 | 
 | ||||||
|     public String getCookiesString() { |     public String getCookiesString() { | ||||||
|         String cookiesString = ""; |         String cookiesString = ""; | ||||||
|         for (Cookie cookie : getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString()))) { |         List<Cookie> cookieList = getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString())); | ||||||
|             cookiesString += cookie.toString() + ";"; | 
 | ||||||
|  |         if (cookieList != null) { | ||||||
|  |             for (Cookie cookie : cookieList) { | ||||||
|  |                 cookiesString += cookie.toString() + ";"; | ||||||
|  |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return cookiesString; |         return cookiesString; | ||||||
|  | |||||||
| @ -26,12 +26,14 @@ package com.owncloud.android.lib.common; | |||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
|  | import java.util.List; | ||||||
| import java.util.concurrent.ConcurrentHashMap; | import java.util.concurrent.ConcurrentHashMap; | ||||||
| import java.util.concurrent.ConcurrentMap; | import java.util.concurrent.ConcurrentMap; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| import android.accounts.Account; | import android.accounts.Account; | ||||||
|  | import android.accounts.AccountManager; | ||||||
| import android.accounts.AuthenticatorException; | import android.accounts.AuthenticatorException; | ||||||
| import android.accounts.OperationCanceledException; | import android.accounts.OperationCanceledException; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
| @ -43,6 +45,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce | |||||||
| import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; | import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; | ||||||
| import com.owncloud.android.lib.common.utils.Log_OC; | import com.owncloud.android.lib.common.utils.Log_OC; | ||||||
| 
 | 
 | ||||||
|  | import okhttp3.Cookie; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Implementation of {@link OwnCloudClientManager} |  * Implementation of {@link OwnCloudClientManager} | ||||||
|  * |  * | ||||||
| @ -108,6 +112,8 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|             reusingKnown = true; |             reusingKnown = true; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         account.loadCredentials(context); | ||||||
|  | 
 | ||||||
|         if (client == null) { |         if (client == null) { | ||||||
|             // no client to reuse - create a new one |             // no client to reuse - create a new one | ||||||
|             client = OwnCloudClientFactory.createOwnCloudClient( |             client = OwnCloudClientFactory.createOwnCloudClient( | ||||||
| @ -121,7 +127,6 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|             // enable cookie tracking |             // enable cookie tracking | ||||||
|             AccountUtils.restoreCookies(account.getSavedAccount(), client, context); |             AccountUtils.restoreCookies(account.getSavedAccount(), client, context); | ||||||
| 
 | 
 | ||||||
|             account.loadCredentials(context); |  | ||||||
|             client.setCredentials(account.getCredentials()); |             client.setCredentials(account.getCredentials()); | ||||||
|             if (accountName != null) { |             if (accountName != null) { | ||||||
|                 mClientsWithKnownUsername.put(accountName, client); |                 mClientsWithKnownUsername.put(accountName, client); | ||||||
| @ -140,6 +145,7 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|                 Log_OC.v(TAG, "reusing client for session " + sessionName); |                 Log_OC.v(TAG, "reusing client for session " + sessionName); | ||||||
|             } |             } | ||||||
|             keepCredentialsUpdated(account, client); |             keepCredentialsUpdated(account, client); | ||||||
|  |             keepCookiesUpdated(context, account, client); | ||||||
|             keepUriUpdated(account, client); |             keepUriUpdated(account, client); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -213,7 +219,6 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { |     private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { | ||||||
|         OwnCloudCredentials recentCredentials = account.getCredentials(); |         OwnCloudCredentials recentCredentials = account.getCredentials(); | ||||||
|         if (recentCredentials != null && !recentCredentials.getAuthToken().equals( |         if (recentCredentials != null && !recentCredentials.getAuthToken().equals( | ||||||
| @ -222,6 +227,15 @@ public class SingleSessionManager implements OwnCloudClientManager { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) { | ||||||
|  |         AccountManager am = AccountManager.get(context.getApplicationContext()); | ||||||
|  |         String currentCookies = am.getUserData(account.getSavedAccount(), AccountUtils.Constants.KEY_COOKIES); | ||||||
|  |         String previousCookies = reusedClient.getCookiesString(); | ||||||
|  |         if (currentCookies != null && previousCookies != "" && !currentCookies.equals(previousCookies)) { | ||||||
|  |             AccountUtils.restoreCookies(account.getSavedAccount(), reusedClient, context); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     // this method is just a patch; we need to distinguish accounts in the same host but |     // this method is just a patch; we need to distinguish accounts in the same host but | ||||||
|     // different paths; but that requires updating the accountNames for apps upgrading |     // different paths; but that requires updating the accountNames for apps upgrading | ||||||
|     private void keepUriUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { |     private void keepUriUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { | ||||||
|  | |||||||
| @ -146,7 +146,6 @@ public abstract class RemoteOperation<T extends Object> implements Runnable { | |||||||
|                 mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). |                 mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). | ||||||
|                         getClientFor(ocAccount, mContext); |                         getClientFor(ocAccount, mContext); | ||||||
|                 mClient.applyCredentials(); |                 mClient.applyCredentials(); | ||||||
|                 mClient.applyCookies(); |  | ||||||
|             } else { |             } else { | ||||||
|                 throw new IllegalStateException("Trying to run a remote operation " + |                 throw new IllegalStateException("Trying to run a remote operation " + | ||||||
|                         "asynchronously with no client and no chance to create one (no account)"); |                         "asynchronously with no client and no chance to create one (no account)"); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user