mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9e17936196 | ||
|
8132baa177 | ||
|
0e11f0d4b8 | ||
|
47fd1c2deb | ||
|
660304ada1 |
@ -37,7 +37,6 @@ import com.owncloud.android.lib.resources.status.RemoteServerInfo
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils
|
||||
import timber.log.Timber
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* ConnectionValidator
|
||||
@ -46,7 +45,7 @@ import java.lang.Exception
|
||||
*/
|
||||
class ConnectionValidator(
|
||||
val context: Context,
|
||||
val clearCookiesOnValidation: Boolean
|
||||
private val clearCookiesOnValidation: Boolean
|
||||
) {
|
||||
fun validate(baseClient: OwnCloudClient, singleSessionManager: SingleSessionManager, context: Context): Boolean {
|
||||
try {
|
||||
@ -61,12 +60,12 @@ class ConnectionValidator(
|
||||
client.account = baseClient.account
|
||||
client.credentials = baseClient.credentials
|
||||
while (validationRetryCount < VALIDATION_RETRY_COUNT) {
|
||||
Timber.d("validationRetryCout %d", validationRetryCount)
|
||||
Timber.d("validationRetryCount %d", validationRetryCount)
|
||||
var successCounter = 0
|
||||
var failCounter = 0
|
||||
|
||||
client.setFollowRedirects(true)
|
||||
if (isOnwCloudStatusOk(client)) {
|
||||
if (isOwnCloudStatusOk(client)) {
|
||||
successCounter++
|
||||
} else {
|
||||
failCounter++
|
||||
@ -103,7 +102,7 @@ class ConnectionValidator(
|
||||
return false
|
||||
}
|
||||
|
||||
private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean {
|
||||
private fun isOwnCloudStatusOk(client: OwnCloudClient): Boolean {
|
||||
val reply = getOwnCloudStatus(client)
|
||||
// dont check status code. It currently relais on the broken redirect code of the owncloud client
|
||||
// TODO: Use okhttp redirect and add this check again
|
||||
@ -138,6 +137,12 @@ class ConnectionValidator(
|
||||
// test if have all the needed to effectively invalidate ...
|
||||
shouldInvalidateAccountCredentials =
|
||||
shouldInvalidateAccountCredentials and (account.savedAccount != null)
|
||||
Timber.d(
|
||||
"""Received error: $httpStatusCode,
|
||||
account: ${account.name}
|
||||
credentials are real: ${credentials !is OwnCloudAnonymousCredentials},
|
||||
so we need to invalidate credentials for account ${account.name} : $shouldInvalidateAccountCredentials"""
|
||||
)
|
||||
return shouldInvalidateAccountCredentials
|
||||
}
|
||||
|
||||
@ -150,6 +155,7 @@ class ConnectionValidator(
|
||||
*
|
||||
*/
|
||||
private fun invalidateAccountCredentials(account: OwnCloudAccount, credentials: OwnCloudCredentials) {
|
||||
Timber.i("Invalidating account credentials for account $account")
|
||||
val am = AccountManager.get(context)
|
||||
am.invalidateAuthToken(
|
||||
account.savedAccount.type,
|
||||
@ -167,8 +173,6 @@ class ConnectionValidator(
|
||||
*
|
||||
* Refresh current credentials if possible, and marks a retry.
|
||||
*
|
||||
* @param status
|
||||
* @param repeatCounter
|
||||
* @return
|
||||
*/
|
||||
private fun checkUnauthorizedAccess(client: OwnCloudClient, singleSessionManager: SingleSessionManager, status: Int): Boolean {
|
||||
@ -181,6 +185,7 @@ class ConnectionValidator(
|
||||
if (credentials.authTokenCanBeRefreshed()) {
|
||||
try {
|
||||
// This command does the actual refresh
|
||||
Timber.i("Trying to refresh auth token for account $account")
|
||||
account.loadCredentials(context)
|
||||
// if mAccount.getCredentials().length() == 0 --> refresh failed
|
||||
client.credentials = account.credentials
|
||||
@ -201,6 +206,7 @@ class ConnectionValidator(
|
||||
if (!credentialsWereRefreshed) {
|
||||
// if credentials are not refreshed, client must be removed
|
||||
// from the OwnCloudClientManager to prevent it is reused once and again
|
||||
Timber.w("Credentials were not refreshed, client will be removed from the Session Manager to prevent using it over and over")
|
||||
singleSessionManager.removeClientFor(account)
|
||||
}
|
||||
}
|
||||
@ -210,6 +216,6 @@ class ConnectionValidator(
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val VALIDATION_RETRY_COUNT = 3
|
||||
private const val VALIDATION_RETRY_COUNT = 3
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,6 @@ import android.net.Uri;
|
||||
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
|
||||
import com.owncloud.android.lib.common.http.HttpClient;
|
||||
import timber.log.Timber;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -124,6 +123,24 @@ public class SingleSessionManager {
|
||||
}
|
||||
} else {
|
||||
Timber.v("reusing client for account %s", accountName);
|
||||
if (client.getAccount() != null &&
|
||||
client.getAccount().getCredentials() != null &&
|
||||
(client.getAccount().getCredentials().getAuthToken() == null || client.getAccount().getCredentials().getAuthToken().isEmpty())
|
||||
) {
|
||||
Timber.i("Client " + client.getAccount().getName() + " needs to refresh credentials");
|
||||
|
||||
//the next two lines are a hack because okHttpclient is used as a singleton instead of being an
|
||||
//injected instance that can be deleted when required
|
||||
client.clearCookies();
|
||||
client.clearCredentials();
|
||||
|
||||
client.setAccount(account);
|
||||
|
||||
account.loadCredentials(context);
|
||||
client.setCredentials(account.getCredentials());
|
||||
|
||||
Timber.i("Client " + account.getName() + " with credentials size" + client.getAccount().getCredentials().getAuthToken().length());
|
||||
}
|
||||
reusingKnown = true;
|
||||
}
|
||||
|
||||
|
@ -112,6 +112,7 @@ public class AccountUtils {
|
||||
String username = AccountUtils.getUsernameForAccount(account);
|
||||
|
||||
if (isOauth2) {
|
||||
Timber.i("Trying to retrieve credentials for oAuth account" + account.name);
|
||||
String accessToken = am.blockingGetAuthToken(
|
||||
account,
|
||||
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
|
||||
|
@ -51,7 +51,7 @@ class LogInterceptor : Interceptor {
|
||||
|
||||
val request = chain.request().also {
|
||||
val requestId = it.headers[OC_X_REQUEST_ID]
|
||||
logHttp(REQUEST, INFO, requestId, "Type: ${it.method} URL: ${it.url}")
|
||||
logHttp(REQUEST, INFO, requestId, "Method: ${it.method} URL: ${it.url}")
|
||||
logHeaders(requestId, it.headers, REQUEST)
|
||||
logRequestBody(requestId, it.body)
|
||||
}
|
||||
@ -64,7 +64,7 @@ class LogInterceptor : Interceptor {
|
||||
RESPONSE,
|
||||
INFO,
|
||||
requestId,
|
||||
"Code: ${it.code} Message: ${it.message} IsSuccessful: ${it.isSuccessful}"
|
||||
"Method: ${request.method} URL: ${request.url} Code: ${it.code} Message: ${it.message}"
|
||||
)
|
||||
logHeaders(requestId, it.headers, RESPONSE)
|
||||
logResponseBody(requestId, it.body)
|
||||
|
@ -36,13 +36,7 @@ import com.owncloud.android.lib.common.http.HttpConstants.VALUE_FORMAT
|
||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||
import com.owncloud.android.lib.resources.CommonOcsResponse
|
||||
import com.owncloud.android.lib.resources.shares.responses.ShareItem
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
import timber.log.Timber
|
||||
import java.lang.reflect.Type
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
@ -52,14 +46,10 @@ import java.net.URL
|
||||
* @author David A. Velasco
|
||||
* @author David González Verdugo
|
||||
* @author Fernando Sanz Velasco
|
||||
*/
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param remoteShareId Share ID
|
||||
*/
|
||||
class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation<ShareResponse>() {
|
||||
class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation<Unit>() {
|
||||
|
||||
private fun buildRequestUri(baseUri: Uri) =
|
||||
baseUri.buildUpon()
|
||||
@ -68,24 +58,12 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
||||
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
||||
.build()
|
||||
|
||||
private fun parseResponse(response: String): ShareResponse? {
|
||||
val moshi = Moshi.Builder().build()
|
||||
val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java)
|
||||
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType)
|
||||
val adapter: JsonAdapter<CommonOcsResponse<List<ShareItem>>> = moshi.adapter(commonOcsType)
|
||||
return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems ->
|
||||
ShareResponse(listOfShareItems.map { shareItem ->
|
||||
shareItem.toRemoteShare()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun onResultUnsuccessful(
|
||||
method: DeleteMethod,
|
||||
response: String?,
|
||||
status: Int
|
||||
): RemoteOperationResult<ShareResponse> {
|
||||
Timber.e("Failed response while unshare link ")
|
||||
): RemoteOperationResult<Unit> {
|
||||
Timber.e("Failed response while removing share ")
|
||||
if (response != null) {
|
||||
Timber.e("*** status code: $status; response message: $response")
|
||||
} else {
|
||||
@ -94,17 +72,14 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
||||
return RemoteOperationResult(method)
|
||||
}
|
||||
|
||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareResponse> {
|
||||
val result = RemoteOperationResult<ShareResponse>(RemoteOperationResult.ResultCode.OK)
|
||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<Unit> {
|
||||
val result = RemoteOperationResult<Unit>(RemoteOperationResult.ResultCode.OK)
|
||||
Timber.d("Successful response: $response")
|
||||
result.data = parseResponse(response!!)
|
||||
Timber.d("*** Unshare link completed ")
|
||||
return result
|
||||
}
|
||||
|
||||
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
|
||||
|
||||
|
||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
||||
val requestUri = buildRequestUri(client.baseUri)
|
||||
|
||||
val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply {
|
||||
@ -129,7 +104,7 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
||||
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
||||
|
||||
companion object {
|
||||
//OCS Route
|
||||
// OCS Route
|
||||
private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares"
|
||||
}
|
||||
}
|
||||
|
@ -58,5 +58,5 @@ interface ShareService : Service {
|
||||
publicUpload: Boolean
|
||||
): RemoteOperationResult<ShareResponse>
|
||||
|
||||
fun deleteShare(remoteId: String): RemoteOperationResult<ShareResponse>
|
||||
fun deleteShare(remoteId: String): RemoteOperationResult<Unit>
|
||||
}
|
||||
|
@ -36,8 +36,7 @@ import com.owncloud.android.lib.resources.shares.ShareType
|
||||
import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation
|
||||
import com.owncloud.android.lib.resources.shares.services.ShareService
|
||||
|
||||
class OCShareService(override val client: OwnCloudClient) :
|
||||
ShareService {
|
||||
class OCShareService(override val client: OwnCloudClient) : ShareService {
|
||||
override fun getShares(
|
||||
remoteFilePath: String,
|
||||
reshares: Boolean,
|
||||
@ -90,7 +89,7 @@ class OCShareService(override val client: OwnCloudClient) :
|
||||
this.retrieveShareDetails = true
|
||||
}.execute(client)
|
||||
|
||||
override fun deleteShare(remoteId: String): RemoteOperationResult<ShareResponse> =
|
||||
override fun deleteShare(remoteId: String): RemoteOperationResult<Unit> =
|
||||
RemoveRemoteShareOperation(
|
||||
remoteId
|
||||
).execute(client)
|
||||
|
Loading…
x
Reference in New Issue
Block a user