src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpRequestBuilderImpl.java
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 55764 34d7cc00f87a
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package jdk.incubator.http;
    26 package jdk.incubator.http;
    27 
    27 
    28 import java.net.URI;
    28 import java.net.URI;
    29 import jdk.incubator.http.HttpRequest.BodyProcessor;
       
    30 import java.time.Duration;
    29 import java.time.Duration;
    31 import java.util.Optional;
    30 import java.util.Optional;
       
    31 import jdk.incubator.http.HttpRequest.BodyPublisher;
       
    32 import jdk.incubator.http.internal.common.HttpHeadersImpl;
       
    33 import static java.lang.String.format;
    32 import static java.util.Objects.requireNonNull;
    34 import static java.util.Objects.requireNonNull;
    33 import jdk.incubator.http.internal.common.HttpHeadersImpl;
       
    34 import static jdk.incubator.http.internal.common.Utils.isValidName;
    35 import static jdk.incubator.http.internal.common.Utils.isValidName;
    35 import static jdk.incubator.http.internal.common.Utils.isValidValue;
    36 import static jdk.incubator.http.internal.common.Utils.isValidValue;
    36 
    37 
    37 class HttpRequestBuilderImpl extends HttpRequest.Builder {
    38 class HttpRequestBuilderImpl extends HttpRequest.Builder {
    38 
    39 
    39     private HttpHeadersImpl userHeaders;
    40     private HttpHeadersImpl userHeaders;
    40     private URI uri;
    41     private URI uri;
    41     private String method;
    42     private String method;
    42     //private HttpClient.Redirect followRedirects;
       
    43     private boolean expectContinue;
    43     private boolean expectContinue;
    44     private HttpRequest.BodyProcessor body;
    44     private BodyPublisher bodyPublisher;
    45     private volatile Optional<HttpClient.Version> version;
    45     private volatile Optional<HttpClient.Version> version;
    46     //private final HttpClientImpl client;
       
    47     //private ProxySelector proxy;
       
    48     private Duration duration;
    46     private Duration duration;
    49 
    47 
    50     public HttpRequestBuilderImpl(URI uri) {
    48     public HttpRequestBuilderImpl(URI uri) {
    51         //this.client = client;
    49         requireNonNull(uri, "uri must be non-null");
    52         checkURI(uri);
    50         checkURI(uri);
    53         this.uri = uri;
    51         this.uri = uri;
    54         this.userHeaders = new HttpHeadersImpl();
    52         this.userHeaders = new HttpHeadersImpl();
    55         this.method = "GET"; // default, as per spec
    53         this.method = "GET"; // default, as per spec
    56         this.version = Optional.empty();
    54         this.version = Optional.empty();
    57     }
    55     }
    58 
    56 
    59     public HttpRequestBuilderImpl() {
    57     public HttpRequestBuilderImpl() {
    60         this.userHeaders = new HttpHeadersImpl();
    58         this.userHeaders = new HttpHeadersImpl();
       
    59         this.method = "GET"; // default, as per spec
    61         this.version = Optional.empty();
    60         this.version = Optional.empty();
    62     }
    61     }
    63 
    62 
    64     @Override
    63     @Override
    65     public HttpRequestBuilderImpl uri(URI uri) {
    64     public HttpRequestBuilderImpl uri(URI uri) {
    66         requireNonNull(uri);
    65         requireNonNull(uri, "uri must be non-null");
    67         checkURI(uri);
    66         checkURI(uri);
    68         this.uri = uri;
    67         this.uri = uri;
    69         return this;
    68         return this;
    70     }
    69     }
    71 
    70 
       
    71     private static IllegalArgumentException newIAE(String message, Object... args) {
       
    72         return new IllegalArgumentException(format(message, args));
       
    73     }
       
    74 
    72     private static void checkURI(URI uri) {
    75     private static void checkURI(URI uri) {
    73         String scheme = uri.getScheme().toLowerCase();
    76         String scheme = uri.getScheme();
    74         if (!scheme.equals("https") && !scheme.equals("http")) {
    77         if (scheme == null)
    75             throw new IllegalArgumentException("invalid URI scheme");
    78             throw newIAE("URI with undefined scheme");
    76         }
    79         scheme = scheme.toLowerCase();
    77     }
    80         if (!(scheme.equals("https") || scheme.equals("http"))) {
    78 /*
    81             throw newIAE("invalid URI scheme %s", scheme);
    79     @Override
    82         }
    80     public HttpRequestBuilderImpl followRedirects(HttpClient.Redirect follow) {
    83         if (uri.getHost() == null) {
    81         requireNonNull(follow);
    84             throw newIAE("unsupported URI %s", uri);
    82         this.followRedirects = follow;
    85         }
    83         return this;
    86     }
    84     }
    87 
    85 */
    88     @Override
       
    89     public HttpRequestBuilderImpl copy() {
       
    90         HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
       
    91         b.userHeaders = this.userHeaders.deepCopy();
       
    92         b.method = this.method;
       
    93         b.expectContinue = this.expectContinue;
       
    94         b.bodyPublisher = bodyPublisher;
       
    95         b.uri = uri;
       
    96         b.duration = duration;
       
    97         b.version = version;
       
    98         return b;
       
    99     }
       
   100 
       
   101     private void checkNameAndValue(String name, String value) {
       
   102         requireNonNull(name, "name");
       
   103         requireNonNull(value, "value");
       
   104         if (!isValidName(name)) {
       
   105             throw newIAE("invalid header name:", name);
       
   106         }
       
   107         if (!isValidValue(value)) {
       
   108             throw newIAE("invalid header value:%s", value);
       
   109         }
       
   110     }
       
   111 
       
   112     @Override
       
   113     public HttpRequestBuilderImpl setHeader(String name, String value) {
       
   114         checkNameAndValue(name, value);
       
   115         userHeaders.setHeader(name, value);
       
   116         return this;
       
   117     }
       
   118 
    86     @Override
   119     @Override
    87     public HttpRequestBuilderImpl header(String name, String value) {
   120     public HttpRequestBuilderImpl header(String name, String value) {
    88         checkNameAndValue(name, value);
   121         checkNameAndValue(name, value);
    89         userHeaders.addHeader(name, value);
   122         userHeaders.addHeader(name, value);
    90         return this;
   123         return this;
    91     }
   124     }
    92 
   125 
    93     @Override
   126     @Override
    94     public HttpRequestBuilderImpl headers(String... params) {
   127     public HttpRequestBuilderImpl headers(String... params) {
    95         requireNonNull(params);
   128         requireNonNull(params);
    96         if (params.length % 2 != 0) {
   129         if (params.length == 0 || params.length % 2 != 0) {
    97             throw new IllegalArgumentException("wrong number of parameters");
   130             throw newIAE("wrong number, %d, of parameters", params.length);
    98         }
   131         }
    99         for (int i = 0; i < params.length; i += 2) {
   132         for (int i = 0; i < params.length; i += 2) {
   100             String name  = params[i];
   133             String name  = params[i];
   101             String value = params[i + 1];
   134             String value = params[i + 1];
   102             header(name, value);
   135             header(name, value);
   103         }
   136         }
   104         return this;
   137         return this;
   105     }
   138     }
   106 
   139 
   107     /*
       
   108     @Override
       
   109     public HttpRequestBuilderImpl proxy(ProxySelector proxy) {
       
   110         requireNonNull(proxy);
       
   111         this.proxy = proxy;
       
   112         return this;
       
   113     }
       
   114 */
       
   115     @Override
       
   116     public HttpRequestBuilderImpl copy() {
       
   117         HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
       
   118         b.userHeaders = this.userHeaders.deepCopy();
       
   119         b.method = this.method;
       
   120         //b.followRedirects = this.followRedirects;
       
   121         b.expectContinue = this.expectContinue;
       
   122         b.body = body;
       
   123         b.uri = uri;
       
   124         //b.proxy = proxy;
       
   125         return b;
       
   126     }
       
   127 
       
   128     @Override
       
   129     public HttpRequestBuilderImpl setHeader(String name, String value) {
       
   130         checkNameAndValue(name, value);
       
   131         userHeaders.setHeader(name, value);
       
   132         return this;
       
   133     }
       
   134 
       
   135     private void checkNameAndValue(String name, String value) {
       
   136         requireNonNull(name, "name");
       
   137         requireNonNull(value, "value");
       
   138         if (!isValidName(name)) {
       
   139             throw new IllegalArgumentException("invalid header name");
       
   140         }
       
   141         if (!isValidValue(value)) {
       
   142             throw new IllegalArgumentException("invalid header value");
       
   143         }
       
   144     }
       
   145 
       
   146     @Override
   140     @Override
   147     public HttpRequestBuilderImpl expectContinue(boolean enable) {
   141     public HttpRequestBuilderImpl expectContinue(boolean enable) {
   148         expectContinue = enable;
   142         expectContinue = enable;
   149         return this;
   143         return this;
   150     }
   144     }
   156         return this;
   150         return this;
   157     }
   151     }
   158 
   152 
   159     HttpHeadersImpl headers() {  return userHeaders; }
   153     HttpHeadersImpl headers() {  return userHeaders; }
   160 
   154 
   161     //HttpClientImpl client() {return client;}
       
   162 
       
   163     URI uri() { return uri; }
   155     URI uri() { return uri; }
   164 
   156 
   165     String method() { return method; }
   157     String method() { return method; }
   166 
   158 
   167     //HttpClient.Redirect followRedirects() { return followRedirects; }
       
   168 
       
   169     //ProxySelector proxy() { return proxy; }
       
   170 
       
   171     boolean expectContinue() { return expectContinue; }
   159     boolean expectContinue() { return expectContinue; }
   172 
   160 
   173     public HttpRequest.BodyProcessor body() { return body; }
   161     BodyPublisher bodyPublisher() { return bodyPublisher; }
   174 
   162 
   175     Optional<HttpClient.Version> version() { return version; }
   163     Optional<HttpClient.Version> version() { return version; }
   176 
   164 
   177     @Override
   165     @Override
   178     public HttpRequest.Builder GET() { return method("GET", null); }
   166     public HttpRequest.Builder GET() {
   179 
   167         return method0("GET", null);
   180     @Override
   168     }
   181     public HttpRequest.Builder POST(BodyProcessor body) {
   169 
   182         return method("POST", body);
   170     @Override
   183     }
   171     public HttpRequest.Builder POST(BodyPublisher body) {
   184 
   172         return method0("POST", requireNonNull(body));
   185     @Override
   173     }
   186     public HttpRequest.Builder DELETE(BodyProcessor body) {
   174 
   187         return method("DELETE", body);
   175     @Override
   188     }
   176     public HttpRequest.Builder DELETE(BodyPublisher body) {
   189 
   177         return method0("DELETE", requireNonNull(body));
   190     @Override
   178     }
   191     public HttpRequest.Builder PUT(BodyProcessor body) {
   179 
   192         return method("PUT", body);
   180     @Override
   193     }
   181     public HttpRequest.Builder PUT(BodyPublisher body) {
   194 
   182         return method0("PUT", requireNonNull(body));
   195     @Override
   183     }
   196     public HttpRequest.Builder method(String method, BodyProcessor body) {
   184 
   197         this.method = requireNonNull(method);
   185     @Override
   198         this.body = body;
   186     public HttpRequest.Builder method(String method, BodyPublisher body) {
       
   187         requireNonNull(method);
       
   188         if (method.equals(""))
       
   189             throw newIAE("illegal method <empty string>");
       
   190         return method0(method, requireNonNull(body));
       
   191     }
       
   192 
       
   193     private HttpRequest.Builder method0(String method, BodyPublisher body) {
       
   194         assert method != null;
       
   195         assert !method.equals("GET") ? body != null : true;
       
   196         assert !method.equals("");
       
   197         this.method = method;
       
   198         this.bodyPublisher = body;
   199         return this;
   199         return this;
   200     }
   200     }
   201 
   201 
   202     @Override
   202     @Override
   203     public HttpRequest build() {
   203     public HttpRequest build() {
       
   204         if (uri == null)
       
   205             throw new IllegalStateException("uri is null");
       
   206         assert method != null;
   204         return new HttpRequestImpl(this);
   207         return new HttpRequestImpl(this);
   205     }
   208     }
   206 
   209 
   207     @Override
   210     @Override
   208     public HttpRequest.Builder timeout(Duration duration) {
   211     public HttpRequest.Builder timeout(Duration duration) {
   211             throw new IllegalArgumentException("Invalid duration: " + duration);
   214             throw new IllegalArgumentException("Invalid duration: " + duration);
   212         this.duration = duration;
   215         this.duration = duration;
   213         return this;
   216         return this;
   214     }
   217     }
   215 
   218 
   216     Duration duration() { return duration; }
   219     Duration timeout() { return duration; }
   217 
   220 
   218 }
   221 }