mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 08:26:10 +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 {
|
public class OwnCloudClient extends HttpClient {
|
||||||
|
|
||||||
private static final String TAG = OwnCloudClient.class.getSimpleName();
|
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 int MAX_REDIRECTIONS_COUNT = 3;
|
||||||
private static final String PARAM_SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header";
|
private static final String PARAM_SINGLE_COOKIE_HEADER = "http.protocol.single-cookie-header";
|
||||||
private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true;
|
private static final boolean PARAM_SINGLE_COOKIE_HEADER_VALUE = true;
|
||||||
@ -71,7 +70,7 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
|
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, String userAgent) {
|
||||||
super(connectionMgr);
|
super(connectionMgr);
|
||||||
|
|
||||||
if (baseUri == null) {
|
if (baseUri == null) {
|
||||||
@ -82,7 +81,7 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
mInstanceNumber = sIntanceCounter++;
|
mInstanceNumber = sIntanceCounter++;
|
||||||
Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");
|
Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");
|
||||||
|
|
||||||
getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
|
getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
|
||||||
getParams().setParameter(
|
getParams().setParameter(
|
||||||
CoreProtocolPNames.PROTOCOL_VERSION,
|
CoreProtocolPNames.PROTOCOL_VERSION,
|
||||||
HttpVersion.HTTP_1_1);
|
HttpVersion.HTTP_1_1);
|
||||||
|
@ -61,19 +61,29 @@ public class OwnCloudClientFactory {
|
|||||||
*
|
*
|
||||||
* @param account The ownCloud account
|
* @param account The ownCloud account
|
||||||
* @param appContext Android application context
|
* @param appContext Android application context
|
||||||
|
* @param userAgent OwnCloud userAgent string
|
||||||
* @return A OwnCloudClient object ready to be used
|
* @return A OwnCloudClient object ready to be used
|
||||||
* @throws AuthenticatorException If the authenticator failed to get the authorization token for the account.
|
* @throws AuthenticatorException If the authenticator failed to get the authorization
|
||||||
* @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account.
|
* token for the account.
|
||||||
* @throws IOException If there was some I/O error while getting 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
|
* @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);
|
//Log_OC.d(TAG, "Creating OwnCloudClient associated to " + account.name);
|
||||||
Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
|
Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
|
||||||
AccountManager am = AccountManager.get(appContext);
|
AccountManager am = AccountManager.get(appContext);
|
||||||
boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
|
// TODO avoid calling to getUserData here
|
||||||
boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
|
boolean isOauth2 =
|
||||||
OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso);
|
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) {
|
if (isOauth2) {
|
||||||
String accessToken = am.blockingGetAuthToken(
|
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));
|
Uri baseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(appContext, account));
|
||||||
AccountManager am = AccountManager.get(appContext);
|
AccountManager am = AccountManager.get(appContext);
|
||||||
boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here
|
// TODO avoid calling to getUserData here
|
||||||
boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null;
|
boolean isOauth2 =
|
||||||
OwnCloudClient client = createOwnCloudClient(baseUri, appContext, !isSamlSso);
|
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
|
if (isOauth2) { // TODO avoid a call to getUserData here
|
||||||
AccountManagerFuture<Bundle> future = am.getAuthToken(
|
AccountManagerFuture<Bundle> future = am.getAuthToken(
|
||||||
@ -159,7 +175,8 @@ public class OwnCloudClientFactory {
|
|||||||
} else {
|
} else {
|
||||||
String username = account.name.substring(0, account.name.lastIndexOf('@'));
|
String username = account.name.substring(0, account.name.lastIndexOf('@'));
|
||||||
//String password = am.getPassword(account);
|
//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(
|
AccountManagerFuture<Bundle> future = am.getAuthToken(
|
||||||
account,
|
account,
|
||||||
AccountTypeUtils.getAuthTokenTypePass(account.type),
|
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 uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH
|
||||||
* @param context Android context where the OwnCloudClient is being created.
|
* @param context Android context where the OwnCloudClient is being created.
|
||||||
|
* @param userAgent OwnCloud userAgent string
|
||||||
* @return A OwnCloudClient object ready to be used
|
* @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 {
|
try {
|
||||||
NetworkUtils.registerAdvancedSslContext(true, context);
|
NetworkUtils.registerAdvancedSslContext(true, context);
|
||||||
} catch (GeneralSecurityException e) {
|
} 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) {
|
} 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.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
|
||||||
client.setFollowRedirects(followRedirects);
|
client.setFollowRedirects(followRedirects);
|
||||||
|
|
||||||
|
@ -42,8 +42,9 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
|
|||||||
|
|
||||||
public interface OwnCloudClientManager {
|
public interface OwnCloudClientManager {
|
||||||
|
|
||||||
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
|
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent)
|
||||||
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException;
|
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
|
||||||
|
IOException;
|
||||||
|
|
||||||
public OwnCloudClient removeClientFor(OwnCloudAccount account);
|
public OwnCloudClient removeClientFor(OwnCloudAccount account);
|
||||||
|
|
||||||
|
@ -40,15 +40,17 @@ public class SimpleFactoryManager implements OwnCloudClientManager {
|
|||||||
private static final String TAG = SimpleFactoryManager.class.getSimpleName();
|
private static final String TAG = SimpleFactoryManager.class.getSimpleName();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
|
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, String userAgent)
|
||||||
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
|
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
|
||||||
|
IOException {
|
||||||
|
|
||||||
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
|
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
|
||||||
|
|
||||||
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
|
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
|
||||||
account.getBaseUri(),
|
account.getBaseUri(),
|
||||||
context.getApplicationContext(),
|
context.getApplicationContext(),
|
||||||
true);
|
true,
|
||||||
|
userAgent);
|
||||||
|
|
||||||
Log_OC.v(TAG, " new client {" +
|
Log_OC.v(TAG, " new client {" +
|
||||||
(account.getName() != null ?
|
(account.getName() != null ?
|
||||||
|
@ -62,8 +62,10 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
|
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context,
|
||||||
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
|
String userAgent)
|
||||||
|
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
|
||||||
|
IOException {
|
||||||
|
|
||||||
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
|
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
|
||||||
if (account == null) {
|
if (account == null) {
|
||||||
@ -107,7 +109,8 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
client = OwnCloudClientFactory.createOwnCloudClient(
|
client = OwnCloudClientFactory.createOwnCloudClient(
|
||||||
account.getBaseUri(),
|
account.getBaseUri(),
|
||||||
context.getApplicationContext(),
|
context.getApplicationContext(),
|
||||||
true); // TODO remove dependency on OwnCloudClientFactory
|
true,
|
||||||
|
userAgent); // TODO remove dependency on OwnCloudClientFactory
|
||||||
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
|
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
|
||||||
// enable cookie tracking
|
// enable cookie tracking
|
||||||
|
|
||||||
|
@ -107,16 +107,18 @@ public abstract class RemoteOperation implements Runnable {
|
|||||||
*/
|
*/
|
||||||
public RemoteOperationResult execute(Account account, Context context, String userAgent) {
|
public RemoteOperationResult execute(Account account, Context context, String userAgent) {
|
||||||
if (account == null)
|
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)
|
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;
|
mAccount = account;
|
||||||
mContext = context.getApplicationContext();
|
mContext = context.getApplicationContext();
|
||||||
mUserAgent = userAgent;
|
mUserAgent = userAgent;
|
||||||
try {
|
try {
|
||||||
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
||||||
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
||||||
getClientFor(ocAccount, mContext);
|
getClientFor(ocAccount, mContext, mUserAgent);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
|
Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e);
|
||||||
return new RemoteOperationResult(e);
|
return new RemoteOperationResult(e);
|
||||||
@ -137,7 +139,8 @@ public abstract class RemoteOperation implements Runnable {
|
|||||||
*/
|
*/
|
||||||
public RemoteOperationResult execute(OwnCloudClient client, String userAgent) {
|
public RemoteOperationResult execute(OwnCloudClient client, String userAgent) {
|
||||||
if (client == null)
|
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;
|
mClient = client;
|
||||||
mUserAgent = userAgent;
|
mUserAgent = userAgent;
|
||||||
return run(client);
|
return run(client);
|
||||||
@ -284,12 +287,12 @@ public abstract class RemoteOperation implements Runnable {
|
|||||||
/** DEPRECATED BLOCK - will be removed at version 1.0 */
|
/** DEPRECATED BLOCK - will be removed at version 1.0 */
|
||||||
if (mCallerActivity != null) {
|
if (mCallerActivity != null) {
|
||||||
mClient = OwnCloudClientFactory.createOwnCloudClient(
|
mClient = OwnCloudClientFactory.createOwnCloudClient(
|
||||||
mAccount, mContext, mCallerActivity);
|
mAccount, mContext, mCallerActivity, mUserAgent);
|
||||||
} else {
|
} else {
|
||||||
/** EOF DEPRECATED */
|
/** EOF DEPRECATED */
|
||||||
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
||||||
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
||||||
getClientFor(ocAccount, mContext);
|
getClientFor(ocAccount, mContext, mUserAgent);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user