-
Notifications
You must be signed in to change notification settings - Fork 339
/
JettyHttpContainerFactory.java
319 lines (292 loc) · 15.8 KB
/
JettyHttpContainerFactory.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
/*
* Copyright (c) 2013, 2024 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.jetty;
import java.net.URI;
import java.util.concurrent.ThreadFactory;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.core.Configuration;
import org.glassfish.jersey.innate.VirtualThreadUtil;
import org.glassfish.jersey.innate.virtual.LoomishExecutors;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.jetty.internal.LocalizationMessages;
import org.glassfish.jersey.process.JerseyProcessingUncaughtExceptionHandler;
import org.glassfish.jersey.server.ContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.Container;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
/**
* Factory for creating and starting Jetty server handlers. This returns
* a handle to the started server as {@link Server} instances, which allows
* the server to be stopped by invoking the {@link org.eclipse.jetty.server.Server#stop()} method.
* <p/>
* To start the server in HTTPS mode an {@link SslContextFactory} can be provided.
* This will be used to decrypt and encrypt information sent over the
* connected TCP socket channel.
*
* @author Arul Dhesiaseelan (aruld@acm.org)
* @author Marek Potociar
*/
public final class JettyHttpContainerFactory {
private JettyHttpContainerFactory() {
}
/**
* Creates a {@link Server} instance that registers an {@link org.eclipse.jetty.server.Handler}.
*
* @param uri uri on which the {@link org.glassfish.jersey.server.ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @return newly created {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri) throws ProcessingException {
return createServer(uri, null, null, true);
}
/**
* Creates a {@link Server} instance that registers an {@link org.eclipse.jetty.server.Handler}.
*
* @param uri uri on which the {@link org.glassfish.jersey.server.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 {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri, final boolean start) throws ProcessingException {
return createServer(uri, null, null, start);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
* resource configuration.
* <p/>
* This implementation defers to the
* {@link org.glassfish.jersey.server.ContainerFactory#createContainer(Class, jakarta.ws.rs.core.Application)} method
* for creating an Container that manages the root resources.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to "http". The URI user information and host
* are ignored If the URI port is not present then port 80 will be
* used. The URI path, query and fragment components are ignored.
* @param config the resource configuration.
* @return newly created {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri, final ResourceConfig config)
throws ProcessingException {
final JettyHttpContainer container = ContainerFactory.createContainer(JettyHttpContainer.class, config);
return createServer(uri, null, container, true);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
* resource configuration.
* <p/>
* This implementation defers to the
* {@link org.glassfish.jersey.server.ContainerFactory#createContainer(Class, jakarta.ws.rs.core.Application)} method
* for creating an Container that manages the root resources.
*
* @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 {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri, final ResourceConfig configuration, final boolean start)
throws ProcessingException {
return createServer(uri, null, ContainerFactory.createContainer(JettyHttpContainer.class, configuration), start);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
* resource configuration.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* used. The URI path, query and fragment components are ignored.
* @param config the resource 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 {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
* @see JettyHttpContainer
* @since 2.12
*/
public static Server createServer(final URI uri, final ResourceConfig config, final boolean start,
final Object parentContext) {
return createServer(uri, null, new JettyHttpContainer(config, parentContext), start);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
* resource configuration.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* used. The URI path, query and fragment components are ignored.
* @param config the resource configuration.
* @param parentContext DI provider specific context with application's registered bindings.
* @return newly created {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
* @see JettyHttpContainer
* @since 2.12
*/
public static Server createServer(final URI uri, final ResourceConfig config, final Object parentContext) {
return createServer(uri, null, new JettyHttpContainer(config, parentContext), true);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes declared by the
* resource configuration.
* <p/>
* This implementation defers to the
* {@link ContainerFactory#createContainer(Class, jakarta.ws.rs.core.Application)} method
* for creating an Container that manages the root resources.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.spi.Container#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param sslContextFactory this is the SSL context factory used to configure SSL connector
* @param config the resource configuration.
* @return newly created {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri, final SslContextFactory.Server sslContextFactory,
final ResourceConfig config)
throws ProcessingException {
final JettyHttpContainer container = ContainerFactory.createContainer(JettyHttpContainer.class, config);
return createServer(uri, sslContextFactory, container, true);
}
/**
* Create a {@link Server} that registers an {@link org.eclipse.jetty.server.Handler} that
* in turn manages all root resource and provider classes found by searching the
* classes referenced in the java classpath.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.spi.Container#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param sslContextFactory this is the SSL context factory used to configure SSL connector
* @param handler the container that handles all HTTP requests
* @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 {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
* @see JettyHttpContainer
*/
public static Server createServer(final URI uri,
final SslContextFactory.Server sslContextFactory,
final JettyHttpContainer handler,
final boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
}
final String scheme = uri.getScheme();
int defaultPort = Container.DEFAULT_HTTP_PORT;
if (sslContextFactory == null) {
if (!"http".equalsIgnoreCase(scheme)) {
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
}
} else {
if (!"https".equalsIgnoreCase(scheme)) {
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
}
defaultPort = Container.DEFAULT_HTTPS_PORT;
}
final int port = (uri.getPort() == -1) ? defaultPort : uri.getPort();
final Configuration configuration = handler != null ? handler.getConfiguration() : null;
final Server server = new Server(new JettyConnectorThreadPool(configuration));
final HttpConfiguration config = new HttpConfiguration();
if (sslContextFactory != null) {
config.setSecureScheme("https");
config.setSecurePort(port);
config.addCustomizer(new SecureRequestCustomizer());
final ServerConnector https = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(config));
https.setPort(port);
server.setConnectors(new Connector[]{https});
} else {
final ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
http.setPort(port);
server.setConnectors(new Connector[]{http});
}
if (handler != null) {
server.setHandler(handler);
}
if (start) {
try {
// Start the server.
server.start();
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_WHEN_CREATING_SERVER(), e);
}
}
return server;
}
// TODO: Use https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/util/thread/QueuedThreadPool.html
// #%3Cinit%3E(int,int,int,int,java.util.concurrent.BlockingQueue,java.lang.ThreadGroup,java.util.concurrent.ThreadFactory)
//
// Keeping this for backwards compatibility for the time being
private static final class JettyConnectorThreadPool extends QueuedThreadPool {
private final ThreadFactory threadFactory;
private JettyConnectorThreadPool(Configuration configuration) {
final LoomishExecutors executors = VirtualThreadUtil.withConfig(configuration, false);
if (executors.isVirtual()) {
super.setMaxThreads(Integer.MAX_VALUE - 1);
}
this.threadFactory = new ThreadFactoryBuilder()
.setNameFormat("jetty-http-server-%d")
.setUncaughtExceptionHandler(new JerseyProcessingUncaughtExceptionHandler())
.setThreadFactory(executors.getThreadFactory())
.build();
}
@Override
public Thread newThread(Runnable runnable) {
return threadFactory.newThread(runnable);
}
}
}