7
7
import okhttp3 .OkHttpClient ;
8
8
import okhttp3 .Request ;
9
9
import okhttp3 .RequestBody ;
10
- import okhttp3 .internal .Util ;
11
10
12
11
import java .io .Closeable ;
13
12
import java .io .File ;
14
- import java .io .InputStream ;
15
13
import java .io .InterruptedIOException ;
16
14
import java .io .IOException ;
17
15
import java .io .OutputStream ;
20
18
import java .util .Map ;
21
19
import java .util .concurrent .TimeUnit ;
22
20
23
- import okio .Buffer ;
24
21
import okio .BufferedSink ;
25
22
26
23
/*>>> import checkers.nullness.quals.Nullable; */
27
24
28
25
/**
29
26
* {@link HttpRequestor} implementation that uses <a href="http://square.github.io/okhttp/">OkHttp
30
27
* v3</a>. You can only use this if your project includes the OkHttp v3 library.
31
- *
32
- * <p>
33
- * To use this, pass {@link #INSTANCE} to the {@link com.dropbox.core.DbxRequestConfig} constructor.
34
- * </p>
35
28
*/
36
29
public class OkHttp3Requestor extends HttpRequestor {
37
30
/**
38
- * A thread-safe instance of {@code OkHttp3Requestor} that connects directly
39
- * (as opposed to using a proxy).
31
+ * @deprecated This field will be removed. Instead, do:
32
+ * {@code new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())}
40
33
*/
34
+ @ Deprecated
41
35
public static final OkHttp3Requestor INSTANCE = new OkHttp3Requestor (defaultOkHttpClient ());
42
36
43
- private final OkHttpClient client ;
37
+ /**
38
+ * Returns an {@code OkHttpClient} instance with the default settings for this SDK.
39
+ */
40
+ public static OkHttpClient defaultOkHttpClient () {
41
+ return defaultOkHttpClientBuilder ().build ();
42
+ }
44
43
45
- private static OkHttpClient defaultOkHttpClient () {
44
+ /**
45
+ * Returns an {@code OkHttpClient.Builder} instance with the default settings for this SDK.
46
+ */
47
+ public static OkHttpClient .Builder defaultOkHttpClientBuilder () {
46
48
return new OkHttpClient .Builder ()
47
49
.connectTimeout (DEFAULT_CONNECT_TIMEOUT_MILLIS , TimeUnit .MILLISECONDS )
48
50
.readTimeout (DEFAULT_READ_TIMEOUT_MILLIS , TimeUnit .MILLISECONDS )
49
51
.writeTimeout (DEFAULT_READ_TIMEOUT_MILLIS , TimeUnit .MILLISECONDS )
50
52
// enables certificate pinning
51
- .sslSocketFactory (SSLConfig .getSSLSocketFactory (), SSLConfig .getTrustManager ())
52
- .build ();
53
+ .sslSocketFactory (SSLConfig .getSSLSocketFactory (), SSLConfig .getTrustManager ());
53
54
}
54
55
56
+ private final OkHttpClient client ;
57
+
55
58
/**
56
59
* Creates a new instance of this requestor that uses {@code client} for its requests.
57
60
*
58
- * <p> NOTE: This constructor will not enable certificate pinning on the client. If you want
59
- * certificate pinning, use the default instance, {@link #INSTANCE}, or clone the default client
60
- * and modify it accordingly:
61
+ * <pre>
62
+ * OkHttpClient client = OkHttp3Requestor.defaultOkHttpClient();
63
+ * HttpRequestor requestor = new OkHttp3Requestor(client);
64
+ * </pre>
61
65
*
62
- * <p> NOTE: This SDK requires that OkHttp clients do not use same-thread executors for issuing
63
- * calls. The SDK relies on the assumption that all asynchronous calls will actually be executed
64
- * asynchronously. Using a same-thread executor for your OkHttp client may result in dead-locks.
66
+ * <p>
67
+ * If you need to make modifications to the {@link OkHttpClient} settings:
68
+ * </p>
65
69
*
66
70
* <pre>
67
- * OkHttpClient client = OkHttpRequestor.INSTANCE.getClient()
68
- * .readTimeout(2, TimeUnit.MINUTES)
69
- * // ... other modifications
70
- * .build();
71
- * HttpRequestor requestor = new OkHttpRequestor(client);
71
+ * OkHttpClient client = OkHttp3Requestor.defaultOkHttpClientBuilder()
72
+ * .readTimeout(2, TimeUnit.MINUTES)
73
+ * ...
74
+ * .build();
72
75
* </pre>
73
76
*
74
- * @param client {@code OkHttpClient} to use for requests, never {@code null}
77
+ * If you don't use {@link #defaultOkHttpClient()} or {@link #defaultOkHttpClientBuilder()},
78
+ * make sure to use Dropbox's hardened SSL settings from {@link SSLConfig}:
79
+ * </p>
75
80
*
76
- * @throws IllegalArgumentException if client uses a same-thread executor for its dispatcher
81
+ * <pre>
82
+ * OkHttpClient client = OkHttpClient.Builder()
83
+ * ...
84
+ * .sslSocketFactory(SSLConfig.getSSLSocketFactory(), SSLConfig.getTrustManager())
85
+ * .build();
86
+ * </pre>
77
87
*/
78
88
public OkHttp3Requestor (OkHttpClient client ) {
79
89
if (client == null ) throw new NullPointerException ("client" );
@@ -82,10 +92,13 @@ public OkHttp3Requestor(OkHttpClient client) {
82
92
}
83
93
84
94
/**
85
- * @deprecated If you need access to the {@link OkHttpClient} instance you passed
86
- * into the constructor, keep track of it yourself.
95
+ * Returns the underlying {@code OkHttpClient} used to make requests.
96
+ *
97
+ * If you want to modify the client for a particular request, create a new instance of this
98
+ * requestor with the modified client.
99
+ *
100
+ * @return underlying {@code OkHttpClient} used by this requestor.
87
101
*/
88
- @ Deprecated
89
102
public OkHttpClient getClient () {
90
103
return client ;
91
104
}
0 commit comments