Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 7e4b43e

Browse files
committed
prepare code for the inclusion of connection validator
1 parent 0e82f98 commit 7e4b43e

File tree

4 files changed

+69
-69
lines changed

4 files changed

+69
-69
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.owncloud.android.lib.common
22

3+
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
4+
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod
35
import timber.log.Timber
46

5-
class ConnectionValidator (
6-
private val ocClient: OwnCloudClient
7-
) {
7+
class ConnectionValidator {
88

9-
fun dosomething() {
10-
Timber.d(ocClient.toString())
9+
fun validate(method: HttpBaseMethod, client: OwnCloudClient) {
10+
Timber.d("hello world")
1111
}
1212
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import android.accounts.AccountManager;
2929
import android.accounts.AccountsException;
3030
import android.net.Uri;
31-
import android.util.Log;
3231

3332
import at.bitfire.dav4jvm.exception.HttpException;
3433
import com.owncloud.android.lib.common.accounts.AccountUtils;
@@ -38,7 +37,6 @@
3837
import com.owncloud.android.lib.common.http.HttpClient;
3938
import com.owncloud.android.lib.common.http.HttpConstants;
4039
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
41-
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod;
4240
import com.owncloud.android.lib.common.network.RedirectionPath;
4341
import com.owncloud.android.lib.common.utils.RandomUtils;
4442
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
@@ -70,26 +68,32 @@ public class OwnCloudClient extends HttpClient {
7068
private Uri mBaseUri;
7169
private OwnCloudVersion mVersion = null;
7270
private OwnCloudAccount mAccount;
73-
private ConnectionValidator mConnectionValidator;
71+
private final ConnectionValidator mConnectionValidator;
72+
private Object mRequestMutex = new Object();
7473

75-
private static Boolean mHoldRequests = false;
74+
// If set to true a mutex will be used to prevent parallel execution of the execute() method
75+
// if false the execute() method can be called even though the mutex is already aquired.
76+
// This is used for the ConnectionValidator, which has to be able to execute OperationsWhile all "normal" operations net
77+
// to be set on hold.
78+
private final Boolean mSynchronizeRequests;
7679

7780
private SingleSessionManager mSingleSessionManager = null;
7881

7982
private boolean mFollowRedirects;
8083

81-
public OwnCloudClient(Uri baseUri) {
84+
public OwnCloudClient(Uri baseUri, ConnectionValidator connectionValidator, boolean synchronizeRequests) {
8285
if (baseUri == null) {
8386
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
8487
}
8588
mBaseUri = baseUri;
89+
mSynchronizeRequests = synchronizeRequests;
8690

8791
mInstanceNumber = sIntanceCounter++;
8892
Timber.d("#" + mInstanceNumber + "Creating OwnCloudClient");
8993

9094
clearCredentials();
9195
clearCookies();
92-
mConnectionValidator = new ConnectionValidator(this);
96+
mConnectionValidator = connectionValidator;
9397
}
9498

9599
public void clearCredentials() {
@@ -99,10 +103,21 @@ public void clearCredentials() {
99103
}
100104

101105
public int executeHttpMethod(HttpBaseMethod method) throws Exception {
106+
if(mSynchronizeRequests) {
107+
synchronized (mRequestMutex) {
108+
return saveExecuteHttpMethod(method);
109+
}
110+
} else {
111+
return saveExecuteHttpMethod(method);
112+
}
113+
}
114+
115+
private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
102116
boolean repeatWithFreshCredentials;
103117
int repeatCounter = 0;
104118
int status;
105119

120+
boolean retry = false;
106121
do {
107122
String requestId = RandomUtils.generateRandomUUID();
108123

@@ -117,44 +132,14 @@ public int executeHttpMethod(HttpBaseMethod method) throws Exception {
117132

118133
status = method.execute();
119134
stacklog(status, method);
120-
/*
121-
synchronized (mHoldRequests) {
122-
while (mHoldRequests) {
123-
while (true) {
124-
try {
125-
throw new Exception("Stack log");
126-
} catch (Exception e) {
127-
Timber.d( "HATL BEFORE" +
128-
"\nThread: " + Thread.currentThread().getName() +
129-
"\nobject: " + this.toString() +
130-
"\nMethod: " + method.getHttpUrl() +
131-
"\ntrace: " + ExceptionUtils.getStackTrace(e));
132-
}
133-
Thread.sleep(40000);
134-
}
135-
}
136-
status = method.execute();
137-
if (status == 302) {
138-
mHoldRequests = true;
139-
while (mHoldRequests) {
140-
try {
141-
throw new Exception("Stack log");
142-
} catch (Exception e) {
143-
Timber.d( "HALT AFTER" +
144-
"\nresponsecode: " + Integer.toString(status) +
145-
"\nThread: " + Thread.currentThread().getName() +
146-
"\nobject: " + this.toString() +
147-
"\nMethod: " + method.getHttpUrl() +
148-
"\ntrace: " + ExceptionUtils.getStackTrace(e));
149-
}
150-
Thread.sleep(40000);
151-
}
152-
}
153135

136+
if (status == HttpConstants.HTTP_MOVED_TEMPORARILY) {
137+
mConnectionValidator.validate(method, this);
138+
retry = true;
154139
}
155-
*/
156140

157141
if (mFollowRedirects) {
142+
158143
status = followRedirection(method).getLastStatus();
159144
}
160145

@@ -163,6 +148,7 @@ public int executeHttpMethod(HttpBaseMethod method) throws Exception {
163148
repeatCounter++;
164149
}
165150
} while (repeatWithFreshCredentials);
151+
// } while (retry);
166152

167153
return status;
168154
}
@@ -384,22 +370,20 @@ private boolean checkUnauthorizedAccess(int status, int repeatCounter) {
384370
boolean credentialsWereRefreshed = false;
385371

386372
if (shouldInvalidateAccountCredentials(status)) {
387-
boolean invalidated = invalidateAccountCredentials();
388-
389-
if (invalidated) {
390-
if (getCredentials().authTokenCanBeRefreshed() &&
391-
repeatCounter < MAX_REPEAT_COUNT_WITH_FRESH_CREDENTIALS) {
392-
try {
393-
mAccount.loadCredentials(getContext());
394-
// if mAccount.getCredentials().length() == 0 --> refresh failed
395-
setCredentials(mAccount.getCredentials());
396-
credentialsWereRefreshed = true;
397-
398-
} catch (AccountsException | IOException e) {
399-
Timber.e(e, "Error while trying to refresh auth token for %s",
400-
mAccount.getSavedAccount().name
401-
);
402-
}
373+
invalidateAccountCredentials();
374+
375+
if (getCredentials().authTokenCanBeRefreshed() &&
376+
repeatCounter < MAX_REPEAT_COUNT_WITH_FRESH_CREDENTIALS) {
377+
try {
378+
mAccount.loadCredentials(getContext());
379+
// if mAccount.getCredentials().length() == 0 --> refresh failed
380+
setCredentials(mAccount.getCredentials());
381+
credentialsWereRefreshed = true;
382+
383+
} catch (AccountsException | IOException e) {
384+
Timber.e(e, "Error while trying to refresh auth token for %s",
385+
mAccount.getSavedAccount().name
386+
);
403387
}
404388

405389
if (!credentialsWereRefreshed && mSingleSessionManager != null) {
@@ -441,16 +425,14 @@ private boolean shouldInvalidateAccountCredentials(int httpStatusCode) {
441425
* <p>
442426
* {@link #shouldInvalidateAccountCredentials(int)} should be called first.
443427
*
444-
* @return 'True' if invalidation was successful, 'false' otherwise.
445428
*/
446-
private boolean invalidateAccountCredentials() {
429+
private void invalidateAccountCredentials() {
447430
AccountManager am = AccountManager.get(getContext());
448431
am.invalidateAuthToken(
449432
mAccount.getSavedAccount().type,
450433
mCredentials.getAuthToken()
451434
);
452435
am.clearPassword(mAccount.getSavedAccount()); // being strict, only needed for Basic Auth credentials
453-
return true;
454436
}
455437

456438
public boolean followRedirects() {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class SingleSessionManager {
4949

5050
private static SingleSessionManager sDefaultSingleton;
5151
private static String sUserAgent;
52+
private static ConnectionValidator sConnectionValidator;
5253

5354
private ConcurrentMap<String, OwnCloudClient> mClientsWithKnownUsername = new ConcurrentHashMap<>();
5455
private ConcurrentMap<String, OwnCloudClient> mClientsWithUnknownUsername = new ConcurrentHashMap<>();
@@ -60,6 +61,14 @@ public static SingleSessionManager getDefaultSingleton() {
6061
return sDefaultSingleton;
6162
}
6263

64+
public static void setConnectionValidator(ConnectionValidator connectionValidator) {
65+
sConnectionValidator = connectionValidator;
66+
}
67+
68+
public static ConnectionValidator getConnectionValidator() {
69+
return sConnectionValidator;
70+
}
71+
6372
public static String getUserAgent() {
6473
return sUserAgent;
6574
}
@@ -68,15 +77,23 @@ public static void setUserAgent(String userAgent) {
6877
sUserAgent = userAgent;
6978
}
7079

71-
private static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
72-
OwnCloudClient client = new OwnCloudClient(uri);
80+
private static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects, ConnectionValidator connectionValidator) {
81+
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true);
7382
client.setFollowRedirects(followRedirects);
7483
HttpClient.setContext(context);
7584

7685
return client;
7786
}
7887

79-
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws OperationCanceledException,
88+
public OwnCloudClient getClientFor(OwnCloudAccount account,
89+
Context context) throws OperationCanceledException,
90+
AuthenticatorException, IOException {
91+
return getClientFor(account, context, getConnectionValidator());
92+
}
93+
94+
public OwnCloudClient getClientFor(OwnCloudAccount account,
95+
Context context,
96+
ConnectionValidator connectionValidator) throws OperationCanceledException,
8097
AuthenticatorException, IOException {
8198

8299
Timber.d("getClientFor starting ");
@@ -115,7 +132,8 @@ public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) thr
115132
client = createOwnCloudClient(
116133
account.getBaseUri(),
117134
context.getApplicationContext(),
118-
true); // TODO remove dependency on OwnCloudClientFactory
135+
true,
136+
connectionValidator); // TODO remove dependency on OwnCloudClientFactory
119137

120138
//the next two lines are a hack because okHttpclient is used as a singleton instead of being an
121139
//injected instance that can be deleted when required

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void grantOwnCloudClient() throws
159159
if (mAccount != null && mContext != null) {
160160
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
161161
mClient = SingleSessionManager.getDefaultSingleton().
162-
getClientFor(ocAccount, mContext);
162+
getClientFor(ocAccount, mContext, SingleSessionManager.getConnectionValidator());
163163
} else {
164164
throw new IllegalStateException("Trying to run a remote operation " +
165165
"asynchronously with no client and no chance to create one (no account)");

0 commit comments

Comments
 (0)