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());
}
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());
}
}
}
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();
}
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));
}
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);
}
}
Aggregations