Search in sources :

Example 31 with WebSocketChannel

use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.

the class JsrHybi07Handshake method createChannel.

@Override
public WebSocketChannel createChannel(WebSocketHttpExchange exchange, final StreamConnection c, final ByteBufferPool buffers) {
    WebSocketChannel channel = super.createChannel(exchange, c, buffers);
    HandshakeUtil.setConfig(channel, config);
    return channel;
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel)

Example 32 with WebSocketChannel

use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.

the class SuspendResumeTestCase method testConnectionWaitsForMessageEnd.

@Test
public void testConnectionWaitsForMessageEnd() throws Exception {
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> message = new AtomicReference<>();
    WebSocketChannel channel = WebSocketClient.connectionBuilder(DefaultServer.getWorker(), DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL() + "/")).connect().get();
    channel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage msg) throws IOException {
            message.set(msg.getData());
            done.countDown();
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            error.printStackTrace();
            message.set("error");
            done.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().free();
            done.countDown();
        }
    });
    channel.resumeReceives();
    Assert.assertTrue(channel.isOpen());
    WebSockets.sendText("Hello World", channel, null);
    Thread.sleep(500);
    serverContainer.pause(null);
    try {
        Assert.assertTrue(done.await(10, TimeUnit.SECONDS));
        Assert.assertEquals("Hello World", message.get());
    } finally {
        serverContainer.resume();
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) Test(org.junit.Test)

Example 33 with WebSocketChannel

use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.

the class SuspendResumeTestCase method testRejectWhenSuspended.

@Test
public void testRejectWhenSuspended() throws Exception {
    try {
        serverContainer.pause(null);
        WebSocketChannel channel = WebSocketClient.connectionBuilder(DefaultServer.getWorker(), DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL() + "/")).connect().get();
        IoUtils.safeClose(channel);
        Assert.fail();
    } catch (UpgradeFailedException e) {
    //expected
    } finally {
        serverContainer.resume();
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) UpgradeFailedException(org.xnio.http.UpgradeFailedException) URI(java.net.URI) Test(org.junit.Test)

Example 34 with WebSocketChannel

use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.

the class SuspendResumeTestCase method testConnectionClosedOnPause.

@Test
public void testConnectionClosedOnPause() throws Exception {
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> message = new AtomicReference<>();
    WebSocketChannel channel = WebSocketClient.connectionBuilder(DefaultServer.getWorker(), DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL() + "/")).connect().get();
    channel.getReceiveSetter().set(new ChannelListener<WebSocketChannel>() {

        @Override
        public void handleEvent(WebSocketChannel channel) {
            try {
                StreamSourceFrameChannel res = channel.receive();
                if (res == null) {
                    return;
                }
                if (res.getType() == WebSocketFrameType.CLOSE) {
                    message.set("closed");
                    done.countDown();
                }
                Channels.drain(res, Long.MAX_VALUE);
            } catch (IOException e) {
                if (message.get() == null) {
                    e.printStackTrace();
                    message.set("error");
                    done.countDown();
                }
            }
        }
    });
    channel.resumeReceives();
    Assert.assertTrue(channel.isOpen());
    Thread.sleep(500);
    serverContainer.pause(null);
    try {
        Assert.assertTrue(done.await(10, TimeUnit.SECONDS));
        Assert.assertEquals("closed", message.get());
    } finally {
        serverContainer.resume();
    }
}
Also used : StreamSourceFrameChannel(io.undertow.websockets.core.StreamSourceFrameChannel) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Test(org.junit.Test)

Example 35 with WebSocketChannel

use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.

the class JsrWebSocketFilter method doFilter.

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    if (req.getHeader(Headers.UPGRADE_STRING) != null) {
        final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp, peerConnections);
        String path;
        if (req.getPathInfo() == null) {
            path = req.getServletPath();
        } else {
            path = req.getServletPath() + req.getPathInfo();
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        PathTemplateMatcher.PathMatchResult<WebSocketHandshakeHolder> matchResult = pathTemplateMatcher.match(path);
        if (matchResult != null) {
            Handshake handshaker = null;
            for (Handshake method : matchResult.getValue().handshakes) {
                if (method.matches(facade)) {
                    handshaker = method;
                    break;
                }
            }
            if (handshaker != null) {
                if (container.isClosed()) {
                    resp.sendError(StatusCodes.SERVICE_UNAVAILABLE);
                    return;
                }
                facade.putAttachment(HandshakeUtil.PATH_PARAMS, matchResult.getParameters());
                facade.putAttachment(HandshakeUtil.PRINCIPAL, req.getUserPrincipal());
                final Handshake selected = handshaker;
                facade.upgradeChannel(new HttpUpgradeListener() {

                    @Override
                    public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                        WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
                        peerConnections.add(channel);
                        callback.onConnect(facade, channel);
                    }
                });
                handshaker.handshake(facade);
                return;
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : WebSocketHandshakeHolder(io.undertow.websockets.jsr.ServerWebSocketContainer.WebSocketHandshakeHolder) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) HttpServletResponse(javax.servlet.http.HttpServletResponse) StreamConnection(org.xnio.StreamConnection) ServletWebSocketHttpExchange(io.undertow.servlet.websockets.ServletWebSocketHttpExchange) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServerExchange(io.undertow.server.HttpServerExchange) PathTemplateMatcher(io.undertow.util.PathTemplateMatcher) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) Handshake(io.undertow.websockets.core.protocol.Handshake)

Aggregations

WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)38 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)18 IOException (java.io.IOException)18 URI (java.net.URI)17 Test (org.junit.Test)17 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)16 BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)7 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)7 WebSocketExtension (io.undertow.websockets.WebSocketExtension)7 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)7 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)7 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)7 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)6 HttpServerExchange (io.undertow.server.HttpServerExchange)5 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)5 ByteBuffer (java.nio.ByteBuffer)5 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)4