From 0313c1e103afeecde9c998d10d6c7c34549e6b86 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Mon, 4 Jan 2021 15:08:10 +0100 Subject: [PATCH 1/9] get okhttp singleton removal changes to compile --- .../lib/common/OwnCloudClientFactory.java | 54 +++++++++++ .../lib/common/SingleSessionManager.java | 1 - .../android/lib/common/http/HttpClient.java | 45 +++++---- .../lib/common/http/methods/HttpBaseMethod.kt | 4 +- .../http/methods/nonwebdav/DeleteMethod.kt | 3 +- .../http/methods/nonwebdav/GetMethod.kt | 3 +- .../http/methods/nonwebdav/HttpMethod.kt | 5 +- .../http/methods/nonwebdav/PostMethod.kt | 5 +- .../http/methods/nonwebdav/PutMethod.kt | 5 +- .../common/http/methods/webdav/CopyMethod.kt | 4 +- .../common/http/methods/webdav/DavMethod.kt | 3 +- .../common/http/methods/webdav/MkColMethod.kt | 3 +- .../common/http/methods/webdav/MoveMethod.kt | 4 +- .../http/methods/webdav/PropfindMethod.kt | 4 +- .../common/http/methods/webdav/PutMethod.kt | 4 +- .../CheckPathExistenceRemoteOperation.kt | 2 +- .../files/CopyRemoteFileOperation.java | 3 +- .../files/CreateRemoteFolderOperation.java | 3 +- .../files/DownloadRemoteFileOperation.java | 2 +- .../files/MoveRemoteFileOperation.java | 2 +- .../files/ReadRemoteFileOperation.java | 3 +- .../files/ReadRemoteFolderOperation.java | 1 + .../files/RemoveRemoteFileOperation.java | 1 + .../files/RenameRemoteFileOperation.java | 3 +- .../files/UploadRemoteFileOperation.java | 2 +- .../ChunkedUploadRemoteFileOperation.java | 3 +- .../shares/CreateRemoteShareOperation.kt | 3 +- .../shares/GetRemoteShareOperation.java | 94 +++++++++++++++++++ .../shares/GetRemoteShareesOperation.kt | 2 +- .../shares/GetRemoteSharesForFileOperation.kt | 2 +- .../shares/RemoveRemoteShareOperation.kt | 3 +- .../shares/UpdateRemoteShareOperation.kt | 2 +- .../status/GetRemoteCapabilitiesOperation.kt | 2 +- .../status/GetRemoteStatusOperation.kt | 1 + .../status/services/ServerInfoService.kt | 1 + .../implementation/OCServerInfoService.kt | 1 + .../users/GetRemoteUserAvatarOperation.kt | 2 +- .../users/GetRemoteUserInfoOperation.kt | 2 +- .../users/GetRemoteUserQuotaOperation.kt | 1 + .../lib/sampleclient/MainActivity.java | 18 ++++ 40 files changed, 256 insertions(+), 50 deletions(-) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java new file mode 100644 index 00000000..24380047 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -0,0 +1,54 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2020 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.common; + +import android.net.Uri; + +import com.owncloud.android.lib.common.http.HttpClient; +import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; + +public class OwnCloudClientFactory { + + /** + * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud + * client connections. + * + * @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); + + client.setFollowRedirects(followRedirects); + HttpClient.setContext(context); + retrieveCookiesFromMiddleware(client); + return client; + } + + private static void retrieveCookiesFromMiddleware(OwnCloudClient client) { + final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation(); + statusOperation.run(client); + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java index e2c75c34..51d3a431 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java @@ -140,7 +140,6 @@ public class SingleSessionManager { client.clearCredentials(); client.setAccount(account); - HttpClient.setContext(context); account.loadCredentials(context); client.setCredentials(account.getCredentials()); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 51d58b53..1e0e760f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -55,30 +55,38 @@ import java.util.concurrent.TimeUnit; */ public class HttpClient { - private static OkHttpClient sOkHttpClient; private static Context sContext; - private static HashMap> sCookieStore = new HashMap<>(); - private static LogInterceptor sLogInterceptor; - private static Interceptor sDebugInterceptor; + private HashMap> mCookieStore = new HashMap<>(); + private LogInterceptor mLogInterceptor; - public static OkHttpClient getOkHttpClient() { - if (sOkHttpClient == null) { + private OkHttpClient mOkHttpClient = null; + + protected HttpClient() { + mLogInterceptor = new LogInterceptor(); + } + + public OkHttpClient getOkHttpClient() { + if(sContext == 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()"); + } + if(mOkHttpClient == null) { try { final X509TrustManager trustManager = new AdvancedX509TrustManager( NetworkUtils.getKnownServersStore(sContext)); final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager); // Automatic cookie handling, NOT PERSISTENT - final CookieJar cookieJar = new CookieJarImpl(sCookieStore); + final CookieJar cookieJar = new CookieJarImpl(mCookieStore); // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok. //.hostnameVerifier(new BrowserCompatHostnameVerifier()); - sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); + mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); } catch (Exception e) { Timber.e(e, "Could not setup SSL system."); } } - return sOkHttpClient; + return mOkHttpClient; } private static SSLContext getSslContext() throws NoSuchAlgorithmException { @@ -109,7 +117,7 @@ public class HttpClient { return sslContext.getSocketFactory(); } - private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, + private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, CookieJar cookieJar) { return new OkHttpClient.Builder() .addNetworkInterceptor(getLogInterceptor()) @@ -125,22 +133,23 @@ public class HttpClient { .build(); } - public static LogInterceptor getLogInterceptor() { - if (sLogInterceptor == null) { - sLogInterceptor = new LogInterceptor(); - } - return sLogInterceptor; - } - public Context getContext() { return sContext; } + public LogInterceptor getLogInterceptor() { + return mLogInterceptor; + } + + public List getCookiesFromUrl(HttpUrl httpUrl) { + return mCookieStore.get(httpUrl.host()); + } + public static void setContext(Context context) { sContext = context; } public void clearCookies() { - sCookieStore.clear(); + mCookieStore.clear(); } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index ee1d2c0e..ec826dd8 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -37,7 +37,7 @@ import java.net.MalformedURLException import java.net.URL import java.util.concurrent.TimeUnit -abstract class HttpBaseMethod constructor(url: URL) { +abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) { var okHttpClient: OkHttpClient var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request @@ -47,7 +47,7 @@ abstract class HttpBaseMethod constructor(url: URL) { var call: Call? = null init { - okHttpClient = HttpClient.getOkHttpClient() + okHttpClient = clientWrapper.okHttpClient request = Request.Builder() .url(httpUrl) .build() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt index 51069bc2..42ddb727 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav +import com.owncloud.android.lib.common.http.HttpClient import java.io.IOException import java.net.URL @@ -31,7 +32,7 @@ import java.net.URL * * @author David González Verdugo */ -class DeleteMethod(url: URL) : HttpMethod(url) { +class DeleteMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) { @Throws(IOException::class) override fun onExecute(): Int { request = request.newBuilder() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt index 65bf40fc..72da9345 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav +import com.owncloud.android.lib.common.http.HttpClient import java.io.IOException import java.net.URL @@ -31,7 +32,7 @@ import java.net.URL * * @author David González Verdugo */ -class GetMethod(url: URL) : HttpMethod(url) { +class GetMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) { @Throws(IOException::class) override fun onExecute(): Int { request = request.newBuilder() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt index da759ada..9a9beb88 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt @@ -23,7 +23,9 @@ */ 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 @@ -33,8 +35,9 @@ import java.net.URL * @author David González Verdugo */ abstract class HttpMethod( + httpClient: HttpClient, url: URL -) : HttpBaseMethod(url) { +) : HttpBaseMethod(httpClient, url) { override lateinit var response: Response diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index ba4f6d4d..ac5d8bcb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -23,6 +23,8 @@ */ 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 @@ -33,9 +35,10 @@ import java.net.URL * @author David González Verdugo */ class PostMethod( + httpClient: HttpClient, url: URL, private val postRequestBody: RequestBody -) : HttpMethod(url) { +) : HttpMethod(httpClient, url) { @Throws(IOException::class) override fun onExecute(): Int { request = request.newBuilder() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index 98be1503..185964ba 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -23,6 +23,8 @@ */ 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 @@ -33,9 +35,10 @@ import java.net.URL * @author David González Verdugo */ class PutMethod( + httpClient: HttpClient, url: URL, private val putRequestBody: RequestBody -) : HttpMethod(url) { +) : HttpMethod(httpClient, url) { @Throws(IOException::class) override fun onExecute(): Int { request = request.newBuilder() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index 2c816d75..9633ac0d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav +import com.owncloud.android.lib.common.http.HttpClient import okhttp3.Response import java.net.URL @@ -33,10 +34,11 @@ import java.net.URL * @author David González Verdugo */ class CopyMethod( + httpClient: HttpClient, val url: URL, private val destinationUrl: String, private val forceOverride: Boolean -) : DavMethod(url) { +) : DavMethod(httpClient, url) { @Throws(Exception::class) public override fun onExecute(): Int { davResource.copy( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index b9bc14cd..732fad8e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -27,6 +27,7 @@ import at.bitfire.dav4jvm.Dav4jvm.log import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.exception.RedirectException +import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import okhttp3.HttpUrl @@ -43,7 +44,7 @@ import java.util.concurrent.TimeUnit * * @author David González Verdugo */ -abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { +abstract class DavMethod protected constructor(httpClient: HttpClient,url: URL) : HttpBaseMethod(httpClient, url) { protected var davResource: DavOCResource override lateinit var response: Response diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt index 03c8aff0..7f1f50aa 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav +import com.owncloud.android.lib.common.http.HttpClient import okhttp3.Response import java.net.URL @@ -32,7 +33,7 @@ import java.net.URL * @author Christian Schabesberger * @author David González Verdugo */ -class MkColMethod(url: URL) : DavMethod(url) { +class MkColMethod(httpClient: HttpClient, url: URL) : DavMethod(httpClient, url) { @Throws(Exception::class) public override fun onExecute(): Int { davResource.mkCol( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt index 4c65cb98..dac3d712 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav +import com.owncloud.android.lib.common.http.HttpClient import okhttp3.Response import java.net.URL @@ -33,10 +34,11 @@ import java.net.URL * @author David González Verdugo */ class MoveMethod( + httpClient: HttpClient, url: URL, private val destinationUrl: String, private val forceOverride: Boolean -) : DavMethod(url) { +) : DavMethod(httpClient, url) { @Throws(Exception::class) public override fun onExecute(): Int { davResource.move( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt index fb0e5a15..e1404614 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -27,6 +27,7 @@ import at.bitfire.dav4jvm.Property import at.bitfire.dav4jvm.Response import at.bitfire.dav4jvm.Response.HrefRelation import at.bitfire.dav4jvm.exception.DavException +import com.owncloud.android.lib.common.http.HttpClient import java.io.IOException import java.net.URL @@ -36,10 +37,11 @@ import java.net.URL * @author David González Verdugo */ class PropfindMethod( + httpClient: HttpClient, url: URL, private val depth: Int, private val propertiesToRequest: Array -) : DavMethod(url) { +) : DavMethod(httpClient, url) { // response val members: MutableList diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index 70dea1de..56ac10f7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -24,6 +24,7 @@ package com.owncloud.android.lib.common.http.methods.webdav import at.bitfire.dav4jvm.exception.HttpException +import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpConstants import okhttp3.RequestBody import java.io.IOException @@ -35,9 +36,10 @@ import java.net.URL * @author David González Verdugo */ class PutMethod( + httpClient: HttpClient, url: URL, private val putRequestBody: RequestBody -) : DavMethod(url) { +) : DavMethod(httpClient, url) { @Throws(IOException::class, HttpException::class) public override fun onExecute(): Int { davResource.put( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index e59388bd..44255c2f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -57,7 +57,7 @@ class CheckPathExistenceRemoteOperation( if (isUserLoggedIn) client.baseFilesWebDavUri.toString() else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath) - val propFindMethod = PropfindMethod(URL(stringUrl), 0, allPropset).apply { + val propFindMethod = PropfindMethod(client, URL(stringUrl), 0, allPropset).apply { setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java index 2f9e5431..c783c999 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java @@ -92,7 +92,8 @@ public class CopyRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; try { CopyMethod copyMethod = - new CopyMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), + new CopyMethod(client, + new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java index 3985ab2c..4761dd71 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java @@ -87,7 +87,8 @@ public class CreateRemoteFolderOperation extends RemoteOperation { RemoteOperationResult result; try { Uri webDavUri = createChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); - final MkColMethod mkcol = new MkColMethod(new URL(webDavUri + WebdavUtils.encodePath(mRemotePath))); + final MkColMethod mkcol = new MkColMethod(client, + new URL(webDavUri + WebdavUtils.encodePath(mRemotePath))); mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS); mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); final int status = client.executeHttpMethod(mkcol); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java index 51ba6b3e..1527ddbb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java @@ -96,7 +96,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; int status; boolean savedFile = false; - mGet = new GetMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); + mGet = new GetMethod(client, new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); Iterator it; FileOutputStream fos = null; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 46181e8f..5b72141e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -100,7 +100,7 @@ public class MoveRemoteFileOperation extends RemoteOperation { // so this uri has to be customizable Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); - final MoveMethod move = new MoveMethod( + final MoveMethod move = new MoveMethod(client, new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 9149e6d3..0049ecc4 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -76,7 +76,8 @@ public class ReadRemoteFileOperation extends RemoteOperation { /// take the duty of check the server for the current state of the file there try { // remote request - propfind = new PropfindMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), + propfind = new PropfindMethod(client, + new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), DEPTH_0, DavUtils.getAllPropset()); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index e10613b3..a4213fe5 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -73,6 +73,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation(ResultCode.INVALID_OVERWRITE); } - final MoveMethod move = new MoveMethod(new URL(client.getUserFilesWebDavUri() + + final MoveMethod move = new MoveMethod(client, + new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mOldRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java index 43181b28..b9e9881b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java @@ -121,7 +121,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); } - mPutMethod = new PutMethod( + mPutMethod = new PutMethod(client, new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody); mPutMethod.setRetryOnConnectionFailure(false); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java index 84396ec8..e30b70e9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java @@ -92,7 +92,8 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation result = new RemoteOperationResult<>(new OperationCancelledException()); break; } else { - mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody); + mPutMethod = new PutMethod(client, + new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody); if (chunkIndex == chunkCount - 1) { // Added a high timeout to the last chunk due to when the last chunk diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 1f043cd1..fc2009bf 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -138,7 +138,6 @@ class CreateRemoteShareOperation( } private fun createFormBody(): FormBody { - val formBodyBuilder = FormBody.Builder() .add(PARAM_PATH, remoteFilePath) .add(PARAM_SHARE_TYPE, shareType.value.toString()) @@ -172,7 +171,7 @@ class CreateRemoteShareOperation( override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val postMethod = PostMethod(URL(requestUri.toString()), createFormBody()).apply { + val postMethod = PostMethod(client, URL(requestUri.toString()), createFormBody()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java new file mode 100644 index 00000000..2746b741 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java @@ -0,0 +1,94 @@ +/* ownCloud Android Library is available under MIT license + * @author David A. Velasco + * @author David González Verdugo + * Copyright (C) 2020 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.resources.shares; + +import android.net.Uri; + +import com.owncloud.android.lib.common.OwnCloudClient; +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.RemoteOperation; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import timber.log.Timber; + +import java.net.URL; + +/** + * Get the data about a Share resource, known its remote ID. + * + * @author David A. Velasco + * @author David González Verdugo + */ + +public class GetRemoteShareOperation extends RemoteOperation { + + private String mRemoteId; + + public GetRemoteShareOperation(String remoteId) { + mRemoteId = remoteId; + } + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; + + try { + Uri requestUri = client.getBaseUri(); + Uri.Builder uriBuilder = requestUri.buildUpon(); + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); + uriBuilder.appendEncodedPath(mRemoteId); + + GetMethod getMethod = new GetMethod(client, new URL(uriBuilder.build().toString())); + getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + + int status = client.executeHttpMethod(getMethod); + + if (isSuccess(status)) { + // Parse xml response and obtain the list of shares + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setOneOrMoreSharesRequired(true); + parser.setOwnCloudVersion(client.getOwnCloudVersion()); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(getMethod.getResponseBodyAsString()); + + } else { + result = new RemoteOperationResult<>(getMethod); + } + + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Timber.e(e, "Exception while getting remote shares"); + } + return result; + } + + private boolean isSuccess(int status) { + return (status == HttpConstants.HTTP_OK); + } +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt index 10154820..5df5b09f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt @@ -131,7 +131,7 @@ class GetRemoteShareesOperation override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val getMethod = GetMethod(URL(requestUri.toString())) + val getMethod = GetMethod(client, URL(requestUri.toString())) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) return try { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index adb4374e..ab290d22 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -119,7 +119,7 @@ class GetRemoteSharesForFileOperation( override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val getMethod = GetMethod(URL(requestUri.toString())).apply { + val getMethod = GetMethod(client, URL(requestUri.toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index 2ce20a2c..e2dbfc16 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -104,9 +104,10 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri(client.baseUri) - val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply { + val deleteMethod = DeleteMethod(client, URL(requestUri.toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index 73c331ad..aa4b1c8e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -200,7 +200,7 @@ class UpdateRemoteShareOperation val formBodyBuilder = createFormBodyBuilder() - val putMethod = PutMethod(URL(requestUri.toString()), formBodyBuilder.build()).apply { + val putMethod = PutMethod(client, URL(requestUri.toString()), formBodyBuilder.build()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt index 8591a68a..987c07ed 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt @@ -60,7 +60,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation() { appendEncodedPath(OCS_ROUTE) // avoid starting "/" in this method appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) } - val getMethod = GetMethod(URL(uriBuilder.build().toString())).apply { + val getMethod = GetMethod(client, URL(uriBuilder.build().toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } val status = client.executeHttpMethod(getMethod) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt index 12be4dab..3be9c82b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt @@ -53,6 +53,7 @@ class GetRemoteStatusOperation : RemoteOperation() { client.baseUri = Uri.parse(newBaseUrl) } + private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult { val baseUrl = client.baseUri.toString() return try { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt index 911b2b80..836bc13d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.resources.status.services + import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.RemoteServerInfo diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt index a9809feb..732b7238 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt @@ -26,6 +26,7 @@ package com.owncloud.android.lib.resources.status.services.implementation + import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt index f7ffdaf3..a1c84724 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -52,7 +52,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + File.separator + avatarDimension Timber.d("avatar URI: %s", endPoint) - val getMethod = GetMethod(URL(endPoint)) + val getMethod = GetMethod(client, URL(endPoint)) val status = client.executeHttpMethod(getMethod) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt index d1f2a43c..b7e86652 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt @@ -51,7 +51,7 @@ class GetRemoteUserInfoOperation : RemoteOperation() { var result: RemoteOperationResult //Get the user try { - val getMethod = GetMethod(URL(client.baseUri.toString() + OCS_ROUTE)) + val getMethod = GetMethod(client, URL(client.baseUri.toString() + OCS_ROUTE)) val status = client.executeHttpMethod(getMethod) val response = getMethod.getResponseBodyAsString() ?: "" if (status == HttpConstants.HTTP_OK) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index 2607bd92..77858191 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -50,6 +50,7 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult = try { val propfindMethod = PropfindMethod( + client, URL(client.userFilesWebDavUri.toString()), DavConstants.DEPTH_0, DavUtils.quotaPropSet diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java index 5da47632..b761c066 100644 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -38,8 +38,13 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +<<<<<<< HEAD import com.owncloud.android.lib.common.ConnectionValidator; import com.owncloud.android.lib.common.OwnCloudClient; +======= +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientFactory; +>>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; @@ -51,6 +56,10 @@ import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.RemoteFile; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; +<<<<<<< HEAD +======= +import info.hannes.timber.DebugTree; +>>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import timber.log.Timber; import java.io.File; @@ -75,17 +84,26 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, super.onCreate(savedInstanceState); setContentView(R.layout.main); +<<<<<<< HEAD Timber.plant(); +======= + Timber.plant(new DebugTree()); +>>>>>>> 5e555278 (get okhttp singleton removal changes to compile) mHandler = new Handler(); final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); SingleSessionManager.setUserAgent(getUserAgent()); +<<<<<<< HEAD SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false)); mClient = new OwnCloudClient(serverUri, SingleSessionManager.getConnectionValidator(), true, SingleSessionManager.getDefaultSingleton()); +======= + mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true); + +>>>>>>> 5e555278 (get okhttp singleton removal changes to compile) mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username), From 79e4287223f4f50cf8b1f587cf57f1b24fd70875 Mon Sep 17 00:00:00 2001 From: Schabi Date: Mon, 18 Jan 2021 11:05:55 +0100 Subject: [PATCH 2/9] clean up http client --- .../android/lib/common/http/HttpClient.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 1e0e760f..3bf4ef2e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -74,35 +74,41 @@ public class HttpClient { try { final X509TrustManager trustManager = new AdvancedX509TrustManager( NetworkUtils.getKnownServersStore(sContext)); - final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager); + + + final SSLContext sslContext = buildSSLContext(); + sslContext.init(null, new TrustManager[]{trustManager}, null); + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + // Automatic cookie handling, NOT PERSISTENT final CookieJar cookieJar = new CookieJarImpl(mCookieStore); - - // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok. - //.hostnameVerifier(new BrowserCompatHostnameVerifier()); mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); + } catch(NoSuchAlgorithmException nsae){ + Timber.e(nsae, "Could not setup SSL system."); + throw new RuntimeException("Could not setup okHttp client.", nsae); } catch (Exception e) { - Timber.e(e, "Could not setup SSL system."); + Timber.e(e, "Could not setup okHttp client."); + throw new RuntimeException("Could not setup okHttp client.", e); } } return mOkHttpClient; } - private static SSLContext getSslContext() throws NoSuchAlgorithmException { + private SSLContext buildSSLContext() throws NoSuchAlgorithmException { try { - return SSLContext.getInstance(TlsVersion.TLS_1_3.javaName()); + return SSLContext.getInstance("TLSv1.3"); } catch (NoSuchAlgorithmException tlsv13Exception) { try { Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); - return SSLContext.getInstance(TlsVersion.TLS_1_2.javaName()); + return SSLContext.getInstance("TLSv1.2"); } catch (NoSuchAlgorithmException tlsv12Exception) { try { Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); - return SSLContext.getInstance(TlsVersion.TLS_1_1.javaName()); + return SSLContext.getInstance("TLSv1.1"); } catch (NoSuchAlgorithmException tlsv11Exception) { Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); - return SSLContext.getInstance(TlsVersion.TLS_1_0.javaName()); + return SSLContext.getInstance("TLSv1"); // should be available in any device; see reference of supported protocols in // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html } @@ -110,13 +116,6 @@ public class HttpClient { } } - private static SSLSocketFactory getNewSslSocketFactory(X509TrustManager trustManager) - throws NoSuchAlgorithmException, KeyManagementException { - final SSLContext sslContext = getSslContext(); - sslContext.init(null, new TrustManager[]{trustManager}, null); - return sslContext.getSocketFactory(); - } - private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, CookieJar cookieJar) { return new OkHttpClient.Builder() From 344c1e11361e5710a4b242db5501d33f8e124d68 Mon Sep 17 00:00:00 2001 From: Schabi Date: Tue, 23 Feb 2021 12:35:22 +0100 Subject: [PATCH 3/9] apply required fixes --- .gitignore | 1 + .../android/lib/common/ConnectionValidator.kt | 4 +- .../android/lib/common/OwnCloudClient.java | 6 ++- .../lib/common/OwnCloudClientFactory.java | 10 +++-- .../lib/common/SingleSessionManager.java | 13 ++++--- .../android/lib/common/http/HttpClient.java | 37 ++++++++----------- .../http/methods/nonwebdav/HttpMethod.kt | 1 - .../http/methods/nonwebdav/PostMethod.kt | 1 - .../http/methods/nonwebdav/PutMethod.kt | 1 - .../common/http/methods/webdav/DavMethod.kt | 2 +- .../UploadFileFromContentUriOperation.kt | 2 +- .../oauth/GetOIDCDiscoveryRemoteOperation.kt | 2 +- .../oauth/RegisterClientRemoteOperation.kt | 1 + .../oauth/TokenRequestRemoteOperation.kt | 2 +- .../lib/resources/status/StatusRequester.kt | 10 ++++- .../lib/sampleclient/MainActivity.java | 4 ++ 16 files changed, 55 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 172bedd2..858115b9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .idea/* !.idea/codeStyles/ +sample_client/.idea/* # files for the dex VM *.dex diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt index d5f2aabd..1bc325c5 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt @@ -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 { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 09b255e2..fd1fad9a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -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"); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java index 24380047..b8148c53 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -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; } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java index 51d3a431..7e40c16f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java @@ -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 diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 3bf4ef2e..afe4735a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -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> 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 } @@ -117,7 +116,7 @@ public class HttpClient { } private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, - CookieJar cookieJar) { + CookieJar cookieJar) { return new OkHttpClient.Builder() .addNetworkInterceptor(getLogInterceptor()) .addNetworkInterceptor(DebugInterceptorFactory.INSTANCE.getInterceptor()) @@ -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(); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt index 9a9beb88..e42f650b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt @@ -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 diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index ac5d8bcb..b4357926 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -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 diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index 185964ba..6ea784df 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -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 diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index 732fad8e..e975f530 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -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 diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt index 9a1e1b19..c0e4c108 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt @@ -49,7 +49,7 @@ class UploadFileFromContentUriOperation( ) : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult { - 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) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt index d2fae59e..623c13df 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt @@ -51,7 +51,7 @@ class GetOIDCDiscoveryRemoteOperation : RemoteOperation() 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) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt index 5d6482da..3cf6ce2a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt @@ -48,6 +48,7 @@ class RegisterClientRemoteOperation( val requestBody = clientRegistrationParams.toRequestBody() val postMethod = PostMethod( + httpClient = client, url = URL(clientRegistrationParams.registrationEndpoint), postRequestBody = requestBody ) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt index 67253c4c..b74b9adb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt @@ -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) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt index 55381f90..66517813 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt @@ -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()) } diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java index b761c066..03ae360c 100644 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -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( From fc4ae27bbebe4600a514f4e09821bc7678938012 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 18 Aug 2021 16:27:51 +0200 Subject: [PATCH 4/9] remove redundant intercept of httpclient with httpmethod --- .../android/lib/common/OwnCloudClient.java | 4 +- .../lib/common/http/methods/HttpBaseMethod.kt | 61 ++++++------ .../http/methods/nonwebdav/DeleteMethod.kt | 8 +- .../http/methods/nonwebdav/GetMethod.kt | 8 +- .../http/methods/nonwebdav/HttpMethod.kt | 7 +- .../http/methods/nonwebdav/PostMethod.kt | 9 +- .../http/methods/nonwebdav/PutMethod.kt | 9 +- .../common/http/methods/webdav/CopyMethod.kt | 7 +- .../common/http/methods/webdav/DavMethod.kt | 97 +++---------------- .../common/http/methods/webdav/MkColMethod.kt | 6 +- .../common/http/methods/webdav/MoveMethod.kt | 7 +- .../http/methods/webdav/PropfindMethod.kt | 7 +- .../common/http/methods/webdav/PutMethod.kt | 7 +- .../CheckPathExistenceRemoteOperation.kt | 6 +- .../files/CopyRemoteFileOperation.java | 2 +- .../files/CreateRemoteFolderOperation.java | 2 +- .../files/DownloadRemoteFileOperation.java | 2 +- .../files/MoveRemoteFileOperation.java | 2 +- .../files/ReadRemoteFileOperation.java | 2 +- .../files/ReadRemoteFolderOperation.java | 1 - .../files/RemoveRemoteFileOperation.java | 1 - .../files/RenameRemoteFileOperation.java | 2 +- .../UploadFileFromContentUriOperation.kt | 4 +- .../files/UploadRemoteFileOperation.java | 2 +- .../ChunkedUploadRemoteFileOperation.java | 2 +- .../oauth/GetOIDCDiscoveryRemoteOperation.kt | 2 +- .../oauth/RegisterClientRemoteOperation.kt | 1 - .../oauth/TokenRequestRemoteOperation.kt | 2 +- .../shares/CreateRemoteShareOperation.kt | 2 +- .../shares/GetRemoteShareOperation.java | 94 ------------------ .../shares/GetRemoteShareesOperation.kt | 2 +- .../shares/GetRemoteSharesForFileOperation.kt | 2 +- .../shares/RemoveRemoteShareOperation.kt | 2 +- .../shares/UpdateRemoteShareOperation.kt | 4 +- .../status/GetRemoteCapabilitiesOperation.kt | 2 +- .../lib/resources/status/StatusRequester.kt | 2 +- .../users/GetRemoteUserAvatarOperation.kt | 2 +- .../users/GetRemoteUserInfoOperation.kt | 2 +- .../users/GetRemoteUserQuotaOperation.kt | 1 - 39 files changed, 109 insertions(+), 276 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index fd1fad9a..2aefd06b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -135,10 +135,10 @@ public class OwnCloudClient extends HttpClient { method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); } - status = method.execute(); + status = method.execute(this); if (shouldConnectionValidatorBeCalled(method, status)) { - retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success + retry = mConnectionValidator.validate(this, mSingleSessionManager, getContext()); // retry on success fail on no success } else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) { retry = true; method.setFollowRedirects(true); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index ec826dd8..29f9ea9a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -37,25 +37,40 @@ import java.net.MalformedURLException import java.net.URL import java.util.concurrent.TimeUnit -abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) { - var okHttpClient: OkHttpClient +abstract class HttpBaseMethod constructor(url: URL) { var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request private var _followPermanentRedirects = false abstract var response: Response - var call: Call? = null + var followRedirects: Boolean? = true + var retryOnConnectionFailure: Boolean? = false + var connectionTimeoutVal: Long? = null + var connectionTimeoutUnit: TimeUnit? = null + var readTimeoutVal: Long? = null + var readTimeoutUnit: TimeUnit? = null + init { - okHttpClient = clientWrapper.okHttpClient request = Request.Builder() .url(httpUrl) .build() } @Throws(Exception::class) - open fun execute(): Int { - return onExecute() + open fun execute(httpClient: HttpClient): Int { + val okHttpClient = httpClient.okHttpClient.newBuilder().apply { + retryOnConnectionFailure?.let { retryOnConnectionFailure(it) } + followRedirects?.let { followRedirects(it) } + readTimeoutUnit?.let { unit -> + readTimeoutVal?.let { readTimeout(it, unit) } + } + connectionTimeoutUnit?.let { unit -> + connectionTimeoutVal?.let { connectTimeout(it, unit) } + } + }.build() + + return onExecute(okHttpClient) } open fun setUrl(url: HttpUrl) { @@ -137,42 +152,26 @@ abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) { // Setter ////////////////////////////// // Connection parameters + /* open fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { - okHttpClient = okHttpClient.newBuilder() - .retryOnConnectionFailure(retryOnConnectionFailure) - .build() + retryOnConnectionFailureVal = true } + */ + open fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { - okHttpClient = okHttpClient.newBuilder() - .readTimeout(readTimeout, timeUnit) - .build() + readTimeoutVal = readTimeout + readTimeoutUnit = timeUnit } open fun setConnectionTimeout( connectionTimeout: Long, timeUnit: TimeUnit ) { - okHttpClient = okHttpClient.newBuilder() - .readTimeout(connectionTimeout, timeUnit) - .build() + connectionTimeoutVal = connectionTimeout + connectionTimeoutUnit = timeUnit } - open fun setFollowRedirects(followRedirects: Boolean) { - okHttpClient = okHttpClient.newBuilder() - .followRedirects(followRedirects) - .build() - } - - open fun getFollowRedirects() = okHttpClient.followRedirects - - open fun setFollowPermanentRedirects(followRedirects: Boolean) { - _followPermanentRedirects = followRedirects - } - - open fun getFollowPermanentRedirects() = _followPermanentRedirects - - /************ *** Call *** ************/ @@ -187,5 +186,5 @@ abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) { // For override ////////////////////////////// @Throws(Exception::class) - protected abstract fun onExecute(): Int + protected abstract fun onExecute(okHttpClient: OkHttpClient): Int } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt index 42ddb727..8ae30904 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav -import com.owncloud.android.lib.common.http.HttpClient +import okhttp3.OkHttpClient import java.io.IOException import java.net.URL @@ -32,12 +32,12 @@ import java.net.URL * * @author David González Verdugo */ -class DeleteMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) { +class DeleteMethod(url: URL) : HttpMethod(url) { @Throws(IOException::class) - override fun onExecute(): Int { + override fun onExecute(okHttpClient: OkHttpClient): Int { request = request.newBuilder() .delete() .build() - return super.onExecute() + return super.onExecute(okHttpClient) } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt index 72da9345..d24c3c88 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav -import com.owncloud.android.lib.common.http.HttpClient +import okhttp3.OkHttpClient import java.io.IOException import java.net.URL @@ -32,12 +32,12 @@ import java.net.URL * * @author David González Verdugo */ -class GetMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) { +class GetMethod(url: URL) : HttpMethod(url) { @Throws(IOException::class) - override fun onExecute(): Int { + override fun onExecute(okHttpClient: OkHttpClient): Int { request = request.newBuilder() .get() .build() - return super.onExecute() + return super.onExecute(okHttpClient) } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt index e42f650b..c91f4228 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt @@ -23,8 +23,8 @@ */ 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 @@ -34,13 +34,12 @@ import java.net.URL * @author David González Verdugo */ abstract class HttpMethod( - httpClient: HttpClient, url: URL -) : HttpBaseMethod(httpClient, url) { +) : HttpBaseMethod(url) { override lateinit var response: Response - public override fun onExecute(): Int { + public override fun onExecute(okHttpClient: OkHttpClient): Int { call = okHttpClient.newCall(request) call?.let { response = it.execute() } return super.statusCode diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index b4357926..5b51c32f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -23,7 +23,7 @@ */ 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 @@ -34,15 +34,14 @@ import java.net.URL * @author David González Verdugo */ class PostMethod( - httpClient: HttpClient, url: URL, private val postRequestBody: RequestBody -) : HttpMethod(httpClient, url) { +) : HttpMethod(url) { @Throws(IOException::class) - override fun onExecute(): Int { + override fun onExecute(okHttpClient: OkHttpClient): Int { request = request.newBuilder() .post(postRequestBody) .build() - return super.onExecute() + return super.onExecute(okHttpClient) } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index 6ea784df..91723b69 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -23,7 +23,7 @@ */ 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 @@ -34,15 +34,14 @@ import java.net.URL * @author David González Verdugo */ class PutMethod( - httpClient: HttpClient, url: URL, private val putRequestBody: RequestBody -) : HttpMethod(httpClient, url) { +) : HttpMethod(url) { @Throws(IOException::class) - override fun onExecute(): Int { + override fun onExecute(okHttpClient: OkHttpClient): Int { request = request.newBuilder() .put(putRequestBody) .build() - return super.onExecute() + return super.onExecute(okHttpClient) } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index 9633ac0d..d10718e0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import com.owncloud.android.lib.common.http.HttpClient +import at.bitfire.dav4jvm.DavOCResource import okhttp3.Response import java.net.URL @@ -34,13 +34,12 @@ import java.net.URL * @author David González Verdugo */ class CopyMethod( - httpClient: HttpClient, val url: URL, private val destinationUrl: String, private val forceOverride: Boolean -) : DavMethod(httpClient, url) { +) : DavMethod(url) { @Throws(Exception::class) - public override fun onExecute(): Int { + public override fun onDavExecute(davResource: DavOCResource): Int { davResource.copy( destinationUrl, forceOverride, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index e975f530..100177ec 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -27,45 +27,39 @@ import at.bitfire.dav4jvm.Dav4jvm.log import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.exception.RedirectException -import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.HttpBaseMethod -import okhttp3.HttpUrl -import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import okhttp3.OkHttpClient import okhttp3.Protocol import okhttp3.Response import okhttp3.ResponseBody.Companion.toResponseBody -import java.net.MalformedURLException import java.net.URL -import java.util.concurrent.TimeUnit /** * Wrapper to perform WebDAV (dav4android) calls * * @author David González Verdugo */ -abstract class DavMethod protected constructor(httpClient: HttpClient, url: URL) : HttpBaseMethod(httpClient, url) { - protected var davResource: DavOCResource - +abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override lateinit var response: Response - - init { - val httpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() - davResource = DavOCResource( - okHttpClient, - httpUrl, - log - ) - } + private var davResource: DavOCResource? = null override fun abort() { - davResource.cancelCall() + davResource?.cancelCall() } + protected abstract fun onDavExecute(davResource: DavOCResource): Int + @Throws(Exception::class) - override fun execute(): Int { + override fun onExecute(okHttpClient: OkHttpClient): Int { return try { - onExecute() + davResource = DavOCResource( + okHttpClient.newBuilder().followRedirects(false).build(), + httpUrl, + log + ) + + onDavExecute(davResource!!) } catch (httpException: HttpException) { // Modify responses with information gathered from exceptions if (httpException is RedirectException) { @@ -92,71 +86,12 @@ abstract class DavMethod protected constructor(httpClient: HttpClient, url: URL) } } - ////////////////////////////// - // Setter - ////////////////////////////// - // Connection parameters - override fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { - super.setReadTimeout(readTimeout, timeUnit) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } - - override fun setConnectionTimeout( - connectionTimeout: Long, - timeUnit: TimeUnit - ) { - super.setConnectionTimeout(connectionTimeout, timeUnit) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } - - override fun setFollowRedirects(followRedirects: Boolean) { - super.setFollowRedirects(followRedirects) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } - - override fun setUrl(url: HttpUrl) { - super.setUrl(url) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } - - override fun setRequestHeader(name: String, value: String) { - super.setRequestHeader(name, value) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } - ////////////////////////////// // Getter ////////////////////////////// - override fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { - super.setRetryOnConnectionFailure(retryOnConnectionFailure) - davResource = DavOCResource( - okHttpClient, - request.url, - log - ) - } + override val isAborted: Boolean - get() = davResource.isCallAborted() + get() = davResource?.isCallAborted() ?: false } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt index 7f1f50aa..8d77cd3a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import com.owncloud.android.lib.common.http.HttpClient +import at.bitfire.dav4jvm.DavOCResource import okhttp3.Response import java.net.URL @@ -33,9 +33,9 @@ import java.net.URL * @author Christian Schabesberger * @author David González Verdugo */ -class MkColMethod(httpClient: HttpClient, url: URL) : DavMethod(httpClient, url) { +class MkColMethod(url: URL) : DavMethod(url) { @Throws(Exception::class) - public override fun onExecute(): Int { + public override fun onDavExecute(davResource: DavOCResource): Int { davResource.mkCol( xmlBody = null, listOfHeaders = super.getRequestHeadersAsHashMap() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt index dac3d712..bc6d9d3e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import com.owncloud.android.lib.common.http.HttpClient +import at.bitfire.dav4jvm.DavOCResource import okhttp3.Response import java.net.URL @@ -34,13 +34,12 @@ import java.net.URL * @author David González Verdugo */ class MoveMethod( - httpClient: HttpClient, url: URL, private val destinationUrl: String, private val forceOverride: Boolean -) : DavMethod(httpClient, url) { +) : DavMethod(url) { @Throws(Exception::class) - public override fun onExecute(): Int { + override fun onDavExecute(davResource: DavOCResource): Int { davResource.move( destinationUrl, forceOverride, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt index e1404614..6bce3472 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -23,11 +23,11 @@ */ package com.owncloud.android.lib.common.http.methods.webdav +import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.Property import at.bitfire.dav4jvm.Response import at.bitfire.dav4jvm.Response.HrefRelation import at.bitfire.dav4jvm.exception.DavException -import com.owncloud.android.lib.common.http.HttpClient import java.io.IOException import java.net.URL @@ -37,11 +37,10 @@ import java.net.URL * @author David González Verdugo */ class PropfindMethod( - httpClient: HttpClient, url: URL, private val depth: Int, private val propertiesToRequest: Array -) : DavMethod(httpClient, url) { +) : DavMethod(url) { // response val members: MutableList @@ -49,7 +48,7 @@ class PropfindMethod( private set @Throws(IOException::class, DavException::class) - public override fun onExecute(): Int { + public override fun onDavExecute(davResource: DavOCResource): Int { davResource.propfind( depth = depth, reqProp = propertiesToRequest, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index 56ac10f7..60330a3a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -23,8 +23,8 @@ */ package com.owncloud.android.lib.common.http.methods.webdav +import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.exception.HttpException -import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpConstants import okhttp3.RequestBody import java.io.IOException @@ -36,12 +36,11 @@ import java.net.URL * @author David González Verdugo */ class PutMethod( - httpClient: HttpClient, url: URL, private val putRequestBody: RequestBody -) : DavMethod(httpClient, url) { +) : DavMethod(url) { @Throws(IOException::class, HttpException::class) - public override fun onExecute(): Int { + public override fun onDavExecute(davResource: DavOCResource): Int { davResource.put( putRequestBody, super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index 44255c2f..afc88306 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -57,11 +57,15 @@ class CheckPathExistenceRemoteOperation( if (isUserLoggedIn) client.baseFilesWebDavUri.toString() else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath) - val propFindMethod = PropfindMethod(client, URL(stringUrl), 0, allPropset).apply { + val propFindMethod = PropfindMethod(URL(stringUrl), 0, allPropset).apply { setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } +<<<<<<< HEAD +======= + propFindMethod.followRedirects = false +>>>>>>> 27ecf79c (remove redundant intercept of httpclient with httpmethod) var status = client.executeHttpMethod(propFindMethod) /* PROPFIND method * 404 NOT FOUND: path doesn't exist, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java index c783c999..9c78425a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CopyRemoteFileOperation.java @@ -92,7 +92,7 @@ public class CopyRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; try { CopyMethod copyMethod = - new CopyMethod(client, + new CopyMethod( new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java index 4761dd71..302df20d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java @@ -87,7 +87,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { RemoteOperationResult result; try { Uri webDavUri = createChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); - final MkColMethod mkcol = new MkColMethod(client, + final MkColMethod mkcol = new MkColMethod( new URL(webDavUri + WebdavUtils.encodePath(mRemotePath))); mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS); mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java index 1527ddbb..51ba6b3e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/DownloadRemoteFileOperation.java @@ -96,7 +96,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; int status; boolean savedFile = false; - mGet = new GetMethod(client, new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); + mGet = new GetMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); Iterator it; FileOutputStream fos = null; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 5b72141e..46181e8f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -100,7 +100,7 @@ public class MoveRemoteFileOperation extends RemoteOperation { // so this uri has to be customizable Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); - final MoveMethod move = new MoveMethod(client, + final MoveMethod move = new MoveMethod( new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 0049ecc4..24496ebe 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -76,7 +76,7 @@ public class ReadRemoteFileOperation extends RemoteOperation { /// take the duty of check the server for the current state of the file there try { // remote request - propfind = new PropfindMethod(client, + propfind = new PropfindMethod( new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), DEPTH_0, DavUtils.getAllPropset()); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index a4213fe5..e10613b3 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -73,7 +73,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation(ResultCode.INVALID_OVERWRITE); } - final MoveMethod move = new MoveMethod(client, + final MoveMethod move = new MoveMethod( new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mOldRemotePath)), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt index c0e4c108..dfcef34d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadFileFromContentUriOperation.kt @@ -49,8 +49,8 @@ class UploadFileFromContentUriOperation( ) : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult { - val putMethod = PutMethod(client, URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(uploadPath)), requestBody).apply { - setRetryOnConnectionFailure(false) + val putMethod = PutMethod(URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(uploadPath)), requestBody).apply { + retryOnConnectionFailure = false addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, requestBody.contentLength().toString()) addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModified) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java index b9e9881b..43181b28 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java @@ -121,7 +121,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); } - mPutMethod = new PutMethod(client, + mPutMethod = new PutMethod( new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody); mPutMethod.setRetryOnConnectionFailure(false); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java index e30b70e9..10b8ea36 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java @@ -92,7 +92,7 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation result = new RemoteOperationResult<>(new OperationCancelledException()); break; } else { - mPutMethod = new PutMethod(client, + mPutMethod = new PutMethod( new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody); if (chunkIndex == chunkCount - 1) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt index 623c13df..d2fae59e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt @@ -51,7 +51,7 @@ class GetOIDCDiscoveryRemoteOperation : RemoteOperation() appendPath(OPENID_CONFIGURATION_RESOURCE) }.build() - val getMethod = GetMethod(client, URL(uriBuilder.toString())).apply { + val getMethod = GetMethod(URL(uriBuilder.toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt index 3cf6ce2a..5d6482da 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/RegisterClientRemoteOperation.kt @@ -48,7 +48,6 @@ class RegisterClientRemoteOperation( val requestBody = clientRegistrationParams.toRequestBody() val postMethod = PostMethod( - httpClient = client, url = URL(clientRegistrationParams.registrationEndpoint), postRequestBody = requestBody ) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt index b74b9adb..67253c4c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/TokenRequestRemoteOperation.kt @@ -52,7 +52,7 @@ class TokenRequestRemoteOperation( try { val requestBody = tokenRequestParams.toRequestBody() - val postMethod = PostMethod(client, URL(tokenRequestParams.tokenEndpoint), requestBody) + val postMethod = PostMethod(URL(tokenRequestParams.tokenEndpoint), requestBody) postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index fc2009bf..ed2b7417 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -171,7 +171,7 @@ class CreateRemoteShareOperation( override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val postMethod = PostMethod(client, URL(requestUri.toString()), createFormBody()).apply { + val postMethod = PostMethod(URL(requestUri.toString()), createFormBody()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java deleted file mode 100644 index 2746b741..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java +++ /dev/null @@ -1,94 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package com.owncloud.android.lib.resources.shares; - -import android.net.Uri; - -import com.owncloud.android.lib.common.OwnCloudClient; -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.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import timber.log.Timber; - -import java.net.URL; - -/** - * Get the data about a Share resource, known its remote ID. - * - * @author David A. Velasco - * @author David González Verdugo - */ - -public class GetRemoteShareOperation extends RemoteOperation { - - private String mRemoteId; - - public GetRemoteShareOperation(String remoteId) { - mRemoteId = remoteId; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; - - try { - Uri requestUri = client.getBaseUri(); - Uri.Builder uriBuilder = requestUri.buildUpon(); - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); - uriBuilder.appendEncodedPath(mRemoteId); - - GetMethod getMethod = new GetMethod(client, new URL(uriBuilder.build().toString())); - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - - int status = client.executeHttpMethod(getMethod); - - if (isSuccess(status)) { - // Parse xml response and obtain the list of shares - ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() - ); - parser.setOneOrMoreSharesRequired(true); - parser.setOwnCloudVersion(client.getOwnCloudVersion()); - parser.setServerBaseUri(client.getBaseUri()); - result = parser.parse(getMethod.getResponseBodyAsString()); - - } else { - result = new RemoteOperationResult<>(getMethod); - } - - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Timber.e(e, "Exception while getting remote shares"); - } - return result; - } - - private boolean isSuccess(int status) { - return (status == HttpConstants.HTTP_OK); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt index 5df5b09f..10154820 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt @@ -131,7 +131,7 @@ class GetRemoteShareesOperation override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val getMethod = GetMethod(client, URL(requestUri.toString())) + val getMethod = GetMethod(URL(requestUri.toString())) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) return try { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index ab290d22..adb4374e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -119,7 +119,7 @@ class GetRemoteSharesForFileOperation( override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val getMethod = GetMethod(client, URL(requestUri.toString())).apply { + val getMethod = GetMethod(URL(requestUri.toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index e2dbfc16..304ead67 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -107,7 +107,7 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper val requestUri = buildRequestUri(client.baseUri) - val deleteMethod = DeleteMethod(client, URL(requestUri.toString())).apply { + val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index aa4b1c8e..a6cc71e3 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -195,17 +195,17 @@ class UpdateRemoteShareOperation return formBodyBuilder } + override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) val formBodyBuilder = createFormBodyBuilder() - val putMethod = PutMethod(client, URL(requestUri.toString()), formBodyBuilder.build()).apply { + val putMethod = PutMethod(URL(requestUri.toString()), formBodyBuilder.build()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } - return try { val status = client.executeHttpMethod(putMethod) val response = putMethod.getResponseBodyAsString() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt index 987c07ed..8591a68a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt @@ -60,7 +60,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation() { appendEncodedPath(OCS_ROUTE) // avoid starting "/" in this method appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) } - val getMethod = GetMethod(client, URL(uriBuilder.build().toString())).apply { + val getMethod = GetMethod(URL(uriBuilder.build().toString())).apply { addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } val status = client.executeHttpMethod(getMethod) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt index 66517813..a63aec5a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt @@ -65,7 +65,7 @@ internal class StatusRequester { } private fun getGetMethod(client: HttpClient, url: String): GetMethod { - return GetMethod(client, URL(url)).apply { + return GetMethod(URL(url)).apply { setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt index a1c84724..f7ffdaf3 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -52,7 +52,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + File.separator + avatarDimension Timber.d("avatar URI: %s", endPoint) - val getMethod = GetMethod(client, URL(endPoint)) + val getMethod = GetMethod(URL(endPoint)) val status = client.executeHttpMethod(getMethod) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt index b7e86652..d1f2a43c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt @@ -51,7 +51,7 @@ class GetRemoteUserInfoOperation : RemoteOperation() { var result: RemoteOperationResult //Get the user try { - val getMethod = GetMethod(client, URL(client.baseUri.toString() + OCS_ROUTE)) + val getMethod = GetMethod(URL(client.baseUri.toString() + OCS_ROUTE)) val status = client.executeHttpMethod(getMethod) val response = getMethod.getResponseBodyAsString() ?: "" if (status == HttpConstants.HTTP_OK) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index 77858191..2607bd92 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -50,7 +50,6 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { override fun run(client: OwnCloudClient): RemoteOperationResult = try { val propfindMethod = PropfindMethod( - client, URL(client.userFilesWebDavUri.toString()), DavConstants.DEPTH_0, DavUtils.quotaPropSet From 09375ce0539cef28cfda8c779fcdd7baba471893 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 24 Mar 2022 17:45:54 +0100 Subject: [PATCH 5/9] rebase okhttp_signleton fix on top of connection validator fix token oauth token refresh error pleasure the linter --- .../android/lib/common/ConnectionValidator.kt | 29 +++++++++++++++++++ .../android/lib/common/OwnCloudClient.java | 15 ++++++---- .../lib/common/http/methods/HttpBaseMethod.kt | 6 ++-- .../CheckPathExistenceRemoteOperation.kt | 4 +-- .../oauth/GetOIDCDiscoveryRemoteOperation.kt | 2 +- .../lib/resources/status/StatusRequester.kt | 12 ++------ .../status/services/ServerInfoService.kt | 2 +- .../implementation/OCServerInfoService.kt | 2 +- .../lib/sampleclient/MainActivity.java | 24 ++------------- 9 files changed, 50 insertions(+), 46 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt index 1bc325c5..23a5a92f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt @@ -1,3 +1,27 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2016 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + package com.owncloud.android.lib.common import android.accounts.AccountManager @@ -15,6 +39,11 @@ import timber.log.Timber import java.io.IOException import java.lang.Exception +/** + * ConnectionValidator + * + * @author Christian Schabesberger + */ class ConnectionValidator( val context: Context, val clearCookiesOnValidation: Boolean diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 2aefd06b..7d1a4409 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -150,12 +150,15 @@ public class OwnCloudClient extends HttpClient { } private boolean shouldConnectionValidatorBeCalled(HttpBaseMethod method, int status) { - return !mFollowRedirects && - !method.getFollowRedirects() && - mConnectionValidator != null && - (status == HttpConstants.HTTP_MOVED_TEMPORARILY || - (!(mCredentials instanceof OwnCloudAnonymousCredentials) && - status == HttpConstants.HTTP_UNAUTHORIZED)); + + return mConnectionValidator != null && ( + (!(mCredentials instanceof OwnCloudAnonymousCredentials) && + status == HttpConstants.HTTP_UNAUTHORIZED + ) || (!mFollowRedirects && + !method.getFollowRedirects() && + status == HttpConstants.HTTP_MOVED_TEMPORARILY + ) + ); } /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index 29f9ea9a..bf58b0fd 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -40,12 +40,12 @@ import java.util.concurrent.TimeUnit abstract class HttpBaseMethod constructor(url: URL) { var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request - private var _followPermanentRedirects = false + var followPermanentRedirects = false abstract var response: Response var call: Call? = null - var followRedirects: Boolean? = true - var retryOnConnectionFailure: Boolean? = false + var followRedirects: Boolean = true + var retryOnConnectionFailure: Boolean = false var connectionTimeoutVal: Long? = null var connectionTimeoutUnit: TimeUnit? = null var readTimeoutVal: Long? = null diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index afc88306..654f7143 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -62,10 +62,8 @@ class CheckPathExistenceRemoteOperation( setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } -<<<<<<< HEAD -======= propFindMethod.followRedirects = false ->>>>>>> 27ecf79c (remove redundant intercept of httpclient with httpmethod) + var status = client.executeHttpMethod(propFindMethod) /* PROPFIND method * 404 NOT FOUND: path doesn't exist, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt index d2fae59e..c3f55dc9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt @@ -55,7 +55,7 @@ class GetOIDCDiscoveryRemoteOperation : RemoteOperation() addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } - getMethod.setFollowRedirects(true) + getMethod.followRedirects = true val status = client.executeHttpMethod(getMethod) val responseBody = getMethod.getResponseBodyAsString() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt index a63aec5a..9c246e9a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt @@ -25,7 +25,6 @@ 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 @@ -64,7 +63,7 @@ internal class StatusRequester { return URL(oldLocationURL.protocol, oldLocationURL.host, oldLocationURL.port, redirectedLocation).toString() } - private fun getGetMethod(client: HttpClient, url: String): GetMethod { + private fun getGetMethod(url: String): GetMethod { return GetMethod(URL(url)).apply { setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) @@ -80,15 +79,10 @@ internal class StatusRequester { fun request(baseLocation: String, client: OwnCloudClient): RequestResult { val currentLocation = baseLocation + OwnCloudClient.STATUS_PATH var status: Int - -<<<<<<< HEAD val getMethod = getGetMethod(currentLocation) - getMethod.setFollowPermanentRedirects(true) + + getMethod.followPermanentRedirects = true status = client.executeHttpMethod(getMethod) -======= - while (true) { - val getMethod = getGetMethod(client, currentLocation) ->>>>>>> 530f8644 (apply required fixes) return RequestResult(getMethod, status, getMethod.getFinalUrl().toString()) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt index 836bc13d..b1eb2871 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt @@ -29,7 +29,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.RemoteServerInfo interface ServerInfoService { - fun checkPathExistence(path: String, isUserLogged: Boolean, client: OwnCloudClient): RemoteOperationResult + fun checkPathExistence(path: String, isUserLoggedIn: Boolean, client: OwnCloudClient): RemoteOperationResult fun getRemoteStatus(path: String, client: OwnCloudClient): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt index 732b7238..d2c891f0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt @@ -38,7 +38,7 @@ class OCServerInfoService : ServerInfoService { override fun checkPathExistence( path: String, - isUserLogged: Boolean, + isUserLoggedIn: Boolean, client: OwnCloudClient ): RemoteOperationResult = CheckPathExistenceRemoteOperation( diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java index 03ae360c..10128afa 100644 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -37,14 +37,9 @@ import android.view.View; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; - -<<<<<<< HEAD import com.owncloud.android.lib.common.ConnectionValidator; import com.owncloud.android.lib.common.OwnCloudClient; -======= -import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; @@ -55,13 +50,8 @@ import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.RemoteFile; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; -import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; -<<<<<<< HEAD -======= import info.hannes.timber.DebugTree; ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import timber.log.Timber; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -84,30 +74,20 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, super.onCreate(savedInstanceState); setContentView(R.layout.main); -<<<<<<< HEAD - Timber.plant(); -======= Timber.plant(new DebugTree()); ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) mHandler = new Handler(); 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, SingleSessionManager.getConnectionValidator(), true, 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 = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); + mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username), From 105830e4f07f9242cdab8cfc299d90827f87e078 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Tue, 19 Apr 2022 14:08:48 +0200 Subject: [PATCH 6/9] apply requested changes --- .../lib/common/OwnCloudClientFactory.java | 58 ------------------- .../android/lib/common/http/HttpClient.java | 9 ++- .../lib/common/http/methods/HttpBaseMethod.kt | 9 +-- .../lib/sampleclient/MainActivity.java | 3 - 4 files changed, 6 insertions(+), 73 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java deleted file mode 100644 index b8148c53..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ /dev/null @@ -1,58 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2020 ownCloud GmbH. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package com.owncloud.android.lib.common; - -import android.content.Context; -import android.net.Uri; - -import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation; - -public class OwnCloudClientFactory { - - /** - * Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud - * client connections. - * - * @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, - ConnectionValidator connectionValidator, - boolean followRedirects, - SingleSessionManager singleSessionManager, - Context context) { - OwnCloudClient client = new OwnCloudClient(uri, connectionValidator, true, singleSessionManager, context); - - client.setFollowRedirects(followRedirects); - retrieveCookiesFromMiddleware(client); - return client; - } - - private static void retrieveCookiesFromMiddleware(OwnCloudClient client) { - final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation(); - statusOperation.run(client); - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index afe4735a..241a50fa 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -61,15 +61,14 @@ public class HttpClient { private OkHttpClient mOkHttpClient = null; protected HttpClient(Context context) { + if(context == null) { + Timber.e("Context may not be NULL!"); + throw new NullPointerException("Context may not be NULL!"); + } mContext = context; } public OkHttpClient getOkHttpClient() { - 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()"); - } if (mOkHttpClient == null) { try { final X509TrustManager trustManager = new AdvancedX509TrustManager( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index bf58b0fd..88a44142 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -60,8 +60,8 @@ abstract class HttpBaseMethod constructor(url: URL) { @Throws(Exception::class) open fun execute(httpClient: HttpClient): Int { val okHttpClient = httpClient.okHttpClient.newBuilder().apply { - retryOnConnectionFailure?.let { retryOnConnectionFailure(it) } - followRedirects?.let { followRedirects(it) } + retryOnConnectionFailure.let { retryOnConnectionFailure(it) } + followRedirects.let { followRedirects(it) } readTimeoutUnit?.let { unit -> readTimeoutVal?.let { readTimeout(it, unit) } } @@ -152,12 +152,7 @@ abstract class HttpBaseMethod constructor(url: URL) { // Setter ////////////////////////////// // Connection parameters - /* - open fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { - retryOnConnectionFailureVal = true - } - */ open fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { readTimeoutVal = readTimeout diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java index 10128afa..b595b8e7 100644 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -74,7 +74,6 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, super.onCreate(savedInstanceState); setContentView(R.layout.main); - Timber.plant(new DebugTree()); mHandler = new Handler(); final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); @@ -86,8 +85,6 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, true, SingleSessionManager.getDefaultSingleton()); - mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); - mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username), From d16ab2e64362c6fbc6bd969ed3892afe558e4c82 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 21 Apr 2022 15:06:52 +0200 Subject: [PATCH 7/9] remove sample client yet again --- .../lib/sampleclient/MainActivity.java | 301 ------------------ 1 file changed, 301 deletions(-) delete mode 100644 sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java deleted file mode 100644 index b595b8e7..00000000 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ /dev/null @@ -1,301 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2020 ownCloud GmbH. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -package com.owncloud.android.lib.sampleclient; - -import android.annotation.SuppressLint; -import android.app.Activity; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.res.AssetManager; -import android.graphics.drawable.BitmapDrawable; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import android.view.View; -import android.widget.ListView; -import android.widget.TextView; -import android.widget.Toast; -import com.owncloud.android.lib.common.ConnectionValidator; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudClientFactory; -import com.owncloud.android.lib.common.SingleSessionManager; -import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; -import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; -import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; -import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; -import com.owncloud.android.lib.resources.files.RemoteFile; -import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; -import info.hannes.timber.DebugTree; -import timber.log.Timber; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -public class MainActivity extends Activity implements OnRemoteOperationListener, OnDatatransferProgressListener { - - private Handler mHandler; - private OwnCloudClient mClient; - private FilesArrayAdapter mFilesAdapter; - private View mFrame; - - /** - * Called when the activity is first created. - */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - - mHandler = new Handler(); - - final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); - - SingleSessionManager.setUserAgent(getUserAgent()); - SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false)); - mClient = new OwnCloudClient(serverUri, - SingleSessionManager.getConnectionValidator(), - true, - SingleSessionManager.getDefaultSingleton()); - - mClient.setCredentials( - OwnCloudCredentialsFactory.newBasicCredentials( - getString(R.string.username), - getString(R.string.password) - ) - ); - - mFilesAdapter = new FilesArrayAdapter(this, R.layout.file_in_list); - ((ListView) findViewById(R.id.list_view)).setAdapter(mFilesAdapter); - - // TODO move to background thread or task - AssetManager assets = getAssets(); - try { - String sampleFileName = getString(R.string.sample_file_name); - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); - upFolder.mkdir(); - File upFile = new File(upFolder, sampleFileName); - FileOutputStream fos = new FileOutputStream(upFile); - InputStream is = assets.open(sampleFileName); - int count; - byte[] buffer = new byte[1024]; - while ((count = is.read(buffer, 0, buffer.length)) >= 0) { - fos.write(buffer, 0, count); - } - is.close(); - fos.close(); - } catch (IOException e) { - Toast.makeText(this, R.string.error_copying_sample_file, Toast.LENGTH_SHORT).show(); - Timber.e(e, getString(R.string.error_copying_sample_file)); - } - - mFrame = findViewById(R.id.frame); - } - - @Override - public void onDestroy() { - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); - File upFile = upFolder.listFiles()[0]; - upFile.delete(); - upFolder.delete(); - super.onDestroy(); - } - - public void onClickHandler(View button) { - switch (button.getId()) { - case R.id.button_refresh: - startRefresh(); - break; - case R.id.button_upload: - startUpload(); - break; - case R.id.button_delete_remote: - startRemoteDeletion(); - break; - case R.id.button_download: - startDownload(); - break; - case R.id.button_delete_local: - startLocalDeletion(); - break; - default: - Toast.makeText(this, R.string.youre_doing_it_wrong, Toast.LENGTH_SHORT).show(); - } - } - - private void startRefresh() { - ReadRemoteFolderOperation refreshOperation = new ReadRemoteFolderOperation(File.separator); - refreshOperation.execute(mClient, this, mHandler); - } - - private void startUpload() { - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); - File fileToUpload = upFolder.listFiles()[0]; - String remotePath = File.separator + fileToUpload.getName(); - String mimeType = getString(R.string.sample_file_mimetype); - - // Get the last modification date of the file from the file system - long timeStampLong = fileToUpload.lastModified() / 1000; - String timeStamp = Long.toString(timeStampLong); - - UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(fileToUpload.getAbsolutePath(), - remotePath, mimeType, timeStamp); - uploadOperation.addDatatransferProgressListener(this); - uploadOperation.execute(mClient, this, mHandler); - } - - private void startRemoteDeletion() { - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); - File fileToUpload = upFolder.listFiles()[0]; - String remotePath = File.separator + fileToUpload.getName(); - - RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); - removeOperation.execute(mClient, this, mHandler); - } - - private void startDownload() { - File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path)); - downFolder.mkdir(); - File upFolder = new File(getCacheDir(), getString(R.string.upload_folder_path)); - File fileToUpload = upFolder.listFiles()[0]; - String remotePath = File.separator + fileToUpload.getName(); - - DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(remotePath, - downFolder.getAbsolutePath()); - downloadOperation.addDatatransferProgressListener(this); - downloadOperation.execute(mClient, this, mHandler); - } - - private void startLocalDeletion() { - File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path)); - File downloadedFile = downFolder.listFiles()[0]; - if (!downloadedFile.delete() && downloadedFile.exists()) { - Toast.makeText(this, R.string.error_deleting_local_file, Toast.LENGTH_SHORT).show(); - } else { - ((TextView) findViewById(R.id.download_progress)).setText("0%"); - findViewById(R.id.frame).setBackgroundDrawable(null); - } - } - - @Override - public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { - if (!result.isSuccess()) { - Toast.makeText(this, R.string.todo_operation_finished_in_fail, Toast.LENGTH_SHORT).show(); - Timber.e(result.getException(), result.getLogMessage()); - - } else if (operation instanceof ReadRemoteFolderOperation) { - onSuccessfulRefresh(result); - - } else if (operation instanceof com.owncloud.android.lib.resources.files.UploadRemoteFileOperation) { - onSuccessfulUpload(); - - } else if (operation instanceof RemoveRemoteFileOperation) { - onSuccessfulRemoteDeletion(); - - } else if (operation instanceof DownloadRemoteFileOperation) { - onSuccessfulDownload(); - - } else { - Toast.makeText(this, R.string.todo_operation_finished_in_success, Toast.LENGTH_SHORT).show(); - } - } - - private void onSuccessfulRefresh(RemoteOperationResult result) { - mFilesAdapter.clear(); - List files = new ArrayList<>(); - for (RemoteFile remoteFile : (List) result.getData()) { - files.add(remoteFile); - } - for (RemoteFile file : files) { - mFilesAdapter.add(file); - } - mFilesAdapter.remove(mFilesAdapter.getItem(0)); - mFilesAdapter.notifyDataSetChanged(); - } - - private void onSuccessfulUpload() { - startRefresh(); - } - - private void onSuccessfulRemoteDeletion() { - startRefresh(); - TextView progressView = findViewById(R.id.upload_progress); - if (progressView != null) { - progressView.setText("0%"); - } - } - - private void onSuccessfulDownload() { - File downFolder = new File(getCacheDir(), getString(R.string.download_folder_path)); - File downloadedFile = downFolder.listFiles()[0]; - BitmapDrawable bDraw = new BitmapDrawable(getResources(), downloadedFile.getAbsolutePath()); - mFrame.setBackgroundDrawable(bDraw); - } - - @SuppressLint("SetTextI18n") - @Override - public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) { - final long percentage = (totalToTransfer > 0 ? totalTransferredSoFar * 100 / totalToTransfer : 0); - final boolean upload = fileName.contains(getString(R.string.upload_folder_path)); - Timber.d("progressRate %s", percentage); - mHandler.post(() -> { - TextView progressView; - if (upload) { - progressView = findViewById(R.id.upload_progress); - } else { - progressView = findViewById(R.id.download_progress); - } - if (progressView != null) { - progressView.setText(percentage + "%"); - } - }); - } - - // user agent - @SuppressLint("StringFormatInvalid") - private String getUserAgent() { - String appString = getResources().getString(R.string.user_agent); - String packageName = getPackageName(); - String version = ""; - - PackageInfo pInfo; - try { - pInfo = getPackageManager().getPackageInfo(packageName, 0); - if (pInfo != null) { - version = pInfo.versionName; - } - } catch (PackageManager.NameNotFoundException e) { - Timber.e(e); - } - - // Mozilla/5.0 (Android) ownCloud-android/1.7.0 - return String.format(appString, version); - } -} \ No newline at end of file From be758eb5eabe83fe2d9d3166ae68840c7ea12536 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 21 Apr 2022 16:10:45 +0200 Subject: [PATCH 8/9] apply requested changes --- .../owncloud/android/lib/common/http/methods/HttpBaseMethod.kt | 2 ++ .../lib/resources/files/CheckPathExistenceRemoteOperation.kt | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index 88a44142..b58feaea 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -49,7 +49,9 @@ abstract class HttpBaseMethod constructor(url: URL) { var connectionTimeoutVal: Long? = null var connectionTimeoutUnit: TimeUnit? = null var readTimeoutVal: Long? = null + private set var readTimeoutUnit: TimeUnit? = null + private set init { request = Request.Builder() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index 654f7143..e59388bd 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -62,8 +62,6 @@ class CheckPathExistenceRemoteOperation( setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } - propFindMethod.followRedirects = false - var status = client.executeHttpMethod(propFindMethod) /* PROPFIND method * 404 NOT FOUND: path doesn't exist, From f6eb631fbf05b7e0867a1dc3a8d7f7ffea631c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garc=C3=ADa=20de=20Prada?= Date: Mon, 25 Apr 2022 08:29:47 +0200 Subject: [PATCH 9/9] Reformat some code --- .gitignore | 1 - .../com/owncloud/android/lib/common/OwnCloudClient.java | 6 +++--- .../com/owncloud/android/lib/common/http/HttpClient.java | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 858115b9..172bedd2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ .idea/* !.idea/codeStyles/ -sample_client/.idea/* # files for the dex VM *.dex diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 7d1a4409..e332b8fa 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -103,7 +103,7 @@ public class OwnCloudClient extends HttpClient { } public int executeHttpMethod(HttpBaseMethod method) throws Exception { - if(mSynchronizeRequests) { + if (mSynchronizeRequests) { synchronized (mRequestMutex) { return saveExecuteHttpMethod(method); } @@ -116,7 +116,7 @@ public class OwnCloudClient extends HttpClient { int repeatCounter = 0; int status; - if(mFollowRedirects) { + if (mFollowRedirects) { method.setFollowRedirects(true); } @@ -139,7 +139,7 @@ public class OwnCloudClient extends HttpClient { if (shouldConnectionValidatorBeCalled(method, status)) { retry = mConnectionValidator.validate(this, mSingleSessionManager, getContext()); // retry on success fail on no success - } else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) { + } else if (method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) { retry = true; method.setFollowRedirects(true); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 241a50fa..20539413 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -31,7 +31,6 @@ import com.owncloud.android.lib.common.network.NetworkUtils; import okhttp3.Cookie; import okhttp3.CookieJar; import okhttp3.HttpUrl; -import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; import okhttp3.TlsVersion; @@ -61,7 +60,7 @@ public class HttpClient { private OkHttpClient mOkHttpClient = null; protected HttpClient(Context context) { - if(context == null) { + if (context == null) { Timber.e("Context may not be NULL!"); throw new NullPointerException("Context may not be NULL!"); }