Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkClient.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.InetSocketAddress;25import java.net.SocketException;26import java.util.ArrayList;27import java.util.List;2829import javax.net.ssl.SNIHostName;30import javax.net.ssl.SNIServerName;31import javax.net.ssl.SSLContext;32import javax.net.ssl.SSLParameters;33import javax.net.ssl.SSLSession;34import javax.net.ssl.SSLSocket;3536/*37* A JDK client based on SSLSocket.38*/39public class JdkClient extends AbstractClient {4041protected final int timeout;42protected final String request;43protected final boolean readResponse;4445protected final ConnectionInterceptor interceptor;46protected final SSLContext context;47protected final SSLSocket socket;4849public JdkClient(Builder builder) throws Exception {50timeout = builder.getTimeout() * 1000;51request = builder.getMessage();52readResponse = builder.isReadResponse();5354interceptor = builder.getInterceptor();55context = getContext(builder);56socket = (SSLSocket) context.getSocketFactory().createSocket();57configClientSocket(builder);58}5960protected SSLContext getContext(Builder builder) throws Exception {61return builder.getContext() == null62? Utilities.createSSLContext(builder.getCertTuple())63: builder.getContext();64}6566protected void configClientSocket(Builder builder) throws SocketException {67socket.setSoTimeout(timeout);68if (builder.getProtocols() != null) {69socket.setEnabledProtocols(Utilities.enumsToStrs(protocol -> {70return JdkUtils.protocol((Protocol) protocol);71}, builder.getProtocols()));72}73if (builder.getCipherSuites() != null) {74socket.setEnabledCipherSuites(75Utilities.enumsToStrs(builder.getCipherSuites()));76}77SSLParameters sslParams = socket.getSSLParameters();78if (builder.getServerNames() != null) {79List<SNIServerName> serverNames = new ArrayList<>();80for(String bufServerName : builder.getServerNames()) {81serverNames.add(new SNIHostName(bufServerName));82}83sslParams.setServerNames(serverNames);84}85if (builder.getAppProtocols() != null) {86sslParams.setApplicationProtocols(builder.getAppProtocols());87}88socket.setSSLParameters(sslParams);89}9091public static class Builder extends AbstractClient.Builder {9293private ConnectionInterceptor interceptor;94private SSLContext context;9596public ConnectionInterceptor getInterceptor() {97return interceptor;98}99100public Builder setInterceptor(ConnectionInterceptor interceptor) {101this.interceptor = interceptor;102return this;103}104105public SSLContext getContext() {106return context;107}108109public Builder setContext(SSLContext context) {110this.context = context;111return this;112}113114@Override115public JdkClient build() throws Exception {116return new JdkClient(this);117}118}119120@Override121public Product getProduct() {122return Jdk.DEFAULT;123}124125public SSLSession getSession() {126return socket.getSession();127}128129@Override130public void connect(String host, int port) throws IOException {131socket.connect(new InetSocketAddress(host, port), timeout);132System.out.println("Client connected");133134Utilities.writeOut(socket.getOutputStream(), request);135System.out.printf("Client sent request: [%s]%n", request);136137if (readResponse) {138String response = Utilities.readIn(socket.getInputStream());139System.out.printf("Client received response: [%s]%n", response);140}141142if (interceptor != null) {143interceptor.beforeExit(socket);144}145}146147@Override148public String getNegoAppProtocol() throws SSLTestException {149return socket.getApplicationProtocol();150}151152public void close() throws IOException {153if (!socket.isClosed()) {154socket.close();155}156}157}158159160