author | sviswanathan |
Tue, 26 Nov 2019 19:19:39 +0300 | |
changeset 59280 | dff8053bdb74 |
parent 52563 | 8934429cfb0b |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package java.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.Authenticator; |
48083 | 30 |
import java.net.CookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.InetSocketAddress; |
48083 | 32 |
import java.net.Proxy; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.net.ProxySelector; |
49765 | 34 |
import java.net.URLPermission; |
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
37 |
import java.time.Duration; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.util.concurrent.Executor; |
48083 | 41 |
import java.util.concurrent.Executors; |
42 |
import java.util.concurrent.ThreadFactory; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import javax.net.ssl.SSLContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import javax.net.ssl.SSLParameters; |
49765 | 45 |
import java.net.http.HttpResponse.BodyHandler; |
46 |
import java.net.http.HttpResponse.PushPromiseHandler; |
|
47 |
import jdk.internal.net.http.HttpClientBuilderImpl; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
/** |
49765 | 50 |
* An HTTP Client. |
51 |
* |
|
52 |
* <p> An {@code HttpClient} can be used to send {@linkplain HttpRequest |
|
53 |
* requests} and retrieve their {@linkplain HttpResponse responses}. An {@code |
|
54 |
* HttpClient} is created through a {@link HttpClient#newBuilder() builder}. The |
|
55 |
* builder can be used to configure per-client state, like: the preferred |
|
56 |
* protocol version ( HTTP/1.1 or HTTP/2 ), whether to follow redirects, a |
|
57 |
* proxy, an authenticator, etc. Once built, an {@code HttpClient} is immutable, |
|
58 |
* and can be used to send multiple requests. |
|
59 |
* |
|
60 |
* <p> An {@code HttpClient} provides configuration information, and resource |
|
50681 | 61 |
* sharing, for all requests sent through it. |
49765 | 62 |
* |
63 |
* <p> A {@link BodyHandler BodyHandler} must be supplied for each {@link |
|
64 |
* HttpRequest} sent. The {@code BodyHandler} determines how to handle the |
|
65 |
* response body, if any. Once an {@link HttpResponse} is received, the |
|
66 |
* headers, response code, and body (typically) are available. Whether the |
|
67 |
* response body bytes have been read or not depends on the type, {@code T}, of |
|
68 |
* the response body. |
|
69 |
* |
|
70 |
* <p> Requests can be sent either synchronously or asynchronously: |
|
71 |
* <ul> |
|
72 |
* <li>{@link HttpClient#send(HttpRequest, BodyHandler)} blocks |
|
73 |
* until the request has been sent and the response has been received.</li> |
|
74 |
* |
|
75 |
* <li>{@link HttpClient#sendAsync(HttpRequest, BodyHandler)} sends the |
|
76 |
* request and receives the response asynchronously. The {@code sendAsync} |
|
77 |
* method returns immediately with a {@link CompletableFuture |
|
78 |
* CompletableFuture}<{@link HttpResponse}>. The {@code |
|
79 |
* CompletableFuture} completes when the response becomes available. The |
|
80 |
* returned {@code CompletableFuture} can be combined in different ways to |
|
81 |
* declare dependencies among several asynchronous tasks.</li> |
|
82 |
* </ul> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
* |
49765 | 84 |
* <p><b>Synchronous Example</b> |
85 |
* <pre>{@code HttpClient client = HttpClient.newBuilder() |
|
86 |
* .version(Version.HTTP_1_1) |
|
87 |
* .followRedirects(Redirect.NORMAL) |
|
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
88 |
* .connectTimeout(Duration.ofSeconds(20)) |
49765 | 89 |
* .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) |
90 |
* .authenticator(Authenticator.getDefault()) |
|
91 |
* .build(); |
|
92 |
* HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
|
93 |
* System.out.println(response.statusCode()); |
|
94 |
* System.out.println(response.body()); }</pre> |
|
95 |
* |
|
96 |
* <p><b>Asynchronous Example</b> |
|
97 |
* <pre>{@code HttpRequest request = HttpRequest.newBuilder() |
|
98 |
* .uri(URI.create("https://foo.com/")) |
|
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
99 |
* .timeout(Duration.ofMinutes(2)) |
49765 | 100 |
* .header("Content-Type", "application/json") |
101 |
* .POST(BodyPublishers.ofFile(Paths.get("file.json"))) |
|
102 |
* .build(); |
|
103 |
* client.sendAsync(request, BodyHandlers.ofString()) |
|
104 |
* .thenApply(HttpResponse::body) |
|
105 |
* .thenAccept(System.out::println); }</pre> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
* |
52563 | 107 |
* <p> <a id="securitychecks"><b>Security checks</b></a> |
49765 | 108 |
* |
109 |
* <p> If a security manager is present then security checks are performed by |
|
110 |
* the HTTP Client's sending methods. An appropriate {@link URLPermission} is |
|
111 |
* required to access the destination server, and proxy server if one has |
|
112 |
* been configured. The form of the {@code URLPermission} required to access a |
|
113 |
* proxy has a {@code method} parameter of {@code "CONNECT"} (for all kinds of |
|
114 |
* proxying) and a {@code URL} string of the form {@code "socket://host:port"} |
|
115 |
* where host and port specify the proxy's address. |
|
116 |
* |
|
117 |
* @implNote If an explicit {@linkplain HttpClient.Builder#executor(Executor) |
|
118 |
* executor} has not been set for an {@code HttpClient}, and a security manager |
|
119 |
* has been installed, then the default executor will execute asynchronous and |
|
120 |
* dependent tasks in a context that is granted no permissions. Custom |
|
121 |
* {@linkplain HttpRequest.BodyPublisher request body publishers}, {@linkplain |
|
122 |
* HttpResponse.BodyHandler response body handlers}, {@linkplain |
|
123 |
* HttpResponse.BodySubscriber response body subscribers}, and {@linkplain |
|
124 |
* WebSocket.Listener WebSocket Listeners}, if executing operations that require |
|
125 |
* privileges, should do so within an appropriate {@linkplain |
|
126 |
* AccessController#doPrivileged(PrivilegedAction) privileged context}. |
|
127 |
* |
|
128 |
* @since 11 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
public abstract class HttpClient { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
* Creates an HttpClient. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
protected HttpClient() {} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
/** |
49765 | 138 |
* Returns a new {@code HttpClient} with default settings. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
* |
48083 | 140 |
* <p> Equivalent to {@code newBuilder().build()}. |
141 |
* |
|
142 |
* <p> The default settings include: the "GET" request method, a preference |
|
143 |
* of {@linkplain HttpClient.Version#HTTP_2 HTTP/2}, a redirection policy of |
|
144 |
* {@linkplain Redirect#NEVER NEVER}, the {@linkplain |
|
145 |
* ProxySelector#getDefault() default proxy selector}, and the {@linkplain |
|
146 |
* SSLContext#getDefault() default SSL context}. |
|
147 |
* |
|
148 |
* @implNote The system-wide default values are retrieved at the time the |
|
149 |
* {@code HttpClient} instance is constructed. Changing the system-wide |
|
150 |
* values after an {@code HttpClient} instance has been built, for |
|
151 |
* instance, by calling {@link ProxySelector#setDefault(ProxySelector)} |
|
152 |
* or {@link SSLContext#setDefault(SSLContext)}, has no effect on already |
|
153 |
* built instances. |
|
154 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
* @return a new HttpClient |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
public static HttpClient newHttpClient() { |
48083 | 158 |
return newBuilder().build(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
* Creates a new {@code HttpClient} builder. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
* |
49765 | 164 |
* @return an {@code HttpClient.Builder} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
public static Builder newBuilder() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
return new HttpClientBuilderImpl(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
/** |
49765 | 171 |
* A builder of {@linkplain HttpClient HTTP Clients}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
* |
49765 | 173 |
* <p> Builders are created by invoking {@link HttpClient#newBuilder() |
48083 | 174 |
* newBuilder}. Each of the setter methods modifies the state of the builder |
175 |
* and returns the same instance. Builders are not thread-safe and should not be |
|
176 |
* used concurrently from multiple threads without external synchronization. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
* |
49765 | 178 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
*/ |
49765 | 180 |
public interface Builder { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
|
48083 | 182 |
/** |
183 |
* A proxy selector that always return {@link Proxy#NO_PROXY} implying |
|
184 |
* a direct connection. |
|
49765 | 185 |
* |
186 |
* <p> This is a convenience object that can be passed to |
|
187 |
* {@link #proxy(ProxySelector)} in order to build an instance of |
|
188 |
* {@link HttpClient} that uses no proxy. |
|
48083 | 189 |
*/ |
190 |
public static final ProxySelector NO_PROXY = ProxySelector.of(null); |
|
191 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
/** |
48083 | 194 |
* Sets a cookie handler. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
* |
48083 | 196 |
* @param cookieHandler the cookie handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
*/ |
49765 | 199 |
public Builder cookieHandler(CookieHandler cookieHandler); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
/** |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
202 |
* Sets the connect timeout duration for this client. |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
203 |
* |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
204 |
* <p> In the case where a new connection needs to be established, if |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
205 |
* the connection cannot be established within the given {@code |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
206 |
* duration}, then {@link HttpClient#send(HttpRequest,BodyHandler) |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
207 |
* HttpClient::send} throws an {@link HttpConnectTimeoutException}, or |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
208 |
* {@link HttpClient#sendAsync(HttpRequest,BodyHandler) |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
209 |
* HttpClient::sendAsync} completes exceptionally with an |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
210 |
* {@code HttpConnectTimeoutException}. If a new connection does not |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
211 |
* need to be established, for example if a connection can be reused |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
212 |
* from a previous request, then this timeout duration has no effect. |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
213 |
* |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
214 |
* @param duration the duration to allow the underlying connection to be |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
215 |
* established |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
216 |
* @return this builder |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
217 |
* @throws IllegalArgumentException if the duration is non-positive |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
218 |
*/ |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
219 |
public Builder connectTimeout(Duration duration); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
220 |
|
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
221 |
/** |
48083 | 222 |
* Sets an {@code SSLContext}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
* |
48083 | 224 |
* <p> If this method is not invoked prior to {@linkplain #build() |
225 |
* building}, then newly built clients will use the {@linkplain |
|
226 |
* SSLContext#getDefault() default context}, which is normally adequate |
|
227 |
* for client applications that do not need to specify protocols, or |
|
228 |
* require client authentication. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
* @param sslContext the SSLContext |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
*/ |
49765 | 233 |
public Builder sslContext(SSLContext sslContext); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
/** |
48083 | 236 |
* Sets an {@code SSLParameters}. |
237 |
* |
|
238 |
* <p> If this method is not invoked prior to {@linkplain #build() |
|
239 |
* building}, then newly built clients will use a default, |
|
240 |
* implementation specific, set of parameters. |
|
241 |
* |
|
242 |
* <p> Some parameters which are used internally by the HTTP Client |
|
243 |
* implementation (such as the application protocol list) should not be |
|
244 |
* set by callers, as they may be ignored. The contents of the given |
|
245 |
* object are copied. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
* @param sslParameters the SSLParameters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
*/ |
49765 | 250 |
public Builder sslParameters(SSLParameters sslParameters); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
/** |
48083 | 253 |
* Sets the executor to be used for asynchronous and dependent tasks. |
254 |
* |
|
255 |
* <p> If this method is not invoked prior to {@linkplain #build() |
|
256 |
* building}, a default executor is created for each newly built {@code |
|
50681 | 257 |
* HttpClient}. |
48083 | 258 |
* |
50681 | 259 |
* @implNote The default executor uses a thread pool, with a custom |
260 |
* thread factory. If a security manager has been installed, the thread |
|
48083 | 261 |
* factory creates threads that run with an access control context that |
262 |
* has no permissions. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
* @param executor the Executor |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
*/ |
49765 | 267 |
public Builder executor(Executor executor); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
* Specifies whether requests will automatically follow redirects issued |
48083 | 271 |
* by the server. |
272 |
* |
|
273 |
* <p> If this method is not invoked prior to {@linkplain #build() |
|
274 |
* building}, then newly built clients will use a default redirection |
|
275 |
* policy of {@link Redirect#NEVER NEVER}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
* @param policy the redirection policy |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
278 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
*/ |
49765 | 280 |
public Builder followRedirects(Redirect policy); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
/** |
48083 | 283 |
* Requests a specific HTTP protocol version where possible. |
284 |
* |
|
285 |
* <p> If this method is not invoked prior to {@linkplain #build() |
|
286 |
* building}, then newly built clients will prefer {@linkplain |
|
287 |
* Version#HTTP_2 HTTP/2}. |
|
288 |
* |
|
289 |
* <p> If set to {@linkplain Version#HTTP_2 HTTP/2}, then each request |
|
290 |
* will attempt to upgrade to HTTP/2. If the upgrade succeeds, then the |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
* response to this request will use HTTP/2 and all subsequent requests |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
* and responses to the same |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
* <a href="https://tools.ietf.org/html/rfc6454#section-4">origin server</a> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
* will use HTTP/2. If the upgrade fails, then the response will be |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
* handled using HTTP/1.1 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
* |
49765 | 297 |
* @implNote Constraints may also affect the selection of protocol version. |
298 |
* For example, if HTTP/2 is requested through a proxy, and if the implementation |
|
299 |
* does not support this mode, then HTTP/1.1 may be used |
|
300 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
* @param version the requested HTTP protocol version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
*/ |
49765 | 304 |
public Builder version(HttpClient.Version version); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
* Sets the default priority for any HTTP/2 requests sent from this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
* client. The value provided must be between {@code 1} and {@code 256} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
* (inclusive). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
* @param priority the priority weighting |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
* @throws IllegalArgumentException if the given priority is out of range |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
*/ |
49765 | 315 |
public Builder priority(int priority); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
/** |
48083 | 318 |
* Sets a {@link java.net.ProxySelector}. |
319 |
* |
|
49765 | 320 |
* @apiNote {@link ProxySelector#of(InetSocketAddress) ProxySelector::of} |
48083 | 321 |
* provides a {@code ProxySelector} which uses a single proxy for all |
322 |
* requests. The system-wide proxy selector can be retrieved by |
|
323 |
* {@link ProxySelector#getDefault()}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
* |
48083 | 325 |
* @implNote |
49765 | 326 |
* If this method is not invoked prior to {@linkplain #build() building}, |
327 |
* then newly built clients will use the {@linkplain |
|
328 |
* ProxySelector#getDefault() default proxy selector}, which is usually |
|
329 |
* adequate for client applications. The default proxy selector supports |
|
52563 | 330 |
* a set of system properties related to |
49765 | 331 |
* <a href="{@docRoot}/java.base/java/net/doc-files/net-properties.html#Proxies"> |
332 |
* proxy settings</a>. This default behavior can be disabled by |
|
333 |
* supplying an explicit proxy selector, such as {@link #NO_PROXY} or |
|
334 |
* one returned by {@link ProxySelector#of(InetSocketAddress) |
|
335 |
* ProxySelector::of}, before {@linkplain #build() building}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
* |
49765 | 337 |
* @param proxySelector the ProxySelector |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
*/ |
49765 | 340 |
public Builder proxy(ProxySelector proxySelector); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
* Sets an authenticator to use for HTTP authentication. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
* |
49765 | 345 |
* @param authenticator the Authenticator |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
* @return this builder |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
*/ |
49765 | 348 |
public Builder authenticator(Authenticator authenticator); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
/** |
48083 | 351 |
* Returns a new {@link HttpClient} built from the current state of this |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
* builder. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
* |
49765 | 354 |
* @return a new {@code HttpClient} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
*/ |
49765 | 356 |
public HttpClient build(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
/** |
49765 | 361 |
* Returns an {@code Optional} containing this client's {@link |
48083 | 362 |
* CookieHandler}. If no {@code CookieHandler} was set in this client's |
363 |
* builder, then the {@code Optional} is empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
* |
48083 | 365 |
* @return an {@code Optional} containing this client's {@code CookieHandler} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
*/ |
48083 | 367 |
public abstract Optional<CookieHandler> cookieHandler(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
/** |
51364
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
370 |
* Returns an {@code Optional} containing the <i>connect timeout duration</i> |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
371 |
* for this client. If the {@linkplain Builder#connectTimeout(Duration) |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
372 |
* connect timeout duration} was not set in the client's builder, then the |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
373 |
* {@code Optional} is empty. |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
374 |
* |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
375 |
* @return an {@code Optional} containing this client's connect timeout |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
376 |
* duration |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
377 |
*/ |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
378 |
public abstract Optional<Duration> connectTimeout(); |
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
379 |
|
31d9e82b2e64
8208391: Differentiate response and connect timeouts in HTTP Client API
chegar
parents:
50681
diff
changeset
|
380 |
/** |
48083 | 381 |
* Returns the follow redirects policy for this client. The default value |
382 |
* for client's built by builders that do not specify a redirect policy is |
|
383 |
* {@link HttpClient.Redirect#NEVER NEVER}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
* @return this client's follow redirects setting |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
public abstract Redirect followRedirects(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
/** |
48083 | 390 |
* Returns an {@code Optional} containing the {@code ProxySelector} |
391 |
* supplied to this client. If no proxy selector was set in this client's |
|
392 |
* builder, then the {@code Optional} is empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
* |
48083 | 394 |
* <p> Even though this method may return an empty optional, the {@code |
49765 | 395 |
* HttpClient} may still have a non-exposed {@linkplain |
48083 | 396 |
* Builder#proxy(ProxySelector) default proxy selector} that is |
397 |
* used for sending HTTP requests. |
|
398 |
* |
|
399 |
* @return an {@code Optional} containing the proxy selector supplied |
|
400 |
* to this client. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
public abstract Optional<ProxySelector> proxy(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
/** |
48083 | 405 |
* Returns this client's {@code SSLContext}. |
406 |
* |
|
407 |
* <p> If no {@code SSLContext} was set in this client's builder, then the |
|
408 |
* {@linkplain SSLContext#getDefault() default context} is returned. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
410 |
* @return this client's SSLContext |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
412 |
public abstract SSLContext sslContext(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
413 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
414 |
/** |
48083 | 415 |
* Returns a copy of this client's {@link SSLParameters}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
416 |
* |
48083 | 417 |
* <p> If no {@code SSLParameters} were set in the client's builder, then an |
418 |
* implementation specific default set of parameters, that the client will |
|
419 |
* use, is returned. |
|
420 |
* |
|
421 |
* @return this client's {@code SSLParameters} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
*/ |
48083 | 423 |
public abstract SSLParameters sslParameters(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
424 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
425 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
* Returns an {@code Optional} containing the {@link Authenticator} set on |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
* this client. If no {@code Authenticator} was set in the client's builder, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
* then the {@code Optional} is empty. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
429 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
430 |
* @return an {@code Optional} containing this client's {@code Authenticator} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
432 |
public abstract Optional<Authenticator> authenticator(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
433 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
434 |
/** |
49765 | 435 |
* Returns the preferred HTTP protocol version for this client. The default |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
43302
diff
changeset
|
436 |
* value is {@link HttpClient.Version#HTTP_2} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
* |
49765 | 438 |
* @implNote Constraints may also affect the selection of protocol version. |
439 |
* For example, if HTTP/2 is requested through a proxy, and if the |
|
440 |
* implementation does not support this mode, then HTTP/1.1 may be used |
|
441 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
442 |
* @return the HTTP protocol version requested |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
443 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
444 |
public abstract HttpClient.Version version(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
445 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
446 |
/** |
49765 | 447 |
* Returns an {@code Optional} containing this client's {@link |
48083 | 448 |
* Executor}. If no {@code Executor} was set in the client's builder, |
449 |
* then the {@code Optional} is empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
* |
48083 | 451 |
* <p> Even though this method may return an empty optional, the {@code |
452 |
* HttpClient} may still have an non-exposed {@linkplain |
|
453 |
* HttpClient.Builder#executor(Executor) default executor} that is used for |
|
454 |
* executing asynchronous and dependent tasks. |
|
455 |
* |
|
456 |
* @return an {@code Optional} containing this client's {@code Executor} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
*/ |
48083 | 458 |
public abstract Optional<Executor> executor(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
459 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
* The HTTP protocol version. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
462 |
* |
49765 | 463 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
465 |
public enum Version { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
466 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
467 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
468 |
* HTTP version 1.1 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
469 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
HTTP_1_1, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
* HTTP version 2 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
HTTP_2 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
477 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
/** |
49765 | 479 |
* Defines the automatic redirection policy. |
480 |
* |
|
481 |
* <p> The automatic redirection policy is checked whenever a {@code 3XX} |
|
482 |
* response code is received. If redirection does not happen automatically, |
|
483 |
* then the response, containing the {@code 3XX} response code, is returned, |
|
484 |
* where it can be handled manually. |
|
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
485 |
* |
50681 | 486 |
* <p> {@code Redirect} policy is set through the {@linkplain |
49765 | 487 |
* HttpClient.Builder#followRedirects(Redirect) Builder.followRedirects} |
488 |
* method. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
489 |
* |
49765 | 490 |
* @implNote When automatic redirection occurs, the request method of the |
491 |
* redirected request may be modified depending on the specific {@code 30X} |
|
492 |
* status code, as specified in <a href="https://tools.ietf.org/html/rfc7231"> |
|
493 |
* RFC 7231</a>. In addition, the {@code 301} and {@code 302} status codes |
|
494 |
* cause a {@code POST} request to be converted to a {@code GET} in the |
|
495 |
* redirected request. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
496 |
* |
49765 | 497 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
498 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
public enum Redirect { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
500 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
501 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
* Never redirect. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
504 |
NEVER, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
505 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
506 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
507 |
* Always redirect. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
508 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
509 |
ALWAYS, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
/** |
49765 | 512 |
* Always redirect, except from HTTPS URLs to HTTP URLs. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
513 |
*/ |
49765 | 514 |
NORMAL |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
515 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
516 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
517 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
518 |
* Sends the given request using this client, blocking if necessary to get |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
519 |
* the response. The returned {@link HttpResponse}{@code <T>} contains the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
520 |
* response status, headers, and body ( as handled by given response body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
521 |
* handler ). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
522 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
523 |
* @param <T> the response body type |
49765 | 524 |
* @param request the request |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
525 |
* @param responseBodyHandler the response body handler |
49765 | 526 |
* @return the response |
48083 | 527 |
* @throws IOException if an I/O error occurs when sending or receiving |
528 |
* @throws InterruptedException if the operation is interrupted |
|
49765 | 529 |
* @throws IllegalArgumentException if the {@code request} argument is not |
530 |
* a request that could have been validly built as specified by {@link |
|
531 |
* HttpRequest.Builder HttpRequest.Builder}. |
|
48083 | 532 |
* @throws SecurityException If a security manager has been installed |
533 |
* and it denies {@link java.net.URLPermission access} to the |
|
534 |
* URL in the given request, or proxy if one is configured. |
|
49765 | 535 |
* See <a href="#securitychecks">security checks</a> for further |
536 |
* information. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
538 |
public abstract <T> HttpResponse<T> |
49765 | 539 |
send(HttpRequest request, HttpResponse.BodyHandler<T> responseBodyHandler) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
throws IOException, InterruptedException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
541 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
542 |
/** |
49765 | 543 |
* Sends the given request asynchronously using this client with the given |
544 |
* response body handler. |
|
545 |
* |
|
546 |
* <p> Equivalent to: {@code sendAsync(request, responseBodyHandler, null)}. |
|
547 |
* |
|
548 |
* @param <T> the response body type |
|
549 |
* @param request the request |
|
550 |
* @param responseBodyHandler the response body handler |
|
551 |
* @return a {@code CompletableFuture<HttpResponse<T>>} |
|
552 |
* @throws IllegalArgumentException if the {@code request} argument is not |
|
553 |
* a request that could have been validly built as specified by {@link |
|
554 |
* HttpRequest.Builder HttpRequest.Builder}. |
|
555 |
*/ |
|
556 |
public abstract <T> CompletableFuture<HttpResponse<T>> |
|
557 |
sendAsync(HttpRequest request, |
|
558 |
BodyHandler<T> responseBodyHandler); |
|
559 |
||
560 |
/** |
|
561 |
* Sends the given request asynchronously using this client with the given |
|
562 |
* response body handler and push promise handler. |
|
563 |
* |
|
564 |
* <p> The returned completable future, if completed successfully, completes |
|
565 |
* with an {@link HttpResponse}{@code <T>} that contains the response status, |
|
566 |
* headers, and body ( as handled by given response body handler ). |
|
567 |
* |
|
568 |
* <p> {@linkplain PushPromiseHandler Push promises} received, if any, are |
|
569 |
* handled by the given {@code pushPromiseHandler}. A {@code null} valued |
|
570 |
* {@code pushPromiseHandler} rejects any push promises. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
571 |
* |
48083 | 572 |
* <p> The returned completable future completes exceptionally with: |
573 |
* <ul> |
|
574 |
* <li>{@link IOException} - if an I/O error occurs when sending or receiving</li> |
|
575 |
* <li>{@link SecurityException} - If a security manager has been installed |
|
576 |
* and it denies {@link java.net.URLPermission access} to the |
|
577 |
* URL in the given request, or proxy if one is configured. |
|
49765 | 578 |
* See <a href="#securitychecks">security checks</a> for further |
579 |
* information.</li> |
|
48083 | 580 |
* </ul> |
581 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
582 |
* @param <T> the response body type |
49765 | 583 |
* @param request the request |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
584 |
* @param responseBodyHandler the response body handler |
49765 | 585 |
* @param pushPromiseHandler push promise handler, may be null |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
586 |
* @return a {@code CompletableFuture<HttpResponse<T>>} |
49765 | 587 |
* @throws IllegalArgumentException if the {@code request} argument is not |
588 |
* a request that could have been validly built as specified by {@link |
|
589 |
* HttpRequest.Builder HttpRequest.Builder}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
591 |
public abstract <T> CompletableFuture<HttpResponse<T>> |
49765 | 592 |
sendAsync(HttpRequest request, |
593 |
BodyHandler<T> responseBodyHandler, |
|
594 |
PushPromiseHandler<T> pushPromiseHandler); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
595 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
596 |
/** |
48083 | 597 |
* Creates a new {@code WebSocket} builder (optional operation). |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
598 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
599 |
* <p> <b>Example</b> |
49765 | 600 |
* <pre>{@code HttpClient client = HttpClient.newHttpClient(); |
601 |
* CompletableFuture<WebSocket> ws = client.newWebSocketBuilder() |
|
602 |
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }</pre> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
603 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
604 |
* <p> Finer control over the WebSocket Opening Handshake can be achieved |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
605 |
* by using a custom {@code HttpClient}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
606 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
607 |
* <p> <b>Example</b> |
49765 | 608 |
* <pre>{@code InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80); |
609 |
* HttpClient client = HttpClient.newBuilder() |
|
610 |
* .proxy(ProxySelector.of(addr)) |
|
611 |
* .build(); |
|
612 |
* CompletableFuture<WebSocket> ws = client.newWebSocketBuilder() |
|
613 |
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }</pre> |
|
48083 | 614 |
* |
615 |
* @implSpec The default implementation of this method throws |
|
616 |
* {@code UnsupportedOperationException}. Clients obtained through |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
617 |
* {@link HttpClient#newHttpClient()} or {@link HttpClient#newBuilder()} |
48083 | 618 |
* return a {@code WebSocket} builder. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
619 |
* |
48083 | 620 |
* @implNote Both builder and {@code WebSocket}s created with it operate in |
621 |
* a non-blocking fashion. That is, their methods do not block before |
|
622 |
* returning a {@code CompletableFuture}. Asynchronous tasks are executed in |
|
623 |
* this {@code HttpClient}'s executor. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
624 |
* |
48083 | 625 |
* <p> When a {@code CompletionStage} returned from |
626 |
* {@link WebSocket.Listener#onClose Listener.onClose} completes, |
|
627 |
* the {@code WebSocket} will send a Close message that has the same code |
|
628 |
* the received message has and an empty reason. |
|
629 |
* |
|
630 |
* @return a {@code WebSocket.Builder} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
631 |
* @throws UnsupportedOperationException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
632 |
* if this {@code HttpClient} does not provide WebSocket support |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
633 |
*/ |
48083 | 634 |
public WebSocket.Builder newWebSocketBuilder() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
throw new UnsupportedOperationException(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
636 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
637 |
} |