Search in sources :

Example 11 with ServerEndpointConfig

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

the class ServerContainerInitializeListener method createWsServerEndpointConfig.

protected ServerEndpointConfig createWsServerEndpointConfig(ServletContext servletContext) {
    final List<Class<? extends Encoder>> encoders = new LinkedList<>();
    final List<Class<? extends Decoder>> decoders = new LinkedList<>();
    encoders.add(OutputMessageEncoder.class);
    decoders.add(InputMessageDecoder.class);
    final ServerEndpointConfig endpointConfig = create(CheWSConnection.class, websocketContext + websocketEndPoint).configurator(createConfigurator()).encoders(encoders).decoders(decoders).build();
    endpointConfig.getUserProperties().put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext));
    endpointConfig.getUserProperties().put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext));
    endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext));
    return endpointConfig;
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Encoder(javax.websocket.Encoder) BaseTextEncoder(org.everrest.websockets.message.BaseTextEncoder) Decoder(javax.websocket.Decoder) BaseTextDecoder(org.everrest.websockets.message.BaseTextDecoder) LinkedList(java.util.LinkedList)

Example 12 with ServerEndpointConfig

use of javax.websocket.server.ServerEndpointConfig 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 13 with ServerEndpointConfig

use of javax.websocket.server.ServerEndpointConfig in project minijax by minijax.

the class WebSocketTest method testRun.

@Test
public void testRun() throws Exception {
    final Minijax minijax = createMinijax();
    minijax.register(WebSocketResource.class);
    minijax.start();
    final MinijaxApplication application = minijax.getDefaultApplication();
    final DeploymentInfo deploymentInfo = new DeploymentInfo();
    MinijaxWebSocketUtils.init(deploymentInfo, application);
    final WebSocketDeploymentInfo webSocketDeploymentInfo = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
    final ServerEndpointConfig endpointConfig = webSocketDeploymentInfo.getProgramaticEndpoints().get(0);
    final MinijaxWebSocketConfigurator configurator = (MinijaxWebSocketConfigurator) endpointConfig.getConfigurator();
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/echo"));
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
        configurator.modifyHandshake(endpointConfig, null, null);
        final AnnotatedEndpoint endpoint = configurator.getEndpointInstance(AnnotatedEndpoint.class);
        assertNotNull(endpoint);
    }
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) AnnotatedEndpoint(io.undertow.websockets.jsr.annotated.AnnotatedEndpoint) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Test(org.junit.Test)

Example 14 with ServerEndpointConfig

use of javax.websocket.server.ServerEndpointConfig in project tomcat70 by apache.

the class WsServerContainer method addEndpoint.

/**
 * Published the provided endpoint implementation at the specified path with
 * the specified configuration. {@link #WsServerContainer(ServletContext)}
 * must be called before calling this method.
 *
 * @param sec   The configuration to use when creating endpoint instances
 * @throws DeploymentException
 */
@Override
public void addEndpoint(ServerEndpointConfig sec) throws DeploymentException {
    if (enforceNoAddAfterHandshake && !addAllowed) {
        throw new DeploymentException(sm.getString("serverContainer.addNotAllowed"));
    }
    if (servletContext == null) {
        throw new DeploymentException(sm.getString("serverContainer.servletContextMissing"));
    }
    String path = sec.getPath();
    // Add method mapping to user properties
    PojoMethodMapping methodMapping = new PojoMethodMapping(sec.getEndpointClass(), sec.getDecoders(), path);
    if (methodMapping.getOnClose() != null || methodMapping.getOnOpen() != null || methodMapping.getOnError() != null || methodMapping.hasMessageHandlers()) {
        sec.getUserProperties().put(PojoEndpointServer.POJO_METHOD_MAPPING_KEY, methodMapping);
    }
    UriTemplate uriTemplate = new UriTemplate(path);
    if (uriTemplate.hasParameters()) {
        Integer key = Integer.valueOf(uriTemplate.getSegmentCount());
        SortedSet<TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key);
        if (templateMatches == null) {
            // Ensure that if concurrent threads execute this block they
            // both end up using the same TreeSet instance
            templateMatches = new TreeSet<TemplatePathMatch>(TemplatePathMatchComparator.getInstance());
            configTemplateMatchMap.putIfAbsent(key, templateMatches);
            templateMatches = configTemplateMatchMap.get(key);
        }
        if (!templateMatches.add(new TemplatePathMatch(sec, uriTemplate))) {
            // Duplicate uriTemplate;
            throw new DeploymentException(sm.getString("serverContainer.duplicatePaths", path, sec.getEndpointClass(), sec.getEndpointClass()));
        }
    } else {
        // Exact match
        ServerEndpointConfig old = configExactMatchMap.put(path, sec);
        if (old != null) {
            // Duplicate path mappings
            throw new DeploymentException(sm.getString("serverContainer.duplicatePaths", path, old.getEndpointClass(), sec.getEndpointClass()));
        }
    }
    endpointsRegistered = true;
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) PojoMethodMapping(org.apache.tomcat.websocket.pojo.PojoMethodMapping)

Example 15 with ServerEndpointConfig

use of javax.websocket.server.ServerEndpointConfig in project tomcat70 by apache.

the class TestWsServerContainer method testSpecExample3.

@Test
public void testSpecExample3() throws Exception {
    WsServerContainer sc = new WsServerContainer(new TesterServletContext());
    ServerEndpointConfig configA = ServerEndpointConfig.Builder.create(Object.class, "/a/{var}/c").build();
    ServerEndpointConfig configB = ServerEndpointConfig.Builder.create(Object.class, "/a/b/c").build();
    ServerEndpointConfig configC = ServerEndpointConfig.Builder.create(Object.class, "/a/{var1}/{var2}").build();
    sc.addEndpoint(configA);
    sc.addEndpoint(configB);
    sc.addEndpoint(configC);
    Assert.assertEquals(configB, sc.findMapping("/a/b/c").getConfig());
    Assert.assertEquals(configA, sc.findMapping("/a/d/c").getConfig());
    Assert.assertEquals(configC, sc.findMapping("/a/x/y").getConfig());
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Aggregations

ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)49 DeploymentException (javax.websocket.DeploymentException)17 ServerEndpoint (javax.websocket.server.ServerEndpoint)14 Test (org.junit.Test)13 ServletException (javax.servlet.ServletException)10 TesterServletContext (org.apache.tomcat.unittest.TesterServletContext)10 WebSocketBaseTest (org.apache.tomcat.websocket.WebSocketBaseTest)10 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)7 Endpoint (javax.websocket.Endpoint)6 ServerContainer (javax.websocket.server.ServerContainer)6 Server (org.eclipse.jetty.server.Server)6 ServerConnector (org.eclipse.jetty.server.ServerConnector)6 IOException (java.io.IOException)5 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)5 Before (org.junit.Before)5 HandshakeResponse (javax.websocket.HandshakeResponse)4 HandshakeRequest (javax.websocket.server.HandshakeRequest)4 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3