1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-26 01:06:42 +00:00

Normalized OwnCloudClient to hold an instance to OwnCloudAnonymousCredentials instance of null

This commit is contained in:
David A. Velasco 2014-06-18 14:55:19 +02:00
parent c01001b5a1
commit 179ef41f4c
3 changed files with 15 additions and 96 deletions

View File

@ -43,6 +43,7 @@ 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.OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials;
import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.network.WebdavUtils;
@ -62,13 +63,10 @@ public class OwnCloudClient extends HttpClient {
private static int sIntanceCounter = 0; private static int sIntanceCounter = 0;
private boolean mFollowRedirects = true; private boolean mFollowRedirects = true;
//private Credentials mCredentials = null;
private OwnCloudCredentials mCredentials = null; private OwnCloudCredentials mCredentials = null;
//private String mSsoSessionCookie = null;
private int mInstanceNumber = 0; private int mInstanceNumber = 0;
private Uri mBaseUri; private Uri mBaseUri;
//private Uri mWebdavUri;
/** /**
* Constructor * Constructor
@ -94,100 +92,36 @@ public class OwnCloudClient extends HttpClient {
getParams().setParameter( getParams().setParameter(
PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
PARAM_SINGLE_COOKIE_HEADER_VALUE); PARAM_SINGLE_COOKIE_HEADER_VALUE);
clearCredentials();
} }
public void setCredentials(OwnCloudCredentials credentials) { public void setCredentials(OwnCloudCredentials credentials) {
if (credentials != null) { if (credentials != null) {
mCredentials = credentials; mCredentials = credentials;
mCredentials.applyTo(this); mCredentials.applyTo(this);
} else { } else {
clearCredentials(); clearCredentials();
} }
} }
/*
public void setBearerCredentials(String accessToken) {
AuthPolicy.registerAuthScheme(BearerAuthScheme.AUTH_POLICY, BearerAuthScheme.class);
List<String> authPrefs = new ArrayList<String>(1);
authPrefs.add(BearerAuthScheme.AUTH_POLICY);
getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
getParams().setAuthenticationPreemptive(true);
mCredentials = new BearerCredentials(accessToken);
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);
mCredentials = new UsernamePasswordCredentials(username, password);
getState().setCredentials(AuthScope.ANY, mCredentials);
mSsoSessionCookie = null;
}
*/
/*
public void setSsoSessionCookie(String accessToken) {
Log.d(TAG + " #" + mInstanceNumber, "Setting session cookie: " + accessToken);
Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mBaseUri);
Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri);
if (accessToken != null && accessToken.length() > 0) {
getParams().setAuthenticationPreemptive(false);
mSsoSessionCookie = accessToken;
mCredentials = null;
Uri serverUri = (mBaseUri != null)? mBaseUri : mWebdavUri;
// TODO refactoring the mess of URIs
String[] cookies = mSsoSessionCookie.split(";");
if (cookies.length > 0) {
//Cookie[] cookies = new Cookie[cookiesStr.length];
for (int i=0; i<cookies.length; i++) {
Cookie cookie = new Cookie();
int equalPos = cookies[i].indexOf('=');
cookie.setName(cookies[i].substring(0, equalPos));
//Log.d(TAG, "Set name for cookie: " + cookies[i].substring(0, equalPos));
cookie.setValue(cookies[i].substring(equalPos + 1));
//Log.d(TAG, "Set value for cookie: " + cookies[i].substring(equalPos + 1));
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
//Log.d(TAG, "Set domain for cookie: " + serverUri.getHost());
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
Log.d(TAG, "Set path for cookie: " + serverUri.getPath());
getState().addCookie(cookie);
}
}
} else {
Log.e(TAG, "Setting access token " + accessToken);
}
}
*/
public void clearCredentials() { public void clearCredentials() {
mCredentials = null; if (!(mCredentials instanceof OwnCloudAnonymousCredentials)) {
getState().clearCredentials(); mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
getState().clearCookies(); }
} 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(getWebdavUri() + WebdavUtils.encodePath(path)); HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path));
try { try {
@ -352,22 +286,10 @@ public class OwnCloudClient extends HttpClient {
return mBaseUri; return mBaseUri;
} }
/*
public final Credentials getCredentials() {
return mCredentials;
}
*/
public final OwnCloudCredentials 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;
} }

View File

@ -30,7 +30,8 @@ public class OwnCloudCredentialsFactory {
@Override @Override
public void applyTo(OwnCloudClient client) { public void applyTo(OwnCloudClient client) {
client.clearCredentials(); client.getState().clearCredentials();
client.getState().clearCookies();
} }
@Override @Override

View File

@ -20,10 +20,6 @@ public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
client.setFollowRedirects(false); client.setFollowRedirects(false);
Uri serverUri = client.getBaseUri(); Uri serverUri = client.getBaseUri();
if (serverUri == null) {
// TODO fix the mess of Uris in OwnCloudClient
serverUri = client.getWebdavUri();
}
String[] cookies = mSessionCookie.split(";"); String[] cookies = mSessionCookie.split(";");
if (cookies.length > 0) { if (cookies.length > 0) {