Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkHttpsClient.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.URL;
26
27
import javax.net.ssl.HttpsURLConnection;
28
import javax.net.ssl.SSLContext;
29
30
/*
31
* A JDK client based on HttpsURLConnection.
32
*/
33
public class JdkHttpsClient extends AbstractClient {
34
35
protected final int timeout;
36
protected final String path;
37
38
protected final SSLContext context;
39
40
public JdkHttpsClient(Builder builder) throws Exception {
41
timeout = builder.getTimeout() * 1000;
42
path = builder.getPath();
43
44
context = getContext(builder);
45
}
46
47
protected SSLContext getContext(Builder builder) throws Exception {
48
return builder.getContext() == null
49
? Utilities.createSSLContext(builder.getCertTuple())
50
: builder.getContext();
51
}
52
53
public static class Builder extends AbstractClient.Builder {
54
55
private String path = "";
56
57
private SSLContext context;
58
59
public String getPath() {
60
return path;
61
}
62
63
public Builder setPath(String path) {
64
this.path = path;
65
return this;
66
}
67
68
public SSLContext getContext() {
69
return context;
70
}
71
72
public Builder setContext(SSLContext context) {
73
this.context = context;
74
return this;
75
}
76
77
@Override
78
public JdkHttpsClient build() throws Exception {
79
return new JdkHttpsClient(this);
80
}
81
}
82
83
@Override
84
public Product getProduct() {
85
return Jdk.DEFAULT;
86
}
87
88
@Override
89
public void connect(String host, int port) throws IOException {
90
String urlStr = String.format("https://%s:%d/%s", host, port, path);
91
if (Utilities.DEBUG) {
92
System.out.println("URL: " + urlStr);
93
}
94
95
HttpsURLConnection conn
96
= (HttpsURLConnection) new URL(urlStr).openConnection();
97
conn.setConnectTimeout(timeout);
98
conn.setReadTimeout(timeout);
99
conn.setSSLSocketFactory(context.getSocketFactory());
100
conn.connect();
101
System.out.println(Utilities.readIn(conn.getInputStream()));
102
}
103
}
104
105