36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.net.http;
|
|
27 |
|
|
28 |
import java.net.Authenticator;
|
|
29 |
import java.net.CookieManager;
|
|
30 |
import java.net.InetSocketAddress;
|
|
31 |
import java.net.NetPermission;
|
|
32 |
import java.net.ProxySelector;
|
|
33 |
import java.net.URI;
|
|
34 |
import java.util.Optional;
|
|
35 |
import java.util.concurrent.ExecutorService;
|
|
36 |
import java.util.concurrent.Executors;
|
|
37 |
import javax.net.ssl.SSLContext;
|
|
38 |
import javax.net.ssl.SSLParameters;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* A container for configuration information common to multiple {@link
|
|
42 |
* HttpRequest}s. All requests are associated with, and created from a {@code
|
|
43 |
* HttpClient}.
|
|
44 |
*
|
|
45 |
* <p> {@code HttpClient}s are immutable and created from a builder returned
|
|
46 |
* from {@link HttpClient#create()}. Request builders that are associated with
|
|
47 |
* an application created client, are created by calling {@link #request(URI) }.
|
|
48 |
* It is also possible to create a request builder directly which is associated
|
|
49 |
* with the <i>default</i> {@code HttpClient} by calling {@link
|
|
50 |
* HttpRequest#create(URI)}.
|
|
51 |
*
|
|
52 |
* <p> The HTTP API functions asynchronously (using {@link
|
|
53 |
* java.util.concurrent.CompletableFuture}) and also in a simple synchronous
|
|
54 |
* mode, where all work may be done on the calling thread. In asynchronous mode,
|
|
55 |
* work is done on the threads supplied by the client's {@link
|
|
56 |
* java.util.concurrent.ExecutorService}.
|
|
57 |
*
|
|
58 |
* <p> <a name="defaultclient"></a> The <i>default</i> {@code HttpClient} is
|
|
59 |
* used whenever a request is created without specifying a client explicitly
|
|
60 |
* (by calling {@link HttpRequest#create(java.net.URI) HttpRequest.create}).
|
|
61 |
* There is only one static instance of this {@code HttpClient}. A reference to
|
|
62 |
* the default client can be obtained by calling {@link #getDefault() }. If a
|
|
63 |
* security manager is set, then a permission is required for this.
|
|
64 |
*
|
|
65 |
* <p> See {@link HttpRequest} for examples of usage of this API.
|
|
66 |
*
|
|
67 |
* @since 9
|
|
68 |
*/
|
|
69 |
public abstract class HttpClient {
|
|
70 |
|
|
71 |
HttpClient() {}
|
|
72 |
|
|
73 |
private static HttpClient defaultClient;
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Creates a new {@code HttpClient} builder.
|
|
77 |
*
|
|
78 |
* @return a {@code HttpClient.Builder}
|
|
79 |
*/
|
|
80 |
public static Builder create() {
|
|
81 |
return new HttpClientBuilderImpl();
|
|
82 |
}
|
|
83 |
|
|
84 |
//public abstract void debugPrint();
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Returns the default {@code HttpClient} that is used when a {@link
|
|
88 |
* HttpRequest} is created without specifying a client. If a security
|
|
89 |
* manager is set, then its {@code checkPermission} method is called with a
|
|
90 |
* {@link java.net.NetPermission} specifying the name "getDefaultHttpClient".
|
|
91 |
* If the caller does not possess this permission a {@code SecurityException}
|
|
92 |
* is thrown.
|
|
93 |
*
|
|
94 |
* @implNote Code running under a security manager can avoid the security
|
|
95 |
* manager check by creating a {@code HttpClient} explicitly.
|
|
96 |
*
|
|
97 |
* @return the default {@code HttpClient}
|
|
98 |
* @throws SecurityException if the caller does not have the required
|
|
99 |
* permission
|
|
100 |
*/
|
|
101 |
public synchronized static HttpClient getDefault() {
|
|
102 |
Utils.checkNetPermission("getDefaultHttpClient");
|
|
103 |
if (defaultClient == null) {
|
|
104 |
Builder b = create();
|
|
105 |
defaultClient = b.executorService(Executors.newCachedThreadPool())
|
|
106 |
.build();
|
|
107 |
}
|
|
108 |
return defaultClient;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Creates a {@code HttpRequest} builder associated with this client.
|
|
113 |
*
|
|
114 |
* @return a new builder
|
|
115 |
*/
|
|
116 |
public abstract HttpRequest.Builder request();
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Creates a {@code HttpRequest} builder associated with this client and
|
|
120 |
* using the given request URI.
|
|
121 |
*
|
|
122 |
* @param uri the request URI
|
|
123 |
* @return a new builder
|
|
124 |
*/
|
|
125 |
public abstract HttpRequest.Builder request(URI uri);
|
|
126 |
|
|
127 |
/**
|
|
128 |
* A builder of immutable {@link HttpClient}s. {@code HttpClient.Builder}s
|
|
129 |
* are created by calling {@link HttpClient#create()}.
|
|
130 |
*
|
|
131 |
* <p> Each of the setter methods in this class modifies the state of the
|
|
132 |
* builder and returns <i>this</i> (ie. the same instance). The methods are
|
|
133 |
* not synchronized and should not be called from multiple threads without
|
|
134 |
* external synchronization.
|
|
135 |
*
|
|
136 |
* <p> {@link #build() } returns a new {@code HttpClient} each time it is
|
|
137 |
* called.
|
|
138 |
*
|
|
139 |
* @since 9
|
|
140 |
*/
|
|
141 |
public abstract static class Builder {
|
|
142 |
|
|
143 |
Builder() {}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Sets a cookie manager.
|
|
147 |
*
|
|
148 |
* @param manager the CookieManager
|
|
149 |
* @return this builder
|
|
150 |
* @throws NullPointerException if {@code manager} is null
|
|
151 |
*/
|
|
152 |
public abstract Builder cookieManager(CookieManager manager);
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Sets an SSLContext. If a security manager is set, then the caller
|
|
156 |
* must have the {@link java.net.NetPermission NetPermission}
|
|
157 |
* ("setSSLContext")
|
|
158 |
*
|
|
159 |
* <p> The effect of not calling this method, is that a default {@link
|
|
160 |
* javax.net.ssl.SSLContext} is used, which is normally adequate for
|
|
161 |
* client applications that do not need to specify protocols, or require
|
|
162 |
* client authentication.
|
|
163 |
*
|
|
164 |
* @param sslContext the SSLContext
|
|
165 |
* @return this builder
|
|
166 |
* @throws NullPointerException if {@code sslContext} is null
|
|
167 |
* @throws SecurityException if a security manager is set and the
|
|
168 |
* caller does not have any required permission
|
|
169 |
*/
|
|
170 |
public abstract Builder sslContext(SSLContext sslContext);
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Sets an SSLParameters. If this method is not called, then a default
|
|
174 |
* set of parameters are used. The contents of the given object are
|
|
175 |
* copied. Some parameters which are used internally by the HTTP protocol
|
|
176 |
* implementation (such as application protocol list) should not be set
|
|
177 |
* by callers, as they are ignored.
|
|
178 |
*
|
|
179 |
* @param sslParameters the SSLParameters
|
|
180 |
* @return this builder
|
|
181 |
* @throws NullPointerException if {@code sslParameters} is null
|
|
182 |
*/
|
|
183 |
public abstract Builder sslParameters(SSLParameters sslParameters);
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Sets the ExecutorService to be used for sending and receiving
|
|
187 |
* asynchronous requests. If this method is not called, a default
|
|
188 |
* executor service is set, which is the one returned from {@link
|
|
189 |
* java.util.concurrent.Executors#newCachedThreadPool()
|
|
190 |
* Executors.newCachedThreadPool}.
|
|
191 |
*
|
|
192 |
* @param s the ExecutorService
|
|
193 |
* @return this builder
|
|
194 |
* @throws NullPointerException if {@code s} is null
|
|
195 |
*/
|
|
196 |
public abstract Builder executorService(ExecutorService s);
|
|
197 |
|
|
198 |
/**
|
|
199 |
* Specifies whether requests will automatically follow redirects issued
|
|
200 |
* by the server. This setting can be overridden on each request. The
|
|
201 |
* default value for this setting is {@link Redirect#NEVER NEVER}
|
|
202 |
*
|
|
203 |
* @param policy the redirection policy
|
|
204 |
* @return this builder
|
|
205 |
* @throws NullPointerException if {@code policy} is null
|
|
206 |
*/
|
|
207 |
public abstract Builder followRedirects(Redirect policy);
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Requests a specific HTTP protocol version where possible. If not set,
|
|
211 |
* the version defaults to {@link HttpClient.Version#HTTP_1_1}. If
|
|
212 |
* {@link HttpClient.Version#HTTP_2} is set, then each request will
|
|
213 |
* attempt to upgrade to HTTP/2. If the upgrade succeeds, then the
|
|
214 |
* response to this request will use HTTP/2 and all subsequent requests
|
|
215 |
* and responses to the same
|
|
216 |
* <a href="https://tools.ietf.org/html/rfc6454#section-4">origin server</a>
|
|
217 |
* will use HTTP/2. If the upgrade fails, then the response will be
|
|
218 |
* handled using HTTP/1.1
|
|
219 |
*
|
|
220 |
* <p>This setting can be over-ridden per request.
|
|
221 |
*
|
|
222 |
* @param version the requested HTTP protocol version
|
|
223 |
* @return this builder
|
|
224 |
* @throws NullPointerException if {@code version} is null
|
|
225 |
*/
|
|
226 |
public abstract Builder version(HttpClient.Version version);
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Sets the default priority for any HTTP/2 requests sent from this
|
|
230 |
* client. The value provided must be between {@code 1} and {@code 255}.
|
|
231 |
*
|
|
232 |
* @param priority the priority weighting
|
|
233 |
* @return this builder
|
|
234 |
* @throws IllegalArgumentException if the given priority is out of range
|
|
235 |
*/
|
|
236 |
public abstract Builder priority(int priority);
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Enables pipelining mode for HTTP/1.1 requests sent through this
|
|
240 |
* client. When pipelining is enabled requests to the same destination
|
|
241 |
* are sent over existing TCP connections that may already have requests
|
|
242 |
* outstanding. This reduces the number of connections, but may have
|
|
243 |
* a performance impact since responses must be delivered in the same
|
|
244 |
* order that they were sent. By default, pipelining is disabled.
|
|
245 |
*
|
|
246 |
* @param enable {@code true} enables pipelining
|
|
247 |
* @return this builder
|
|
248 |
* @throws UnsupportedOperationException if pipelining mode is not
|
|
249 |
* supported by this implementation
|
|
250 |
*/
|
|
251 |
public abstract Builder pipelining(boolean enable);
|
|
252 |
|
|
253 |
/**
|
|
254 |
* Sets a {@link java.net.ProxySelector} for this client. If no selector
|
|
255 |
* is set, then no proxies are used. If a {@code null} parameter is
|
|
256 |
* given then the system wide default proxy selector is used.
|
|
257 |
*
|
|
258 |
* @implNote {@link java.net.ProxySelector#of(InetSocketAddress)}
|
|
259 |
* provides a ProxySelector which uses one proxy for all requests.
|
|
260 |
*
|
|
261 |
* @param selector the ProxySelector
|
|
262 |
* @return this builder
|
|
263 |
*/
|
|
264 |
public abstract Builder proxy(ProxySelector selector);
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Sets an authenticator to use for HTTP authentication.
|
|
268 |
*
|
|
269 |
* @param a the Authenticator
|
|
270 |
* @return this builder
|
|
271 |
*/
|
|
272 |
public abstract Builder authenticator(Authenticator a);
|
|
273 |
|
|
274 |
/**
|
|
275 |
* Returns a {@link HttpClient} built from the current state of this
|
|
276 |
* builder.
|
|
277 |
*
|
|
278 |
* @return this builder
|
|
279 |
*/
|
|
280 |
public abstract HttpClient build();
|
|
281 |
}
|
|
282 |
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Returns an {@code Optional} which contains this client's {@link
|
|
286 |
* CookieManager}. If no CookieManager was set in this client's builder,
|
|
287 |
* then the {@code Optional} is empty.
|
|
288 |
*
|
|
289 |
* @return an {@code Optional} containing this client's CookieManager
|
|
290 |
*/
|
|
291 |
public abstract Optional<CookieManager> cookieManager();
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Returns the follow-redirects setting for this client. The default value
|
|
295 |
* for this setting is {@link HttpClient.Redirect#NEVER}
|
|
296 |
*
|
|
297 |
* @return this client's follow redirects setting
|
|
298 |
*/
|
|
299 |
public abstract Redirect followRedirects();
|
|
300 |
|
|
301 |
/**
|
|
302 |
* Returns an {@code Optional} containing the ProxySelector for this client.
|
|
303 |
* If no proxy is set then the {@code Optional} is empty.
|
|
304 |
*
|
|
305 |
* @return an {@code Optional} containing this client's proxy selector
|
|
306 |
*/
|
|
307 |
public abstract Optional<ProxySelector> proxy();
|
|
308 |
|
|
309 |
/**
|
|
310 |
* Returns the SSLContext, if one was set on this client. If a security
|
|
311 |
* manager is set then then caller must then the caller must have the
|
|
312 |
* {@link java.net.NetPermission NetPermission}("getSSLContext") permission.
|
|
313 |
* If no SSLContext was set, then the default context is returned.
|
|
314 |
*
|
|
315 |
* @return this client's SSLContext
|
|
316 |
*/
|
|
317 |
public abstract SSLContext sslContext();
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Returns an {@code Optional} containing the {@link SSLParameters} set on
|
|
321 |
* this client. If no {@code SSLParameters} were set in the client's builder,
|
|
322 |
* then the {@code Optional} is empty.
|
|
323 |
*
|
|
324 |
* @return an {@code Optional} containing this client's SSLParameters
|
|
325 |
*/
|
|
326 |
public abstract Optional<SSLParameters> sslParameters();
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Returns an {@code Optional} containing the {@link Authenticator} set on
|
|
330 |
* this client. If no {@code Authenticator} was set in the client's builder,
|
|
331 |
* then the {@code Optional} is empty.
|
|
332 |
*
|
|
333 |
* @return an {@code Optional} containing this client's Authenticator
|
|
334 |
*/
|
|
335 |
public abstract Optional<Authenticator> authenticator();
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Returns the HTTP protocol version requested for this client. The default
|
|
339 |
* value is {@link HttpClient.Version#HTTP_1_1}
|
|
340 |
*
|
|
341 |
* @return the HTTP protocol version requested
|
|
342 |
*/
|
|
343 |
public abstract HttpClient.Version version();
|
|
344 |
|
|
345 |
/**
|
|
346 |
* Returns whether this client supports HTTP/1.1 pipelining.
|
|
347 |
*
|
|
348 |
* @return whether pipelining allowed
|
|
349 |
*/
|
|
350 |
public abstract boolean pipelining();
|
|
351 |
|
|
352 |
/**
|
|
353 |
* Returns the {@code ExecutorService} set on this client. If an {@code
|
|
354 |
* ExecutorService} was not set on the client's builder, then a default
|
|
355 |
* object is returned. The default ExecutorService is created independently
|
|
356 |
* for each client.
|
|
357 |
*
|
|
358 |
* @return this client's ExecutorService
|
|
359 |
*/
|
|
360 |
public abstract ExecutorService executorService();
|
|
361 |
|
|
362 |
/**
|
|
363 |
* The HTTP protocol version.
|
|
364 |
*
|
|
365 |
* @since 9
|
|
366 |
*/
|
|
367 |
public static enum Version {
|
|
368 |
|
|
369 |
/**
|
|
370 |
* HTTP version 1.1
|
|
371 |
*/
|
|
372 |
HTTP_1_1,
|
|
373 |
|
|
374 |
/**
|
|
375 |
* HTTP version 2
|
|
376 |
*/
|
|
377 |
HTTP_2
|
|
378 |
}
|
|
379 |
|
|
380 |
/**
|
|
381 |
* Defines automatic redirection policy. This is checked whenever a 3XX
|
|
382 |
* response code is received. If redirection does not happen automatically
|
|
383 |
* then the response is returned to the user, where it can be handled
|
|
384 |
* manually.
|
|
385 |
*
|
|
386 |
* <p> {@code Redirect} policy is set via the {@link
|
|
387 |
* HttpClient.Builder#followRedirects(HttpClient.Redirect)} method.
|
|
388 |
*
|
|
389 |
* @since 9
|
|
390 |
*/
|
|
391 |
public static enum Redirect {
|
|
392 |
|
|
393 |
/**
|
|
394 |
* Never redirect.
|
|
395 |
*/
|
|
396 |
NEVER,
|
|
397 |
|
|
398 |
/**
|
|
399 |
* Always redirect.
|
|
400 |
*/
|
|
401 |
ALWAYS,
|
|
402 |
|
|
403 |
/**
|
|
404 |
* Redirect to same protocol only. Redirection may occur from HTTP URLs
|
|
405 |
* to other HTTP URLs, and from HTTPS URLs to other HTTPS URLs.
|
|
406 |
*/
|
|
407 |
SAME_PROTOCOL,
|
|
408 |
|
|
409 |
/**
|
|
410 |
* Redirect always except from HTTPS URLs to HTTP URLs.
|
|
411 |
*/
|
|
412 |
SECURE
|
|
413 |
}
|
|
414 |
|
|
415 |
}
|