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