forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JdkHttpServerFactory.java
390 lines (338 loc) · 15.9 KB
/
JdkHttpServerFactory.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.jdkhttp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import javax.ws.rs.ProcessingException;
import javax.net.ssl.SSLContext;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.jdkhttp.internal.LocalizationMessages;
import org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
/**
* Factory for creating {@link HttpServer JDK HttpServer} instances to run Jersey applications.
*
* @author Miroslav Fuksa
* @author Marek Potociar
*/
public final class JdkHttpServerFactory {
private static final Logger LOG = Logger.getLogger(JdkHttpServerFactory.class.getName());
/**
* Create and start the {@link HttpServer JDK HttpServer} with the Jersey application deployed
* at the given {@link URI}.
* <p>
* The returned {@link HttpServer JDK HttpServer} is started.
* </p>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation
* occurs.
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration) {
return createHttpServer(uri, configuration, true);
}
/**
* Create (and possibly start) the {@link HttpServer JDK HttpServer} with the JAX-RS / Jersey application deployed
* on the given {@link URI}.
* <p>
* The {@code start} flag controls whether or not the returned {@link HttpServer JDK HttpServer} is started.
* </p>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @param start if set to {@code false}, the created server will not be automatically started.
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation occurs.
* @since 2.8
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration, final boolean start) {
return createHttpServer(uri, new JdkHttpHandlerContainer(configuration), start);
}
/**
* Create (and possibly start) the {@link HttpServer JDK HttpServer} with the JAX-RS / Jersey application deployed
* on the given {@link URI}.
* <p/>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @param parentContext DI provider specific context with application's registered bindings.
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation occurs.
* @see org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer
* @since 2.12
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration,
final Object parentContext) {
return createHttpServer(uri, new JdkHttpHandlerContainer(configuration, parentContext), true);
}
/**
* Create and start the {@link HttpServer JDK HttpServer}, eventually {@code HttpServer}'s subclass
* {@link HttpsServer JDK HttpsServer} with the JAX-RS / Jersey application deployed on the given {@link URI}.
* <p>
* The returned {@link HttpServer JDK HttpServer} is started.
* </p>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @param sslContext custom {@link SSLContext} to be passed to the server
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation occurs.
* @since 2.18
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration,
final SSLContext sslContext) {
return createHttpServer(uri, new JdkHttpHandlerContainer(configuration),
sslContext, true);
}
/**
* Create (and possibly start) the {@link HttpServer JDK HttpServer}, eventually {@code HttpServer}'s subclass
* {@link HttpsServer JDK HttpsServer} with the JAX-RS / Jersey application deployed on the given {@link URI}.
* <p>
* The {@code start} flag controls whether or not the returned {@link HttpServer JDK HttpServer} is started.
* </p>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @param sslContext custom {@link SSLContext} to be passed to the server
* @param start if set to {@code false}, the created server will not be automatically started.
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation occurs.
* @since 2.17
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration,
final SSLContext sslContext, final boolean start) {
return createHttpServer(uri,
new JdkHttpHandlerContainer(configuration),
sslContext,
start);
}
/**
* Create (and possibly start) the {@link HttpServer JDK HttpServer}, eventually {@code HttpServer}'s subclass
* {@link HttpsServer} with the JAX-RS / Jersey application deployed on the given {@link URI}.
* <p>
* The {@code start} flag controls whether or not the returned {@link HttpServer JDK HttpServer} is started.
* </p>
*
* @param uri the {@link URI uri} on which the Jersey application will be deployed.
* @param configuration the Jersey server-side application configuration.
* @param parentContext DI provider specific context with application's registered bindings.
* @param sslContext custom {@link SSLContext} to be passed to the server
* @param start if set to {@code false}, the created server will not be automatically started.
* @return Newly created {@link HttpServer}.
* @throws ProcessingException thrown when problems during server creation occurs.
* @since 2.18
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration,
final Object parentContext,
final SSLContext sslContext, final boolean start) {
return createHttpServer(uri,
new JdkHttpHandlerContainer(configuration, parentContext),
sslContext,
start
);
}
private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler,
final boolean start) {
return createHttpServer(uri, handler, null, start);
}
private static HttpServer createHttpServer(final URI uri,
final JdkHttpHandlerContainer handler,
final SSLContext sslContext,
final boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_NULL());
}
final String scheme = uri.getScheme();
final boolean isHttp = "http".equalsIgnoreCase(scheme);
final boolean isHttps = "https".equalsIgnoreCase(scheme);
final HttpsConfigurator httpsConfigurator = sslContext != null ? new HttpsConfigurator(sslContext) : null;
if (isHttp) {
if (httpsConfigurator != null) {
// attempt to use https with http scheme
LOG.warning(LocalizationMessages.WARNING_CONTAINER_URI_SCHEME_SECURED());
}
} else if (isHttps) {
if (httpsConfigurator == null) {
if (start) {
// The SSLContext (via HttpsConfigurator) has to be set before the server starts.
// Starting https server w/o SSL is invalid, it will lead to error anyway.
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_HTTPS_NO_SSL());
} else {
// Creating the https server w/o SSL context, but not starting it is valid.
// However, server.setHttpsConfigurator() must be called before the start.
LOG.info(LocalizationMessages.INFO_CONTAINER_HTTPS_NO_SSL());
}
}
} else {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_SCHEME_UNKNOWN(uri));
}
final String path = uri.getPath();
if (path == null) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_NULL(uri));
} else if (path.isEmpty()) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_EMPTY(uri));
} else if (path.charAt(0) != '/') {
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_START(uri));
}
final int port = (uri.getPort() == -1)
? (isHttp ? Container.DEFAULT_HTTP_PORT : Container.DEFAULT_HTTPS_PORT)
: uri.getPort();
final InetSocketAddress socketAddress = (uri.getHost() == null)
? new InetSocketAddress(port)
: new InetSocketAddress(uri.getHost(), port);
final HttpServer server;
try {
server = isHttp
? HttpServer.create(socketAddress, 0)
: HttpsServer.create(socketAddress, 0);
} catch (final IOException ioe) {
throw new ProcessingException(LocalizationMessages.ERROR_CONTAINER_EXCEPTION_IO(), ioe);
}
if (isHttps && httpsConfigurator != null) {
((HttpsServer) server).setHttpsConfigurator(httpsConfigurator);
}
server.setExecutor(Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.setNameFormat("jdk-http-server-%d")
.setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler())
.build()));
server.createContext(path, handler);
final HttpServer wrapper = isHttp
? createHttpServerWrapper(server, handler)
: createHttpsServerWrapper((HttpsServer) server, handler);
if (start) {
wrapper.start();
}
return wrapper;
}
private static HttpServer createHttpsServerWrapper(final HttpsServer delegate, final JdkHttpHandlerContainer handler) {
return new HttpsServer() {
@Override
public void setHttpsConfigurator(final HttpsConfigurator httpsConfigurator) {
delegate.setHttpsConfigurator(httpsConfigurator);
}
@Override
public HttpsConfigurator getHttpsConfigurator() {
return delegate.getHttpsConfigurator();
}
@Override
public void bind(final InetSocketAddress inetSocketAddress, final int i) throws IOException {
delegate.bind(inetSocketAddress, i);
}
@Override
public void start() {
delegate.start();
handler.onServerStart();
}
@Override
public void setExecutor(final Executor executor) {
delegate.setExecutor(executor);
}
@Override
public Executor getExecutor() {
return delegate.getExecutor();
}
@Override
public void stop(final int i) {
handler.onServerStop();
delegate.stop(i);
}
@Override
public HttpContext createContext(final String s, final HttpHandler httpHandler) {
return delegate.createContext(s, httpHandler);
}
@Override
public HttpContext createContext(final String s) {
return delegate.createContext(s);
}
@Override
public void removeContext(final String s) throws IllegalArgumentException {
delegate.removeContext(s);
}
@Override
public void removeContext(final HttpContext httpContext) {
delegate.removeContext(httpContext);
}
@Override
public InetSocketAddress getAddress() {
return delegate.getAddress();
}
};
}
private static HttpServer createHttpServerWrapper(final HttpServer delegate, final JdkHttpHandlerContainer handler) {
return new HttpServer() {
@Override
public void bind(final InetSocketAddress inetSocketAddress, final int i) throws IOException {
delegate.bind(inetSocketAddress, i);
}
@Override
public void start() {
delegate.start();
handler.onServerStart();
}
@Override
public void setExecutor(final Executor executor) {
delegate.setExecutor(executor);
}
@Override
public Executor getExecutor() {
return delegate.getExecutor();
}
@Override
public void stop(final int i) {
handler.onServerStop();
delegate.stop(i);
}
@Override
public HttpContext createContext(final String s, final HttpHandler httpHandler) {
return delegate.createContext(s, httpHandler);
}
@Override
public HttpContext createContext(final String s) {
return delegate.createContext(s);
}
@Override
public void removeContext(final String s) throws IllegalArgumentException {
delegate.removeContext(s);
}
@Override
public void removeContext(final HttpContext httpContext) {
delegate.removeContext(httpContext);
}
@Override
public InetSocketAddress getAddress() {
return delegate.getAddress();
}
};
}
/**
* Prevents instantiation.
*/
private JdkHttpServerFactory() {
throw new AssertionError("Instantiation not allowed.");
}
}