mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Merge remote-tracking branch 'origin/add_cookie_based_session_support' into add_cookie_based_session_support
This commit is contained in:
		
						commit
						0fa6919761
					
				
							
								
								
									
										95
									
								
								src/com/owncloud/android/lib/common/OwnCloudAccount.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								src/com/owncloud/android/lib/common/OwnCloudAccount.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,95 @@ | ||||
| /* ownCloud Android client application | ||||
|  *   Copyright (C) 2014 ownCloud Inc. | ||||
|  * | ||||
|  *   This program is free software: you can redistribute it and/or modify | ||||
|  *   it under the terms of the GNU General Public License version 2, | ||||
|  *   as published by the Free Software Foundation. | ||||
|  * | ||||
|  *   This program is distributed in the hope that it will be useful, | ||||
|  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  *   GNU General Public License for more details. | ||||
|  * | ||||
|  *   You should have received a copy of the GNU General Public License | ||||
|  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| package com.owncloud.android.lib.common; | ||||
| 
 | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.accounts.AccountUtils; | ||||
| import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; | ||||
| 
 | ||||
| import android.accounts.Account; | ||||
| import android.accounts.AuthenticatorException; | ||||
| import android.accounts.OperationCanceledException; | ||||
| import android.content.Context; | ||||
| import android.net.Uri; | ||||
| 
 | ||||
| /** | ||||
|  * OwnCloud Account | ||||
|  *  | ||||
|  * @author David A. Velasco | ||||
|  */ | ||||
| public class OwnCloudAccount { | ||||
| 
 | ||||
|     private Uri mBaseUri;  | ||||
|      | ||||
|     private OwnCloudCredentials mCredentials; | ||||
|      | ||||
|     private String mSavedAccountName; | ||||
|      | ||||
|      | ||||
|     public OwnCloudAccount(Account savedAccount, Context context)  | ||||
|     		throws AccountNotFoundException, AuthenticatorException,  | ||||
|     		IOException, OperationCanceledException { | ||||
|     	 | ||||
|     	if (savedAccount == null) { | ||||
|     		throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null"); | ||||
|     	} | ||||
|     	if (context == null) { | ||||
|     		throw new IllegalArgumentException("Parameter 'context' cannot be null"); | ||||
|     	} | ||||
|     	 | ||||
|     	mSavedAccountName = savedAccount.name; | ||||
|         mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, savedAccount)); | ||||
|         mCredentials = AccountUtils.getCredentialsForAccount(context, savedAccount); | ||||
|     } | ||||
|      | ||||
|      | ||||
|     public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { | ||||
|         if (baseUri == null) { | ||||
|             throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); | ||||
|         } | ||||
|         mSavedAccountName = null; | ||||
|         mBaseUri = baseUri; | ||||
|         mCredentials = credentials != null ?  | ||||
|         		credentials : OwnCloudCredentialsFactory.getAnonymousCredentials(); | ||||
|         String username = credentials.getUsername(); | ||||
|         if (username != null) { | ||||
|         	mSavedAccountName = AccountUtils.buildAccountName(mBaseUri, username); | ||||
|         } | ||||
|     } | ||||
|      | ||||
| 
 | ||||
| 	public boolean isAnonymous() { | ||||
|         return (mCredentials == null); | ||||
|     } | ||||
|      | ||||
|     public Uri getBaseUri() { | ||||
|         return mBaseUri; | ||||
|     } | ||||
|              | ||||
|     public OwnCloudCredentials getCredentials() { | ||||
|         return mCredentials; | ||||
|     } | ||||
|      | ||||
|     public String getName() { | ||||
|     	return mSavedAccountName; | ||||
|     } | ||||
| 
 | ||||
|      | ||||
| } | ||||
| @ -30,6 +30,11 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { | ||||
| 		); | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String getUsername() { | ||||
| 		return mUsername; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String getAuthToken() { | ||||
| 		return mPassword; | ||||
|  | ||||
| @ -32,6 +32,12 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { | ||||
| 		); | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String getUsername() { | ||||
| 		// its unknown | ||||
| 		return null; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String getAuthToken() { | ||||
| 		return mAccessToken; | ||||
|  | ||||
| @ -59,4 +59,6 @@ public interface OwnCloudClientManager { | ||||
|     		throws AccountNotFoundException, AuthenticatorException,  | ||||
|     		IOException, OperationCanceledException; | ||||
| 
 | ||||
| 	public OwnCloudClient getClientFor(OwnCloudAccount account, Context context); | ||||
|      | ||||
| } | ||||
|  | ||||
| @ -9,6 +9,8 @@ public class OwnCloudClientManagerFactory { | ||||
| 	 | ||||
| 	public final static Policy DEFAULT_POLICY = Policy.ALWAYS_NEW_CLIENT; | ||||
| 	 | ||||
| 	private static OwnCloudClientManager mDefaultSingleton; | ||||
| 
 | ||||
| 	public static OwnCloudClientManager newDefaultOwnCloudClientManager() { | ||||
| 		return newOwnCloudClientManager(DEFAULT_POLICY); | ||||
| 	} | ||||
| @ -16,15 +18,21 @@ public class OwnCloudClientManagerFactory { | ||||
| 	public static OwnCloudClientManager newOwnCloudClientManager(Policy policy) { | ||||
| 		switch (policy) { | ||||
| 			case ALWAYS_NEW_CLIENT: | ||||
| 				return new SingleSessionManager(); | ||||
| 				return new SimpleFactoryManager(); | ||||
| 				 | ||||
| 			case SINGLE_SESSION_PER_ACCOUNT: | ||||
| 				return new SimpleFactoryManager(); | ||||
| 				return new SingleSessionManager(); | ||||
| 				 | ||||
| 			default: | ||||
| 				throw new IllegalArgumentException("Unknown policy"); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|     public static OwnCloudClientManager getDefaultSingleton() { | ||||
|     	if (mDefaultSingleton == null) { | ||||
|     		mDefaultSingleton = newDefaultOwnCloudClientManager(); | ||||
|     	} | ||||
|     	return mDefaultSingleton; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -21,6 +21,8 @@ public interface OwnCloudCredentials { | ||||
| 
 | ||||
| 	public void applyTo(OwnCloudClient ownCloudClient); | ||||
| 
 | ||||
| 	public String getUsername(); | ||||
| 	 | ||||
| 	public String getAuthToken(); | ||||
| 	 | ||||
| 	public boolean authTokenExpires(); | ||||
|  | ||||
| @ -42,6 +42,12 @@ public class OwnCloudCredentialsFactory { | ||||
| 		public boolean authTokenExpires() { | ||||
| 			return false; | ||||
| 		} | ||||
| 
 | ||||
| 		@Override | ||||
| 		public String getUsername() { | ||||
| 			// no user name | ||||
| 			return null; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -39,6 +39,12 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials { | ||||
|         } | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String getUsername() { | ||||
| 		// its unknown | ||||
| 		return null; | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public String getAuthToken() { | ||||
| 		return mSessionCookie; | ||||
|  | ||||
| @ -7,25 +7,46 @@ import android.accounts.AuthenticatorException; | ||||
| import android.accounts.OperationCanceledException; | ||||
| import android.content.Context; | ||||
| import android.net.Uri; | ||||
| import android.util.Log; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException; | ||||
| 
 | ||||
| public class SimpleFactoryManager implements OwnCloudClientManager { | ||||
|      | ||||
| 	private static final String TAG = OwnCloudClientManager.class.getSimpleName(); | ||||
| 
 | ||||
| 	@Override | ||||
| 	public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { | ||||
| 		Log.d(TAG, "getClientFor(OwnCloudAccount ... : "); | ||||
| 		OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( | ||||
| 				account.getBaseUri(),  | ||||
| 				context.getApplicationContext(), | ||||
| 				true); | ||||
| 
 | ||||
| 		Log.d(TAG, "    new client " + client.hashCode()); | ||||
| 		client.setCredentials(account.getCredentials()); | ||||
| 		return client; | ||||
| 	} | ||||
| 
 | ||||
| 	 | ||||
| 	@Override | ||||
| 	public OwnCloudClient getClientFor(Account savedAccount, Context context) | ||||
| 			throws OperationCanceledException, AuthenticatorException, AccountNotFoundException, | ||||
| 			IOException { | ||||
| 		Log.d(TAG, "getClientFor(Account ... : "); | ||||
| 
 | ||||
| 		return OwnCloudClientFactory.createOwnCloudClient( | ||||
| 		OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( | ||||
| 				savedAccount,  | ||||
| 				context.getApplicationContext()); | ||||
| 		 | ||||
| 		Log.d(TAG, "    new client " + client.hashCode()); | ||||
| 		return client; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public OwnCloudClient getClientFor(Uri serverBaseUri, OwnCloudCredentials credentials, | ||||
| 			Context context) { | ||||
| 		Log.d(TAG, "getClientFor(Uri ... : "); | ||||
| 		 | ||||
| 		OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( | ||||
| 				serverBaseUri,  | ||||
| @ -33,6 +54,7 @@ public class SimpleFactoryManager implements OwnCloudClientManager { | ||||
| 				true); | ||||
| 
 | ||||
| 		client.setCredentials(credentials); | ||||
| 		Log.d(TAG, "    new client " + client.hashCode()); | ||||
| 		return client; | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -34,6 +34,7 @@ import android.accounts.AuthenticatorException; | ||||
| import android.accounts.OperationCanceledException; | ||||
| import android.content.Context; | ||||
| import android.net.Uri; | ||||
| import android.util.Log; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.OwnCloudClientFactory; | ||||
| @ -55,17 +56,71 @@ public class SingleSessionManager implements OwnCloudClientManager { | ||||
|      | ||||
| 	//private static final String TAG = SingleSessionManager.class.getSimpleName(); | ||||
| 
 | ||||
| 	private static final String TAG = SingleSessionManager.class.getSimpleName(); | ||||
| 
 | ||||
| 	private static OwnCloudClientManager mInstance = null; | ||||
| 	 | ||||
|     private Map<String, Map<OwnCloudCredentials, OwnCloudClient>> mClientsPerServer =  | ||||
|             new HashMap<String, Map<OwnCloudCredentials, OwnCloudClient>>(); | ||||
|      | ||||
|     private Map<String, OwnCloudClient> mClientsWithKnownUsername =  | ||||
|     		new HashMap<String, OwnCloudClient>(); | ||||
|      | ||||
|     public static OwnCloudClientManager getInstance() { | ||||
|     	if (mInstance == null) { | ||||
|     		mInstance = new SingleSessionManager(); | ||||
|     private Map<String, OwnCloudClient> mClientsWithUnknownUsername =  | ||||
|     		new HashMap<String, OwnCloudClient>(); | ||||
|      | ||||
|      | ||||
|     @Override | ||||
|     public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { | ||||
| 		Log.d(TAG, "getClientFor(OwnCloudAccount ... : "); | ||||
|     	if (account == null) { | ||||
|     		throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account"); | ||||
|     	} | ||||
|     	return mInstance ; | ||||
| 
 | ||||
|     	OwnCloudClient client = null; | ||||
|     	String accountName = account.getName(); | ||||
|     	String sessionName = AccountUtils.buildAccountName( | ||||
|     			account.getBaseUri(),  | ||||
|     			account.getCredentials().getAuthToken()); | ||||
|     	 | ||||
|     	if (accountName != null) { | ||||
|     		client = mClientsWithKnownUsername.get(accountName); | ||||
|     	} | ||||
|     	if (client == null) { | ||||
|     		if (accountName != null) { | ||||
|     			client = mClientsWithUnknownUsername.remove(sessionName); | ||||
|     			if (client != null) { | ||||
|     	    		Log.d(TAG, "    reusing client {" + sessionName + ", " + client.hashCode() + "}"); | ||||
|     				mClientsWithKnownUsername.put(accountName, client); | ||||
|     	    		Log.d(TAG, "    moved client to {" + accountName + ", " + client.hashCode() + "}"); | ||||
|     			} | ||||
|     		} else { | ||||
|         		client = mClientsWithUnknownUsername.get(sessionName); | ||||
|     		} | ||||
|     	} else { | ||||
|     		Log.d(TAG, "    reusing client {" + accountName + ", " + client.hashCode() + "}"); | ||||
|     	} | ||||
|     	 | ||||
|     	if (client == null) { | ||||
|     		// no client to reuse - create a new one | ||||
|     		client = OwnCloudClientFactory.createOwnCloudClient( | ||||
|     				account.getBaseUri(),  | ||||
|     				context.getApplicationContext(),  | ||||
|     				true); | ||||
|     		client.setCredentials(account.getCredentials()); | ||||
|     		if (accountName != null) { | ||||
|     			mClientsWithKnownUsername.put(accountName, client); | ||||
|     			Log.d(TAG, "    new client {" + accountName + ", " + client.hashCode() + "}"); | ||||
| 
 | ||||
|     		} else { | ||||
|     			mClientsWithUnknownUsername.put(sessionName, client); | ||||
|     			Log.d(TAG, "    new client {" + sessionName + ", " + client.hashCode() + "}"); | ||||
|     		} | ||||
|     	} else { | ||||
|     		Log.d(TAG, "    reusing client {" + sessionName + ", " + client.hashCode() + "}"); | ||||
|     	} | ||||
|     	 | ||||
|     	return client; | ||||
|     } | ||||
|      | ||||
|      | ||||
|  | ||||
| @ -37,7 +37,9 @@ import android.accounts.AccountsException; | ||||
| import android.accounts.AuthenticatorException; | ||||
| import android.accounts.OperationCanceledException; | ||||
| import android.content.Context; | ||||
| 
 | ||||
| import android.util.Log; | ||||
| import android.net.Uri; | ||||
| 
 | ||||
| public class AccountUtils { | ||||
|     public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; | ||||
| @ -188,6 +190,15 @@ public class AccountUtils { | ||||
| 	} | ||||
| 
 | ||||
| 	 | ||||
|     public static String buildAccountName(Uri serverBaseUrl, String username) { | ||||
|         String accountName = username + "@" + serverBaseUrl.getHost(); | ||||
|         if (serverBaseUrl.getPort() >= 0) { | ||||
|             accountName += ":" + serverBaseUrl.getPort(); | ||||
|         } | ||||
|         return accountName; | ||||
|     } | ||||
|      | ||||
| 
 | ||||
| 	public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) { | ||||
| 
 | ||||
| 		// Account Manager | ||||
|  | ||||
| @ -28,6 +28,7 @@ import java.io.IOException; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.OwnCloudClientFactory; | ||||
| import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; | ||||
| import com.owncloud.android.lib.common.OwnCloudCredentials; | ||||
| import com.owncloud.android.lib.common.SingleSessionManager; | ||||
| import com.owncloud.android.lib.common.accounts.AccountUtils; | ||||
| @ -105,7 +106,8 @@ public abstract class RemoteOperation implements Runnable { | ||||
|         mAccount = account; | ||||
|         mContext = context.getApplicationContext(); | ||||
|         try { | ||||
|             mClient = SingleSessionManager.getInstance().getClientFor(mAccount, mContext); | ||||
|             mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). | ||||
|             		getClientFor(mAccount, mContext); | ||||
|         } catch (Exception e) { | ||||
|             Log.e(TAG, "Error while trying to access to " + mAccount.name, e); | ||||
|             return new RemoteOperationResult(e); | ||||
| @ -252,7 +254,7 @@ public abstract class RemoteOperation implements Runnable { | ||||
|                             		mAccount, mContext, mCallerActivity); | ||||
|                         } else { | ||||
|                         /** EOF DEPRECATED */ | ||||
|                             mClient = SingleSessionManager.getInstance(). | ||||
|                             mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). | ||||
|                             		getClientFor(mAccount, mContext); | ||||
|                         } | ||||
|                         // Save Client Cookies | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user