Search in sources :

Example 1 with Configurator

use of javax.websocket.server.ServerEndpointConfig.Configurator in project tomcat by apache.

the class WsServerContainer method addEndpoint.

/**
     * Provides the equivalent of {@link #addEndpoint(ServerEndpointConfig)}
     * for publishing plain old java objects (POJOs) that have been annotated as
     * WebSocket endpoints.
     *
     * @param pojo   The annotated POJO
     */
@Override
public void addEndpoint(Class<?> pojo) throws DeploymentException {
    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());
    // ServerEndpointConfig
    ServerEndpointConfig sec;
    Class<? extends Configurator> configuratorClazz = annotation.configurator();
    Configurator configurator = null;
    if (!configuratorClazz.equals(Configurator.class)) {
        try {
            configurator = annotation.configurator().newInstance();
        } catch (InstantiationException | IllegalAccessException 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();
    addEndpoint(sec);
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Configurator(javax.websocket.server.ServerEndpointConfig.Configurator) DeploymentException(javax.websocket.DeploymentException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 2 with Configurator

use of javax.websocket.server.ServerEndpointConfig.Configurator in project che by eclipse.

the class ServerContainerInitializeListener method createConfigurator.

private Configurator createConfigurator() {
    return new Configurator() {

        @Override
        public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
            super.modifyHandshake(sec, request, response);
            final HttpSession httpSession = (HttpSession) request.getHttpSession();
            if (httpSession != null) {
                sec.getUserProperties().put(HTTP_SESSION_ATTRIBUTE, httpSession);
            }
            sec.getUserProperties().put(SECURITY_CONTEXT, createSecurityContext(request));
            sec.getUserProperties().put(ENVIRONMENT_CONTEXT, EnvironmentContext.getCurrent());
        }
    };
}
Also used : HandshakeResponse(javax.websocket.HandshakeResponse) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Configurator(javax.websocket.server.ServerEndpointConfig.Configurator) HttpSession(javax.servlet.http.HttpSession) HandshakeRequest(javax.websocket.server.HandshakeRequest)

Example 3 with Configurator

use of javax.websocket.server.ServerEndpointConfig.Configurator in project jetty.project by eclipse.

the class PongContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    try {
        Configurator config = new Config();
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/ping").configurator(config).build());
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/pong").configurator(config).build());
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongSocket.class, "/ping-socket").build());
        container.addEndpoint(ServerEndpointConfig.Builder.create(PongSocket.class, "/pong-socket").build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint directly", e);
    }
}
Also used : Configurator(javax.websocket.server.ServerEndpointConfig.Configurator) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Aggregations

ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)3 Configurator (javax.websocket.server.ServerEndpointConfig.Configurator)3 DeploymentException (javax.websocket.DeploymentException)2 HttpSession (javax.servlet.http.HttpSession)1 HandshakeResponse (javax.websocket.HandshakeResponse)1 HandshakeRequest (javax.websocket.server.HandshakeRequest)1 ServerContainer (javax.websocket.server.ServerContainer)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1