1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

apply required fixes

This commit is contained in:
Schabi 2021-02-23 12:35:22 +01:00 committed by Abel García de Prada
parent 79e4287223
commit 344c1e1136
16 changed files with 55 additions and 42 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
.idea/* .idea/*
!.idea/codeStyles/ !.idea/codeStyles/
sample_client/.idea/*
# files for the dex VM # files for the dex VM
*.dex *.dex

View File

@ -19,10 +19,10 @@ class ConnectionValidator(
val context: Context, val context: Context,
val clearCookiesOnValidation: Boolean val clearCookiesOnValidation: Boolean
) { ) {
fun validate(baseClient: OwnCloudClient, singleSessionManager: SingleSessionManager): Boolean { fun validate(baseClient: OwnCloudClient, singleSessionManager: SingleSessionManager, context: Context): Boolean {
try { try {
var validationRetryCount = 0 var validationRetryCount = 0
val client = OwnCloudClient(baseClient.baseUri, null, false, singleSessionManager) val client = OwnCloudClient(baseClient.baseUri, null, false, singleSessionManager, context)
if (clearCookiesOnValidation) { if (clearCookiesOnValidation) {
client.clearCookies() client.clearCookies()
} else { } else {

View File

@ -25,6 +25,7 @@
package com.owncloud.android.lib.common; package com.owncloud.android.lib.common;
import android.content.Context;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.accounts.AccountUtils;
@ -76,7 +77,10 @@ public class OwnCloudClient extends HttpClient {
public OwnCloudClient(Uri baseUri, public OwnCloudClient(Uri baseUri,
ConnectionValidator connectionValidator, ConnectionValidator connectionValidator,
boolean synchronizeRequests, boolean synchronizeRequests,
SingleSessionManager singleSessionManager) { SingleSessionManager singleSessionManager,
Context context) {
super(context);
if (baseUri == null) { if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
} }

View File

@ -24,6 +24,7 @@
package com.owncloud.android.lib.common; package com.owncloud.android.lib.common;
import android.content.Context;
import android.net.Uri; import android.net.Uri;
import com.owncloud.android.lib.common.http.HttpClient; import com.owncloud.android.lib.common.http.HttpClient;
@ -38,11 +39,14 @@ public class OwnCloudClientFactory {
* @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH * @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH
* @return A OwnCloudClient object ready to be used * @return A OwnCloudClient object ready to be used
*/ */
public static OwnCloudClient createOwnCloudClient(Uri uri, boolean followRedirects) { public static OwnCloudClient createOwnCloudClient(Uri uri,
OwnCloudClient client = new OwnCloudClient(uri); ConnectionValidator connectionValidator,
boolean followRedirects,
SingleSessionManager singleSessionManager,
Context context) {
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager, context);
client.setFollowRedirects(followRedirects); client.setFollowRedirects(followRedirects);
HttpClient.setContext(context);
retrieveCookiesFromMiddleware(client); retrieveCookiesFromMiddleware(client);
return client; return client;
} }

View File

@ -77,10 +77,11 @@ public class SingleSessionManager {
sUserAgent = userAgent; sUserAgent = userAgent;
} }
private static OwnCloudClient createOwnCloudClient(Uri uri, Context context, ConnectionValidator connectionValidator, SingleSessionManager singleSessionManager) { private static OwnCloudClient createOwnCloudClient(Uri uri,
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager); Context context,
HttpClient.setContext(context); ConnectionValidator connectionValidator,
SingleSessionManager singleSessionManager) {
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager, context);
return client; return client;
} }
@ -130,9 +131,9 @@ public class SingleSessionManager {
// no client to reuse - create a new one // no client to reuse - create a new one
client = createOwnCloudClient( client = createOwnCloudClient(
account.getBaseUri(), account.getBaseUri(),
context.getApplicationContext(), context,
connectionValidator, connectionValidator,
this); this); // TODO remove dependency on OwnCloudClientFactory
//the next two lines are a hack because okHttpclient is used as a singleton instead of being an //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 //injected instance that can be deleted when required

View File

@ -41,7 +41,6 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -55,26 +54,26 @@ import java.util.concurrent.TimeUnit;
*/ */
public class HttpClient { public class HttpClient {
private static Context sContext; private Context mContext;
private HashMap<String, List<Cookie>> mCookieStore = new HashMap<>(); private HashMap<String, List<Cookie>> mCookieStore = new HashMap<>();
private LogInterceptor mLogInterceptor; private LogInterceptor mLogInterceptor = new LogInterceptor();
private OkHttpClient mOkHttpClient = null; private OkHttpClient mOkHttpClient = null;
protected HttpClient() { protected HttpClient(Context context) {
mLogInterceptor = new LogInterceptor(); mContext = context;
} }
public OkHttpClient getOkHttpClient() { public OkHttpClient getOkHttpClient() {
if(sContext == null) { if (mContext == null) {
Timber.e("Context not initialized call HttpClient.setContext(applicationContext) in the MainApp.onCrate()"); Timber.e("Context not initialized call HttpClient.setContext(applicationContext) in the MainApp.onCrate()");
throw new RuntimeException("Context not initialized call HttpClient.setContext(applicationContext) in the MainApp.onCrate()"); throw new RuntimeException("Context not initialized call HttpClient.setContext(applicationContext) in the" +
" MainApp.onCrate()");
} }
if (mOkHttpClient == null) { if (mOkHttpClient == null) {
try { try {
final X509TrustManager trustManager = new AdvancedX509TrustManager( final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext)); NetworkUtils.getKnownServersStore(mContext));
final SSLContext sslContext = buildSSLContext(); final SSLContext sslContext = buildSSLContext();
sslContext.init(null, new TrustManager[]{trustManager}, null); sslContext.init(null, new TrustManager[]{trustManager}, null);
@ -97,18 +96,18 @@ public class HttpClient {
private SSLContext buildSSLContext() throws NoSuchAlgorithmException { private SSLContext buildSSLContext() throws NoSuchAlgorithmException {
try { try {
return SSLContext.getInstance("TLSv1.3"); return SSLContext.getInstance(TlsVersion.TLS_1_3.javaName());
} catch (NoSuchAlgorithmException tlsv13Exception) { } catch (NoSuchAlgorithmException tlsv13Exception) {
try { try {
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
return SSLContext.getInstance("TLSv1.2"); return SSLContext.getInstance(TlsVersion.TLS_1_2.javaName());
} catch (NoSuchAlgorithmException tlsv12Exception) { } catch (NoSuchAlgorithmException tlsv12Exception) {
try { try {
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
return SSLContext.getInstance("TLSv1.1"); return SSLContext.getInstance(TlsVersion.TLS_1_1.javaName());
} catch (NoSuchAlgorithmException tlsv11Exception) { } catch (NoSuchAlgorithmException tlsv11Exception) {
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
return SSLContext.getInstance("TLSv1"); return SSLContext.getInstance(TlsVersion.TLS_1_0.javaName());
// should be available in any device; see reference of supported protocols in // should be available in any device; see reference of supported protocols in
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
} }
@ -133,7 +132,7 @@ public class HttpClient {
} }
public Context getContext() { public Context getContext() {
return sContext; return mContext;
} }
public LogInterceptor getLogInterceptor() { public LogInterceptor getLogInterceptor() {
@ -144,10 +143,6 @@ public class HttpClient {
return mCookieStore.get(httpUrl.host()); return mCookieStore.get(httpUrl.host());
} }
public static void setContext(Context context) {
sContext = context;
}
public void clearCookies() { public void clearCookies() {
mCookieStore.clear(); mCookieStore.clear();
} }

View File

@ -25,7 +25,6 @@ package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpClient
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
import okhttp3.OkHttpClient
import okhttp3.Response import okhttp3.Response
import java.net.URL import java.net.URL

View File

@ -24,7 +24,6 @@
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL

View File

@ -24,7 +24,6 @@
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL

View File

@ -49,7 +49,7 @@ class UploadFileFromContentUriOperation(
) : RemoteOperation<Unit>() { ) : RemoteOperation<Unit>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
val putMethod = PutMethod(URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(uploadPath)), requestBody).apply { val putMethod = PutMethod(client, URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(uploadPath)), requestBody).apply {
setRetryOnConnectionFailure(false) setRetryOnConnectionFailure(false)
addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, requestBody.contentLength().toString()) addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, requestBody.contentLength().toString())
addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModified) addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModified)

View File

@ -51,7 +51,7 @@ class GetOIDCDiscoveryRemoteOperation : RemoteOperation<OIDCDiscoveryResponse>()
appendPath(OPENID_CONFIGURATION_RESOURCE) appendPath(OPENID_CONFIGURATION_RESOURCE)
}.build() }.build()
val getMethod = GetMethod(URL(uriBuilder.toString())).apply { val getMethod = GetMethod(client, URL(uriBuilder.toString())).apply {
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }

View File

@ -48,6 +48,7 @@ class RegisterClientRemoteOperation(
val requestBody = clientRegistrationParams.toRequestBody() val requestBody = clientRegistrationParams.toRequestBody()
val postMethod = PostMethod( val postMethod = PostMethod(
httpClient = client,
url = URL(clientRegistrationParams.registrationEndpoint), url = URL(clientRegistrationParams.registrationEndpoint),
postRequestBody = requestBody postRequestBody = requestBody
) )

View File

@ -52,7 +52,7 @@ class TokenRequestRemoteOperation(
try { try {
val requestBody = tokenRequestParams.toRequestBody() val requestBody = tokenRequestParams.toRequestBody()
val postMethod = PostMethod(URL(tokenRequestParams.tokenEndpoint), requestBody) val postMethod = PostMethod(client, URL(tokenRequestParams.tokenEndpoint), requestBody)
postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth) postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth)

View File

@ -25,6 +25,7 @@
package com.owncloud.android.lib.resources.status package com.owncloud.android.lib.resources.status
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.http.HttpClient
import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.HttpConstants
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
@ -63,8 +64,8 @@ internal class StatusRequester {
return URL(oldLocationURL.protocol, oldLocationURL.host, oldLocationURL.port, redirectedLocation).toString() return URL(oldLocationURL.protocol, oldLocationURL.host, oldLocationURL.port, redirectedLocation).toString()
} }
private fun getGetMethod(url: String): GetMethod { private fun getGetMethod(client: HttpClient, url: String): GetMethod {
return GetMethod(URL(url)).apply { return GetMethod(client, URL(url)).apply {
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
} }
@ -80,9 +81,14 @@ internal class StatusRequester {
val currentLocation = baseLocation + OwnCloudClient.STATUS_PATH val currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
var status: Int var status: Int
<<<<<<< HEAD
val getMethod = getGetMethod(currentLocation) val getMethod = getGetMethod(currentLocation)
getMethod.setFollowPermanentRedirects(true) getMethod.setFollowPermanentRedirects(true)
status = client.executeHttpMethod(getMethod) status = client.executeHttpMethod(getMethod)
=======
while (true) {
val getMethod = getGetMethod(client, currentLocation)
>>>>>>> 530f8644 (apply required fixes)
return RequestResult(getMethod, status, getMethod.getFinalUrl().toString()) return RequestResult(getMethod, status, getMethod.getFinalUrl().toString())
} }

View File

@ -94,6 +94,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); final Uri serverUri = Uri.parse(getString(R.string.server_base_url));
SingleSessionManager.setUserAgent(getUserAgent()); SingleSessionManager.setUserAgent(getUserAgent());
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false)); SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false));
mClient = new OwnCloudClient(serverUri, mClient = new OwnCloudClient(serverUri,
@ -102,6 +103,9 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
SingleSessionManager.getDefaultSingleton()); SingleSessionManager.getDefaultSingleton());
======= =======
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true); mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true);
=======
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
>>>>>>> 530f8644 (apply required fixes)
>>>>>>> 5e555278 (get okhttp singleton removal changes to compile) >>>>>>> 5e555278 (get okhttp singleton removal changes to compile)
mClient.setCredentials( mClient.setCredentials(