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.Authenticator; 29 import java.net.CookieHandler; 30 import java.net.ProxySelector; 31 import java.util.concurrent.Executor; 32 import javax.net.ssl.SSLContext; 33 import javax.net.ssl.SSLParameters; 34 import java.net.http.HttpClient; 35 import jdk.internal.net.http.common.Utils; 36 import static java.util.Objects.requireNonNull; 37 38 public class HttpClientBuilderImpl implements HttpClient.Builder { 39 40 CookieHandler cookieHandler; 41 HttpClient.Redirect followRedirects; 42 ProxySelector proxy; 43 Authenticator authenticator; 44 HttpClient.Version version; 45 Executor executor; 46 // Security parameters 47 SSLContext sslContext; 48 SSLParameters sslParams; 49 int priority = -1; 50 51 @Override 52 public HttpClientBuilderImpl cookieHandler(CookieHandler cookieHandler) { 53 requireNonNull(cookieHandler); 54 this.cookieHandler = cookieHandler; 55 return this; 56 } 57 58 59 @Override 60 public HttpClientBuilderImpl sslContext(SSLContext sslContext) { 61 requireNonNull(sslContext); 62 this.sslContext = sslContext; 63 return this; 64 } 65 66 67 @Override 68 public HttpClientBuilderImpl sslParameters(SSLParameters sslParameters) { 69 requireNonNull(sslParameters); 70 this.sslParams = Utils.copySSLParameters(sslParameters); 71 return this; 72 } 73 74 75 @Override 76 public HttpClientBuilderImpl executor(Executor s) { 77 requireNonNull(s); 78 this.executor = s; 79 return this; 80 } 81 82 83 @Override 84 public HttpClientBuilderImpl followRedirects(HttpClient.Redirect policy) { 85 requireNonNull(policy); 86 this.followRedirects = policy; 87 return this; 88 } 89 90 91 @Override 92 public HttpClientBuilderImpl version(HttpClient.Version version) { 93 requireNonNull(version); 94 this.version = version; 95 return this; 96 } 97 98 99 @Override 100 public HttpClientBuilderImpl priority(int priority) { 101 if (priority < 1 || priority > 256) { 102 throw new IllegalArgumentException("priority must be between 1 and 256"); 103 } 104 this.priority = priority; 105 return this; 106 } 107 108 @Override 109 public HttpClientBuilderImpl proxy(ProxySelector proxy) { 110 requireNonNull(proxy); 111 this.proxy = proxy; 112 return this; 113 } 114 115 116 @Override 117 public HttpClientBuilderImpl authenticator(Authenticator a) { 118 requireNonNull(a); 119 this.authenticator = a; 120 return this; 121 } 122 123 @Override 124 public HttpClient build() { 125 return HttpClientImpl.create(this); 126 } 127 }