< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/HttpRequestBuilderImpl.java

Print this page


   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 static java.lang.String.format;
  34 import static java.util.Objects.requireNonNull;
  35 import static jdk.incubator.http.internal.common.Utils.isValidName;
  36 import static jdk.incubator.http.internal.common.Utils.isValidValue;
  37 
  38 class HttpRequestBuilderImpl extends HttpRequest.Builder {
  39 
  40     private HttpHeadersImpl userHeaders;
  41     private URI uri;
  42     private String method;
  43     private boolean expectContinue;
  44     private BodyPublisher bodyPublisher;
  45     private volatile Optional<HttpClient.Version> version;
  46     private Duration duration;
  47 
  48     public HttpRequestBuilderImpl(URI uri) {
  49         requireNonNull(uri, "uri must be non-null");
  50         checkURI(uri);
  51         this.uri = uri;
  52         this.userHeaders = new HttpHeadersImpl();
  53         this.method = "GET"; // default, as per spec
  54         this.version = Optional.empty();
  55     }
  56 
  57     public HttpRequestBuilderImpl() {
  58         this.userHeaders = new HttpHeadersImpl();


  85         }
  86     }
  87 
  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 
 119     @Override
 120     public HttpRequestBuilderImpl header(String name, String value) {
 121         checkNameAndValue(name, value);
 122         userHeaders.addHeader(name, value);
 123         return this;
 124     }
 125 
 126     @Override
 127     public HttpRequestBuilderImpl headers(String... params) {
 128         requireNonNull(params);


   1 /*
   2  * Copyright (c) 2015, 2018, 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.internal.net.http;
  27 
  28 import java.net.URI;
  29 import java.time.Duration;
  30 import java.util.Optional;
  31 import java.net.http.HttpClient;
  32 import java.net.http.HttpRequest;
  33 import java.net.http.HttpRequest.BodyPublisher;
  34 import jdk.internal.net.http.common.HttpHeadersImpl;
  35 import jdk.internal.net.http.common.Utils;
  36 import static java.lang.String.format;
  37 import static java.util.Objects.requireNonNull;
  38 import static jdk.internal.net.http.common.Utils.isValidName;
  39 import static jdk.internal.net.http.common.Utils.isValidValue;
  40 
  41 public class HttpRequestBuilderImpl extends HttpRequest.Builder {
  42 
  43     private HttpHeadersImpl userHeaders;
  44     private URI uri;
  45     private String method;
  46     private boolean expectContinue;
  47     private BodyPublisher bodyPublisher;
  48     private volatile Optional<HttpClient.Version> version;
  49     private Duration duration;
  50 
  51     public HttpRequestBuilderImpl(URI uri) {
  52         requireNonNull(uri, "uri must be non-null");
  53         checkURI(uri);
  54         this.uri = uri;
  55         this.userHeaders = new HttpHeadersImpl();
  56         this.method = "GET"; // default, as per spec
  57         this.version = Optional.empty();
  58     }
  59 
  60     public HttpRequestBuilderImpl() {
  61         this.userHeaders = new HttpHeadersImpl();


  88         }
  89     }
  90 
  91     @Override
  92     public HttpRequestBuilderImpl copy() {
  93         HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
  94         b.userHeaders = this.userHeaders.deepCopy();
  95         b.method = this.method;
  96         b.expectContinue = this.expectContinue;
  97         b.bodyPublisher = bodyPublisher;
  98         b.uri = uri;
  99         b.duration = duration;
 100         b.version = version;
 101         return b;
 102     }
 103 
 104     private void checkNameAndValue(String name, String value) {
 105         requireNonNull(name, "name");
 106         requireNonNull(value, "value");
 107         if (!isValidName(name)) {
 108             throw newIAE("invalid header name: \"%s\"", name);
 109         }
 110         if (!Utils.ALLOWED_HEADERS.test(name)) {
 111             throw newIAE("restricted header name: \"%s\"", name);
 112         }
 113         if (!isValidValue(value)) {
 114             throw newIAE("invalid header value: \"%s\"", value);
 115         }
 116     }
 117 
 118     @Override
 119     public HttpRequestBuilderImpl setHeader(String name, String value) {
 120         checkNameAndValue(name, value);
 121         userHeaders.setHeader(name, value);
 122         return this;
 123     }
 124 
 125     @Override
 126     public HttpRequestBuilderImpl header(String name, String value) {
 127         checkNameAndValue(name, value);
 128         userHeaders.addHeader(name, value);
 129         return this;
 130     }
 131 
 132     @Override
 133     public HttpRequestBuilderImpl headers(String... params) {
 134         requireNonNull(params);


< prev index next >