Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkHttpsClient.java
41154 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.net.URL;2526import javax.net.ssl.HttpsURLConnection;27import javax.net.ssl.SSLContext;2829/*30* A JDK client based on HttpsURLConnection.31*/32public class JdkHttpsClient extends AbstractClient {3334protected final int timeout;35protected final String path;3637protected final SSLContext context;3839public JdkHttpsClient(Builder builder) throws Exception {40timeout = builder.getTimeout() * 1000;41path = builder.getPath();4243context = getContext(builder);44}4546protected SSLContext getContext(Builder builder) throws Exception {47return builder.getContext() == null48? Utilities.createSSLContext(builder.getCertTuple())49: builder.getContext();50}5152public static class Builder extends AbstractClient.Builder {5354private String path = "";5556private SSLContext context;5758public String getPath() {59return path;60}6162public Builder setPath(String path) {63this.path = path;64return this;65}6667public SSLContext getContext() {68return context;69}7071public Builder setContext(SSLContext context) {72this.context = context;73return this;74}7576@Override77public JdkHttpsClient build() throws Exception {78return new JdkHttpsClient(this);79}80}8182@Override83public Product getProduct() {84return Jdk.DEFAULT;85}8687@Override88public void connect(String host, int port) throws IOException {89String urlStr = String.format("https://%s:%d/%s", host, port, path);90if (Utilities.DEBUG) {91System.out.println("URL: " + urlStr);92}9394HttpsURLConnection conn95= (HttpsURLConnection) new URL(urlStr).openConnection();96conn.setConnectTimeout(timeout);97conn.setReadTimeout(timeout);98conn.setSSLSocketFactory(context.getSocketFactory());99conn.connect();100System.out.println(Utilities.readIn(conn.getInputStream()));101}102}103104105