Search in sources :

Example 76 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project knox by apache.

the class ProxyInboundClientTest method testBinarymessage.

@Test(timeout = 3000)
public void testBinarymessage() throws IOException, DeploymentException {
    final String textMessage = "Echo";
    final ByteBuffer binarymessage = ByteBuffer.wrap(textMessage.getBytes());
    final AtomicBoolean isTestComplete = new AtomicBoolean(false);
    final WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    final ProxyInboundClient client = new ProxyInboundClient(new MessageEventCallback() {

        /**
         * A generic callback, can be left un-implemented
         *
         * @param message
         */
        @Override
        public void doCallback(String message) {
        }

        /**
         * Callback when connection is established.
         *
         * @param session
         */
        @Override
        public void onConnectionOpen(Object session) {
        }

        /**
         * Callback when connection is closed.
         *
         * @param reason
         */
        @Override
        public void onConnectionClose(CloseReason reason) {
            isTestComplete.set(true);
        }

        /**
         * Callback when there is an error in connection.
         *
         * @param cause
         */
        @Override
        public void onError(Throwable cause) {
            isTestComplete.set(true);
        }

        /**
         * Callback when a text message is received.
         *
         * @param message
         * @param session
         */
        @Override
        public void onMessageText(String message, Object session) {
            recievedMessage = message;
            isTestComplete.set(true);
        }

        /**
         * Callback when a binary message is received.
         *
         * @param message
         * @param last
         * @param session
         */
        @Override
        public void onMessageBinary(byte[] message, boolean last, Object session) {
            recievedBinaryMessage = message;
            isTestComplete.set(true);
        }
    });
    Assert.assertThat(client, instanceOf(javax.websocket.Endpoint.class));
    Session session = container.connectToServer(client, serverUri);
    session.getBasicRemote().sendBinary(binarymessage);
    while (!isTestComplete.get()) {
    /* just wait for the test to finish */
    }
    Assert.assertEquals("Binary message does not match", textMessage, new String(recievedBinaryMessage));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CloseReason(javax.websocket.CloseReason) Session(javax.websocket.Session) Test(org.junit.Test)

Example 77 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project knox by apache.

the class ProxyInboundClientTest method testTextMaxBufferLimit.

@Test(timeout = 3000)
public void testTextMaxBufferLimit() throws IOException, DeploymentException {
    final String longMessage = RandomStringUtils.random(100000);
    final AtomicBoolean isTestComplete = new AtomicBoolean(false);
    final WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    final ProxyInboundClient client = new ProxyInboundClient(new MessageEventCallback() {

        /**
         * A generic callback, can be left un-implemented
         *
         * @param message
         */
        @Override
        public void doCallback(String message) {
        }

        /**
         * Callback when connection is established.
         *
         * @param session
         */
        @Override
        public void onConnectionOpen(Object session) {
        }

        /**
         * Callback when connection is closed.
         *
         * @param reason
         */
        @Override
        public void onConnectionClose(CloseReason reason) {
            isTestComplete.set(true);
        }

        /**
         * Callback when there is an error in connection.
         *
         * @param cause
         */
        @Override
        public void onError(Throwable cause) {
            isTestComplete.set(true);
        }

        /**
         * Callback when a text message is received.
         *
         * @param message
         * @param session
         */
        @Override
        public void onMessageText(String message, Object session) {
            recievedMessage = message;
            isTestComplete.set(true);
        }

        /**
         * Callback when a binary message is received.
         *
         * @param message
         * @param last
         * @param session
         */
        @Override
        public void onMessageBinary(byte[] message, boolean last, Object session) {
        }
    });
    Assert.assertThat(client, instanceOf(javax.websocket.Endpoint.class));
    Session session = container.connectToServer(client, serverUri);
    session.getBasicRemote().sendText(longMessage);
    while (!isTestComplete.get()) {
    /* just wait for the test to finish */
    }
    Assert.assertEquals(longMessage, recievedMessage);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CloseReason(javax.websocket.CloseReason) Session(javax.websocket.Session) Test(org.junit.Test)

Example 78 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project knox by apache.

the class WebsocketEchoTest method testGatewayRewriteEcho.

/**
 * Test websocket rewrite rules proxying through gateway.
 *
 * @throws Exception
 */
@Test
public void testGatewayRewriteEcho() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    WebsocketClient client = new WebsocketClient();
    Session session = container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/123foo456bar/channels"));
    session.getBasicRemote().sendText("Echo");
    client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
    assertThat(client.messageQueue.get(0), is("Echo"));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Example 79 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project knox by apache.

the class WebsocketEchoTest method testGatewayEcho.

/**
 * Test websocket proxying through gateway.
 *
 * @throws Exception
 */
@Test
public void testGatewayEcho() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    WebsocketClient client = new WebsocketClient();
    Session session = container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/ws"));
    session.getBasicRemote().sendText("Echo");
    client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
    assertThat(client.messageQueue.get(0), is("Echo"));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Example 80 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project wildfly by wildfly.

the class WebSocketSecurityTestCase method makeCall.

@Override
protected void makeCall(final String user, final String pass, final int expectedCode) throws Exception {
    AnnotatedClient endpoint = new AnnotatedClient();
    endpoint.setCredentials(user, pass);
    WebSocketContainer serverContainer = ContainerProvider.getWebSocketContainer();
    if (expectedCode == 200) {
        connectToServer(serverContainer, endpoint);
        Assert.assertEquals("Hello anil", endpoint.getMessage());
    } else {
        boolean exceptionThrown = false;
        try {
            connectToServer(serverContainer, endpoint);
        } catch (DeploymentException e) {
            exceptionThrown = true;
        } finally {
            Assert.assertTrue("We expected that 'DeploymentException' is thrown as we provided incorrect " + "credentials to ws endpoint.", exceptionThrown);
        }
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)85 URI (java.net.URI)52 Session (javax.websocket.Session)52 Test (org.junit.Test)51 Context (org.apache.catalina.Context)29 Tomcat (org.apache.catalina.startup.Tomcat)29 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)27 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 DeploymentException (javax.websocket.DeploymentException)13 Endpoint (javax.websocket.Endpoint)13 EndpointConfig (javax.websocket.EndpointConfig)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)10 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)10 IOException (java.io.IOException)8 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 PrintWriter (java.io.PrintWriter)6