Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 925227b4 authored by David A. Velasco's avatar David A. Velasco
Browse files

Merge pull request #56 from owncloud/add_user_agent_support

Added support to define user agent.
parents 0dd68c1f ab268fbc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -27,4 +27,5 @@
    <string name="server_base_url"></string>
    <string name="username"></string>
    <string name="password"></string>
    <string name ="user_agent">Mozilla/5.0 (Android) ownCloud sample </string>
</resources>
+14 −8
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.http.HttpStatus;
import org.apache.http.params.CoreProtocolPNames;

@@ -54,7 +55,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;
@@ -82,7 +82,8 @@ public class OwnCloudClient extends HttpClient {
        mInstanceNumber = sIntanceCounter++;
        Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");

        getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
        String userAgent = OwnCloudClientManagerFactory.getUserAgent();
        getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
        getParams().setParameter(
        		CoreProtocolPNames.PROTOCOL_VERSION, 
        		HttpVersion.HTTP_1_1);
@@ -170,7 +171,7 @@ public class OwnCloudClient extends HttpClient {
     * 
     * @param method                HTTP method request.
     * @param readTimeout           Timeout to set for data reception
     * @param conntionTimout    Timeout to set for connection establishment
     * @param connectionTimeout     Timeout to set for connection establishment
     */
    public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout) 
    		throws HttpException, IOException {
@@ -208,6 +209,11 @@ public class OwnCloudClient extends HttpClient {
	            customRedirectionNeeded = mFollowRedirects;
	        }

            // Update User Agent
            HttpParams params = method.getParams();
            String userAgent = OwnCloudClientManagerFactory.getUserAgent();
            params.setParameter(HttpMethodParams.USER_AGENT, userAgent);

	        Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + 
	        		method.getName() + " " + method.getPath());
        
@@ -227,7 +233,7 @@ public class OwnCloudClient extends HttpClient {
	        return status;
	        
        } catch (IOException e) {
        	Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occured", e);
        	Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e);
        	throw e;
        }
    }
+33 −14
Original line number Diff line number Diff line
@@ -62,17 +62,25 @@ public class OwnCloudClientFactory {
     * @param account                       The ownCloud account
     * @param appContext                    Android application context
     * @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)
            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;
        // 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);
        
        if (isOauth2) {    
@@ -116,11 +124,17 @@ 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)
            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;
        // 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);
        
        if (isOauth2) {    // TODO avoid a call to getUserData here
@@ -159,7 +173,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,20 +197,24 @@ 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.
     * @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) {
        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());
+2 −1
Original line number Diff line number Diff line
@@ -43,7 +43,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
public interface OwnCloudClientManager {

	public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
            throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException;
            throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
            IOException;
    
    public OwnCloudClient removeClientFor(OwnCloudAccount account);

+11 −1
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ public class OwnCloudClientManagerFactory {
	
	private static OwnCloudClientManager sDefaultSingleton;

    private static String sUserAgent;

	public static OwnCloudClientManager newDefaultOwnCloudClientManager() {
		return newOwnCloudClientManager(sDefaultPolicy);
	}
@@ -72,6 +74,14 @@ public class OwnCloudClientManagerFactory {
    	sDefaultPolicy = policy;
    }

    public static void setUserAgent(String userAgent){
        sUserAgent = userAgent;
    }

    public static String getUserAgent() {
        return sUserAgent;
    }

	private static boolean defaultSingletonMustBeUpdated(Policy policy) {
		if (sDefaultSingleton == null) {
			return false;
Loading