use of jakarta.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class WsSci method onStartup.
@Override
public void onStartup(Set<Class<?>> clazzes, ServletContext ctx) throws ServletException {
WsServerContainer sc = init(ctx, true);
if (clazzes == null || clazzes.size() == 0) {
return;
}
// Group the discovered classes by type
Set<ServerApplicationConfig> serverApplicationConfigs = new HashSet<>();
Set<Class<? extends Endpoint>> scannedEndpointClazzes = new HashSet<>();
Set<Class<?>> scannedPojoEndpoints = new HashSet<>();
try {
// wsPackage is "jakarta.websocket."
String wsPackage = ContainerProvider.class.getName();
wsPackage = wsPackage.substring(0, wsPackage.lastIndexOf('.') + 1);
for (Class<?> clazz : clazzes) {
int modifiers = clazz.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || !isExported(clazz)) {
// package - skip it.
continue;
}
// Protect against scanning the WebSocket API JARs
if (clazz.getName().startsWith(wsPackage)) {
continue;
}
if (ServerApplicationConfig.class.isAssignableFrom(clazz)) {
serverApplicationConfigs.add((ServerApplicationConfig) clazz.getConstructor().newInstance());
}
if (Endpoint.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") Class<? extends Endpoint> endpoint = (Class<? extends Endpoint>) clazz;
scannedEndpointClazzes.add(endpoint);
}
if (clazz.isAnnotationPresent(ServerEndpoint.class)) {
scannedPojoEndpoints.add(clazz);
}
}
} catch (ReflectiveOperationException e) {
throw new ServletException(e);
}
// Filter the results
Set<ServerEndpointConfig> filteredEndpointConfigs = new HashSet<>();
Set<Class<?>> filteredPojoEndpoints = new HashSet<>();
if (serverApplicationConfigs.isEmpty()) {
filteredPojoEndpoints.addAll(scannedPojoEndpoints);
} else {
for (ServerApplicationConfig config : serverApplicationConfigs) {
Set<ServerEndpointConfig> configFilteredEndpoints = config.getEndpointConfigs(scannedEndpointClazzes);
if (configFilteredEndpoints != null) {
filteredEndpointConfigs.addAll(configFilteredEndpoints);
}
Set<Class<?>> configFilteredPojos = config.getAnnotatedEndpointClasses(scannedPojoEndpoints);
if (configFilteredPojos != null) {
filteredPojoEndpoints.addAll(configFilteredPojos);
}
}
}
try {
// Deploy endpoints
for (ServerEndpointConfig config : filteredEndpointConfigs) {
sc.addEndpoint(config);
}
// Deploy POJOs
for (Class<?> clazz : filteredPojoEndpoints) {
sc.addEndpoint(clazz, true);
}
} catch (DeploymentException e) {
throw new ServletException(e);
}
}
use of jakarta.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class WsServerContainer method addEndpoint.
void addEndpoint(Class<?> pojo, boolean fromAnnotatedPojo) throws DeploymentException {
if (deploymentFailed) {
throw new DeploymentException(sm.getString("serverContainer.failedDeployment", servletContext.getContextPath(), servletContext.getVirtualServerName()));
}
ServerEndpointConfig sec;
try {
ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class);
if (annotation == null) {
throw new DeploymentException(sm.getString("serverContainer.missingAnnotation", pojo.getName()));
}
String path = annotation.value();
// Validate encoders
validateEncoders(annotation.encoders(), getInstanceManager(Thread.currentThread().getContextClassLoader()));
// ServerEndpointConfig
Class<? extends Configurator> configuratorClazz = annotation.configurator();
Configurator configurator = null;
if (!configuratorClazz.equals(Configurator.class)) {
try {
configurator = annotation.configurator().getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new DeploymentException(sm.getString("serverContainer.configuratorFail", annotation.configurator().getName(), pojo.getClass().getName()), e);
}
}
sec = ServerEndpointConfig.Builder.create(pojo, path).decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).subprotocols(Arrays.asList(annotation.subprotocols())).configurator(configurator).build();
} catch (DeploymentException de) {
failDeployment();
throw de;
}
addEndpoint(sec, fromAnnotatedPojo);
}
use of jakarta.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class TesterEndpointConfig method contextInitialized.
@Override
public void contextInitialized(ServletContextEvent sce) {
super.contextInitialized(sce);
ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
try {
ServerEndpointConfig sec = getServerEndpointConfig();
if (sec == null) {
sc.addEndpoint(getEndpointClass());
} else {
sc.addEndpoint(sec);
}
} catch (DeploymentException e) {
throw new RuntimeException(e);
}
}
use of jakarta.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class TestWsServerContainer method testDuplicatePaths21.
/*
* Simulates a class that gets picked up for extending Endpoint and for
* being annotated.
*/
@Test(expected = DeploymentException.class)
public void testDuplicatePaths21() throws Exception {
WsServerContainer sc = new WsServerContainer(new TesterServletContext());
ServerEndpointConfig configA = ServerEndpointConfig.Builder.create(PojoTemplate.class, "/foo/{a}").build();
sc.addEndpoint(configA, false);
sc.addEndpoint(PojoTemplate.class, true);
}
use of jakarta.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class TestWsServerContainer method testDuplicatePaths22.
/*
* POJO auto deployment followed by programmatic duplicate. Keep POJO.
*/
@Test
public void testDuplicatePaths22() throws Exception {
WsServerContainer sc = new WsServerContainer(new TesterServletContext());
ServerEndpointConfig configA = ServerEndpointConfig.Builder.create(PojoTemplate.class, "/foo/{a}").build();
sc.addEndpoint(PojoTemplate.class, true);
sc.addEndpoint(configA);
Assert.assertNotEquals(configA, sc.findMapping("/foo/{a}").getConfig());
}
Aggregations