Skip to content

Commit

Permalink
Merge pull request #92 from nextcloud/e2e
Browse files Browse the repository at this point in the history
First release of E2E
  • Loading branch information
tobiasKaminsky authored Jan 9, 2018
2 parents 25e69a4 + 4bf1d42 commit fca8d44
Show file tree
Hide file tree
Showing 31 changed files with 1,278 additions and 137 deletions.
15 changes: 10 additions & 5 deletions src/com/owncloud/android/lib/common/DynamicSessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,25 @@ public class DynamicSessionManager implements OwnCloudClientManager {

@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException,
IOException {
return getClientFor(account, context, false);
}

@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, boolean useNextcloudUserAgent)
throws AccountUtils.AccountNotFoundException,
OperationCanceledException, AuthenticatorException, IOException {

OwnCloudVersion ownCloudVersion = null;
if (account.getSavedAccount() != null) {
ownCloudVersion = AccountUtils.getServerVersionForAccount(
account.getSavedAccount(), context
);
ownCloudVersion = AccountUtils.getServerVersionForAccount(account.getSavedAccount(), context);
}

if (ownCloudVersion != null && ownCloudVersion.isPreemptiveAuthenticationPreferred()) {
return mSingleSessionManager.getClientFor(account, context);
return mSingleSessionManager.getClientFor(account, context, useNextcloudUserAgent);
} else {
return mSimpleFactoryManager.getClientFor(account, context);
return mSimpleFactoryManager.getClientFor(account, context, useNextcloudUserAgent);
}
}

Expand Down
35 changes: 23 additions & 12 deletions src/com/owncloud/android/lib/common/OwnCloudClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,36 @@ public class OwnCloudClient extends HttpClient {

private OwnCloudVersion mVersion = null;

private boolean mUseNextcloudUserAgent = false;

/**
* Constructor
*/
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr) {
public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, boolean useNextcloudUserAgent) {
super(connectionMgr);

if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
}
mBaseUri = baseUri;
mUseNextcloudUserAgent = useNextcloudUserAgent;

mInstanceNumber = sIntanceCounter++;
Log_OC.d(TAG + " #" + mInstanceNumber, "Creating OwnCloudClient");

String userAgent = OwnCloudClientManagerFactory.getUserAgent();
getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
getParams().setParameter(
PARAM_PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
String userAgent;

if (useNextcloudUserAgent) {
userAgent = OwnCloudClientManagerFactory.getNextcloudUserAgent();
} else {
userAgent = OwnCloudClientManagerFactory.getUserAgent();
}

getParams().setCookiePolicy(
CookiePolicy.IGNORE_COOKIES);
getParams().setParameter(
PARAM_SINGLE_COOKIE_HEADER, // to avoid problems with some web servers
PARAM_SINGLE_COOKIE_HEADER_VALUE);
getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
getParams().setParameter(PARAM_PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
// to avoid problems with some web servers
getParams().setParameter(PARAM_SINGLE_COOKIE_HEADER, PARAM_SINGLE_COOKIE_HEADER_VALUE);

applyProxySettings();

Expand Down Expand Up @@ -208,7 +213,13 @@ public int executeMethod(HttpMethod method) throws IOException {
try {
// Update User Agent
HttpParams params = method.getParams();
String userAgent = OwnCloudClientManagerFactory.getUserAgent();

String userAgent;
if (mUseNextcloudUserAgent) {
userAgent = OwnCloudClientManagerFactory.getUserAgent();
} else {
userAgent = OwnCloudClientManagerFactory.getNextcloudUserAgent();
}
params.setParameter(HttpMethodParams.USER_AGENT, userAgent);

Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " +
Expand Down
13 changes: 8 additions & 5 deletions src/com/owncloud/android/lib/common/OwnCloudClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public static OwnCloudClient createOwnCloudClient (Account account, Context appC
* @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,
boolean useNextcloudUserAgent) {
try {
NetworkUtils.registerAdvancedSslContext(true, context);
} catch (GeneralSecurityException e) {
Expand All @@ -221,13 +221,16 @@ public static OwnCloudClient createOwnCloudClient(Uri uri, Context context,
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(),
useNextcloudUserAgent);
client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
client.setFollowRedirects(followRedirects);

return client;
}


public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
return createOwnCloudClient(uri, context, followRedirects, false);
}
}
10 changes: 7 additions & 3 deletions src/com/owncloud/android/lib/common/OwnCloudClientManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

package com.owncloud.android.lib.common;

import java.io.IOException;

import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;

import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;

import java.io.IOException;


/**
* Manager to create and reuse OwnCloudClient instances to access remote OC servers.
Expand All @@ -42,7 +42,11 @@

public interface OwnCloudClientManager {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum Policy {
private static OwnCloudClientManager sDefaultSingleton;

private static String sUserAgent;
private static String sNextcloudUserAgent;

public static OwnCloudClientManager newDefaultOwnCloudClientManager() {
return newOwnCloudClientManager(sDefaultPolicy);
Expand Down Expand Up @@ -86,6 +87,14 @@ public static String getUserAgent() {
return sUserAgent;
}

public static void setNextcloudUserAgent(String userAgent) {
sNextcloudUserAgent = userAgent;
}

public static String getNextcloudUserAgent() {
return sNextcloudUserAgent;
}

private static boolean defaultSingletonMustBeUpdated(Policy policy) {
if (sDefaultSingleton == null) {
return false;
Expand Down
19 changes: 10 additions & 9 deletions src/com/owncloud/android/lib/common/SimpleFactoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ public class SimpleFactoryManager implements OwnCloudClientManager {
private static final String TAG = SimpleFactoryManager.class.getSimpleName();

@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
return getClientFor(account, context, false);
}

@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, boolean useNextcloudUserAgent)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
IOException {

Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");

OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true);
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(account.getBaseUri(),
context.getApplicationContext(), true, useNextcloudUserAgent);

Log_OC.v(TAG, " new client {" +
(account.getName() != null ?
account.getName() :
AccountUtils.buildAccountName(account.getBaseUri(), "")

(account.getName() != null ? account.getName() : AccountUtils.buildAccountName(account.getBaseUri(), "")
) + ", " + client.hashCode() + "}");

if (account.getCredentials() == null) {
Expand Down
126 changes: 64 additions & 62 deletions src/com/owncloud/android/lib/common/SingleSessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@

package com.owncloud.android.lib.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import android.accounts.Account;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
Expand All @@ -44,6 +35,13 @@
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Implementation of {@link OwnCloudClientManager}
*
Expand All @@ -62,92 +60,96 @@ public class SingleSessionManager implements OwnCloudClientManager {

private ConcurrentMap<String, OwnCloudClient> mClientsWithUnknownUsername =
new ConcurrentHashMap<String, OwnCloudClient>();



@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
return getClientFor(account, context, false);
}

@Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context, boolean useNextcloudUserAgent)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException,
IOException {

if (Log.isLoggable(TAG, Log.DEBUG)) {
Log_OC.d(TAG, "getClientFor starting ");
}
if (account == null) {
throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account");
}
if (account == null) {
throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account");
}

OwnCloudClient client = null;
String accountName = account.getName();
String sessionName = account.getCredentials() == null ? "" :
AccountUtils.buildAccountName (
account.getBaseUri(),
account.getCredentials().getAuthToken()
)
;
OwnCloudClient client = null;
String accountName = account.getName();
String sessionName = account.getCredentials() == null ? "" :
AccountUtils.buildAccountName(
account.getBaseUri(),
account.getCredentials().getAuthToken()
);

if (accountName != null) {
client = mClientsWithKnownUsername.get(accountName);
}
boolean reusingKnown = false; // just for logs
if (client == null) {
if (accountName != null) {
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
if (accountName != null) {
client = mClientsWithKnownUsername.get(accountName);
}
boolean reusingKnown = false; // just for logs
if (client == null) {
if (accountName != null) {
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for session " + sessionName);
}
mClientsWithKnownUsername.put(accountName, client);
mClientsWithKnownUsername.put(accountName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "moved client to account " + accountName);
}
}
} else {
client = mClientsWithUnknownUsername.get(sessionName);
}
} else {
}
} else {
client = mClientsWithUnknownUsername.get(sessionName);
}
} else {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for account " + accountName);
}
reusingKnown = true;
}
if (client == null) {
// no client to reuse - create a new one
client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true); // TODO remove dependency on OwnCloudClientFactory
reusingKnown = true;
}

if (client == null) {
// no client to reuse - create a new one
client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
true, useNextcloudUserAgent); // TODO remove dependency on OwnCloudClientFactory
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// enable cookie tracking
AccountUtils.restoreCookies(accountName, client, context);
// enable cookie tracking

AccountUtils.restoreCookies(accountName, client, context);

account.loadCredentials(context);
client.setCredentials(account.getCredentials());
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
client.setCredentials(account.getCredentials());
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "new client for account " + accountName);
}

} else {
mClientsWithUnknownUsername.put(sessionName, client);
} else {
mClientsWithUnknownUsername.put(sessionName, client);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "new client for session " + sessionName);
}
}
} else {
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for session " + sessionName);
}
keepCredentialsUpdated(account, client);
keepUriUpdated(account, client);
}
}
} else {
if (!reusingKnown && Log.isLoggable(TAG, Log.VERBOSE)) {
Log_OC.v(TAG, "reusing client for session " + sessionName);
}
keepCredentialsUpdated(account, client);
keepUriUpdated(account, client);
}

if (Log.isLoggable(TAG, Log.DEBUG)) {
Log_OC.d(TAG, "getClientFor finishing ");
}
return client;
return client;
}


Expand Down
Loading

0 comments on commit fca8d44

Please sign in to comment.