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

Commit e72fec72 authored by rperezb's avatar rperezb
Browse files

Merge pull request #29 from owncloud/add_cookie_based_session_support

Add cookie based session support
parents f323dda0 3c116514
Loading
Loading
Loading
Loading
+7 −1
Original line number Original line Diff line number Diff line
@@ -36,6 +36,7 @@ import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
import com.owncloud.android.lib.resources.files.RemoteFile;
import com.owncloud.android.lib.resources.files.RemoteFile;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -80,7 +81,12 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
        
        
    	Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0);
    	Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0);
    	mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
    	mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
    	mClient.setBasicCredentials(getString(R.string.username), getString(R.string.password));
    	mClient.setCredentials(
    			OwnCloudCredentialsFactory.newBasicCredentials(
    					getString(R.string.username), 
    					getString(R.string.password)
				)
		);
    	
    	
    	mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
    	mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list);
    	((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
    	((ListView)findViewById(R.id.list_view)).setAdapter(mFilesAdapter);
+98 −0
Original line number Original line Diff line number Diff line
/* 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);
        if (mCredentials == null) {
        	mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
        }
    }
    
    
    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 = mCredentials.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;
    }

    
}
 No newline at end of file
+48 −0
Original line number Original line Diff line number Diff line
package com.owncloud.android.lib.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;

public class OwnCloudBasicCredentials implements OwnCloudCredentials {

	private String mUsername;
	private String mPassword;

	public OwnCloudBasicCredentials(String username, String password) {
		mUsername = username != null ? username : "";
		mPassword = password != null ? password : "";
	}

	@Override
	public void applyTo(OwnCloudClient client) {
        List<String> authPrefs = new ArrayList<String>(1);
        authPrefs.add(AuthPolicy.BASIC);
        client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
        
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(
        		AuthScope.ANY, 
        		new UsernamePasswordCredentials(mUsername, mPassword)
		);
	}

	@Override
	public String getUsername() {
		return mUsername;
	}

	@Override
	public String getAuthToken() {
		return mPassword;
	}

	@Override
	public boolean authTokenExpires() {
		return false;
	}

}
+51 −0
Original line number Original line Diff line number Diff line
package com.owncloud.android.lib.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;

import com.owncloud.android.lib.common.network.BearerAuthScheme;
import com.owncloud.android.lib.common.network.BearerCredentials;

public class OwnCloudBearerCredentials implements OwnCloudCredentials {

	private String mAccessToken;
	
	public OwnCloudBearerCredentials(String accessToken) {
		mAccessToken = accessToken != null ? accessToken : "";
	}

	@Override
	public void applyTo(OwnCloudClient client) {
	    AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
	    
	    List<String> authPrefs = new ArrayList<String>(1);
	    authPrefs.add(BearerAuthScheme.AUTH_POLICY);
	    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
	    
	    client.getParams().setAuthenticationPreemptive(true);
	    client.getState().setCredentials(
	    		AuthScope.ANY, 
	    		new BearerCredentials(mAccessToken)
		);
	}

	@Override
	public String getUsername() {
		// its unknown
		return null;
	}
	
	@Override
	public String getAuthToken() {
		return mAccessToken;
	}

	@Override
	public boolean authTokenExpires() {
		return true;
	}

}
+211 −93
Original line number Original line Diff line number Diff line
@@ -27,10 +27,8 @@ package com.owncloud.android.lib.common;


import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpConnectionManager;
@@ -39,17 +37,14 @@ import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;
import org.apache.http.HttpStatus;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.CoreProtocolPNames;


import com.owncloud.android.lib.common.network.BearerAuthScheme;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials;
import com.owncloud.android.lib.common.network.BearerCredentials;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.network.WebdavUtils;




@@ -57,74 +52,82 @@ import android.net.Uri;
import android.util.Log;
import android.util.Log;


public class OwnCloudClient extends HttpClient {
public class OwnCloudClient extends HttpClient {
    private static final int MAX_REDIRECTIONS_COUNT = 3;
	
	
    private Uri mUri;
    private static final String TAG = OwnCloudClient.class.getSimpleName();
    private Uri mWebdavUri;
    private Credentials mCredentials;
    private boolean mFollowRedirects;
    private String mSsoSessionCookie;
    final private static String TAG = OwnCloudClient.class.getSimpleName();
    public static final String USER_AGENT = "Android-ownCloud";
    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;
    
    private static byte[] sExhaustBuffer = new byte[1024];
    
    
    static private byte[] sExhaustBuffer = new byte[1024];
    private static int sIntanceCounter = 0;
    private boolean mFollowRedirects = true;
    private OwnCloudCredentials mCredentials = null;
    private int mInstanceNumber = 0;
    
    private Uri mBaseUri;
    
    
    /**
    /**
     * Constructor
     * Constructor
     */
     */
    public OwnCloudClient(HttpConnectionManager connectionMgr) {
    public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
        super(connectionMgr);
        super(connectionMgr);
        Log.d(TAG, "Creating OwnCloudClient");
        
        getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
        if (baseUri == null) {
        getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        	throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
        mFollowRedirects = true;
        mSsoSessionCookie = null;
        }
        }
        mBaseUri = baseUri;
        
        mInstanceNumber = sIntanceCounter++;
        Log.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");
        
        
    public void setBearerCredentials(String accessToken) {
        getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
        AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
        getParams().setParameter(
        		CoreProtocolPNames.PROTOCOL_VERSION, 
        		HttpVersion.HTTP_1_1);
        
        
        List<String> authPrefs = new ArrayList<String>(1);
        getParams().setCookiePolicy(
        authPrefs.add(BearerAuthScheme.AUTH_POLICY);
        		CookiePolicy.BROWSER_COMPATIBILITY);	// to keep sessions
        getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
        getParams().setParameter(
        		PARAM_SINGLE_COOKIE_HEADER, 			// to avoid problems with some web servers
        		PARAM_SINGLE_COOKIE_HEADER_VALUE);
        
        
        mCredentials = new BearerCredentials(accessToken);
        clearCredentials();
        getState().setCredentials(AuthScope.ANY, mCredentials);
        mSsoSessionCookie = null;
    }
    }


    public void setBasicCredentials(String username, String password) {
        List<String> authPrefs = new ArrayList<String>(1);
        authPrefs.add(AuthPolicy.BASIC);
        getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);        
    
    
        getParams().setAuthenticationPreemptive(true);
    public void setCredentials(OwnCloudCredentials credentials) {
        mCredentials = new UsernamePasswordCredentials(username, password);
    	if (credentials != null) {
        getState().setCredentials(AuthScope.ANY, mCredentials);
    		mCredentials = credentials;
        mSsoSessionCookie = null;
    		mCredentials.applyTo(this);
    	} else {
    		clearCredentials();
    	}
    	}
    
    public void setSsoSessionCookie(String accessToken) {
        getParams().setAuthenticationPreemptive(false);
        getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
        mSsoSessionCookie = accessToken;
        mCredentials = null;
    }
    }
    
    
    public void clearCredentials() {
		if (!(mCredentials instanceof OwnCloudAnonymousCredentials)) {
			mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
		}
		mCredentials.applyTo(this);
	}
    
    
    /**
    /**
     * Check if a file exists in the OC server
     * Check if a file exists in the OC server
     * 
     * 
     * TODO replace with ExistenceOperation
     * @deprecated	Use ExistenceCheckOperation instead
     * 
     * 
     * @return              	'true' if the file exists; 'false' it doesn't exist
     * @return              	'true' if the file exists; 'false' it doesn't exist
     * @throws  	Exception   When the existence could not be determined
     * @throws  	Exception   When the existence could not be determined
     */
     */
    @Deprecated
    public boolean existsFile(String path) throws IOException, HttpException {
    public boolean existsFile(String path) throws IOException, HttpException {
        HeadMethod head = new HeadMethod(mWebdavUri.toString() + WebdavUtils.encodePath(path));
        HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path));
        try {
        try {
            int status = executeMethod(head);
            int status = executeMethod(head);
            Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status + ((status != HttpStatus.SC_OK)?"(FAIL)":""));
            Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status +
            		((status != HttpStatus.SC_OK)?"(FAIL)":""));
            exhaustResponse(head.getResponseBodyAsStream());
            exhaustResponse(head.getResponseBodyAsStream());
            return (status == HttpStatus.SC_OK);
            return (status == HttpStatus.SC_OK);
            
            
@@ -140,19 +143,21 @@ public class OwnCloudClient extends HttpClient {
     * 
     * 
     * Sets the socket and connection timeouts only for the method received.
     * Sets the socket and connection timeouts only for the method received.
     * 
     * 
     * The timeouts are both in milliseconds; 0 means 'infinite'; < 0 means 'do not change the default'
     * The timeouts are both in milliseconds; 0 means 'infinite'; 
     * < 0 means 'do not change the default'
     * 
     * 
     * @param method            HTTP method request.
     * @param method            HTTP method request.
     * @param readTimeout       Timeout to set for data reception
     * @param readTimeout       Timeout to set for data reception
     * @param conntionTimout    Timeout to set for connection establishment
     * @param conntionTimout    Timeout to set for connection establishment
     */
     */
    public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout) throws HttpException, IOException {
    public int executeMethod(HttpMethodBase method, int readTimeout, int connectionTimeout) 
    		throws HttpException, IOException {
        int oldSoTimeout = getParams().getSoTimeout();
        int oldSoTimeout = getParams().getSoTimeout();
        int oldConnectionTimeout = getHttpConnectionManager().getParams().getConnectionTimeout();
        int oldConnectionTimeout = getHttpConnectionManager().getParams().getConnectionTimeout();
        try {
        try {
            if (readTimeout >= 0) { 
            if (readTimeout >= 0) { 
                method.getParams().setSoTimeout(readTimeout);   // this should be enough...
                method.getParams().setSoTimeout(readTimeout);   // this should be enough...
                getParams().setSoTimeout(readTimeout);          // ... but this looks like necessary for HTTPS
                getParams().setSoTimeout(readTimeout);          // ... but HTTPS needs this
            }
            }
            if (connectionTimeout >= 0) {
            if (connectionTimeout >= 0) {
                getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
                getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
@@ -167,42 +172,71 @@ public class OwnCloudClient extends HttpClient {
    
    
    @Override
    @Override
    public int executeMethod(HttpMethod method) throws IOException, HttpException {
    public int executeMethod(HttpMethod method) throws IOException, HttpException {
        try {	// just to log 
	        boolean customRedirectionNeeded = false;
	        boolean customRedirectionNeeded = false;
	        
	        try {
	        try {
	            method.setFollowRedirects(mFollowRedirects);
	            method.setFollowRedirects(mFollowRedirects);
	        } catch (Exception e) {
	        } catch (Exception e) {
            //if (mFollowRedirects) Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() + " method, custom redirection will be used if needed");
	        	/*
	            if (mFollowRedirects) 
	        		Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName() 
	        			+ " method, custom redirection will be used if needed");
        		*/
	            customRedirectionNeeded = mFollowRedirects;
	            customRedirectionNeeded = mFollowRedirects;
	        }
	        }
        if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) {
        
            method.addRequestHeader("Cookie", mSsoSessionCookie);
	        Log.d(TAG + " #" + mInstanceNumber, "REQUEST " + 
        }
	        		method.getName() + " " + method.getPath());
        
	        logCookiesAtRequest(method.getRequestHeaders(), "before");
	        logCookiesAtState("before");
	        
	        int status = super.executeMethod(method);
	        int status = super.executeMethod(method);
        
	        if (customRedirectionNeeded) {
	        	status = patchRedirection(status, method);
	        }

	        logCookiesAtRequest(method.getRequestHeaders(), "after");
	        logCookiesAtState("after");
	        logSetCookiesAtResponse(method.getResponseHeaders());
	        
	        return status;
	        
        } catch (IOException e) {
        	Log.d(TAG + " #" + mInstanceNumber, "Exception occured", e);
        	throw e;
        }
    }

	private int patchRedirection(int status, HttpMethod method) throws HttpException, IOException {
        int redirectionsCount = 0;
        int redirectionsCount = 0;
        while (customRedirectionNeeded &&
        while (redirectionsCount < MAX_REDIRECTIONS_COUNT &&
                redirectionsCount < MAX_REDIRECTIONS_COUNT &&
                (   status == HttpStatus.SC_MOVED_PERMANENTLY || 
                (   status == HttpStatus.SC_MOVED_PERMANENTLY || 
                    status == HttpStatus.SC_MOVED_TEMPORARILY ||
                    status == HttpStatus.SC_MOVED_TEMPORARILY ||
                    status == HttpStatus.SC_TEMPORARY_REDIRECT)
                    status == HttpStatus.SC_TEMPORARY_REDIRECT)
                ) {
                ) {
            
            
            Header location = method.getResponseHeader("Location");
            Header location = method.getResponseHeader("Location");
            if (location == null) {
            	location = method.getResponseHeader("location");
            }
            if (location != null) {
            if (location != null) {
                Log.d(TAG,  "Location to redirect: " + location.getValue());
                Log.d(TAG + " #" + mInstanceNumber,  
                		"Location to redirect: " + location.getValue());
                method.setURI(new URI(location.getValue(), true));
                method.setURI(new URI(location.getValue(), true));
                status = super.executeMethod(method);
                status = super.executeMethod(method);
                redirectionsCount++;
                redirectionsCount++;
                
                
            } else {
            } else {
                Log.d(TAG,  "No location to redirect!");
                Log.d(TAG + " #" + mInstanceNumber,  "No location to redirect!");
                status = HttpStatus.SC_NOT_FOUND;
                status = HttpStatus.SC_NOT_FOUND;
            }
            }
        }
        }
        
        return status;
        return status;
	}
	}



	/**
	/**
     * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
     * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
     * 
     * 
@@ -215,53 +249,137 @@ public class OwnCloudClient extends HttpClient {
                responseBodyAsStream.close();
                responseBodyAsStream.close();
            
            
            } catch (IOException io) {
            } catch (IOException io) {
                Log.e(TAG, "Unexpected exception while exhausting not interesting HTTP response; will be IGNORED", io);
                Log.e(TAG, "Unexpected exception while exhausting not interesting HTTP response;" +
                		" will be IGNORED", io);
            }
            }
        }
        }
    }
    }


    /**
    /**
     * Sets the connection and wait-for-data timeouts to be applied by default to the methods performed by this client.
     * Sets the connection and wait-for-data timeouts to be applied by default to the methods 
     * performed by this client.
     */
     */
    public void setDefaultTimeouts(int defaultDataTimeout, int defaultConnectionTimeout) {
    public void setDefaultTimeouts(int defaultDataTimeout, int defaultConnectionTimeout) {
    	if (defaultDataTimeout >= 0) { 
    		getParams().setSoTimeout(defaultDataTimeout);
    		getParams().setSoTimeout(defaultDataTimeout);
    	}
    	if (defaultConnectionTimeout >= 0) {
    		getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout);
    		getHttpConnectionManager().getParams().setConnectionTimeout(defaultConnectionTimeout);
    	}
    	}

    /**
     * Sets the Webdav URI for the helper methods that receive paths as parameters, instead of full URLs
     * @param uri
     */
    public void setWebdavUri(Uri uri) {
        mWebdavUri = uri;
    }
    }


    public Uri getWebdavUri() {
    public Uri getWebdavUri() {
        return mWebdavUri;
    	if (mCredentials instanceof OwnCloudBearerCredentials) {
    		return Uri.parse(mBaseUri + AccountUtils.ODAV_PATH);
    	} else {
    		return Uri.parse(mBaseUri + AccountUtils.WEBDAV_PATH_4_0);
    	}
    }
    }
    
    
    /**
    /**
     * Sets the base URI for the helper methods that receive paths as parameters, instead of full URLs
     * Sets the root URI to the ownCloud server.   
     *
     * Use with care. 
     * 
     * @param uri
     * @param uri
     */
     */
    public void setBaseUri(Uri uri) {
    public void setBaseUri(Uri uri) {
        mUri = uri;
        if (uri == null) {
        	throw new IllegalArgumentException("URI cannot be NULL");
        }
        mBaseUri = uri;
    }
    }


    public Uri getBaseUri() {
    public Uri getBaseUri() {
        return mUri;
        return mBaseUri;
    }
    }


    public final Credentials getCredentials() {
    public final OwnCloudCredentials getCredentials() {
        return mCredentials;
        return mCredentials;
    }
    }
    
    
    public final String getSsoSessionCookie() {
        return mSsoSessionCookie;
    }

    public void setFollowRedirects(boolean followRedirects) {
    public void setFollowRedirects(boolean followRedirects) {
        mFollowRedirects = followRedirects;
        mFollowRedirects = followRedirects;
    }
    }


    
	private void logCookiesAtRequest(Header[] headers, String when) {
        int counter = 0;
        for (int i=0; i<headers.length; i++) {
        	if (headers[i].getName().toLowerCase().equals("cookie")) {
        		Log.d(TAG + " #" + mInstanceNumber, 
        				"Cookies at request (" + when + ") (" + counter++ + "): " + 
        						headers[i].getValue());
        	}
        }
        if (counter == 0) {
    		Log.d(TAG + " #" + mInstanceNumber, "No cookie at request before");
        }
	}

    private void logCookiesAtState(String string) {
        Cookie[] cookies = getState().getCookies();
        if (cookies.length == 0) {
    		Log.d(TAG + " #" + mInstanceNumber, "No cookie at STATE before");
        } else {
    		Log.d(TAG + " #" + mInstanceNumber, "Cookies at STATE (before)");
	        for (int i=0; i<cookies.length; i++) {
	    		Log.d(TAG + " #" + mInstanceNumber, "    (" + i + "):" +
	    				"\n        name: " + cookies[i].getName() +
	    				"\n        value: " + cookies[i].getValue() +
	    				"\n        domain: " + cookies[i].getDomain() +
	    				"\n        path: " + cookies[i].getPath()
	    				);
	        }
        }
	}

	private void logSetCookiesAtResponse(Header[] headers) {
        int counter = 0;
        for (int i=0; i<headers.length; i++) {
        	if (headers[i].getName().toLowerCase().equals("set-cookie")) {
        		Log.d(TAG + " #" + mInstanceNumber, 
        				"Set-Cookie (" + counter++ + "): " + headers[i].getValue());
        	}
        }
        if (counter == 0) {
    		Log.d(TAG + " #" + mInstanceNumber, "No set-cookie");
        }
        
	}
	
	public String getCookiesString(){
		Cookie[] cookies = getState().getCookies(); 
		String cookiesString ="";
		for (Cookie cookie: cookies) {
			cookiesString = cookiesString + cookie.toString() + ";";
			
			logCookie(cookie);
		}
		
		return cookiesString;
		
	}

	public int getConnectionTimeout() {
        return getHttpConnectionManager().getParams().getConnectionTimeout();
	}
	
	public int getDataTimeout() {
		return getParams().getSoTimeout();
	}
	
	private void logCookie(Cookie cookie) {
    	Log.d(TAG, "Cookie name: "+ cookie.getName() );
    	Log.d(TAG, "       value: "+ cookie.getValue() );
    	Log.d(TAG, "       domain: "+ cookie.getDomain());
    	Log.d(TAG, "       path: "+ cookie.getPath() );
    	Log.d(TAG, "       version: "+ cookie.getVersion() );
    	Log.d(TAG, "       expiryDate: " + 
    			(cookie.getExpiryDate() != null ? cookie.getExpiryDate().toString() : "--"));
    	Log.d(TAG, "       comment: "+ cookie.getComment() );
    	Log.d(TAG, "       secure: "+ cookie.getSecure() );
    }


}
}
Loading