Search in sources :

Example 11 with ServerEndpointConfig

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);
    }
}
Also used : ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) ServerApplicationConfig(jakarta.websocket.server.ServerApplicationConfig) Endpoint(jakarta.websocket.Endpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) ServletException(jakarta.servlet.ServletException) Endpoint(jakarta.websocket.Endpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) DeploymentException(jakarta.websocket.DeploymentException) HashSet(java.util.HashSet)

Example 12 with ServerEndpointConfig

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);
}
Also used : ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) Configurator(jakarta.websocket.server.ServerEndpointConfig.Configurator) DeploymentException(jakarta.websocket.DeploymentException) ServerEndpoint(jakarta.websocket.server.ServerEndpoint)

Example 13 with ServerEndpointConfig

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);
    }
}
Also used : ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) DeploymentException(jakarta.websocket.DeploymentException) ServerContainer(jakarta.websocket.server.ServerContainer)

Example 14 with ServerEndpointConfig

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);
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 15 with ServerEndpointConfig

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());
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Aggregations

ServerEndpointConfig (jakarta.websocket.server.ServerEndpointConfig)20 TesterServletContext (org.apache.tomcat.unittest.TesterServletContext)14 WebSocketBaseTest (org.apache.tomcat.websocket.WebSocketBaseTest)14 Test (org.junit.Test)14 DeploymentException (jakarta.websocket.DeploymentException)4 ServerEndpoint (jakarta.websocket.server.ServerEndpoint)2 ServletException (jakarta.servlet.ServletException)1 Endpoint (jakarta.websocket.Endpoint)1 ServerApplicationConfig (jakarta.websocket.server.ServerApplicationConfig)1 ServerContainer (jakarta.websocket.server.ServerContainer)1 Configurator (jakarta.websocket.server.ServerEndpointConfig.Configurator)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ApplicationContext (org.springframework.context.ApplicationContext)1