Search in sources :

Example 36 with WebSocketContainer

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

the class WebSocketInjectionSupportTestCase method testWebSocketInjectionAndInterception.

@Test
public void testWebSocketInjectionAndInterception() throws Exception {
    AnnotatedClient.reset();
    AnnotatedEndpoint.reset();
    ComponentInterceptor.resetInterceptions();
    final WebSocketContainer serverContainer = ContainerProvider.getWebSocketContainer();
    serverContainer.connectToServer(AnnotatedClient.class, new URI("ws", "", TestSuiteEnvironment.getServerAddress(), TestSuiteEnvironment.getHttpPort(), "/websocket/websocket/cruel", "", ""));
    Assert.assertEquals("Hello cruel World", AnnotatedClient.getMessage());
    Assert.assertTrue("Client endpoint's injection not correct.", AnnotatedClient.injectionOK);
    Assert.assertTrue("Server endpoint's injection not correct.", AnnotatedEndpoint.injectionOK);
    Assert.assertTrue("PostConstruct method on client endpoint instance not called.", AnnotatedClient.postConstructCalled);
    Assert.assertTrue("PostConstruct method on server endpoint instance not called.", AnnotatedEndpoint.postConstructCalled);
    Assert.assertEquals("AroundConstruct interceptor method not invoked for client endpoint.", "AroundConstructInterceptor#Joe#AnnotatedClient", AnnotatedClient.getName());
    Assert.assertEquals("AroundConstruct interceptor method not invoked for server endpoint.", "AroundConstructInterceptor#Joe#AnnotatedEndpoint", AnnotatedEndpoint.getName());
    Assert.assertEquals(2, ComponentInterceptor.getInterceptions().size());
    Assert.assertEquals("open", ComponentInterceptor.getInterceptions().get(0).getMethodName());
    Assert.assertEquals("message", ComponentInterceptor.getInterceptions().get(1).getMethodName());
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Test(org.junit.Test)

Example 37 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 38 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)

Example 39 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.

the class SessionInfoIT method testSessionInfo.

@Test
public void testSessionInfo() throws Exception {
    URI serverURI = new URI("ws://localhost:58080/cdi-webapp/");
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientSessionInfoSocket socket = new ClientSessionInfoSocket();
    container.connectToServer(socket, serverURI.resolve("sessioninfo"));
    assertThat("Await open", socket.openLatch.await(1, TimeUnit.SECONDS), is(true));
    socket.session.getBasicRemote().sendText("info");
    socket.messages.awaitEventCount(1, 2, TimeUnit.SECONDS);
    System.out.printf("socket.messages.size = %s%n", socket.messages.size());
    String msg = socket.messages.poll();
    System.out.printf("Message is [%s]%n", msg);
    assertThat("Message", msg, containsString("HttpSession = HttpSession"));
    socket.session.getBasicRemote().sendText("close");
    assertThat("Await close", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Test(org.junit.Test)

Example 40 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project javaee7-samples by javaee-samples.

the class TestLocalClient method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:" + request.getLocalPort() + request.getContextPath() + "/websocket";
        out.println("Connecting to " + uri);
        container.connectToServer(MyClient.class, URI.create(uri));
        out.println("<br><br>Look in server.log for log messages from message exchange between client/server.");
        out.println("</body>");
        out.println("</html>");
    } catch (DeploymentException ex) {
        Logger.getLogger(TestLocalClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) DeploymentException(javax.websocket.DeploymentException) PrintWriter(java.io.PrintWriter)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)68 URI (java.net.URI)47 Session (javax.websocket.Session)43 Test (org.junit.Test)40 Context (org.apache.catalina.Context)32 Tomcat (org.apache.catalina.startup.Tomcat)32 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)30 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 Endpoint (javax.websocket.Endpoint)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)13 EndpointConfig (javax.websocket.EndpointConfig)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)9 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)9 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)9 ClientEndpoint (javax.websocket.ClientEndpoint)8 DeploymentException (javax.websocket.DeploymentException)8 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 ServerConfigListener (org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener)7