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/codeStyles/
sample_client/.idea/*
# files for the dex VM
*.dex

View File

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

View File

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

View File

@ -24,6 +24,7 @@
package com.owncloud.android.lib.common;
import android.content.Context;
import android.net.Uri;
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
* @return A OwnCloudClient object ready to be used
*/
public static OwnCloudClient createOwnCloudClient(Uri uri, boolean followRedirects) {
OwnCloudClient client = new OwnCloudClient(uri);
public static OwnCloudClient createOwnCloudClient(Uri uri,
ConnectionValidator connectionValidator,
boolean followRedirects,
SingleSessionManager singleSessionManager,
Context context) {
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager, context);
client.setFollowRedirects(followRedirects);
HttpClient.setContext(context);
retrieveCookiesFromMiddleware(client);
return client;
}

View File

@ -77,10 +77,11 @@ public class SingleSessionManager {
sUserAgent = userAgent;
}
private static OwnCloudClient createOwnCloudClient(Uri uri, Context context, ConnectionValidator connectionValidator, SingleSessionManager singleSessionManager) {
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager);
HttpClient.setContext(context);
private static OwnCloudClient createOwnCloudClient(Uri uri,
Context context,
ConnectionValidator connectionValidator,
SingleSessionManager singleSessionManager) {
OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager, context);
return client;
}
@ -130,9 +131,9 @@ public class SingleSessionManager {
// no client to reuse - create a new one
client = createOwnCloudClient(
account.getBaseUri(),
context.getApplicationContext(),
context,
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
//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.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
@ -55,26 +54,26 @@ import java.util.concurrent.TimeUnit;
*/
public class HttpClient {
private static Context sContext;
private Context mContext;
private HashMap<String, List<Cookie>> mCookieStore = new HashMap<>();
private LogInterceptor mLogInterceptor;
private LogInterceptor mLogInterceptor = new LogInterceptor();
private OkHttpClient mOkHttpClient = null;
protected HttpClient() {
mLogInterceptor = new LogInterceptor();
protected HttpClient(Context context) {
mContext = context;
}
public OkHttpClient getOkHttpClient() {
if(sContext == null) {
if (mContext == null) {
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 {
final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext));
NetworkUtils.getKnownServersStore(mContext));
final SSLContext sslContext = buildSSLContext();
sslContext.init(null, new TrustManager[]{trustManager}, null);
@ -84,7 +83,7 @@ public class HttpClient {
final CookieJar cookieJar = new CookieJarImpl(mCookieStore);
mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
} catch(NoSuchAlgorithmException nsae){
} catch (NoSuchAlgorithmException nsae) {
Timber.e(nsae, "Could not setup SSL system.");
throw new RuntimeException("Could not setup okHttp client.", nsae);
} catch (Exception e) {
@ -97,18 +96,18 @@ public class HttpClient {
private SSLContext buildSSLContext() throws NoSuchAlgorithmException {
try {
return SSLContext.getInstance("TLSv1.3");
return SSLContext.getInstance(TlsVersion.TLS_1_3.javaName());
} catch (NoSuchAlgorithmException tlsv13Exception) {
try {
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) {
try {
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) {
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
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
}
@ -133,7 +132,7 @@ public class HttpClient {
}
public Context getContext() {
return sContext;
return mContext;
}
public LogInterceptor getLogInterceptor() {
@ -144,10 +143,6 @@ public class HttpClient {
return mCookieStore.get(httpUrl.host());
}
public static void setContext(Context context) {
sContext = context;
}
public void clearCookies() {
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.methods.HttpBaseMethod
import okhttp3.OkHttpClient
import okhttp3.Response
import java.net.URL

View File

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

View File

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

View File

@ -44,7 +44,7 @@ import java.util.concurrent.TimeUnit
*
* @author David González Verdugo
*/
abstract class DavMethod protected constructor(httpClient: HttpClient,url: URL) : HttpBaseMethod(httpClient, url) {
abstract class DavMethod protected constructor(httpClient: HttpClient, url: URL) : HttpBaseMethod(httpClient, url) {
protected var davResource: DavOCResource
override lateinit var response: Response

View File

@ -49,7 +49,7 @@ class UploadFileFromContentUriOperation(
) : RemoteOperation<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)
addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, requestBody.contentLength().toString())
addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModified)

View File

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

View File

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

View File

@ -52,7 +52,7 @@ class TokenRequestRemoteOperation(
try {
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)

View File

@ -25,6 +25,7 @@
package com.owncloud.android.lib.resources.status
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.methods.nonwebdav.GetMethod
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()
}
private fun getGetMethod(url: String): GetMethod {
return GetMethod(URL(url)).apply {
private fun getGetMethod(client: HttpClient, url: String): GetMethod {
return GetMethod(client, URL(url)).apply {
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
}
@ -80,9 +81,14 @@ internal class StatusRequester {
val currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
var status: Int
<<<<<<< HEAD
val getMethod = getGetMethod(currentLocation)
getMethod.setFollowPermanentRedirects(true)
status = client.executeHttpMethod(getMethod)
=======
while (true) {
val getMethod = getGetMethod(client, currentLocation)
>>>>>>> 530f8644 (apply required fixes)
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));
SingleSessionManager.setUserAgent(getUserAgent());
<<<<<<< HEAD
<<<<<<< HEAD
SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false));
mClient = new OwnCloudClient(serverUri,
@ -102,6 +103,9 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
SingleSessionManager.getDefaultSingleton());
=======
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true);
=======
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
>>>>>>> 530f8644 (apply required fixes)
>>>>>>> 5e555278 (get okhttp singleton removal changes to compile)
mClient.setCredentials(