Search in sources :

Example 41 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project undertow by undertow-io.

the class WebsocketBasicAuthTestCase method testWrappedRequest.

@Test
public void testWrappedRequest() throws Exception {
    ProgramaticClientEndpoint endpoint = new ProgramaticClientEndpoint();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    ContainerProvider.getWebSocketContainer().connectToServer(endpoint, clientEndpointConfig, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/servletContext/wrapper"));
    Assert.assertEquals("wrapped", endpoint.getResponses().poll(15, TimeUnit.SECONDS));
    endpoint.session.close();
    endpoint.closeLatch.await(10, TimeUnit.SECONDS);
}
Also used : ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI) Test(org.junit.Test)

Example 42 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project undertow by undertow-io.

the class ServerWebSocketContainer method addEndpointInternal.

private synchronized void addEndpointInternal(final Class<?> endpoint, boolean requiresCreation) throws DeploymentException {
    ServerEndpoint serverEndpoint = endpoint.getAnnotation(ServerEndpoint.class);
    ClientEndpoint clientEndpoint = endpoint.getAnnotation(ClientEndpoint.class);
    if (serverEndpoint != null) {
        JsrWebSocketLogger.ROOT_LOGGER.addingAnnotatedServerEndpoint(endpoint, serverEndpoint.value());
        final PathTemplate template = PathTemplate.create(serverEndpoint.value());
        if (seenPaths.contains(template)) {
            PathTemplate existing = null;
            for (PathTemplate p : seenPaths) {
                if (p.compareTo(template) == 0) {
                    existing = p;
                    break;
                }
            }
            throw JsrWebSocketMessages.MESSAGES.multipleEndpointsWithOverlappingPaths(template, existing);
        }
        seenPaths.add(template);
        Class<? extends ServerEndpointConfig.Configurator> configuratorClass = serverEndpoint.configurator();
        EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, serverEndpoint.decoders(), serverEndpoint.encoders());
        AnnotatedEndpointFactory annotatedEndpointFactory = AnnotatedEndpointFactory.create(endpoint, encodingFactory, template.getParameterNames());
        InstanceFactory<?> instanceFactory = null;
        try {
            instanceFactory = classIntrospecter.createInstanceFactory(endpoint);
        } catch (Exception e) {
            //so it is possible that this is still valid if a custom configurator is in use
            if (configuratorClass == ServerEndpointConfig.Configurator.class) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
            } else {
                instanceFactory = new InstanceFactory<Object>() {

                    @Override
                    public InstanceHandle<Object> createInstance() throws InstantiationException {
                        throw JsrWebSocketMessages.MESSAGES.endpointDoesNotHaveAppropriateConstructor(endpoint);
                    }
                };
            }
        }
        ServerEndpointConfig.Configurator configurator;
        if (configuratorClass != ServerEndpointConfig.Configurator.class) {
            try {
                configurator = classIntrospecter.createInstanceFactory(configuratorClass).createInstance().getInstance();
            } catch (InstantiationException | NoSuchMethodException e) {
                throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
            }
        } else {
            configurator = DefaultContainerConfigurator.INSTANCE;
        }
        ServerEndpointConfig config = ServerEndpointConfig.Builder.create(endpoint, serverEndpoint.value()).decoders(Arrays.asList(serverEndpoint.decoders())).encoders(Arrays.asList(serverEndpoint.encoders())).subprotocols(Arrays.asList(serverEndpoint.subprotocols())).extensions(Collections.<Extension>emptyList()).configurator(configurator).build();
        ConfiguredServerEndpoint confguredServerEndpoint = new ConfiguredServerEndpoint(config, instanceFactory, template, encodingFactory, annotatedEndpointFactory, installedExtensions);
        configuredServerEndpoints.add(confguredServerEndpoint);
        handleAddingFilterMapping();
    } else if (clientEndpoint != null) {
        JsrWebSocketLogger.ROOT_LOGGER.addingAnnotatedClientEndpoint(endpoint);
        EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, clientEndpoint.decoders(), clientEndpoint.encoders());
        InstanceFactory<?> instanceFactory;
        try {
            instanceFactory = classIntrospecter.createInstanceFactory(endpoint);
        } catch (Exception e) {
            try {
                //this endpoint cannot be created by the container, the user will instantiate it
                instanceFactory = new ConstructorInstanceFactory<>(endpoint.getConstructor());
            } catch (NoSuchMethodException e1) {
                if (requiresCreation) {
                    throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
                } else {
                    instanceFactory = new InstanceFactory<Object>() {

                        @Override
                        public InstanceHandle<Object> createInstance() throws InstantiationException {
                            throw new InstantiationException();
                        }
                    };
                }
            }
        }
        AnnotatedEndpointFactory factory = AnnotatedEndpointFactory.create(endpoint, encodingFactory, Collections.<String>emptySet());
        ClientEndpointConfig.Configurator configurator = null;
        try {
            configurator = classIntrospecter.createInstanceFactory(clientEndpoint.configurator()).createInstance().getInstance();
        } catch (InstantiationException | NoSuchMethodException e) {
            throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
        }
        ClientEndpointConfig config = ClientEndpointConfig.Builder.create().decoders(Arrays.asList(clientEndpoint.decoders())).encoders(Arrays.asList(clientEndpoint.encoders())).preferredSubprotocols(Arrays.asList(clientEndpoint.subprotocols())).configurator(configurator).build();
        ConfiguredClientEndpoint configuredClientEndpoint = new ConfiguredClientEndpoint(config, factory, encodingFactory, instanceFactory);
        clientEndpoints.put(endpoint, configuredClientEndpoint);
    } else {
        throw JsrWebSocketMessages.MESSAGES.classWasNotAnnotated(endpoint);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) PathTemplate(io.undertow.util.PathTemplate) ServerEndpoint(javax.websocket.server.ServerEndpoint) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UpgradeFailedException(org.xnio.http.UpgradeFailedException) AnnotatedEndpointFactory(io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory) ConstructorInstanceFactory(io.undertow.servlet.util.ConstructorInstanceFactory) InstanceFactory(io.undertow.servlet.api.InstanceFactory) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint)

Example 43 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project undertow by undertow-io.

the class ProgramaticLazyEndpointTest method testStringOnMessage.

@org.junit.Test
public void testStringOnMessage() throws Exception {
    SSLContext context = DefaultServer.getClientSSLContext();
    ProgramaticClientEndpoint endpoint = new ProgramaticClientEndpoint();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(DefaultWebSocketClientSslProvider.SSL_CONTEXT, context);
    ContainerProvider.getWebSocketContainer().connectToServer(endpoint, clientEndpointConfig, new URI("wss://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default") + "/foo"));
    Assert.assertEquals("Hello Stuart", endpoint.getResponses().poll(15, TimeUnit.SECONDS));
    endpoint.session.close();
    endpoint.closeLatch.await(10, TimeUnit.SECONDS);
}
Also used : SSLContext(javax.net.ssl.SSLContext) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI)

Example 44 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project spring-framework by spring-projects.

the class StandardWebSocketClient method doHandshakeInternal.

@Override
protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
    int port = getPort(uri);
    InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
    InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);
    final StandardWebSocketSession session = new StandardWebSocketSession(headers, attributes, localAddress, remoteAddress);
    final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create().configurator(new StandardWebSocketClientConfigurator(headers)).preferredSubprotocols(protocols).extensions(adaptExtensions(extensions)).build();
    endpointConfig.getUserProperties().putAll(getUserProperties());
    final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session);
    Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {

        @Override
        public WebSocketSession call() throws Exception {
            webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
            return session;
        }
    };
    if (this.taskExecutor != null) {
        return this.taskExecutor.submitListenable(connectTask);
    } else {
        ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
        task.run();
        return task;
    }
}
Also used : StandardWebSocketHandlerAdapter(org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter) Endpoint(javax.websocket.Endpoint) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) InetSocketAddress(java.net.InetSocketAddress) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Endpoint(javax.websocket.Endpoint) Callable(java.util.concurrent.Callable) WebSocketSession(org.springframework.web.socket.WebSocketSession) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession)

Aggregations

ClientEndpointConfig (javax.websocket.ClientEndpointConfig)44 Test (org.junit.Test)26 URI (java.net.URI)25 Session (javax.websocket.Session)19 Endpoint (javax.websocket.Endpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 WebSocketContainer (javax.websocket.WebSocketContainer)12 ClientEndpoint (javax.websocket.ClientEndpoint)8 Context (org.apache.catalina.Context)7 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)7 Tomcat (org.apache.catalina.startup.Tomcat)7 ArrayList (java.util.ArrayList)6 EndpointConfig (javax.websocket.EndpointConfig)6 Extension (javax.websocket.Extension)6 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)6 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)5 List (java.util.List)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)4