io.undertow.websockets.jsr.ServerWebSocketContainer Java Examples
The following examples show how to use
io.undertow.websockets.jsr.ServerWebSocketContainer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 6 votes |
@org.junit.Test @Ignore("UT3 - P4") public void testPingPong() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new PingWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(PongWebSocketFrame.class, payload, latch)); latch.get(10, TimeUnit.SECONDS); Assert.assertNull(cause.get()); client.destroy(); }
Example #2
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 6 votes |
private ServletContext deployServlet(final ServerWebSocketContainer deployment) throws ServletException { final DeploymentInfo builder; builder = new DeploymentInfo() .setClassLoader(getClass().getClassLoader()) .setContextPath("/") .setClassIntrospecter(TestClassIntrospector.INSTANCE) .setDeploymentName("websocket.war") .addFilter(new FilterInfo("filter", JsrWebSocketFilter.class)) .addFilterUrlMapping("filter", "/*", DispatcherType.REQUEST) .addServletContextAttribute(javax.websocket.server.ServerContainer.class.getName(), deployment); final PathHandler root = new PathHandler(); final ServletContainer container = ServletContainer.Factory.newInstance(); DeploymentManager manager = container.addDeployment(builder); manager.deploy(); root.addPrefixPath(builder.getContextPath(), manager.start()); DefaultServer.setRootHandler(root); return manager.getDeployment().getServletContext(); }
Example #3
Source File: UndertowRequestUpgradeStrategy.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<Extension> selectedExtensions, final Endpoint endpoint) throws HandshakeFailureException { HttpServletRequest servletRequest = getHttpServletRequest(request); HttpServletResponse servletResponse = getHttpServletResponse(response); final ServletWebSocketHttpExchange exchange = createHttpExchange(servletRequest, servletResponse); exchange.putAttachment(HandshakeUtil.PATH_PARAMS, Collections.<String, String>emptyMap()); ServerWebSocketContainer wsContainer = (ServerWebSocketContainer) getContainer(servletRequest); final EndpointSessionHandler endpointSessionHandler = new EndpointSessionHandler(wsContainer); final ConfiguredServerEndpoint configuredServerEndpoint = createConfiguredServerEndpoint( selectedProtocol, selectedExtensions, endpoint, servletRequest); final Handshake handshake = getHandshakeToUse(exchange, configuredServerEndpoint); exchange.upgradeChannel(new HttpUpgradeListener() { @Override public void handleUpgrade(StreamConnection connection, HttpServerExchange serverExchange) { Object bufferPool = ReflectionUtils.invokeMethod(getBufferPoolMethod, exchange); WebSocketChannel channel = (WebSocketChannel) ReflectionUtils.invokeMethod( createChannelMethod, handshake, exchange, connection, bufferPool); if (peerConnections != null) { peerConnections.add(channel); } endpointSessionHandler.onConnect(exchange, channel); } }); handshake.handshake(exchange); }
Example #4
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testBinaryWithByteBuffer() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() { @Override public void onMessage(ByteBuffer message) { ByteBuffer buf = ByteBuffer.allocate(message.remaining()); buf.put(message); buf.flip(); session.getAsyncRemote().sendBinary(buf); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #5
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testBinaryWithByteArray() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<byte[]>() { @Override public void onMessage(byte[] message) { session.getAsyncRemote().sendBinary(ByteBuffer.wrap(message.clone())); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #6
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testText() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String message) { session.getAsyncRemote().sendText(message); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #7
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testBinaryWithByteBufferByFuture() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Future<Void>> sendResult = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() { @Override public void onMessage(ByteBuffer message) { ByteBuffer buf = ByteBuffer.allocate(message.remaining()); buf.put(message); buf.flip(); sendResult.set(session.getAsyncRemote().sendBinary(buf)); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch)); latch.get(); Future<Void> result = sendResult.get(); client.destroy(); }
Example #8
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testTextByFuture() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Future<Void>> sendResult = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(String message) { sendResult.set(session.getAsyncRemote().sendText(message)); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch)); latch.get(); sendResult.get(); client.destroy(); }
Example #9
Source File: SuspendResumeTestCase.java From quarkus-http with Apache License 2.0 | 5 votes |
@BeforeClass public static void setup() throws ServletException { final ServletContainer container = ServletContainer.Factory.newInstance(); DeploymentInfo builder = new DeploymentInfo() .setClassLoader(TestMessagesReceivedInOrder.class.getClassLoader()) .setContextPath("/") .setResourceManager(new TestResourceLoader(TestMessagesReceivedInOrder.class)) .setClassIntrospecter(TestClassIntrospector.INSTANCE) .addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, new WebSocketDeploymentInfo() .addListener(new WebSocketDeploymentInfo.ContainerReadyListener() { @Override public void ready(ServerWebSocketContainer c) { serverContainer = c; } }) .addEndpoint(SuspendResumeEndpoint.class) ) .setDeploymentName("servletContext.war"); DeploymentManager manager = container.addDeployment(builder); manager.deploy(); DefaultServer.setRootHandler(Handlers.path().addPrefixPath("/", manager.start())); }
Example #10
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testBinaryWithByteBufferAsync() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() { @Override public void onMessage(ByteBuffer message, boolean last) { Assert.assertTrue(last); ByteBuffer buf = ByteBuffer.allocate(message.remaining()); buf.put(message); buf.flip(); session.getAsyncRemote().sendBinary(buf); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #11
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@org.junit.Test public void testTextAsync() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Partial<String>() { StringBuilder sb = new StringBuilder(); @Override public void onMessage(String message, boolean last) { sb.append(message); if (!last) { return; } session.getAsyncRemote().sendText(sb.toString()); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #12
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 5 votes |
@Test @Ignore("UT3 - P2") public void testErrorHandling() throws Exception { ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(ProgramaticErrorEndpoint.class, "/").configurator(new InstanceConfigurator(new ProgramaticErrorEndpoint())).build()); deployServlet(builder); AnnotatedClientEndpoint c = new AnnotatedClientEndpoint(); Session session = ContainerProvider.getWebSocketContainer().connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); Assert.assertEquals("hi", ProgramaticErrorEndpoint.getMessage()); session.getAsyncRemote().sendText("app-error"); Assert.assertEquals("app-error", ProgramaticErrorEndpoint.getMessage()); Assert.assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage()); Assert.assertTrue(c.isOpen()); session.getBasicRemote().sendText("io-error"); Assert.assertEquals("io-error", ProgramaticErrorEndpoint.getMessage()); Assert.assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage()); Assert.assertTrue(c.isOpen()); ((UndertowSession) session).forceClose(); Assert.assertEquals("CLOSED", ProgramaticErrorEndpoint.getMessage()); }
Example #13
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 4 votes |
@org.junit.Test public void testTextUsingWriter() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<String>() { @Override public void onMessage(final String message) { DefaultServer.getUndertow().getWorker().execute(new Runnable() { @Override public void run() { try { Writer writer = session.getBasicRemote().getSendWriter(); writer.write(message); writer.close(); } catch (IOException e) { e.printStackTrace(); cause.set(e); latch.completeExceptionally(e); } } }); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #14
Source File: UndertowRequestUpgradeStrategy.java From java-technology-stack with MIT License | 4 votes |
@Override public ServerWebSocketContainer getContainer(HttpServletRequest request) { return (ServerWebSocketContainer) super.getContainer(request); }
Example #15
Source File: UndertowRequestUpgradeStrategy.java From spring-analysis-note with MIT License | 4 votes |
@Override public ServerWebSocketContainer getContainer(HttpServletRequest request) { return (ServerWebSocketContainer) super.getContainer(request); }
Example #16
Source File: JsrWebSocketServerTest.java From quarkus-http with Apache License 2.0 | 4 votes |
@org.junit.Test public void testBinaryWithByteArrayUsingStream() throws Exception { final byte[] payload = "payload".getBytes(); final AtomicReference<Throwable> cause = new AtomicReference<>(); final AtomicBoolean connected = new AtomicBoolean(false); final CompletableFuture<?> latch = new CompletableFuture<>(); class TestEndPoint extends Endpoint { @Override public void onOpen(final Session session, EndpointConfig config) { connected.set(true); session.addMessageHandler(new MessageHandler.Whole<byte[]>() { @Override public void onMessage(final byte[] message) { DefaultServer.getUndertow().getWorker().execute(new Runnable() { @Override public void run() { try { OutputStream out = session.getBasicRemote().getSendStream(); out.write(message); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); cause.set(e); latch.completeExceptionally(e); } } }); } }); } } ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getEventLoopSupplier(), Collections.EMPTY_LIST, false, false); builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build()); deployServlet(builder); WebSocketTestClient client = new WebSocketTestClient(new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/")); client.connect(); client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch)); latch.get(); Assert.assertNull(cause.get()); client.destroy(); }
Example #17
Source File: DoUpgradeServlet.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { ((ServerWebSocketContainer)ContainerProvider.getWebSocketContainer()).doUpgrade(req, resp, new ServerEndpointConfig() { @Override public Class<?> getEndpointClass() { if(req.getParameter("annotated") != null) { return EchoEndpoint.class; } else { return EchoProgramaticEndpoint.class; } } @Override public String getPath() { return req.getPathInfo(); } @Override public List<String> getSubprotocols() { return Collections.emptyList(); } @Override public List<Extension> getExtensions() { return Collections.emptyList(); } @Override public Configurator getConfigurator() { return null; } @Override public List<Class<? extends Encoder>> getEncoders() { return Collections.emptyList(); } @Override public List<Class<? extends Decoder>> getDecoders() { return Collections.emptyList(); } @Override public Map<String, Object> getUserProperties() { return Collections.emptyMap(); } }, Collections.singletonMap("foo", req.getPathInfo())); }