mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
try shifting over to connection validator for updating credentials
intermediate commit
This commit is contained in:
parent
c2c351c912
commit
ddb15a33f1
@ -11,12 +11,21 @@ import org.apache.commons.lang3.exception.ExceptionUtils
|
||||
import timber.log.Timber
|
||||
import java.lang.Exception
|
||||
|
||||
class ConnectionValidator {
|
||||
class ConnectionValidator (
|
||||
val clearCookiesOnValidation: Boolean
|
||||
){
|
||||
|
||||
fun validate(method: HttpBaseMethod, baseClient: OwnCloudClient): Boolean {
|
||||
try {
|
||||
var validationRetryCount = 0
|
||||
val client = OwnCloudClient(baseClient.baseUri, null, false)
|
||||
if (clearCookiesOnValidation) {
|
||||
client.cookiesForBaseUri = emptyList()
|
||||
} else {
|
||||
client.cookiesForBaseUri = baseClient.cookiesForBaseUri
|
||||
}
|
||||
//TODO: Also handle cookies
|
||||
|
||||
client.credentials = baseClient.credentials
|
||||
client.setFollowRedirects(true)
|
||||
while (validationRetryCount < 5) {
|
||||
|
@ -47,6 +47,7 @@ import timber.log.Timber;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
|
||||
@ -131,24 +132,25 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
|
||||
status = method.execute();
|
||||
Timber.d("-------------------------------------");
|
||||
stacklog(status, method);
|
||||
|
||||
if (mConnectionValidator != null &&
|
||||
status == HttpConstants.HTTP_MOVED_TEMPORARILY) {
|
||||
mConnectionValidator.validate(method, this);
|
||||
retry = true;
|
||||
}
|
||||
|
||||
if (mFollowRedirects) {
|
||||
|
||||
} else if (mFollowRedirects) {
|
||||
status = followRedirection(method).getLastStatus();
|
||||
}
|
||||
|
||||
/*
|
||||
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
|
||||
if (repeatWithFreshCredentials) {
|
||||
repeatCounter++;
|
||||
}
|
||||
} while (repeatWithFreshCredentials);
|
||||
|
||||
*/
|
||||
} while (retry);
|
||||
// } while (retry);
|
||||
|
||||
return status;
|
||||
@ -164,6 +166,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
"\nobject: " + this.toString() +
|
||||
"\nMethod: " + method.toString() +
|
||||
"\nUrl: " + method.getHttpUrl() +
|
||||
"\nCookeis: " + getCookiesString() +
|
||||
"\ntrace: " + ExceptionUtils.getStackTrace(e) +
|
||||
"---------------------------");
|
||||
}
|
||||
@ -220,7 +223,8 @@ public class OwnCloudClient extends HttpClient {
|
||||
// due to it will be set a different url
|
||||
exhaustResponse(method.getResponseBodyAsStream());
|
||||
|
||||
method.setUrl(HttpUrl.parse(location));
|
||||
Timber.d("+++++++++++++++++++++++++++++++++++++++ %s", getFullUrl(location));
|
||||
method.setUrl(getFullUrl(location));
|
||||
final String destination = method.getRequestHeader("Destination") != null
|
||||
? method.getRequestHeader("Destination")
|
||||
: method.getRequestHeader("destination");
|
||||
@ -252,6 +256,14 @@ public class OwnCloudClient extends HttpClient {
|
||||
return redirectionPath;
|
||||
}
|
||||
|
||||
private HttpUrl getFullUrl(String redirection) {
|
||||
if(redirection.startsWith("/")) {
|
||||
return HttpUrl.parse(mBaseUri.toString() + redirection);
|
||||
} else {
|
||||
return HttpUrl.parse(redirection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
|
||||
*
|
||||
@ -322,7 +334,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
public String getCookiesString() {
|
||||
StringBuilder cookiesString = new StringBuilder();
|
||||
List<Cookie> cookieList = getCookiesFromUrl(HttpUrl.parse(mBaseUri.toString()));
|
||||
List<Cookie> cookieList = getCookiesForBaseUri();
|
||||
|
||||
if (cookieList != null) {
|
||||
for (Cookie cookie : cookieList) {
|
||||
@ -333,13 +345,18 @@ public class OwnCloudClient extends HttpClient {
|
||||
return cookiesString.toString();
|
||||
}
|
||||
|
||||
public void setCookiesForCurrentAccount(List<Cookie> cookies) {
|
||||
public void setCookiesForBaseUri(List<Cookie> cookies) {
|
||||
getOkHttpClient().cookieJar().saveFromResponse(
|
||||
HttpUrl.parse(getAccount().getBaseUri().toString()),
|
||||
HttpUrl.parse(mBaseUri.toString()),
|
||||
cookies
|
||||
);
|
||||
}
|
||||
|
||||
public List<Cookie> getCookiesForBaseUri() {
|
||||
return getOkHttpClient().cookieJar().loadForRequest(
|
||||
HttpUrl.parse(mBaseUri.toString()));
|
||||
}
|
||||
|
||||
public OwnCloudVersion getOwnCloudVersion() {
|
||||
return mVersion;
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ import com.owncloud.android.lib.common.http.HttpConstants;
|
||||
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
|
||||
import com.owncloud.android.lib.common.network.CertificateCombinedException;
|
||||
import okhttp3.Headers;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.json.JSONException;
|
||||
import timber.log.Timber;
|
||||
|
||||
@ -112,6 +113,14 @@ public class RemoteOperationResult<T>
|
||||
*/
|
||||
public RemoteOperationResult(Exception e) {
|
||||
mException = e;
|
||||
//TODO: Do propper exception handling and remove this
|
||||
Timber.e("---------------------------------" +
|
||||
"\nCreate RemoteOperationResult from exception." +
|
||||
"\n Message: %s" +
|
||||
"\n Stacktrace: %s" +
|
||||
"\n---------------------------------",
|
||||
ExceptionUtils.getMessage(e),
|
||||
ExceptionUtils.getStackTrace(e));
|
||||
|
||||
if (e instanceof OperationCancelledException) {
|
||||
mCode = ResultCode.CANCELLED;
|
||||
@ -321,7 +330,7 @@ public class RemoteOperationResult<T>
|
||||
mHttpPhrase = errorMessage;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Timber.w("Error reading exception from server: %s", e.getMessage());
|
||||
Timber.w("Error reading exception from server: %s\nTrace: %s", e.getMessage(), ExceptionUtils.getStackTrace(e));
|
||||
// mCode stays as set in this(success, httpCode, headers)
|
||||
}
|
||||
}
|
||||
|
@ -45,27 +45,28 @@ import timber.log.Timber
|
||||
class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
|
||||
|
||||
public override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
|
||||
client.baseUri = buildFullHttpsUrl(client.baseUri)
|
||||
if(!usesHttpOrHttps(client.baseUri)) {
|
||||
client.baseUri = buildFullHttpsUrl(client.baseUri)
|
||||
}
|
||||
|
||||
var result = tryToConnect(client)
|
||||
/*
|
||||
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
|
||||
Timber.d("Establishing secure connection failed, trying non secure connection")
|
||||
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
|
||||
result = tryToConnect(client)
|
||||
}
|
||||
*/
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
|
||||
val baseUrl = client.baseUri.toString()
|
||||
client.setFollowRedirects(false)
|
||||
return try {
|
||||
val requester = StatusRequester()
|
||||
val requestResult = requester.requestAndFollowRedirects(baseUrl, client)
|
||||
requester.handleRequestResult(requestResult, baseUrl).also {
|
||||
client.baseUri = Uri.parse(it.data.baseUrl)
|
||||
}
|
||||
val requestResult = requester.request(baseUrl, client)
|
||||
requester.handleRequestResult(requestResult, baseUrl)
|
||||
} catch (e: JSONException) {
|
||||
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
||||
} catch (e: Exception) {
|
||||
|
@ -47,7 +47,7 @@ internal class StatusRequester {
|
||||
redirectedUrl: String
|
||||
) = redirectedToNonSecureLocationBefore ||
|
||||
(baseUrl.startsWith(HTTPS_SCHEME) &&
|
||||
!redirectedUrl.startsWith(HTTPS_SCHEME))
|
||||
!redirectedUrl.startsWith(HTTPS_SCHEME))
|
||||
|
||||
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
|
||||
/** Redirection with different endpoint.
|
||||
@ -77,32 +77,14 @@ internal class StatusRequester {
|
||||
val lastLocation: String
|
||||
)
|
||||
|
||||
fun requestAndFollowRedirects(baseLocation: String, client: OwnCloudClient): RequestResult {
|
||||
fun request(baseLocation: String, client: OwnCloudClient): RequestResult {
|
||||
var currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
|
||||
var redirectedToUnsecureLocation = false
|
||||
var status: Int
|
||||
|
||||
while (true) {
|
||||
val getMethod = getGetMethod(currentLocation)
|
||||
|
||||
status = client.executeHttpMethod(getMethod)
|
||||
val result =
|
||||
if (status.isSuccess()) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
|
||||
else RemoteOperationResult(getMethod)
|
||||
|
||||
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
|
||||
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
|
||||
} else {
|
||||
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
|
||||
redirectedToUnsecureLocation =
|
||||
isRedirectedToNonSecureConnection(
|
||||
redirectedToUnsecureLocation,
|
||||
currentLocation,
|
||||
nextLocation
|
||||
)
|
||||
currentLocation = nextLocation
|
||||
}
|
||||
}
|
||||
val getMethod = getGetMethod(currentLocation)
|
||||
status = client.executeHttpMethod(getMethod)
|
||||
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
|
||||
}
|
||||
|
||||
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK
|
||||
|
Loading…
x
Reference in New Issue
Block a user