-
Notifications
You must be signed in to change notification settings - Fork 337
/
GrizzlyHttpServerFactory.java
328 lines (300 loc) · 16.2 KB
/
GrizzlyHttpServerFactory.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
/*
* 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.grizzly2.httpserver;
import java.io.IOException;
import java.net.URI;
import javax.ws.rs.ProcessingException;
import org.glassfish.jersey.grizzly2.httpserver.internal.LocalizationMessages;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler;
import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpHandlerRegistration;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.http.server.ServerConfiguration;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.grizzly.utils.Charsets;
/**
* Factory for creating Grizzly Http Server.
* <p>
* Should you need to fine tune the underlying Grizzly transport layer, you can obtain direct access to the corresponding
* Grizzly structures with <tt>server.getListener("grizzly").getTransport()</tt>. To make certain options take effect,
* you need to work with an inactive HttpServer instance (that is the one that has not been started yet).
* To obtain such an instance, use one of the below factory methods with {@code start} parameter set to {@code false}.
* When the {@code start} parameter is not present, the factory method returns an already started instance.
* </p>
*
* @author Pavel Bucek
* @author Jakub Podlesak
* @author Marek Potociar
* @see HttpServer
* @see GrizzlyHttpContainer
*/
public final class GrizzlyHttpServerFactory {
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used as
* context path, the rest will be ignored.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri) {
return createHttpServer(uri, (GrizzlyHttpContainer) null, false, null, true);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest will be ignored.
* @param start if set to false, server will not get started, which allows to configure the underlying transport
* layer, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final boolean start) {
return createHttpServer(uri, (GrizzlyHttpContainer) null, false, null, start);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri URI on which the Jersey web application will be deployed. Only first path segment will be
* used as context path, the rest will be ignored.
* @param configuration web application configuration.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration) {
return createHttpServer(
uri,
new GrizzlyHttpContainer(configuration),
false,
null,
true
);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri URI on which the Jersey web application will be deployed. Only first path segment will be
* used as context path, the rest will be ignored.
* @param configuration web application configuration.
* @param start if set to false, server will not get started, which allows to configure the underlying
* transport layer, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri, final ResourceConfig configuration, final boolean start) {
return createHttpServer(
uri,
new GrizzlyHttpContainer(configuration),
false,
null,
start);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri URI on which the Jersey web application will be deployed. Only first path segment
* will be used as context path, the rest will be ignored.
* @param configuration web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig configuration,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator) {
return createHttpServer(
uri,
new GrizzlyHttpContainer(configuration),
secure,
sslEngineConfigurator,
true);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri URI on which the Jersey web application will be deployed. Only first path segment
* will be used as context path, the rest will be ignored.
* @param configuration web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param start if set to false, server will not get started, which allows to configure the
* underlying transport, see above for details.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig configuration,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final boolean start) {
return createHttpServer(
uri,
new GrizzlyHttpContainer(configuration),
secure,
sslEngineConfigurator,
start);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param config web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param parentContext DI provider specific context with application's registered bindings.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @since 2.12
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig config,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final Object parentContext) {
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentContext), secure, sslEngineConfigurator,
true);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param config web application configuration.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param parentContext DI provider specific context with application's registered bindings.
* @param start if set to false, server will not get started, this allows end users to set
* additional properties on the underlying listener.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @since 2.37
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig config,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final Object parentContext,
final boolean start) {
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentContext), secure, sslEngineConfigurator, start);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param config web application configuration.
* @param parentContext DI provider specific context with application's registered bindings.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @since 2.12
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig config,
final Object parentContext) {
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentContext), false, null, true);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param config web application configuration.
* @param parentContext DI provider specific context with application's registered bindings.
* @param start if set to false, server will not get started, this allows end users to set
* additional properties on the underlying listener.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
* @since 2.37
*/
public static HttpServer createHttpServer(final URI uri,
final ResourceConfig config,
final Object parentContext,
final boolean start) {
return createHttpServer(uri, new GrizzlyHttpContainer(config, parentContext), false, null, start);
}
/**
* Create new {@link HttpServer} instance.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param handler {@link HttpHandler} instance.
* @param secure used for call {@link NetworkListener#setSecure(boolean)}.
* @param sslEngineConfigurator Ssl settings to be passed to {@link NetworkListener#setSSLEngineConfig}.
* @param start if set to false, server will not get started, this allows end users to set
* additional properties on the underlying listener.
* @return newly created {@code HttpServer}.
* @throws ProcessingException in case of any failure when creating a new {@code HttpServer} instance.
* @see GrizzlyHttpContainer
*/
public static HttpServer createHttpServer(final URI uri,
final GrizzlyHttpContainer handler,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator,
final boolean start) {
final String host = (uri.getHost() == null) ? NetworkListener.DEFAULT_NETWORK_HOST : uri.getHost();
final int port = (uri.getPort() == -1)
? (secure ? Container.DEFAULT_HTTPS_PORT : Container.DEFAULT_HTTP_PORT)
: uri.getPort();
final NetworkListener listener = new NetworkListener("grizzly", host, port);
listener.getTransport().getWorkerThreadPoolConfig().setThreadFactory(new ThreadFactoryBuilder()
.setNameFormat("grizzly-http-server-%d")
.setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler())
.build());
listener.setSecure(secure);
if (sslEngineConfigurator != null) {
listener.setSSLEngineConfig(sslEngineConfigurator);
}
final HttpServer server = new HttpServer();
server.addListener(listener);
// Map the path to the processor.
final ServerConfiguration config = server.getServerConfiguration();
if (handler != null) {
final String path = uri.getPath().replaceAll("/{2,}", "/");
final String contextPath = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
config.addHttpHandler(handler, HttpHandlerRegistration.bulder().contextPath(contextPath).build());
}
config.setPassTraceRequest(true);
config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);
if (start) {
try {
// Start the server.
server.start();
} catch (final IOException ex) {
server.shutdownNow();
throw new ProcessingException(LocalizationMessages.FAILED_TO_START_SERVER(ex.getMessage()), ex);
}
}
return server;
}
/**
* Prevents instantiation.
*/
private GrizzlyHttpServerFactory() {
}
}