/
HttpsServerHelper.java
139 lines (124 loc) · 4.55 KB
/
HttpsServerHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright 2005-2017 Restlet
*
* The contents of this file are subject to the terms of one of the following
* open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
* select the license that you prefer but you may not use this file except in
* compliance with one of these Licenses.
*
* You can obtain a copy of the Apache 2.0 license at
* http://www.opensource.org/licenses/apache-2.0
*
* You can obtain a copy of the EPL 1.0 license at
* http://www.opensource.org/licenses/eclipse-1.0
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://restlet.com/products/restlet-framework
*
* Restlet is a registered trademark of Restlet S.A.S.
*/
package org.restlet.engine.connector;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.engine.ssl.DefaultSslContextFactory;
import org.restlet.engine.ssl.SslContextFactory;
import org.restlet.engine.ssl.SslUtils;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;
/**
* Internal HTTPS server connector. Here is the list of additional parameters
* that are supported. They should be set in the Server's context before it is
* started:
* <table>
* <tr>
* <th>Parameter name</th>
* <th>Value type</th>
* <th>Default value</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>sslContextFactory</td>
* <td>String</td>
* <td>org.restlet.engine.ssl.DefaultSslContextFactory</td>
* <td>Let you specify a {@link SslContextFactory} qualified class name as a
* parameter, or an instance as an attribute for a more complete and flexible
* SSL context setting.</td>
* </tr>
* </table>
* For the default SSL parameters see the Javadocs of the
* {@link DefaultSslContextFactory} class.
*
* @author Jerome Louvel
*/
public class HttpsServerHelper extends NetServerHelper {
/** The underlying HTTPS server. */
private volatile HttpsServer server;
/**
* Constructor.
*
* @param server
* The server to help.
*/
public HttpsServerHelper(Server server) {
super(server);
getProtocols().add(Protocol.HTTPS);
}
/** Starts the Restlet. */
@Override
public void start() throws Exception {
// Use ephemeral port
int port = getHelped().getPort() > 0 ? getHelped().getPort() : 0;
if (getHelped().getAddress() != null) {
// This call may throw UnknownHostException and otherwise always
// returns an instance of INetAddress.
// Note: textual representation of inet addresses are supported
InetAddress iaddr = InetAddress.getByName(getHelped().getAddress());
// Note: the backlog of 50 is the default
setAddress(new InetSocketAddress(iaddr, port));
} else {
// Listens to any local IP address
setAddress(new InetSocketAddress(port));
}
// Complete initialization
this.server = HttpsServer.create(getAddress(), 0);
// Initialize the SSL context
SslContextFactory sslContextFactory = SslUtils.getSslContextFactory(this);
SSLContext sslContext = sslContextFactory.createSslContext();
final SSLParameters sslParams = sslContext.getDefaultSSLParameters();
server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
params.setSSLParameters(sslParams);
}
});
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
HttpsServerHelper.this.handle(new HttpExchangeCall(getHelped(),
httpExchange, true));
}
});
// creates a default executor
server.setExecutor(createThreadPool());
server.start();
setConfidential(true);
setEphemeralPort(server.getAddress().getPort());
super.start();
}
@Override
public synchronized void stop() throws Exception {
super.stop();
this.server.stop(0);
}
}