1 /* 2 * Copyright (c) 2015, 2016, 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 */ 24 package java.net.http; 25 26 import java.net.URI; 27 import java.net.ProxySelector; 28 import java.util.Objects; 29 import java.util.concurrent.TimeUnit; 30 31 class HttpRequestBuilderImpl extends HttpRequest.Builder { 32 33 private HttpHeadersImpl userHeaders; 34 private URI uri; 35 private String method; 36 private HttpClient.Redirect followRedirects; 37 private boolean expectContinue; 38 private HttpRequest.BodyProcessor body; 39 private HttpClient.Version version; 40 private final HttpClientImpl client; 41 private ProxySelector proxy; 42 private long timeval; 43 44 public HttpRequestBuilderImpl(HttpClientImpl client, URI uri) { 45 this.client = client; 46 checkURI(uri); 47 this.uri = uri; 48 this.version = client.version(); 49 this.userHeaders = new HttpHeadersImpl(); 50 } 51 52 @Override 53 public HttpRequestBuilderImpl body(HttpRequest.BodyProcessor reqproc) { 54 Objects.requireNonNull(reqproc); 55 this.body = reqproc; 56 return this; 57 } 58 59 @Override 60 public HttpRequestBuilderImpl uri(URI uri) { 61 Objects.requireNonNull(uri); 62 checkURI(uri); 63 this.uri = uri; 64 return this; 65 } 66 67 private static void checkURI(URI uri) { 68 String scheme = uri.getScheme().toLowerCase(); 69 if (!scheme.equals("https") && !scheme.equals("http")) 70 throw new IllegalArgumentException("invalid URI scheme"); 71 } 72 73 @Override 74 public HttpRequestBuilderImpl followRedirects(HttpClient.Redirect follow) { 75 Objects.requireNonNull(follow); 76 this.followRedirects = follow; 77 return this; 78 } 79 80 @Override 81 public HttpRequestBuilderImpl header(String name, String value) { 82 Objects.requireNonNull(name); 83 Objects.requireNonNull(value); 84 Utils.validateToken(name, "invalid header name"); 85 userHeaders.addHeader(name, value); 86 return this; 87 } 88 89 @Override 90 public HttpRequestBuilderImpl headers(String... params) { 91 Objects.requireNonNull(params); 92 if (params.length % 2 != 0) { 93 throw new IllegalArgumentException("wrong number of parameters"); 94 } 95 for (int i=0; i<params.length; ) { 96 String name = params[i]; 97 String value = params[i+1]; 98 header(name, value); 99 i+=2; 100 } 101 return this; 102 } 103 104 @Override 105 public HttpRequestBuilderImpl proxy(ProxySelector proxy) { 106 Objects.requireNonNull(proxy); 107 this.proxy = proxy; 108 return this; 109 } 110 111 @Override 112 public HttpRequestBuilderImpl copy() { 113 HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.client, this.uri); 114 b.userHeaders = this.userHeaders.deepCopy(); 115 b.method = this.method; 116 b.followRedirects = this.followRedirects; 117 b.expectContinue = this.expectContinue; 118 b.body = body; 119 b.uri = uri; 120 b.proxy = proxy; 121 return b; 122 } 123 124 @Override 125 public HttpRequestBuilderImpl setHeader(String name, String value) { 126 Objects.requireNonNull(name); 127 Objects.requireNonNull(value); 128 userHeaders.setHeader(name, value); 129 return this; 130 } 131 132 @Override 133 public HttpRequestBuilderImpl expectContinue(boolean enable) { 134 expectContinue = enable; 135 return this; 136 } 137 138 @Override 139 public HttpRequestBuilderImpl version(HttpClient.Version version) { 140 Objects.requireNonNull(version); 141 this.version = version; 142 return this; 143 } 144 145 HttpHeadersImpl headers() { return userHeaders; } 146 147 URI uri() { return uri; } 148 149 String method() { return method; } 150 151 HttpClient.Redirect followRedirects() { return followRedirects; } 152 153 ProxySelector proxy() { return proxy; } 154 155 boolean expectContinue() { return expectContinue; } 156 157 HttpRequest.BodyProcessor body() { return body; } 158 159 HttpClient.Version version() { return version; } 160 161 @Override 162 public HttpRequest GET() { return method("GET"); } 163 164 @Override 165 public HttpRequest POST() { return method("POST"); } 166 167 @Override 168 public HttpRequest PUT() { return method("PUT"); } 169 170 @Override 171 public HttpRequest method(String method) { 172 Objects.requireNonNull(method); 173 this.method = method; 174 return new HttpRequestImpl(client, method, this); 175 } 176 177 @Override 178 public HttpRequest.Builder timeout(TimeUnit timeunit, long timeval) { 179 Objects.requireNonNull(timeunit); 180 this.timeval = timeunit.toMillis(timeval); 181 return this; 182 } 183 184 long timeval() { return timeval; } 185 }