From 87582c4a8ea293d582ee7d245bd5df8dc6c2e7c2 Mon Sep 17 00:00:00 2001 From: masensio Date: Wed, 18 Mar 2015 17:08:19 +0100 Subject: [PATCH] Add user-agent string to setup options --- .../android/lib/common/OwnCloudClient.java | 5 +- .../lib/common/OwnCloudClientFactory.java | 57 +++++++++++++------ .../lib/common/OwnCloudClientManager.java | 5 +- .../lib/common/SimpleFactoryManager.java | 8 ++- .../lib/common/SingleSessionManager.java | 9 ++- .../common/operations/RemoteOperation.java | 15 +++-- 6 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java index 482456f4..ab12a5cb 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java @@ -54,7 +54,6 @@ import com.owncloud.android.lib.common.utils.Log_OC; public class OwnCloudClient extends HttpClient { private static final String TAG = OwnCloudClient.class.getSimpleName(); - public static final String USER_AGENT = "Android-ownCloud"; private static final int MAX_REDIRECTIONS_COUNT = 3; private static final String PARAM_SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header"; private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true; @@ -71,7 +70,7 @@ public class OwnCloudClient extends HttpClient { /** * Constructor */ - public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) { + public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, String userAgent) { super(connectionMgr); if (baseUri == null) { @@ -82,7 +81,7 @@ public class OwnCloudClient extends HttpClient { mInstanceNumber = sIntanceCounter++; Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient"); - getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); + getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent); getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java index fb45bdac..b0cd5fa3 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -61,19 +61,29 @@ public class OwnCloudClientFactory { * * @param account The ownCloud account * @param appContext Android application context + * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient object ready to be used - * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account. - * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account. - * @throws IOException If there was some I/O error while getting the authorization token for the account. + * @throws AuthenticatorException If the authenticator failed to get the authorization + * token for the account. + * @throws OperationCanceledException If the authenticator operation was cancelled while + * getting the authorization token for the account. + * @throws IOException If there was some I/O error while getting the + * authorization token for the account. * @throws AccountNotFoundException If 'account' is unknown for the AccountManager */ - public static OwnCloudClient createOwnCloudClient (Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { + public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, + String userAgent) + throws OperationCanceledException, AuthenticatorException, IOException, + AccountNotFoundException { //Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name); Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); - boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here - boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); + // TODO avoid calling to getUserData here + boolean isOauth2 = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; + boolean isSamlSso = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); if (isOauth2) { String accessToken = am.blockingGetAuthToken( @@ -116,12 +126,18 @@ public class OwnCloudClientFactory { } - public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, Activity currentActivity) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { + public static OwnCloudClient createOwnCloudClient (Account account, Context appContext, + Activity currentActivity, String userAgent) + throws OperationCanceledException, AuthenticatorException, IOException, + AccountNotFoundException { Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); - boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here - boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; - OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso); + // TODO avoid calling to getUserData here + boolean isOauth2 = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; + boolean isSamlSso = + am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; + OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso, userAgent); if (isOauth2) { // TODO avoid a call to getUserData here AccountManagerFuture future = am.getAuthToken( @@ -159,7 +175,8 @@ public class OwnCloudClientFactory { } else { String username = account.name.substring(0, account.name.lastIndexOf('@')); //String password = am.getPassword(account); - //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), false); + //String password = am.blockingGetAuthToken(account, MainApp.getAuthTokenTypePass(), + // false); AccountManagerFuture future = am.getAuthToken( account, AccountTypeUtils.getAuthTokenTypePass(account.type), @@ -182,23 +199,29 @@ public class OwnCloudClientFactory { } /** - * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud client connections. + * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud + * client connections. * * @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH * @param context Android context where the OwnCloudClient is being created. + * @param userAgent OwnCloud userAgent string * @return A OwnCloudClient object ready to be used */ - public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) { + public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, + boolean followRedirects, String userAgent) { try { NetworkUtils.registerAdvancedSslContext(true, context); } catch (GeneralSecurityException e) { - Log_OC.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in the system will be used for HTTPS connections", e); + Log_OC.e(TAG, "Advanced SSL Context could not be loaded. Default SSL management in" + + " the system will be used for HTTPS connections", e); } catch (IOException e) { - Log_OC.e(TAG, "The local server truststore could not be read. Default SSL management in the system will be used for HTTPS connections", e); + Log_OC.e(TAG, "The local server truststore could not be read. Default SSL management" + + " in the system will be used for HTTPS connections", e); } - OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager()); + OwnCloudClient client = new OwnCloudClient(uri, NetworkUtils.getMultiThreadedConnManager(), + userAgent); client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); client.setFollowRedirects(followRedirects); diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java index 40dab1ae..c9994f91 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -42,8 +42,9 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce public interface OwnCloudClientManager { - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException; + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException; public OwnCloudClient removeClientFor(OwnCloudAccount account); diff --git a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java index b06299d0..ad7120c0 100644 --- a/src/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ b/src/com/owncloud/android/lib/common/SimpleFactoryManager.java @@ -40,15 +40,17 @@ public class SimpleFactoryManager implements OwnCloudClientManager { private static final String TAG = SimpleFactoryManager.class.getSimpleName(); @Override - public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { + public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException { Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true); + true, + userAgent); Log_OC.v(TAG, " new client {" + (account.getName() != null ? diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index 7d60b17d..a69ac35c 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -62,8 +62,10 @@ public class SingleSessionManager implements OwnCloudClientManager { @Override - public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) - throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException { + public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context, + String userAgent) + throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, + IOException { Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); if (account == null) { @@ -107,7 +109,8 @@ public class SingleSessionManager implements OwnCloudClientManager { client = OwnCloudClientFactory.createOwnCloudClient( account.getBaseUri(), context.getApplicationContext(), - true); // TODO remove dependency on OwnCloudClientFactory + true, + userAgent); // TODO remove dependency on OwnCloudClientFactory client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // enable cookie tracking diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 09f471c9..4fc22829 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -107,16 +107,18 @@ public abstract class RemoteOperation implements Runnable { */ public RemoteOperationResult execute(Account account, Context context, String userAgent) { if (account == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + + "Account"); if (context == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context"); + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + + "Context"); mAccount = account; mContext = context.getApplicationContext(); mUserAgent = userAgent; try { OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext); + getClientFor(ocAccount, mContext, mUserAgent); } catch (Exception e) { Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e); return new RemoteOperationResult(e); @@ -137,7 +139,8 @@ public abstract class RemoteOperation implements Runnable { */ public RemoteOperationResult execute(OwnCloudClient client, String userAgent) { if (client == null) - throw new IllegalArgumentException("Trying to execute a remote operation with a NULL OwnCloudClient"); + throw new IllegalArgumentException("Trying to execute a remote operation with a NULL " + + "OwnCloudClient"); mClient = client; mUserAgent = userAgent; return run(client); @@ -284,12 +287,12 @@ public abstract class RemoteOperation implements Runnable { /** DEPRECATED BLOCK - will be removed at version 1.0 */ if (mCallerActivity != null) { mClient = OwnCloudClientFactory.createOwnCloudClient( - mAccount, mContext, mCallerActivity); + mAccount, mContext, mCallerActivity, mUserAgent); } else { /** EOF DEPRECATED */ OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext); mClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, mContext); + getClientFor(ocAccount, mContext, mUserAgent); } } else {