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),