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

get okhttp singleton removal changes to compile

This commit is contained in:
Christian Schabesberger 2021-01-04 15:08:10 +01:00 committed by Abel García de Prada
parent 06e361afaa
commit 0313c1e103
40 changed files with 256 additions and 50 deletions

View File

@ -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);
}
}

View File

@ -140,7 +140,6 @@ public class SingleSessionManager {
client.clearCredentials(); client.clearCredentials();
client.setAccount(account); client.setAccount(account);
HttpClient.setContext(context);
account.loadCredentials(context); account.loadCredentials(context);
client.setCredentials(account.getCredentials()); client.setCredentials(account.getCredentials());

View File

@ -55,30 +55,38 @@ import java.util.concurrent.TimeUnit;
*/ */
public class HttpClient { public class HttpClient {
private static OkHttpClient sOkHttpClient;
private static Context sContext; private static Context sContext;
private static HashMap<String, List<Cookie>> sCookieStore = new HashMap<>(); private HashMap<String, List<Cookie>> mCookieStore = new HashMap<>();
private static LogInterceptor sLogInterceptor; private LogInterceptor mLogInterceptor;
private static Interceptor sDebugInterceptor;
public static OkHttpClient getOkHttpClient() { private OkHttpClient mOkHttpClient = null;
if (sOkHttpClient == 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 { try {
final X509TrustManager trustManager = new AdvancedX509TrustManager( final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext)); NetworkUtils.getKnownServersStore(sContext));
final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager); final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager);
// Automatic cookie handling, NOT PERSISTENT // 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. // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
//.hostnameVerifier(new BrowserCompatHostnameVerifier()); //.hostnameVerifier(new BrowserCompatHostnameVerifier());
sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
} catch (Exception e) { } catch (Exception e) {
Timber.e(e, "Could not setup SSL system."); Timber.e(e, "Could not setup SSL system.");
} }
} }
return sOkHttpClient; return mOkHttpClient;
} }
private static SSLContext getSslContext() throws NoSuchAlgorithmException { private static SSLContext getSslContext() throws NoSuchAlgorithmException {
@ -109,7 +117,7 @@ public class HttpClient {
return sslContext.getSocketFactory(); return sslContext.getSocketFactory();
} }
private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
CookieJar cookieJar) { CookieJar cookieJar) {
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.addNetworkInterceptor(getLogInterceptor()) .addNetworkInterceptor(getLogInterceptor())
@ -125,22 +133,23 @@ public class HttpClient {
.build(); .build();
} }
public static LogInterceptor getLogInterceptor() {
if (sLogInterceptor == null) {
sLogInterceptor = new LogInterceptor();
}
return sLogInterceptor;
}
public Context getContext() { public Context getContext() {
return sContext; return sContext;
} }
public LogInterceptor getLogInterceptor() {
return mLogInterceptor;
}
public List<Cookie> getCookiesFromUrl(HttpUrl httpUrl) {
return mCookieStore.get(httpUrl.host());
}
public static void setContext(Context context) { public static void setContext(Context context) {
sContext = context; sContext = context;
} }
public void clearCookies() { public void clearCookies() {
sCookieStore.clear(); mCookieStore.clear();
} }
} }

View File

@ -37,7 +37,7 @@ import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
abstract class HttpBaseMethod constructor(url: URL) { abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) {
var okHttpClient: OkHttpClient var okHttpClient: OkHttpClient
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
var request: Request var request: Request
@ -47,7 +47,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
var call: Call? = null var call: Call? = null
init { init {
okHttpClient = HttpClient.getOkHttpClient() okHttpClient = clientWrapper.okHttpClient
request = Request.Builder() request = Request.Builder()
.url(httpUrl) .url(httpUrl)
.build() .build()

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
@ -31,7 +32,7 @@ import java.net.URL
* *
* @author David González Verdugo * @author David González Verdugo
*/ */
class DeleteMethod(url: URL) : HttpMethod(url) { class DeleteMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
@Throws(IOException::class) @Throws(IOException::class)
override fun onExecute(): Int { override fun onExecute(): Int {
request = request.newBuilder() request = request.newBuilder()

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
@ -31,7 +32,7 @@ import java.net.URL
* *
* @author David González Verdugo * @author David González Verdugo
*/ */
class GetMethod(url: URL) : HttpMethod(url) { class GetMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
@Throws(IOException::class) @Throws(IOException::class)
override fun onExecute(): Int { override fun onExecute(): Int {
request = request.newBuilder() request = request.newBuilder()

View File

@ -23,7 +23,9 @@
*/ */
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
import okhttp3.OkHttpClient
import okhttp3.Response import okhttp3.Response
import java.net.URL import java.net.URL
@ -33,8 +35,9 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
abstract class HttpMethod( abstract class HttpMethod(
httpClient: HttpClient,
url: URL url: URL
) : HttpBaseMethod(url) { ) : HttpBaseMethod(httpClient, url) {
override lateinit var response: Response override lateinit var response: Response

View File

@ -23,6 +23,8 @@
*/ */
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
@ -33,9 +35,10 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class PostMethod( class PostMethod(
httpClient: HttpClient,
url: URL, url: URL,
private val postRequestBody: RequestBody private val postRequestBody: RequestBody
) : HttpMethod(url) { ) : HttpMethod(httpClient, url) {
@Throws(IOException::class) @Throws(IOException::class)
override fun onExecute(): Int { override fun onExecute(): Int {
request = request.newBuilder() request = request.newBuilder()

View File

@ -23,6 +23,8 @@
*/ */
package com.owncloud.android.lib.common.http.methods.nonwebdav package com.owncloud.android.lib.common.http.methods.nonwebdav
import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
@ -33,9 +35,10 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class PutMethod( class PutMethod(
httpClient: HttpClient,
url: URL, url: URL,
private val putRequestBody: RequestBody private val putRequestBody: RequestBody
) : HttpMethod(url) { ) : HttpMethod(httpClient, url) {
@Throws(IOException::class) @Throws(IOException::class)
override fun onExecute(): Int { override fun onExecute(): Int {
request = request.newBuilder() request = request.newBuilder()

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.common.http.methods.webdav package com.owncloud.android.lib.common.http.methods.webdav
import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.Response import okhttp3.Response
import java.net.URL import java.net.URL
@ -33,10 +34,11 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class CopyMethod( class CopyMethod(
httpClient: HttpClient,
val url: URL, val url: URL,
private val destinationUrl: String, private val destinationUrl: String,
private val forceOverride: Boolean private val forceOverride: Boolean
) : DavMethod(url) { ) : DavMethod(httpClient, url) {
@Throws(Exception::class) @Throws(Exception::class)
public override fun onExecute(): Int { public override fun onExecute(): Int {
davResource.copy( davResource.copy(

View File

@ -27,6 +27,7 @@ import at.bitfire.dav4jvm.Dav4jvm.log
import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.DavOCResource
import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.exception.RedirectException 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.HttpConstants
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
import okhttp3.HttpUrl import okhttp3.HttpUrl
@ -43,7 +44,7 @@ import java.util.concurrent.TimeUnit
* *
* @author David González Verdugo * @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 protected var davResource: DavOCResource
override lateinit var response: Response override lateinit var response: Response

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.common.http.methods.webdav package com.owncloud.android.lib.common.http.methods.webdav
import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.Response import okhttp3.Response
import java.net.URL import java.net.URL
@ -32,7 +33,7 @@ import java.net.URL
* @author Christian Schabesberger * @author Christian Schabesberger
* @author David González Verdugo * @author David González Verdugo
*/ */
class MkColMethod(url: URL) : DavMethod(url) { class MkColMethod(httpClient: HttpClient, url: URL) : DavMethod(httpClient, url) {
@Throws(Exception::class) @Throws(Exception::class)
public override fun onExecute(): Int { public override fun onExecute(): Int {
davResource.mkCol( davResource.mkCol(

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.common.http.methods.webdav package com.owncloud.android.lib.common.http.methods.webdav
import com.owncloud.android.lib.common.http.HttpClient
import okhttp3.Response import okhttp3.Response
import java.net.URL import java.net.URL
@ -33,10 +34,11 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class MoveMethod( class MoveMethod(
httpClient: HttpClient,
url: URL, url: URL,
private val destinationUrl: String, private val destinationUrl: String,
private val forceOverride: Boolean private val forceOverride: Boolean
) : DavMethod(url) { ) : DavMethod(httpClient, url) {
@Throws(Exception::class) @Throws(Exception::class)
public override fun onExecute(): Int { public override fun onExecute(): Int {
davResource.move( davResource.move(

View File

@ -27,6 +27,7 @@ import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.Response.HrefRelation import at.bitfire.dav4jvm.Response.HrefRelation
import at.bitfire.dav4jvm.exception.DavException import at.bitfire.dav4jvm.exception.DavException
import com.owncloud.android.lib.common.http.HttpClient
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
@ -36,10 +37,11 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class PropfindMethod( class PropfindMethod(
httpClient: HttpClient,
url: URL, url: URL,
private val depth: Int, private val depth: Int,
private val propertiesToRequest: Array<Property.Name> private val propertiesToRequest: Array<Property.Name>
) : DavMethod(url) { ) : DavMethod(httpClient, url) {
// response // response
val members: MutableList<Response> val members: MutableList<Response>

View File

@ -24,6 +24,7 @@
package com.owncloud.android.lib.common.http.methods.webdav package com.owncloud.android.lib.common.http.methods.webdav
import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.exception.HttpException
import com.owncloud.android.lib.common.http.HttpClient
import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.HttpConstants
import okhttp3.RequestBody import okhttp3.RequestBody
import java.io.IOException import java.io.IOException
@ -35,9 +36,10 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class PutMethod( class PutMethod(
httpClient: HttpClient,
url: URL, url: URL,
private val putRequestBody: RequestBody private val putRequestBody: RequestBody
) : DavMethod(url) { ) : DavMethod(httpClient, url) {
@Throws(IOException::class, HttpException::class) @Throws(IOException::class, HttpException::class)
public override fun onExecute(): Int { public override fun onExecute(): Int {
davResource.put( davResource.put(

View File

@ -57,7 +57,7 @@ class CheckPathExistenceRemoteOperation(
if (isUserLoggedIn) client.baseFilesWebDavUri.toString() if (isUserLoggedIn) client.baseFilesWebDavUri.toString()
else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath) 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) setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
} }

View File

@ -92,7 +92,8 @@ public class CopyRemoteFileOperation extends RemoteOperation<String> {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
CopyMethod copyMethod = 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), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite); mOverwrite);

View File

@ -87,7 +87,8 @@ public class CreateRemoteFolderOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
try { try {
Uri webDavUri = createChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); 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.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
final int status = client.executeHttpMethod(mkcol); final int status = client.executeHttpMethod(mkcol);

View File

@ -96,7 +96,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
int status; int status;
boolean savedFile = false; 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<OnDatatransferProgressListener> it; Iterator<OnDatatransferProgressListener> it;
FileOutputStream fos = null; FileOutputStream fos = null;

View File

@ -100,7 +100,7 @@ public class MoveRemoteFileOperation extends RemoteOperation {
// so this uri has to be customizable // so this uri has to be customizable
Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
final MoveMethod move = new MoveMethod( final MoveMethod move = new MoveMethod(client,
new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)),
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite); mOverwrite);

View File

@ -76,7 +76,8 @@ public class ReadRemoteFileOperation extends RemoteOperation<RemoteFile> {
/// take the duty of check the server for the current state of the file there /// take the duty of check the server for the current state of the file there
try { try {
// remote request // 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, DEPTH_0,
DavUtils.getAllPropset()); DavUtils.getAllPropset());

View File

@ -73,6 +73,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation<ArrayList<RemoteF
try { try {
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
client,
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
DavConstants.DEPTH_1, DavConstants.DEPTH_1,
DavUtils.getAllPropset()); DavUtils.getAllPropset());

View File

@ -72,6 +72,7 @@ public class RemoveRemoteFileOperation extends RemoteOperation {
Uri srcWebDavUri = removeChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri(); Uri srcWebDavUri = removeChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
DeleteMethod deleteMethod = new DeleteMethod( DeleteMethod deleteMethod = new DeleteMethod(
client,
new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath))); new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath)));
int status = client.executeHttpMethod(deleteMethod); int status = client.executeHttpMethod(deleteMethod);

View File

@ -91,7 +91,8 @@ public class RenameRemoteFileOperation extends RemoteOperation {
return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); return new RemoteOperationResult<>(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)), WebdavUtils.encodePath(mOldRemotePath)),
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false); client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false);

View File

@ -121,7 +121,7 @@ public class UploadRemoteFileOperation extends RemoteOperation {
mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners);
} }
mPutMethod = new PutMethod( mPutMethod = new PutMethod(client,
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody); new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody);
mPutMethod.setRetryOnConnectionFailure(false); mPutMethod.setRetryOnConnectionFailure(false);

View File

@ -92,7 +92,8 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation
result = new RemoteOperationResult<>(new OperationCancelledException()); result = new RemoteOperationResult<>(new OperationCancelledException());
break; break;
} else { } 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) { if (chunkIndex == chunkCount - 1) {
// Added a high timeout to the last chunk due to when the last chunk // Added a high timeout to the last chunk due to when the last chunk

View File

@ -138,7 +138,6 @@ class CreateRemoteShareOperation(
} }
private fun createFormBody(): FormBody { private fun createFormBody(): FormBody {
val formBodyBuilder = FormBody.Builder() val formBodyBuilder = FormBody.Builder()
.add(PARAM_PATH, remoteFilePath) .add(PARAM_PATH, remoteFilePath)
.add(PARAM_SHARE_TYPE, shareType.value.toString()) .add(PARAM_SHARE_TYPE, shareType.value.toString())
@ -172,7 +171,7 @@ class CreateRemoteShareOperation(
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
val requestUri = buildRequestUri(client.baseUri) 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) setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8)
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }

View File

@ -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<ShareParserResult> {
private String mRemoteId;
public GetRemoteShareOperation(String remoteId) {
mRemoteId = remoteId;
}
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult<ShareParserResult> 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);
}
}

View File

@ -131,7 +131,7 @@ class GetRemoteShareesOperation
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareeOcsResponse> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareeOcsResponse> {
val requestUri = buildRequestUri(client.baseUri) 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) getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
return try { return try {

View File

@ -119,7 +119,7 @@ class GetRemoteSharesForFileOperation(
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
val requestUri = buildRequestUri(client.baseUri) 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) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }

View File

@ -104,9 +104,10 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
val requestUri = buildRequestUri(client.baseUri) 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) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }

View File

@ -200,7 +200,7 @@ class UpdateRemoteShareOperation
val formBodyBuilder = createFormBodyBuilder() 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) setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8)
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }

View File

@ -60,7 +60,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
appendEncodedPath(OCS_ROUTE) // avoid starting "/" in this method appendEncodedPath(OCS_ROUTE) // avoid starting "/" in this method
appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) 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) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
} }
val status = client.executeHttpMethod(getMethod) val status = client.executeHttpMethod(getMethod)

View File

@ -53,6 +53,7 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
client.baseUri = Uri.parse(newBaseUrl) client.baseUri = Uri.parse(newBaseUrl)
} }
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> { private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
val baseUrl = client.baseUri.toString() val baseUrl = client.baseUri.toString()
return try { return try {

View File

@ -23,6 +23,7 @@
*/ */
package com.owncloud.android.lib.resources.status.services package com.owncloud.android.lib.resources.status.services
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.status.RemoteServerInfo import com.owncloud.android.lib.resources.status.RemoteServerInfo

View File

@ -26,6 +26,7 @@
package com.owncloud.android.lib.resources.status.services.implementation package com.owncloud.android.lib.resources.status.services.implementation
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation

View File

@ -52,7 +52,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe
client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + File.separator + avatarDimension client.baseUri.toString() + NON_OFFICIAL_AVATAR_PATH + client.credentials.username + File.separator + avatarDimension
Timber.d("avatar URI: %s", endPoint) Timber.d("avatar URI: %s", endPoint)
val getMethod = GetMethod(URL(endPoint)) val getMethod = GetMethod(client, URL(endPoint))
val status = client.executeHttpMethod(getMethod) val status = client.executeHttpMethod(getMethod)

View File

@ -51,7 +51,7 @@ class GetRemoteUserInfoOperation : RemoteOperation<RemoteUserInfo>() {
var result: RemoteOperationResult<RemoteUserInfo> var result: RemoteOperationResult<RemoteUserInfo>
//Get the user //Get the user
try { 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 status = client.executeHttpMethod(getMethod)
val response = getMethod.getResponseBodyAsString() ?: "" val response = getMethod.getResponseBodyAsString() ?: ""
if (status == HttpConstants.HTTP_OK) { if (status == HttpConstants.HTTP_OK) {

View File

@ -50,6 +50,7 @@ class GetRemoteUserQuotaOperation : RemoteOperation<RemoteQuota>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteQuota> = override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteQuota> =
try { try {
val propfindMethod = PropfindMethod( val propfindMethod = PropfindMethod(
client,
URL(client.userFilesWebDavUri.toString()), URL(client.userFilesWebDavUri.toString()),
DavConstants.DEPTH_0, DavConstants.DEPTH_0,
DavUtils.quotaPropSet DavUtils.quotaPropSet

View File

@ -38,8 +38,13 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
<<<<<<< HEAD
import com.owncloud.android.lib.common.ConnectionValidator; 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.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.SingleSessionManager;
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; 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.RemoteFile;
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; 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 timber.log.Timber;
import java.io.File; import java.io.File;
@ -75,17 +84,26 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
<<<<<<< HEAD
Timber.plant(); Timber.plant();
=======
Timber.plant(new DebugTree());
>>>>>>> 5e555278 (get okhttp singleton removal changes to compile)
mHandler = new Handler(); mHandler = new Handler();
final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); final Uri serverUri = Uri.parse(getString(R.string.server_base_url));
SingleSessionManager.setUserAgent(getUserAgent()); SingleSessionManager.setUserAgent(getUserAgent());
<<<<<<< HEAD
SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false)); SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false));
mClient = new OwnCloudClient(serverUri, mClient = new OwnCloudClient(serverUri,
SingleSessionManager.getConnectionValidator(), SingleSessionManager.getConnectionValidator(),
true, true,
SingleSessionManager.getDefaultSingleton()); SingleSessionManager.getDefaultSingleton());
=======
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true);
>>>>>>> 5e555278 (get okhttp singleton removal changes to compile)
mClient.setCredentials( mClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials( OwnCloudCredentialsFactory.newBasicCredentials(
getString(R.string.username), getString(R.string.username),