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