Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkClient.java
41154 views
1
/*
2
* Copyright (c) 2020, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.IOException;
25
import java.net.InetSocketAddress;
26
import java.net.SocketException;
27
import java.util.ArrayList;
28
import java.util.List;
29
30
import javax.net.ssl.SNIHostName;
31
import javax.net.ssl.SNIServerName;
32
import javax.net.ssl.SSLContext;
33
import javax.net.ssl.SSLParameters;
34
import javax.net.ssl.SSLSession;
35
import javax.net.ssl.SSLSocket;
36
37
/*
38
* A JDK client based on SSLSocket.
39
*/
40
public class JdkClient extends AbstractClient {
41
42
protected final int timeout;
43
protected final String request;
44
protected final boolean readResponse;
45
46
protected final ConnectionInterceptor interceptor;
47
protected final SSLContext context;
48
protected final SSLSocket socket;
49
50
public JdkClient(Builder builder) throws Exception {
51
timeout = builder.getTimeout() * 1000;
52
request = builder.getMessage();
53
readResponse = builder.isReadResponse();
54
55
interceptor = builder.getInterceptor();
56
context = getContext(builder);
57
socket = (SSLSocket) context.getSocketFactory().createSocket();
58
configClientSocket(builder);
59
}
60
61
protected SSLContext getContext(Builder builder) throws Exception {
62
return builder.getContext() == null
63
? Utilities.createSSLContext(builder.getCertTuple())
64
: builder.getContext();
65
}
66
67
protected void configClientSocket(Builder builder) throws SocketException {
68
socket.setSoTimeout(timeout);
69
if (builder.getProtocols() != null) {
70
socket.setEnabledProtocols(Utilities.enumsToStrs(protocol -> {
71
return JdkUtils.protocol((Protocol) protocol);
72
}, builder.getProtocols()));
73
}
74
if (builder.getCipherSuites() != null) {
75
socket.setEnabledCipherSuites(
76
Utilities.enumsToStrs(builder.getCipherSuites()));
77
}
78
SSLParameters sslParams = socket.getSSLParameters();
79
if (builder.getServerNames() != null) {
80
List<SNIServerName> serverNames = new ArrayList<>();
81
for(String bufServerName : builder.getServerNames()) {
82
serverNames.add(new SNIHostName(bufServerName));
83
}
84
sslParams.setServerNames(serverNames);
85
}
86
if (builder.getAppProtocols() != null) {
87
sslParams.setApplicationProtocols(builder.getAppProtocols());
88
}
89
socket.setSSLParameters(sslParams);
90
}
91
92
public static class Builder extends AbstractClient.Builder {
93
94
private ConnectionInterceptor interceptor;
95
private SSLContext context;
96
97
public ConnectionInterceptor getInterceptor() {
98
return interceptor;
99
}
100
101
public Builder setInterceptor(ConnectionInterceptor interceptor) {
102
this.interceptor = interceptor;
103
return this;
104
}
105
106
public SSLContext getContext() {
107
return context;
108
}
109
110
public Builder setContext(SSLContext context) {
111
this.context = context;
112
return this;
113
}
114
115
@Override
116
public JdkClient build() throws Exception {
117
return new JdkClient(this);
118
}
119
}
120
121
@Override
122
public Product getProduct() {
123
return Jdk.DEFAULT;
124
}
125
126
public SSLSession getSession() {
127
return socket.getSession();
128
}
129
130
@Override
131
public void connect(String host, int port) throws IOException {
132
socket.connect(new InetSocketAddress(host, port), timeout);
133
System.out.println("Client connected");
134
135
Utilities.writeOut(socket.getOutputStream(), request);
136
System.out.printf("Client sent request: [%s]%n", request);
137
138
if (readResponse) {
139
String response = Utilities.readIn(socket.getInputStream());
140
System.out.printf("Client received response: [%s]%n", response);
141
}
142
143
if (interceptor != null) {
144
interceptor.beforeExit(socket);
145
}
146
}
147
148
@Override
149
public String getNegoAppProtocol() throws SSLTestException {
150
return socket.getApplicationProtocol();
151
}
152
153
public void close() throws IOException {
154
if (!socket.isClosed()) {
155
socket.close();
156
}
157
}
158
}
159
160