mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Add user-agent string to setup options
This commit is contained in:
parent
fc07af2364
commit
87582c4a8e
@ -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);
|
||||
|
@ -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<Bundle> 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<Bundle> 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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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 ?
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user