Search in sources :

Example 26 with DeploymentException

use of javax.websocket.DeploymentException in project che by eclipse.

the class ServerContainerInitializeListener method contextInitialized.

@Override
public final void contextInitialized(ServletContextEvent sce) {
    final ServletContext servletContext = sce.getServletContext();
    websocketContext = MoreObjects.firstNonNull(servletContext.getInitParameter("org.everrest.websocket.context"), "");
    websocketEndPoint = MoreObjects.firstNonNull(servletContext.getInitParameter("org.eclipse.che.websocket.endpoint"), "");
    eventBusEndPoint = MoreObjects.firstNonNull(servletContext.getInitParameter("org.eclipse.che.eventbus.endpoint"), "");
    webApplicationDeclaredRoles = new WebApplicationDeclaredRoles(servletContext);
    everrestConfiguration = (EverrestConfiguration) servletContext.getAttribute(EVERREST_CONFIG_ATTRIBUTE);
    if (everrestConfiguration == null) {
        everrestConfiguration = new EverrestConfiguration();
    }
    final ServerContainer serverContainer = (ServerContainer) servletContext.getAttribute("javax.websocket.server.ServerContainer");
    try {
        wsServerEndpointConfig = createWsServerEndpointConfig(servletContext);
        eventbusServerEndpointConfig = createEventbusServerEndpointConfig(servletContext);
        serverContainer.addEndpoint(wsServerEndpointConfig);
        serverContainer.addEndpoint(eventbusServerEndpointConfig);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : WebApplicationDeclaredRoles(org.everrest.core.tools.WebApplicationDeclaredRoles) EverrestConfiguration(org.everrest.core.impl.EverrestConfiguration) ServletContext(javax.servlet.ServletContext) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Example 27 with DeploymentException

use of javax.websocket.DeploymentException in project jetty.project by eclipse.

the class ServerContainer method getServerEndpointMetadata.

public ServerEndpointMetadata getServerEndpointMetadata(final Class<?> endpoint, final ServerEndpointConfig config) throws DeploymentException {
    ServerEndpointMetadata metadata = null;
    ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
    if (anno != null) {
        // Annotated takes precedence here
        AnnotatedServerEndpointMetadata ametadata = new AnnotatedServerEndpointMetadata(this, endpoint, config);
        AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(ametadata);
        metadata = ametadata;
        scanner.scan();
    } else if (Endpoint.class.isAssignableFrom(endpoint)) {
        // extends Endpoint
        @SuppressWarnings("unchecked") Class<? extends Endpoint> eendpoint = (Class<? extends Endpoint>) endpoint;
        metadata = new SimpleServerEndpointMetadata(eendpoint, config);
    } else {
        StringBuilder err = new StringBuilder();
        err.append("Not a recognized websocket [");
        err.append(endpoint.getName());
        err.append("] does not extend @").append(ServerEndpoint.class.getName());
        err.append(" or extend from ").append(Endpoint.class.getName());
        throw new DeploymentException("Unable to identify as valid Endpoint: " + endpoint);
    }
    return metadata;
}
Also used : ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) DeploymentException(javax.websocket.DeploymentException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 28 with DeploymentException

use of javax.websocket.DeploymentException in project jetty.project by eclipse.

the class WebSocketServerContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
    if (!isEnabledViaContext(context, ENABLE_KEY, true)) {
        LOG.info("JSR-356 is disabled by configuration");
        return;
    }
    ContextHandler handler = ContextHandler.getContextHandler(context);
    if (handler == null) {
        throw new ServletException("Not running on Jetty, JSR-356 support unavailable");
    }
    if (!(handler instanceof ServletContextHandler)) {
        throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support unavailable");
    }
    ServletContextHandler jettyContext = (ServletContextHandler) handler;
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(context.getClassLoader());
        // Create the Jetty ServerContainer implementation
        ServerContainer jettyContainer = configureContext(jettyContext);
        // make sure context is cleaned up when the context stops
        context.addListener(new ContextDestroyListener());
        if (c.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No JSR-356 annotations or interfaces discovered");
            }
            return;
        }
        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.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 (InstantiationException | IllegalAccessException 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 {
                jettyContainer.addEndpoint(config);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deploying {} @ServerEndpoint(s)", deployableAnnotatedEndpoints.size());
        }
        for (Class<?> annotatedClass : deployableAnnotatedEndpoints) {
            try {
                jettyContainer.addEndpoint(annotatedClass);
            } catch (DeploymentException e) {
                throw new ServletException(e);
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServerApplicationConfig(javax.websocket.server.ServerApplicationConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletException(javax.servlet.ServletException) ServerEndpoint(javax.websocket.server.ServerEndpoint) Endpoint(javax.websocket.Endpoint) DeploymentException(javax.websocket.DeploymentException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServerContainer(org.eclipse.jetty.websocket.jsr356.server.ServerContainer) HashSet(java.util.HashSet)

Example 29 with DeploymentException

use of javax.websocket.DeploymentException in project jetty.project by eclipse.

the class BasicEchoSocketConfigContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    // Build up a configuration with a specific path
    // Intentionally using alternate path in config (which differs from @ServerEndpoint declaration)
    String path = "/echo-alt";
    ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(BasicEchoSocket.class, path);
    try {
        container.addEndpoint(builder.build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint via config file", e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Example 30 with DeploymentException

use of javax.websocket.DeploymentException in project jetty.project by eclipse.

the class IdleTimeoutContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    // Build up a configuration with a specific path
    String path = "/idle-onopen-endpoint";
    ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(OnOpenIdleTimeoutEndpoint.class, path);
    try {
        container.addEndpoint(builder.build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint via config file", e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Aggregations

DeploymentException (javax.websocket.DeploymentException)36 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)16 ServerContainer (javax.websocket.server.ServerContainer)11 ServletException (javax.servlet.ServletException)7 WebSocketContainer (javax.websocket.WebSocketContainer)7 IOException (java.io.IOException)6 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 Endpoint (javax.websocket.Endpoint)5 ServerEndpoint (javax.websocket.server.ServerEndpoint)5 ExecutionException (java.util.concurrent.ExecutionException)3 ClientEndpoint (javax.websocket.ClientEndpoint)3 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)3 Extension (javax.websocket.Extension)3 WebSocketExtension (io.undertow.websockets.WebSocketExtension)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 EOFException (java.io.EOFException)2 InetSocketAddress (java.net.InetSocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 HashMap (java.util.HashMap)2