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

Commit ddb15a3

Browse files
committed
try shifting over to connection validator for updating credentials
intermediate commit
1 parent c2c351c commit ddb15a3

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ import org.apache.commons.lang3.exception.ExceptionUtils
1111
import timber.log.Timber
1212
import java.lang.Exception
1313

14-
class ConnectionValidator {
14+
class ConnectionValidator (
15+
val clearCookiesOnValidation: Boolean
16+
){
1517

1618
fun validate(method: HttpBaseMethod, baseClient: OwnCloudClient): Boolean {
1719
try {
1820
var validationRetryCount = 0
1921
val client = OwnCloudClient(baseClient.baseUri, null, false)
22+
if (clearCookiesOnValidation) {
23+
client.cookiesForBaseUri = emptyList()
24+
} else {
25+
client.cookiesForBaseUri = baseClient.cookiesForBaseUri
26+
}
27+
//TODO: Also handle cookies
28+
2029
client.credentials = baseClient.credentials
2130
client.setFollowRedirects(true)
2231
while (validationRetryCount < 5) {

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
import java.io.IOException;
4949
import java.io.InputStream;
50+
import java.util.ArrayList;
5051
import java.util.List;
5152

5253
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
@@ -131,24 +132,25 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
131132
}
132133

133134
status = method.execute();
135+
Timber.d("-------------------------------------");
134136
stacklog(status, method);
135137

136138
if (mConnectionValidator != null &&
137139
status == HttpConstants.HTTP_MOVED_TEMPORARILY) {
138140
mConnectionValidator.validate(method, this);
139141
retry = true;
140-
}
141-
142-
if (mFollowRedirects) {
143-
142+
} else if (mFollowRedirects) {
144143
status = followRedirection(method).getLastStatus();
145144
}
146145

146+
/*
147147
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
148148
if (repeatWithFreshCredentials) {
149149
repeatCounter++;
150150
}
151-
} while (repeatWithFreshCredentials);
151+
152+
*/
153+
} while (retry);
152154
// } while (retry);
153155

154156
return status;
@@ -164,6 +166,7 @@ private void stacklog(int status, HttpBaseMethod method) {
164166
"\nobject: " + this.toString() +
165167
"\nMethod: " + method.toString() +
166168
"\nUrl: " + method.getHttpUrl() +
169+
"\nCookeis: " + getCookiesString() +
167170
"\ntrace: " + ExceptionUtils.getStackTrace(e) +
168171
"---------------------------");
169172
}
@@ -220,7 +223,8 @@ public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception
220223
// due to it will be set a different url
221224
exhaustResponse(method.getResponseBodyAsStream());
222225

223-
method.setUrl(HttpUrl.parse(location));
226+
Timber.d("+++++++++++++++++++++++++++++++++++++++ %s", getFullUrl(location));
227+
method.setUrl(getFullUrl(location));
224228
final String destination = method.getRequestHeader("Destination") != null
225229
? method.getRequestHeader("Destination")
226230
: method.getRequestHeader("destination");
@@ -252,6 +256,14 @@ public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception
252256
return redirectionPath;
253257
}
254258

259+
private HttpUrl getFullUrl(String redirection) {
260+
if(redirection.startsWith("/")) {
261+
return HttpUrl.parse(mBaseUri.toString() + redirection);
262+
} else {
263+
return HttpUrl.parse(redirection);
264+
}
265+
}
266+
255267
/**
256268
* Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
257269
*
@@ -322,7 +334,7 @@ public void setCredentials(OwnCloudCredentials credentials) {
322334

323335
public String getCookiesString() {
324336
StringBuilder cookiesString = new StringBuilder();
325-
List<Cookie> cookieList = getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString()));
337+
List<Cookie> cookieList = getCookiesForBaseUri();
326338

327339
if (cookieList != null) {
328340
for (Cookie cookie : cookieList) {
@@ -333,13 +345,18 @@ public String getCookiesString() {
333345
return cookiesString.toString();
334346
}
335347

336-
public void setCookiesForCurrentAccount(List<Cookie> cookies) {
348+
public void setCookiesForBaseUri(List<Cookie> cookies) {
337349
getOkHttpClient().cookieJar().saveFromResponse(
338-
HttpUrl.parse(getAccount().getBaseUri().toString()),
350+
HttpUrl.parse(mBaseUri.toString()),
339351
cookies
340352
);
341353
}
342354

355+
public List<Cookie> getCookiesForBaseUri() {
356+
return getOkHttpClient().cookieJar().loadForRequest(
357+
HttpUrl.parse(mBaseUri.toString()));
358+
}
359+
343360
public OwnCloudVersion getOwnCloudVersion() {
344361
return mVersion;
345362
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
3535
import com.owncloud.android.lib.common.network.CertificateCombinedException;
3636
import okhttp3.Headers;
37+
import org.apache.commons.lang3.exception.ExceptionUtils;
3738
import org.json.JSONException;
3839
import timber.log.Timber;
3940

@@ -112,6 +113,14 @@ public RemoteOperationResult(RemoteOperationResult prevRemoteOperation) {
112113
*/
113114
public RemoteOperationResult(Exception e) {
114115
mException = e;
116+
//TODO: Do propper exception handling and remove this
117+
Timber.e("---------------------------------" +
118+
"\nCreate RemoteOperationResult from exception." +
119+
"\n Message: %s" +
120+
"\n Stacktrace: %s" +
121+
"\n---------------------------------",
122+
ExceptionUtils.getMessage(e),
123+
ExceptionUtils.getStackTrace(e));
115124

116125
if (e instanceof OperationCancelledException) {
117126
mCode = ResultCode.CANCELLED;
@@ -321,7 +330,7 @@ private void parseErrorMessageAndSetCode(String bodyResponse, ResultCode resultC
321330
mHttpPhrase = errorMessage;
322331
}
323332
} catch (Exception e) {
324-
Timber.w("Error reading exception from server: %s", e.getMessage());
333+
Timber.w("Error reading exception from server: %s\nTrace: %s", e.getMessage(), ExceptionUtils.getStackTrace(e));
325334
// mCode stays as set in this(success, httpCode, headers)
326335
}
327336
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,28 @@ import timber.log.Timber
4545
class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
4646

4747
public override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
48-
client.baseUri = buildFullHttpsUrl(client.baseUri)
48+
if(!usesHttpOrHttps(client.baseUri)) {
49+
client.baseUri = buildFullHttpsUrl(client.baseUri)
50+
}
4951

5052
var result = tryToConnect(client)
53+
/*
5154
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
5255
Timber.d("Establishing secure connection failed, trying non secure connection")
5356
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
5457
result = tryToConnect(client)
5558
}
59+
*/
5660

5761
return result
5862
}
5963

6064
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
6165
val baseUrl = client.baseUri.toString()
62-
client.setFollowRedirects(false)
6366
return try {
6467
val requester = StatusRequester()
65-
val requestResult = requester.requestAndFollowRedirects(baseUrl, client)
66-
requester.handleRequestResult(requestResult, baseUrl).also {
67-
client.baseUri = Uri.parse(it.data.baseUrl)
68-
}
68+
val requestResult = requester.request(baseUrl, client)
69+
requester.handleRequestResult(requestResult, baseUrl)
6970
} catch (e: JSONException) {
7071
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
7172
} catch (e: Exception) {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class StatusRequester {
4747
redirectedUrl: String
4848
) = redirectedToNonSecureLocationBefore ||
4949
(baseUrl.startsWith(HTTPS_SCHEME) &&
50-
!redirectedUrl.startsWith(HTTPS_SCHEME))
50+
!redirectedUrl.startsWith(HTTPS_SCHEME))
5151

5252
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
5353
/** Redirection with different endpoint.
@@ -77,32 +77,14 @@ internal class StatusRequester {
7777
val lastLocation: String
7878
)
7979

80-
fun requestAndFollowRedirects(baseLocation: String, client: OwnCloudClient): RequestResult {
80+
fun request(baseLocation: String, client: OwnCloudClient): RequestResult {
8181
var currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
8282
var redirectedToUnsecureLocation = false
8383
var status: Int
8484

85-
while (true) {
86-
val getMethod = getGetMethod(currentLocation)
87-
88-
status = client.executeHttpMethod(getMethod)
89-
val result =
90-
if (status.isSuccess()) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
91-
else RemoteOperationResult(getMethod)
92-
93-
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
94-
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
95-
} else {
96-
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
97-
redirectedToUnsecureLocation =
98-
isRedirectedToNonSecureConnection(
99-
redirectedToUnsecureLocation,
100-
currentLocation,
101-
nextLocation
102-
)
103-
currentLocation = nextLocation
104-
}
105-
}
85+
val getMethod = getGetMethod(currentLocation)
86+
status = client.executeHttpMethod(getMethod)
87+
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
10688
}
10789

10890
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK

0 commit comments

Comments
 (0)