Search in sources :

Example 1 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project tomcat by apache.

the class WsWebSocketContainer method connectToServer.

@Override
public Session connectToServer(Class<?> annotatedEndpointClass, URI path) throws DeploymentException {
    ClientEndpointConfig config = createClientEndpointConfig(annotatedEndpointClass);
    ClientEndpointHolder holder = new PojoClassHolder(annotatedEndpointClass, config);
    return connectToServerRecursive(holder, config, path, new HashSet<>());
}
Also used : ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig)

Example 2 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project tomcat by apache.

the class WsWebSocketContainer method createClientEndpointConfig.

private ClientEndpointConfig createClientEndpointConfig(Class<?> annotatedEndpointClass) throws DeploymentException {
    ClientEndpoint annotation = annotatedEndpointClass.getAnnotation(ClientEndpoint.class);
    if (annotation == null) {
        throw new DeploymentException(sm.getString("wsWebSocketContainer.missingAnnotation", annotatedEndpointClass.getName()));
    }
    Class<? extends ClientEndpointConfig.Configurator> configuratorClazz = annotation.configurator();
    ClientEndpointConfig.Configurator configurator = null;
    if (!ClientEndpointConfig.Configurator.class.equals(configuratorClazz)) {
        try {
            configurator = configuratorClazz.getConstructor().newInstance();
        } catch (ReflectiveOperationException e) {
            throw new DeploymentException(sm.getString("wsWebSocketContainer.defaultConfiguratorFail"), e);
        }
    }
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    // Avoid NPE when using RI API JAR - see BZ 56343
    if (configurator != null) {
        builder.configurator(configurator);
    }
    ClientEndpointConfig config = builder.decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).preferredSubprotocols(Arrays.asList(annotation.subprotocols())).build();
    return config;
}
Also used : DeploymentException(jakarta.websocket.DeploymentException) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) ClientEndpoint(jakarta.websocket.ClientEndpoint)

Example 3 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project tomcat by apache.

the class TestWsSessionSuspendResume method test.

@Test
public void test() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + Config.PATH));
    CountDownLatch latch = new CountDownLatch(2);
    wsSession.addMessageHandler(String.class, message -> {
        Assert.assertTrue("[echo, echo, echo]".equals(message));
        latch.countDown();
    });
    for (int i = 0; i < 8; i++) {
        wsSession.getBasicRemote().sendText("echo");
    }
    boolean latchResult = latch.await(30, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    wsSession.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) TesterEndpointConfig(org.apache.tomcat.websocket.server.TesterEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(jakarta.websocket.Endpoint) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 4 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project tomcat by apache.

the class TestWsWebSocketContainer method doTestPerMessageDeflateClient.

private void doTestPerMessageDeflateClient(String msg, int count) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
    List<Extension> extensions = new ArrayList<>(1);
    extensions.add(perMessageDeflate);
    ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(count);
    BasicText handler = new BasicText(latch, msg);
    wsSession.addMessageHandler(handler);
    for (int i = 0; i < count; i++) {
        wsSession.getBasicRemote().sendText(msg);
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    ((WsWebSocketContainer) wsContainer).destroy();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(jakarta.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Extension(jakarta.websocket.Extension) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Session(jakarta.websocket.Session)

Example 5 with ClientEndpointConfig

use of jakarta.websocket.ClientEndpointConfig in project tomcat by apache.

the class TestWebSocketFrameClient method testConnectToDigestEndpoint.

@Test
public void testConnectToDigestEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext(URI_PROTECTED, null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded("/*");
    tomcat.addUser(USER, PWD);
    tomcat.addRole(USER, ROLE);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctx.addConstraint(sc);
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    ctx.setLoginConfig(lc);
    AuthenticatorBase digestAuthenticator = new org.apache.catalina.authenticator.DigestAuthenticator();
    ctx.getPipeline().addValve(digestAuthenticator);
    tomcat.start();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, USER);
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD, PWD);
    echoTester(URI_PROTECTED, clientEndpointConfig);
}
Also used : Context(org.apache.catalina.Context) AuthenticatorBase(org.apache.catalina.authenticator.AuthenticatorBase) Tomcat(org.apache.catalina.startup.Tomcat) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection) Test(org.junit.Test)

Aggregations

ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)22 URI (java.net.URI)15 Context (org.apache.catalina.Context)13 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)13 Tomcat (org.apache.catalina.startup.Tomcat)13 Test (org.junit.Test)12 Session (jakarta.websocket.Session)11 WebSocketContainer (jakarta.websocket.WebSocketContainer)11 Endpoint (jakarta.websocket.Endpoint)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)7 SSLContext (javax.net.ssl.SSLContext)6 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)5 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 KeyStore (java.security.KeyStore)3 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)3 Test (org.junit.jupiter.api.Test)3 ClientEndpoint (jakarta.websocket.ClientEndpoint)2