36 import java.util.Collection; |
36 import java.util.Collection; |
37 import java.util.LinkedList; |
37 import java.util.LinkedList; |
38 import java.util.List; |
38 import java.util.List; |
39 import java.util.concurrent.CompletableFuture; |
39 import java.util.concurrent.CompletableFuture; |
40 |
40 |
|
41 import static java.lang.String.format; |
41 import static java.util.Objects.requireNonNull; |
42 import static java.util.Objects.requireNonNull; |
42 import static jdk.incubator.http.internal.common.Pair.pair; |
43 import static jdk.incubator.http.internal.common.Pair.pair; |
43 |
44 |
44 public final class BuilderImpl implements Builder { |
45 public final class BuilderImpl implements Builder { |
45 |
46 |
50 private final Collection<String> subprotocols = new LinkedList<>(); |
51 private final Collection<String> subprotocols = new LinkedList<>(); |
51 private Duration timeout; |
52 private Duration timeout; |
52 |
53 |
53 public BuilderImpl(HttpClient client, URI uri, Listener listener) { |
54 public BuilderImpl(HttpClient client, URI uri, Listener listener) { |
54 this.client = requireNonNull(client, "client"); |
55 this.client = requireNonNull(client, "client"); |
55 this.uri = requireNonNull(uri, "uri"); |
56 this.uri = checkURI(requireNonNull(uri, "uri")); |
56 this.listener = requireNonNull(listener, "listener"); |
57 this.listener = requireNonNull(listener, "listener"); |
|
58 } |
|
59 |
|
60 private static IllegalArgumentException newIAE(String message, Object... args) { |
|
61 return new IllegalArgumentException(format(message, args)); |
|
62 } |
|
63 |
|
64 private static URI checkURI(URI uri) { |
|
65 String scheme = uri.getScheme(); |
|
66 if (scheme == null) |
|
67 throw newIAE("URI with undefined scheme"); |
|
68 scheme = scheme.toLowerCase(); |
|
69 if (!(scheme.equals("ws") || scheme.equals("wss"))) |
|
70 throw newIAE("invalid URI scheme %s", scheme); |
|
71 if (uri.getHost() == null) |
|
72 throw newIAE("URI must contain a host: %s", uri); |
|
73 if (uri.getFragment() != null) |
|
74 throw newIAE("URI must not contain a fragment: %s", uri); |
|
75 return uri; |
57 } |
76 } |
58 |
77 |
59 @Override |
78 @Override |
60 public Builder header(String name, String value) { |
79 public Builder header(String name, String value) { |
61 requireNonNull(name, "name"); |
80 requireNonNull(name, "name"); |