Search in sources :

Example 46 with WebSocketContainer

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

the class WebSocketTestCase method assertWebSocket.

private void assertWebSocket(URL webapp) throws InterruptedException, IOException, DeploymentException, URISyntaxException {
    AnnotatedClient endpoint = new AnnotatedClient();
    WebSocketContainer serverContainer = ContainerProvider.getWebSocketContainer();
    try (Session session = serverContainer.connectToServer(endpoint, new URI("ws", "", TestSuiteEnvironment.getServerAddress(), TestSuiteEnvironment.getHttpPort(), webapp.getPath() + "websocket/Stuart", "", ""))) {
        Assert.assertEquals("Hello Stuart", endpoint.getMessage());
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Session(javax.websocket.Session)

Example 47 with WebSocketContainer

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

the class TerminalInterpreterTest method testInvalidCommand.

@Test
public void testInvalidCommand() {
    Session session = null;
    WebSocketContainer webSocketContainer = null;
    try {
        // mock connect terminal
        boolean running = terminal.terminalThreadIsRunning();
        assertTrue(running);
        URI uri = URI.create("ws://localhost:" + terminal.getTerminalPort() + "/terminal/");
        webSocketContainer = ContainerProvider.getWebSocketContainer();
        // Attempt Connect
        session = webSocketContainer.connectToServer(TerminalSocketTest.class, uri);
        // Send Start terminal service message
        String terminalReadyCmd = String.format("{\"type\":\"TERMINAL_READY\"," + "\"noteId\":\"noteId-1\",\"paragraphId\":\"paragraphId-1\"}");
        LOGGER.info("send > " + terminalReadyCmd);
        session.getBasicRemote().sendText(terminalReadyCmd);
        Thread.sleep(10000);
        LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
        String msg = TerminalSocketTest.ReceivedMsg.get(0);
        LOGGER.info(msg);
        // {"text":"\u001b[?1034hbash-3.2$ \r\u001b[Kbash-3.2$
        // \r\u001b[Kbash-3.2$ ","type":"TERMINAL_PRINT"}
        String pattern = "\\{\"text\":\".*\"type\":\"TERMINAL_PRINT\"}";
        boolean isMatch = Pattern.matches(pattern, msg);
        assertTrue(isMatch);
        // Send invalid_command message
        String echoHelloWorldCmd = String.format("{\"type\":\"TERMINAL_COMMAND\"," + "\"command\":\"invalid_command\r\"}");
        LOGGER.info("send > " + echoHelloWorldCmd);
        session.getBasicRemote().sendText(echoHelloWorldCmd);
        Thread.sleep(5000);
        LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
        // [{"text":"invalid_co \rmmand\r\n","type":"TERMINAL_PRINT"},
        // {"text":"bash: invalid_command: command not found\r\n","type":"TERMINAL_PRINT"},
        // {"text":"bash-3.2$ ","type":"TERMINAL_PRINT"}]
        boolean return_invalid_command = false;
        for (String msg2 : TerminalSocketTest.ReceivedMsg) {
            boolean find = msg2.contains("invalid_command: command not found");
            if (find) {
                LOGGER.info("find return terminal print: " + msg2);
                return_invalid_command = true;
                break;
            }
        }
        assertTrue(return_invalid_command);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (DeploymentException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        try {
            session.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        // the JSR-356 spec does not handle lifecycles (yet)
        if (webSocketContainer instanceof LifeCycle) {
            try {
                ((LifeCycle) webSocketContainer).stop();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) TerminalSocketTest(org.apache.zeppelin.shell.terminal.TerminalSocketTest) WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) DeploymentException(javax.websocket.DeploymentException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Session(javax.websocket.Session) Test(org.junit.Test) TerminalSocketTest(org.apache.zeppelin.shell.terminal.TerminalSocketTest)

Example 48 with WebSocketContainer

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

the class TerminalInterpreterTest method testValidCommand.

@Test
public void testValidCommand() {
    Session session = null;
    WebSocketContainer webSocketContainer = null;
    try {
        // mock connect terminal
        boolean running = terminal.terminalThreadIsRunning();
        assertTrue(running);
        URI uri = URI.create("ws://localhost:" + terminal.getTerminalPort() + "/terminal/");
        webSocketContainer = ContainerProvider.getWebSocketContainer();
        // Attempt Connect
        session = webSocketContainer.connectToServer(TerminalSocketTest.class, uri);
        // Send Start terminal service message
        String terminalReadyCmd = String.format("{\"type\":\"TERMINAL_READY\"," + "\"noteId\":\"noteId-1\",\"paragraphId\":\"paragraphId-1\"}");
        LOGGER.info("send > " + terminalReadyCmd);
        session.getBasicRemote().sendText(terminalReadyCmd);
        Thread.sleep(10000);
        LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
        String msg = TerminalSocketTest.ReceivedMsg.get(0);
        LOGGER.info(msg);
        // {"text":"\u001b[?1034hbash-3.2$ \r\u001b[Kbash-3.2$
        // \r\u001b[Kbash-3.2$ ","type":"TERMINAL_PRINT"}
        String pattern = "\\{\"text\":\".*\"type\":\"TERMINAL_PRINT\"}";
        boolean isMatch = Pattern.matches(pattern, msg);
        assertTrue(isMatch);
        // Send echo 'hello world!' message
        String echoHelloWorldCmd = String.format("{\"type\":\"TERMINAL_COMMAND\"," + "\"command\":\"echo 'hello world!'\r\"}");
        LOGGER.info("send > " + echoHelloWorldCmd);
        session.getBasicRemote().sendText(echoHelloWorldCmd);
        Thread.sleep(5000);
        // [{"text":"echo \u0027hell \ro world!\u0027\r\n","type":"TERMINAL_PRINT"},
        // {"text":"hello world!\r\nbash-3.2$ ","type":"TERMINAL_PRINT"}]
        LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
        boolean return_hello_world = false;
        for (String msg2 : TerminalSocketTest.ReceivedMsg) {
            boolean find = msg2.contains("hello world!");
            if (find) {
                LOGGER.info("find return terminal print: " + msg2);
                return_hello_world = true;
                break;
            }
        }
        assertTrue(return_hello_world);
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (DeploymentException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        try {
            session.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        // the JSR-356 spec does not handle lifecycles (yet)
        if (webSocketContainer instanceof LifeCycle) {
            try {
                ((LifeCycle) webSocketContainer).stop();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) TerminalSocketTest(org.apache.zeppelin.shell.terminal.TerminalSocketTest) WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) DeploymentException(javax.websocket.DeploymentException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Session(javax.websocket.Session) Test(org.junit.Test) TerminalSocketTest(org.apache.zeppelin.shell.terminal.TerminalSocketTest)

Example 49 with WebSocketContainer

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

the class ClasspathAsWebappTest method run.

@Test
public void run() throws MalformedURLException {
    MyInitializer.found = null;
    MyBean.VALUE = null;
    try (final Container container = new Container(new Configuration().http(NetworkUtil.getNextAvailablePort()).property("openejb.container.additional.exclude", "org.apache.tomee.embedded.").property("openejb.additional.include", "tomee-").user("tomee", "tomeepwd").loginConfig(new LoginConfigBuilder().basic()).securityConstaint(new SecurityConstaintBuilder().addAuthRole("**").authConstraint(true).addCollection("api", "/api/resource2/"))).deployPathsAsWebapp(JarLocation.jarLocation(MyInitializer.class)).inject(this)) {
        // Servlet (initializer, servlet)
        assertNotNull(MyInitializer.found);
        final Iterator<Class<?>> it = MyInitializer.found.iterator();
        while (it.hasNext()) {
            // ThreadStackRule defines one for instance
            final Class<?> next = it.next();
            if (next.getEnclosingClass() != null && !Modifier.isStatic(next.getModifiers())) {
                it.remove();
            }
        }
        assertEquals(1, MyInitializer.found.size());
        assertEquals(Task1.class, MyInitializer.found.iterator().next());
        try {
            assertEquals("Servlet!", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/s")));
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        try {
            assertEquals("WebServlet", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/w")));
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        // JSP
        try {
            assertEquals("JSP", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/?test=JSP")).trim());
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        // CDI
        assertNotNull(bean);
        assertNull(bean.value());
        MyBean.VALUE = "cdi";
        assertEquals("cdi", bean.value());
        // EJB
        MyBean.VALUE = "ejb";
        assertEquals("ejb", anEjb.run());
        // JAXRS
        try {
            assertEquals("jaxrs", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource")));
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        // JAXRS + servlet security
        try {
            final URL url = new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/");
            final HttpURLConnection c = HttpURLConnection.class.cast(url.openConnection());
            c.setRequestProperty("Authorization", "Basic " + printBase64Binary("tomee:tomeepwd".getBytes()));
            assertEquals("tomee", IO.slurp(c.getInputStream()));
            c.disconnect();
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        try {
            assertEquals("tomee", IO.slurp(new URL("http://tomee:tomeepwd@localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/")));
            fail("should have been not authorized");
        } catch (final IOException e) {
        // ok
        }
        // WebSocket
        final WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
        try {
            WebSocketClient.message = null;
            final WebSocketClient webSocketClient = new WebSocketClient();
            final Session session = webSocketContainer.connectToServer(webSocketClient, new URI("ws://localhost:" + container.getConfiguration().getHttpPort() + "/ws"));
            webSocketClient.latch.await(20, TimeUnit.SECONDS);
            session.close();
            assertEquals("websocket", WebSocketClient.message);
        } catch (final Exception e) {
            fail(e.getMessage());
        }
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WebSocketContainer(javax.websocket.WebSocketContainer) HttpURLConnection(java.net.HttpURLConnection) Session(javax.websocket.Session) Test(org.junit.Test)

Example 50 with WebSocketContainer

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

the class WSTest method run.

@Test
public void run() throws InterruptedException, DeploymentException, IOException, URISyntaxException {
    CountDownLatch cdl = new CountDownLatch(5);
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    String wsEndpoint = String.format("ws://localhost:%d/ws-chat", CONTAINER.getConfiguration().getHttpPort());
    Session session = container.connectToServer(new ChatClient(cdl), new URI(wsEndpoint));
    assertTrue(cdl.await(20, TimeUnit.SECONDS));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

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