39
39
import javax .net .ssl .SSLSocketFactory ;
40
40
import javax .net .ssl .TrustManager ;
41
41
import javax .net .ssl .X509TrustManager ;
42
+ import java .security .KeyManagementException ;
42
43
import java .security .NoSuchAlgorithmException ;
43
- import java .util .ArrayList ;
44
44
import java .util .Arrays ;
45
45
import java .util .HashMap ;
46
- import java .util .HashSet ;
47
46
import java .util .List ;
48
- import java .util .Set ;
49
47
import java .util .concurrent .TimeUnit ;
50
48
51
49
/**
52
50
* Client used to perform network operations
53
51
*
54
52
* @author David González Verdugo
55
53
*/
54
+
56
55
public class HttpClient {
57
56
private static OkHttpClient sOkHttpClient ;
58
57
private static Context sContext ;
@@ -64,66 +63,13 @@ public static OkHttpClient getOkHttpClient() {
64
63
try {
65
64
final X509TrustManager trustManager = new AdvancedX509TrustManager (
66
65
NetworkUtils .getKnownServersStore (sContext ));
67
-
68
- SSLContext sslContext ;
69
-
70
- try {
71
- sslContext = SSLContext .getInstance ("TLSv1.3" );
72
- } catch (NoSuchAlgorithmException tlsv13Exception ) {
73
- try {
74
- Timber .w ("TLSv1.3 is not supported in this device; falling through TLSv1.2" );
75
- sslContext = SSLContext .getInstance ("TLSv1.2" );
76
- } catch (NoSuchAlgorithmException tlsv12Exception ) {
77
- try {
78
- Timber .w ("TLSv1.2 is not supported in this device; falling through TLSv1.1" );
79
- sslContext = SSLContext .getInstance ("TLSv1.1" );
80
- } catch (NoSuchAlgorithmException tlsv11Exception ) {
81
- Timber .w ("TLSv1.1 is not supported in this device; falling through TLSv1.0" );
82
- sslContext = SSLContext .getInstance ("TLSv1" );
83
- // should be available in any device; see reference of supported protocols in
84
- // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
85
- }
86
- }
87
- }
88
-
89
- sslContext .init (null , new TrustManager []{trustManager }, null );
90
-
91
- SSLSocketFactory sslSocketFactory ;
92
-
93
- sslSocketFactory = sslContext .getSocketFactory ();
94
-
66
+ final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory (trustManager );
95
67
// Automatic cookie handling, NOT PERSISTENT
96
- CookieJar cookieJar = new CookieJar () {
97
- @ Override
98
- public void saveFromResponse (HttpUrl url , List <Cookie > cookies ) {
99
- // Avoid duplicated cookies
100
- Set <Cookie > nonDuplicatedCookiesSet = new HashSet <>(cookies );
101
- List <Cookie > nonDuplicatedCookiesList = new ArrayList <>(nonDuplicatedCookiesSet );
102
-
103
- sCookieStore .put (url .host (), nonDuplicatedCookiesList );
104
- }
105
-
106
- @ Override
107
- public List <Cookie > loadForRequest (HttpUrl url ) {
108
- List <Cookie > cookies = sCookieStore .get (url .host ());
109
- return cookies != null ? cookies : new ArrayList <>();
110
- }
111
- };
112
-
113
- OkHttpClient .Builder clientBuilder = new OkHttpClient .Builder ()
114
- .addNetworkInterceptor (getLogInterceptor ())
115
- .protocols (Arrays .asList (Protocol .HTTP_1_1 ))
116
- .readTimeout (HttpConstants .DEFAULT_DATA_TIMEOUT , TimeUnit .MILLISECONDS )
117
- .writeTimeout (HttpConstants .DEFAULT_DATA_TIMEOUT , TimeUnit .MILLISECONDS )
118
- .connectTimeout (HttpConstants .DEFAULT_CONNECTION_TIMEOUT , TimeUnit .MILLISECONDS )
119
- .followRedirects (false )
120
- .sslSocketFactory (sslSocketFactory , trustManager )
121
- .hostnameVerifier ((asdf , usdf ) -> true )
122
- .cookieJar (cookieJar );
68
+ final CookieJar cookieJar = new CookieJarImpl (sCookieStore );
69
+
123
70
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
124
71
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
125
-
126
- sOkHttpClient = clientBuilder .build ();
72
+ sOkHttpClient = buildNewOkHttpClient (sslSocketFactory , trustManager , cookieJar );
127
73
128
74
} catch (Exception e ) {
129
75
Timber .e (e , "Could not setup SSL system." );
@@ -132,6 +78,49 @@ public List<Cookie> loadForRequest(HttpUrl url) {
132
78
return sOkHttpClient ;
133
79
}
134
80
81
+ private static SSLContext getSslContext () throws NoSuchAlgorithmException {
82
+ try {
83
+ return SSLContext .getInstance ("TLSv1.3" );
84
+ } catch (NoSuchAlgorithmException tlsv13Exception ) {
85
+ try {
86
+ Timber .w ("TLSv1.3 is not supported in this device; falling through TLSv1.2" );
87
+ return SSLContext .getInstance ("TLSv1.2" );
88
+ } catch (NoSuchAlgorithmException tlsv12Exception ) {
89
+ try {
90
+ Timber .w ("TLSv1.2 is not supported in this device; falling through TLSv1.1" );
91
+ return SSLContext .getInstance ("TLSv1.1" );
92
+ } catch (NoSuchAlgorithmException tlsv11Exception ) {
93
+ Timber .w ("TLSv1.1 is not supported in this device; falling through TLSv1.0" );
94
+ return SSLContext .getInstance ("TLSv1" );
95
+ // should be available in any device; see reference of supported protocols in
96
+ // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
97
+ }
98
+ }
99
+ }
100
+ }
101
+
102
+ private static SSLSocketFactory getNewSslSocketFactory (X509TrustManager trustManager )
103
+ throws NoSuchAlgorithmException , KeyManagementException {
104
+ final SSLContext sslContext = getSslContext ();
105
+ sslContext .init (null , new TrustManager []{trustManager }, null );
106
+ return sslContext .getSocketFactory ();
107
+ }
108
+
109
+ private static OkHttpClient buildNewOkHttpClient (SSLSocketFactory sslSocketFactory , X509TrustManager trustManager ,
110
+ CookieJar cookieJar ) {
111
+ return new OkHttpClient .Builder ()
112
+ .addNetworkInterceptor (getLogInterceptor ())
113
+ .protocols (Arrays .asList (Protocol .HTTP_1_1 ))
114
+ .readTimeout (HttpConstants .DEFAULT_DATA_TIMEOUT , TimeUnit .MILLISECONDS )
115
+ .writeTimeout (HttpConstants .DEFAULT_DATA_TIMEOUT , TimeUnit .MILLISECONDS )
116
+ .connectTimeout (HttpConstants .DEFAULT_CONNECTION_TIMEOUT , TimeUnit .MILLISECONDS )
117
+ .followRedirects (false )
118
+ .sslSocketFactory (sslSocketFactory , trustManager )
119
+ .hostnameVerifier ((asdf , usdf ) -> true )
120
+ .cookieJar (cookieJar )
121
+ .build ();
122
+ }
123
+
135
124
public Context getContext () {
136
125
return sContext ;
137
126
}
0 commit comments