author | dfuchs |
Thu, 16 Nov 2017 19:56:44 +0000 | |
branch | http-client-branch |
changeset 55821 | fa0fc03c0853 |
parent 55764 | 34d7cc00f87a |
child 55824 | b922df193260 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http.internal.websocket; |
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 jdk.incubator.http.HttpClient; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import jdk.incubator.http.WebSocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import jdk.incubator.http.WebSocket.Builder; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import jdk.incubator.http.WebSocket.Listener; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import jdk.incubator.http.internal.common.Pair; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
|
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
34 |
import java.net.ProxySelector; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.time.Duration; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.Collection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.List; |
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
40 |
import java.util.Optional; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
43 |
import static java.lang.String.format; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import static java.util.Objects.requireNonNull; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import static jdk.incubator.http.internal.common.Pair.pair; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
public final class BuilderImpl implements Builder { |
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 |
private final HttpClient client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
private final Listener listener; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
52 |
private final Collection<Pair<String, String>> headers = new LinkedList<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
private final Collection<String> subprotocols = new LinkedList<>(); |
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
54 |
private final Optional<ProxySelector> proxySelector; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
private Duration timeout; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
57 |
public BuilderImpl(HttpClient client, URI uri, Listener listener, ProxySelector proxySelector) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
this.client = requireNonNull(client, "client"); |
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
59 |
this.uri = checkURI(requireNonNull(uri, "uri")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
this.listener = requireNonNull(listener, "listener"); |
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
61 |
this.proxySelector = Optional.ofNullable(proxySelector); |
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
62 |
// if the proxy selector was supplied by the user, it should be present |
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
63 |
// on the client and should be the same than what we get as argument. |
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
64 |
assert !client.proxy().isPresent() || client.proxy().get() == proxySelector; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
55764
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
67 |
private static IllegalArgumentException newIAE(String message, Object... args) { |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
68 |
return new IllegalArgumentException(format(message, args)); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
69 |
} |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
70 |
|
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
71 |
private static URI checkURI(URI uri) { |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
72 |
String scheme = uri.getScheme(); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
73 |
if (scheme == null) |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
74 |
throw newIAE("URI with undefined scheme"); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
75 |
scheme = scheme.toLowerCase(); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
76 |
if (!(scheme.equals("ws") || scheme.equals("wss"))) |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
77 |
throw newIAE("invalid URI scheme %s", scheme); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
78 |
if (uri.getHost() == null) |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
79 |
throw newIAE("URI must contain a host: %s", uri); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
80 |
if (uri.getFragment() != null) |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
81 |
throw newIAE("URI must not contain a fragment: %s", uri); |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
82 |
return uri; |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
83 |
} |
34d7cc00f87a
http-client-branch: WebSocket permission checks, test updates, and more
chegar
parents:
47216
diff
changeset
|
84 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
public Builder header(String name, String value) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
requireNonNull(name, "name"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
requireNonNull(value, "value"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
headers.add(pair(name, value)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
return this; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
@Override |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
94 |
public Builder subprotocols(String mostPreferred, |
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
95 |
String... lesserPreferred) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
requireNonNull(mostPreferred, "mostPreferred"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
requireNonNull(lesserPreferred, "lesserPreferred"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
List<String> subprotocols = new LinkedList<>(); |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
100 |
subprotocols.add(mostPreferred); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
for (int i = 0; i < lesserPreferred.length; i++) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
String p = lesserPreferred[i]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
requireNonNull(p, "lesserPreferred[" + i + "]"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
subprotocols.add(p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
this.subprotocols.clear(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
this.subprotocols.addAll(subprotocols); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
return this; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
public Builder connectTimeout(Duration timeout) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
this.timeout = requireNonNull(timeout, "timeout"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
return this; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
public CompletableFuture<WebSocket> buildAsync() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
return WebSocketImpl.newInstanceAsync(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
HttpClient getClient() { return client; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
URI getUri() { return uri; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
Listener getListener() { return listener; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
|
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
42460
diff
changeset
|
128 |
Collection<Pair<String, String>> getHeaders() { return headers; } |
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 |
Collection<String> getSubprotocols() { return subprotocols; } |
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 |
Duration getConnectTimeout() { return timeout; } |
55821
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
133 |
|
fa0fc03c0853
http-client-branch: HttpClient uses ProxySelector.getDefault() by default
dfuchs
parents:
55764
diff
changeset
|
134 |
Optional<ProxySelector> proxySelector() { return proxySelector; } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
} |