Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 0313c1e

Browse files
theScrabiabelgardep
authored andcommitted
get okhttp singleton removal changes to compile
1 parent 06e361a commit 0313c1e

40 files changed

+256
-50
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
25+
package com.owncloud.android.lib.common;
26+
27+
import android.net.Uri;
28+
29+
import com.owncloud.android.lib.common.http.HttpClient;
30+
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
31+
32+
public class OwnCloudClientFactory {
33+
34+
/**
35+
* Creates a OwnCloudClient to access a URL and sets the desired parameters for ownCloud
36+
* client connections.
37+
*
38+
* @param uri URL to the ownCloud server; BASE ENTRY POINT, not WebDavPATH
39+
* @return A OwnCloudClient object ready to be used
40+
*/
41+
public static OwnCloudClient createOwnCloudClient(Uri uri, boolean followRedirects) {
42+
OwnCloudClient client = new OwnCloudClient(uri);
43+
44+
client.setFollowRedirects(followRedirects);
45+
HttpClient.setContext(context);
46+
retrieveCookiesFromMiddleware(client);
47+
return client;
48+
}
49+
50+
private static void retrieveCookiesFromMiddleware(OwnCloudClient client) {
51+
final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation();
52+
statusOperation.run(client);
53+
}
54+
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public OwnCloudClient getClientFor(OwnCloudAccount account,
140140
client.clearCredentials();
141141

142142
client.setAccount(account);
143-
HttpClient.setContext(context);
144143

145144
account.loadCredentials(context);
146145
client.setCredentials(account.getCredentials());

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,38 @@
5555
*/
5656

5757
public class HttpClient {
58-
private static OkHttpClient sOkHttpClient;
5958
private static Context sContext;
60-
private static HashMap<String, List<Cookie>> sCookieStore = new HashMap<>();
61-
private static LogInterceptor sLogInterceptor;
62-
private static Interceptor sDebugInterceptor;
59+
private HashMap<String, List<Cookie>> mCookieStore = new HashMap<>();
60+
private LogInterceptor mLogInterceptor;
6361

64-
public static OkHttpClient getOkHttpClient() {
65-
if (sOkHttpClient == null) {
62+
private OkHttpClient mOkHttpClient = null;
63+
64+
protected HttpClient() {
65+
mLogInterceptor = new LogInterceptor();
66+
}
67+
68+
public OkHttpClient getOkHttpClient() {
69+
if(sContext == null) {
70+
Timber.e("Context not initialized call HttpClient.setContext(applicationContext) in the MainApp.onCrate()");
71+
throw new RuntimeException("Context not initialized call HttpClient.setContext(applicationContext) in the MainApp.onCrate()");
72+
}
73+
if(mOkHttpClient == null) {
6674
try {
6775
final X509TrustManager trustManager = new AdvancedX509TrustManager(
6876
NetworkUtils.getKnownServersStore(sContext));
6977
final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager);
7078
// Automatic cookie handling, NOT PERSISTENT
71-
final CookieJar cookieJar = new CookieJarImpl(sCookieStore);
79+
final CookieJar cookieJar = new CookieJarImpl(mCookieStore);
7280

7381
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
7482
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
75-
sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
83+
mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
7684

7785
} catch (Exception e) {
7886
Timber.e(e, "Could not setup SSL system.");
7987
}
8088
}
81-
return sOkHttpClient;
89+
return mOkHttpClient;
8290
}
8391

8492
private static SSLContext getSslContext() throws NoSuchAlgorithmException {
@@ -109,7 +117,7 @@ private static SSLSocketFactory getNewSslSocketFactory(X509TrustManager trustMan
109117
return sslContext.getSocketFactory();
110118
}
111119

112-
private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
120+
private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
113121
CookieJar cookieJar) {
114122
return new OkHttpClient.Builder()
115123
.addNetworkInterceptor(getLogInterceptor())
@@ -125,22 +133,23 @@ private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFacto
125133
.build();
126134
}
127135

128-
public static LogInterceptor getLogInterceptor() {
129-
if (sLogInterceptor == null) {
130-
sLogInterceptor = new LogInterceptor();
131-
}
132-
return sLogInterceptor;
133-
}
134-
135136
public Context getContext() {
136137
return sContext;
137138
}
138139

140+
public LogInterceptor getLogInterceptor() {
141+
return mLogInterceptor;
142+
}
143+
144+
public List<Cookie> getCookiesFromUrl(HttpUrl httpUrl) {
145+
return mCookieStore.get(httpUrl.host());
146+
}
147+
139148
public static void setContext(Context context) {
140149
sContext = context;
141150
}
142151

143152
public void clearCookies() {
144-
sCookieStore.clear();
153+
mCookieStore.clear();
145154
}
146155
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import java.net.MalformedURLException
3737
import java.net.URL
3838
import java.util.concurrent.TimeUnit
3939

40-
abstract class HttpBaseMethod constructor(url: URL) {
40+
abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) {
4141
var okHttpClient: OkHttpClient
4242
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
4343
var request: Request
@@ -47,7 +47,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
4747
var call: Call? = null
4848

4949
init {
50-
okHttpClient = HttpClient.getOkHttpClient()
50+
okHttpClient = clientWrapper.okHttpClient
5151
request = Request.Builder()
5252
.url(httpUrl)
5353
.build()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
2627
import java.io.IOException
2728
import java.net.URL
2829

@@ -31,7 +32,7 @@ import java.net.URL
3132
*
3233
* @author David González Verdugo
3334
*/
34-
class DeleteMethod(url: URL) : HttpMethod(url) {
35+
class DeleteMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
3536
@Throws(IOException::class)
3637
override fun onExecute(): Int {
3738
request = request.newBuilder()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
2627
import java.io.IOException
2728
import java.net.URL
2829

@@ -31,7 +32,7 @@ import java.net.URL
3132
*
3233
* @author David González Verdugo
3334
*/
34-
class GetMethod(url: URL) : HttpMethod(url) {
35+
class GetMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
3536
@Throws(IOException::class)
3637
override fun onExecute(): Int {
3738
request = request.newBuilder()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
2627
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
28+
import okhttp3.OkHttpClient
2729
import okhttp3.Response
2830
import java.net.URL
2931

@@ -33,8 +35,9 @@ import java.net.URL
3335
* @author David González Verdugo
3436
*/
3537
abstract class HttpMethod(
38+
httpClient: HttpClient,
3639
url: URL
37-
) : HttpBaseMethod(url) {
40+
) : HttpBaseMethod(httpClient, url) {
3841

3942
override lateinit var response: Response
4043

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
27+
import okhttp3.OkHttpClient
2628
import okhttp3.RequestBody
2729
import java.io.IOException
2830
import java.net.URL
@@ -33,9 +35,10 @@ import java.net.URL
3335
* @author David González Verdugo
3436
*/
3537
class PostMethod(
38+
httpClient: HttpClient,
3639
url: URL,
3740
private val postRequestBody: RequestBody
38-
) : HttpMethod(url) {
41+
) : HttpMethod(httpClient, url) {
3942
@Throws(IOException::class)
4043
override fun onExecute(): Int {
4144
request = request.newBuilder()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
27+
import okhttp3.OkHttpClient
2628
import okhttp3.RequestBody
2729
import java.io.IOException
2830
import java.net.URL
@@ -33,9 +35,10 @@ import java.net.URL
3335
* @author David González Verdugo
3436
*/
3537
class PutMethod(
38+
httpClient: HttpClient,
3639
url: URL,
3740
private val putRequestBody: RequestBody
38-
) : HttpMethod(url) {
41+
) : HttpMethod(httpClient, url) {
3942
@Throws(IOException::class)
4043
override fun onExecute(): Int {
4144
request = request.newBuilder()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.webdav
2525

26+
import com.owncloud.android.lib.common.http.HttpClient
2627
import okhttp3.Response
2728
import java.net.URL
2829

@@ -33,10 +34,11 @@ import java.net.URL
3334
* @author David González Verdugo
3435
*/
3536
class CopyMethod(
37+
httpClient: HttpClient,
3638
val url: URL,
3739
private val destinationUrl: String,
3840
private val forceOverride: Boolean
39-
) : DavMethod(url) {
41+
) : DavMethod(httpClient, url) {
4042
@Throws(Exception::class)
4143
public override fun onExecute(): Int {
4244
davResource.copy(

0 commit comments

Comments
 (0)