src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpRequestBuilderImpl.java
branchhttp-client-branch
changeset 56079 d23b02f37fce
parent 56078 6c11b48a0695
child 56080 64846522c0d5
equal deleted inserted replaced
56078:6c11b48a0695 56079:d23b02f37fce
     1 /*
       
     2  * Copyright (c) 2015, 2017, 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 jdk.incubator.http;
       
    27 
       
    28 import java.net.URI;
       
    29 import java.time.Duration;
       
    30 import java.util.Optional;
       
    31 import jdk.incubator.http.HttpRequest.BodyPublisher;
       
    32 import jdk.incubator.http.internal.common.HttpHeadersImpl;
       
    33 import jdk.incubator.http.internal.common.Utils;
       
    34 
       
    35 import static java.lang.String.format;
       
    36 import static java.util.Objects.requireNonNull;
       
    37 import static jdk.incubator.http.internal.common.Utils.isValidName;
       
    38 import static jdk.incubator.http.internal.common.Utils.isValidValue;
       
    39 
       
    40 class HttpRequestBuilderImpl extends HttpRequest.Builder {
       
    41 
       
    42     private HttpHeadersImpl userHeaders;
       
    43     private URI uri;
       
    44     private String method;
       
    45     private boolean expectContinue;
       
    46     private BodyPublisher bodyPublisher;
       
    47     private volatile Optional<HttpClient.Version> version;
       
    48     private Duration duration;
       
    49 
       
    50     public HttpRequestBuilderImpl(URI uri) {
       
    51         requireNonNull(uri, "uri must be non-null");
       
    52         checkURI(uri);
       
    53         this.uri = uri;
       
    54         this.userHeaders = new HttpHeadersImpl();
       
    55         this.method = "GET"; // default, as per spec
       
    56         this.version = Optional.empty();
       
    57     }
       
    58 
       
    59     public HttpRequestBuilderImpl() {
       
    60         this.userHeaders = new HttpHeadersImpl();
       
    61         this.method = "GET"; // default, as per spec
       
    62         this.version = Optional.empty();
       
    63     }
       
    64 
       
    65     @Override
       
    66     public HttpRequestBuilderImpl uri(URI uri) {
       
    67         requireNonNull(uri, "uri must be non-null");
       
    68         checkURI(uri);
       
    69         this.uri = uri;
       
    70         return this;
       
    71     }
       
    72 
       
    73     private static IllegalArgumentException newIAE(String message, Object... args) {
       
    74         return new IllegalArgumentException(format(message, args));
       
    75     }
       
    76 
       
    77     private static void checkURI(URI uri) {
       
    78         String scheme = uri.getScheme();
       
    79         if (scheme == null)
       
    80             throw newIAE("URI with undefined scheme");
       
    81         scheme = scheme.toLowerCase();
       
    82         if (!(scheme.equals("https") || scheme.equals("http"))) {
       
    83             throw newIAE("invalid URI scheme %s", scheme);
       
    84         }
       
    85         if (uri.getHost() == null) {
       
    86             throw newIAE("unsupported URI %s", uri);
       
    87         }
       
    88     }
       
    89 
       
    90     @Override
       
    91     public HttpRequestBuilderImpl copy() {
       
    92         HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
       
    93         b.userHeaders = this.userHeaders.deepCopy();
       
    94         b.method = this.method;
       
    95         b.expectContinue = this.expectContinue;
       
    96         b.bodyPublisher = bodyPublisher;
       
    97         b.uri = uri;
       
    98         b.duration = duration;
       
    99         b.version = version;
       
   100         return b;
       
   101     }
       
   102 
       
   103     private void checkNameAndValue(String name, String value) {
       
   104         requireNonNull(name, "name");
       
   105         requireNonNull(value, "value");
       
   106         if (!isValidName(name)) {
       
   107             throw newIAE("invalid header name: \"%s\"", name);
       
   108         }
       
   109         if (!Utils.ALLOWED_HEADERS.test(name)) {
       
   110             throw newIAE("restricted header name: \"%s\"", name);
       
   111         }
       
   112         if (!isValidValue(value)) {
       
   113             throw newIAE("invalid header value: \"%s\"", value);
       
   114         }
       
   115     }
       
   116 
       
   117     @Override
       
   118     public HttpRequestBuilderImpl setHeader(String name, String value) {
       
   119         checkNameAndValue(name, value);
       
   120         userHeaders.setHeader(name, value);
       
   121         return this;
       
   122     }
       
   123 
       
   124     @Override
       
   125     public HttpRequestBuilderImpl header(String name, String value) {
       
   126         checkNameAndValue(name, value);
       
   127         userHeaders.addHeader(name, value);
       
   128         return this;
       
   129     }
       
   130 
       
   131     @Override
       
   132     public HttpRequestBuilderImpl headers(String... params) {
       
   133         requireNonNull(params);
       
   134         if (params.length == 0 || params.length % 2 != 0) {
       
   135             throw newIAE("wrong number, %d, of parameters", params.length);
       
   136         }
       
   137         for (int i = 0; i < params.length; i += 2) {
       
   138             String name  = params[i];
       
   139             String value = params[i + 1];
       
   140             header(name, value);
       
   141         }
       
   142         return this;
       
   143     }
       
   144 
       
   145     @Override
       
   146     public HttpRequestBuilderImpl expectContinue(boolean enable) {
       
   147         expectContinue = enable;
       
   148         return this;
       
   149     }
       
   150 
       
   151     @Override
       
   152     public HttpRequestBuilderImpl version(HttpClient.Version version) {
       
   153         requireNonNull(version);
       
   154         this.version = Optional.of(version);
       
   155         return this;
       
   156     }
       
   157 
       
   158     HttpHeadersImpl headers() {  return userHeaders; }
       
   159 
       
   160     URI uri() { return uri; }
       
   161 
       
   162     String method() { return method; }
       
   163 
       
   164     boolean expectContinue() { return expectContinue; }
       
   165 
       
   166     BodyPublisher bodyPublisher() { return bodyPublisher; }
       
   167 
       
   168     Optional<HttpClient.Version> version() { return version; }
       
   169 
       
   170     @Override
       
   171     public HttpRequest.Builder GET() {
       
   172         return method0("GET", null);
       
   173     }
       
   174 
       
   175     @Override
       
   176     public HttpRequest.Builder POST(BodyPublisher body) {
       
   177         return method0("POST", requireNonNull(body));
       
   178     }
       
   179 
       
   180     @Override
       
   181     public HttpRequest.Builder DELETE(BodyPublisher body) {
       
   182         return method0("DELETE", requireNonNull(body));
       
   183     }
       
   184 
       
   185     @Override
       
   186     public HttpRequest.Builder PUT(BodyPublisher body) {
       
   187         return method0("PUT", requireNonNull(body));
       
   188     }
       
   189 
       
   190     @Override
       
   191     public HttpRequest.Builder method(String method, BodyPublisher body) {
       
   192         requireNonNull(method);
       
   193         if (method.equals(""))
       
   194             throw newIAE("illegal method <empty string>");
       
   195         if (method.equals("CONNECT"))
       
   196             throw newIAE("method CONNECT is not supported");
       
   197         return method0(method, requireNonNull(body));
       
   198     }
       
   199 
       
   200     private HttpRequest.Builder method0(String method, BodyPublisher body) {
       
   201         assert method != null;
       
   202         assert !method.equals("GET") ? body != null : true;
       
   203         assert !method.equals("");
       
   204         this.method = method;
       
   205         this.bodyPublisher = body;
       
   206         return this;
       
   207     }
       
   208 
       
   209     @Override
       
   210     public HttpRequest build() {
       
   211         if (uri == null)
       
   212             throw new IllegalStateException("uri is null");
       
   213         assert method != null;
       
   214         return new HttpRequestImpl(this);
       
   215     }
       
   216 
       
   217     @Override
       
   218     public HttpRequest.Builder timeout(Duration duration) {
       
   219         requireNonNull(duration);
       
   220         if (duration.isNegative() || Duration.ZERO.equals(duration))
       
   221             throw new IllegalArgumentException("Invalid duration: " + duration);
       
   222         this.duration = duration;
       
   223         return this;
       
   224     }
       
   225 
       
   226     Duration timeout() { return duration; }
       
   227 
       
   228 }