-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JavaxWebSocketServletContainerInitializer.java
302 lines (266 loc) · 12.1 KB
/
JavaxWebSocketServletContainerInitializer.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
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.javax.server.config;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerContainer;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
import org.eclipse.jetty.websocket.javax.server.internal.JavaxWebSocketServerContainer;
import org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@HandlesTypes({ServerApplicationConfig.class, ServerEndpoint.class, Endpoint.class})
public class JavaxWebSocketServletContainerInitializer implements ServletContainerInitializer
{
/**
* The ServletContext attribute key name for the
* ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
*/
public static final String ATTR_JAVAX_SERVER_CONTAINER = javax.websocket.server.ServerContainer.class.getName();
public static final String ENABLE_KEY = "org.eclipse.jetty.websocket.javax";
public static final String HTTPCLIENT_ATTRIBUTE = "org.eclipse.jetty.websocket.javax.HttpClient";
private static final Logger LOG = LoggerFactory.getLogger(JavaxWebSocketServletContainerInitializer.class);
private final Configurator configurator;
public JavaxWebSocketServletContainerInitializer()
{
this(null);
}
public JavaxWebSocketServletContainerInitializer(Configurator configurator)
{
this.configurator = configurator;
}
/**
* Test a ServletContext for {@code init-param} or {@code attribute} at {@code keyName} for
* true or false setting that determines if the specified feature is enabled (or not).
*
* @param context the context to search
* @param keyName the key name
* @return the value for the feature key, otherwise null if key is not set in context
*/
private static Boolean isEnabledViaContext(ServletContext context, String keyName)
{
// Try context parameters first
String cp = context.getInitParameter(keyName);
if (cp != null)
{
return TypeUtil.isTrue(cp);
}
// Next, try attribute on context
Object enable = context.getAttribute(keyName);
if (enable != null)
{
return TypeUtil.isTrue(enable);
}
return null;
}
public interface Configurator
{
void accept(ServletContext servletContext, ServerContainer serverContainer) throws DeploymentException;
}
/**
* Configure the {@link ServletContextHandler} to call {@link JavaxWebSocketServletContainerInitializer#onStartup(Set, ServletContext)}
* during the {@link ServletContext} initialization phase.
*
* @param context the context to add listener to
* @param configurator the lambda that is called to allow the {@link ServerContainer} to
* be configured during the {@link ServletContext} initialization phase
*/
public static void configure(ServletContextHandler context, Configurator configurator)
{
if (!context.isStopped())
throw new IllegalStateException("configure should be called before starting");
context.addServletContainerInitializer(new JavaxWebSocketServletContainerInitializer(configurator));
}
/**
* Immediately initialize the {@link ServletContext} with the default (and empty) {@link ServerContainer}.
*
* <p>
* This method is typically called from {@link #onStartup(Set, ServletContext)} itself or from
* another dependent {@link ServletContainerInitializer} that requires minimal setup to
* be performed.
* </p>
* <p>
* This method SHOULD NOT BE CALLED by users of Jetty.
* Use the {@link #configure(ServletContextHandler, Configurator)} method instead.
* </p>
* <p>
* There is no enablement check here, and no automatic deployment of endpoints at this point
* in time. It merely sets up the {@link ServletContext} so with the basics needed to start
* configuring for `javax.websocket.server` based endpoints.
* </p>
*
* @param context the context to work with
* @return the default {@link ServerContainer} for this context
*/
private static ServerContainer initialize(ServletContextHandler context)
{
JavaxWebSocketServerContainer serverContainer = JavaxWebSocketServerContainer.getContainer(context.getServletContext());
if (serverContainer == null)
{
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServer(), context.getServletContext());
FilterHolder filterHolder = WebSocketUpgradeFilter.ensureFilter(context.getServletContext());
WebSocketMappings mapping = WebSocketMappings.ensureMappings(context.getServletContext());
serverContainer = JavaxWebSocketServerContainer.ensureContainer(context.getServletContext());
if (LOG.isDebugEnabled())
LOG.debug("configureContext {} {} {} {}", mapping, components, filterHolder, serverContainer);
}
return serverContainer;
}
@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException
{
Boolean enableKey = isEnabledViaContext(context, ENABLE_KEY);
boolean websocketEnabled = true;
if (enableKey != null)
websocketEnabled = enableKey;
if (!websocketEnabled)
{
LOG.info("Javax Websocket is disabled by configuration for context {}", context.getContextPath());
return;
}
ServletContextHandler servletContextHandler = ServletContextHandler.getServletContextHandler(context, "Javax WebSocket SCI");
ServerContainer container = initialize(servletContextHandler);
try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader()))
{
// Create the Jetty ServerContainer implementation
if (LOG.isDebugEnabled())
LOG.debug("Found {} classes", c.size());
// Now process the incoming classes
Set<Class<? extends Endpoint>> discoveredExtendedEndpoints = new HashSet<>();
Set<Class<?>> discoveredAnnotatedEndpoints = new HashSet<>();
Set<Class<? extends ServerApplicationConfig>> serverAppConfigs = new HashSet<>();
filterClasses(c, discoveredExtendedEndpoints, discoveredAnnotatedEndpoints, serverAppConfigs);
if (LOG.isDebugEnabled())
{
LOG.debug("Discovered {} extends Endpoint classes", discoveredExtendedEndpoints.size());
LOG.debug("Discovered {} @ServerEndpoint classes", discoveredAnnotatedEndpoints.size());
LOG.debug("Discovered {} ServerApplicationConfig classes", serverAppConfigs.size());
}
// Process the server app configs to determine endpoint filtering
boolean wasFiltered = false;
Set<ServerEndpointConfig> deployableExtendedEndpointConfigs = new HashSet<>();
Set<Class<?>> deployableAnnotatedEndpoints = new HashSet<>();
for (Class<? extends ServerApplicationConfig> clazz : serverAppConfigs)
{
if (LOG.isDebugEnabled())
LOG.debug("Found ServerApplicationConfig: {}", clazz);
try
{
ServerApplicationConfig config = clazz.getDeclaredConstructor().newInstance();
Set<ServerEndpointConfig> seconfigs = config.getEndpointConfigs(discoveredExtendedEndpoints);
if (seconfigs != null)
{
wasFiltered = true;
deployableExtendedEndpointConfigs.addAll(seconfigs);
}
Set<Class<?>> annotatedClasses = config.getAnnotatedEndpointClasses(discoveredAnnotatedEndpoints);
if (annotatedClasses != null)
{
wasFiltered = true;
deployableAnnotatedEndpoints.addAll(annotatedClasses);
}
}
catch (Exception e)
{
throw new ServletException("Unable to instantiate: " + clazz.getName(), e);
}
}
// Default behavior if nothing filtered
if (!wasFiltered)
{
deployableAnnotatedEndpoints.addAll(discoveredAnnotatedEndpoints);
// Note: it is impossible to determine path of "extends Endpoint" discovered classes
deployableExtendedEndpointConfigs = new HashSet<>();
}
if (LOG.isDebugEnabled())
{
LOG.debug("Deploying {} ServerEndpointConfig(s)", deployableExtendedEndpointConfigs.size());
}
// Deploy what should be deployed.
for (ServerEndpointConfig config : deployableExtendedEndpointConfigs)
{
try
{
container.addEndpoint(config);
}
catch (DeploymentException e)
{
throw new ServletException(e);
}
}
if (LOG.isDebugEnabled())
{
LOG.debug("Deploying {} @ServerEndpoint(s)", deployableAnnotatedEndpoints.size());
}
for (Class<?> annotatedClass : deployableAnnotatedEndpoints)
{
try
{
container.addEndpoint(annotatedClass);
}
catch (DeploymentException e)
{
throw new ServletException(e);
}
}
}
// Call the configurator after startup.
if (configurator != null)
{
try
{
configurator.accept(context, container);
}
catch (DeploymentException e)
{
throw new RuntimeException("Failed to deploy WebSocket Endpoint", e);
}
}
}
@SuppressWarnings("unchecked")
private void filterClasses(Set<Class<?>> c, Set<Class<? extends Endpoint>> discoveredExtendedEndpoints, Set<Class<?>> discoveredAnnotatedEndpoints,
Set<Class<? extends ServerApplicationConfig>> serverAppConfigs)
{
for (Class<?> clazz : c)
{
if (ServerApplicationConfig.class.isAssignableFrom(clazz))
{
serverAppConfigs.add((Class<? extends ServerApplicationConfig>)clazz);
}
if (Endpoint.class.isAssignableFrom(clazz))
{
discoveredExtendedEndpoints.add((Class<? extends Endpoint>)clazz);
}
ServerEndpoint endpoint = clazz.getAnnotation(ServerEndpoint.class);
if (endpoint != null)
{
discoveredAnnotatedEndpoints.add(clazz);
}
}
}
}