use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class WebSocketClient13TestCase method testTextMessageWss.
@Test
public void testTextMessageWss() throws Exception {
UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI("wss://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"))).setSsl(ssl);
IoFuture<WebSocketChannel> future = connectionBuilder.connect();
future.await(4, TimeUnit.SECONDS);
final WebSocketChannel webSocketChannel = future.get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
result.set(data);
latch.countDown();
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
webSocketChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello World").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello World", result.get());
webSocketChannel.sendClose();
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class HttpClientTestCase method testPostRequest.
@Test
public void testPostRequest() throws Exception {
//
final UndertowClient client = createClient();
final String postMessage = "This is a post request";
final List<String> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(10);
final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
result.setResponseListener(new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
new StringReadChannelListener(DefaultServer.getBufferPool()) {
@Override
protected void stringDone(String string) {
responses.add(string);
latch.countDown();
}
@Override
protected void error(IOException e) {
e.printStackTrace();
latch.countDown();
}
}.setup(result.getResponseChannel());
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
});
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
});
}
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(10, responses.size());
for (final String response : responses) {
Assert.assertEquals(postMessage, response);
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class WebSocketExtensionBasicTestCase method testExtensionsHeaders.
/**
* Simulate an extensions request.
*
* <pre>{@code
*
* GET / HTTP/1.1
* User-Agent: AutobahnTestSuite/0.7.0-0.9.0
* Host: localhost:7777
* Upgrade: WebSocket
* Connection: Upgrade
* Pragma: no-cache
* Cache-Control: no-cache
* Sec-WebSocket-Key: pRAuwtkO0SUKzufqA2g+ig==
* Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover; client_max_window_bits
* Sec-WebSocket-Version: 13
*
* }
* </pre>
*/
@Test
public void testExtensionsHeaders() throws Exception {
XnioWorker client;
Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
// List format
final String SEC_WEBSOCKET_EXTENSIONS_EXPECTED = "[permessage-deflate; client_no_context_takeover]";
List<WebSocketExtension> extensions = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensions);
Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
extensionHandshakes.add(new PerMessageDeflateHandshake(true));
final WebSocketChannel clientChannel = WebSocketClient.connect(client, null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage - Client - Received: " + data.getBytes().length + " bytes . Data: " + data);
result.set(data);
latch.countDown();
}
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
WebSocketLogger.ROOT_LOGGER.info("onError");
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
clientChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello, World!").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello, World!", result.get());
clientChannel.sendClose();
client.shutdown();
stopWorker(client);
Assert.assertEquals(SEC_WEBSOCKET_EXTENSIONS_EXPECTED, debug.getResponseExtensions().toString());
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class WebSocketClient13TestCase method testMessageViaWssProxy.
@Test
@ProxyIgnore
public void testMessageViaWssProxy() throws Exception {
final WebSocketChannel webSocketChannel = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerSSLAddress())).setSsl(new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext())).setProxyUri(new URI("http", null, DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default") + 10, "/proxy", null, null)).connect().get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
result.set(data);
latch.countDown();
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
webSocketChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello World").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello World", result.get());
webSocketChannel.sendClose();
Assert.assertEquals("CONNECT " + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"), connectLog.poll());
}
use of io.undertow.util.StringWriteChannelListener in project undertow by undertow-io.
the class HttpReadListener method sendBadRequestAndClose.
private void sendBadRequestAndClose(final StreamConnection connection, final Exception exception) {
UndertowLogger.REQUEST_IO_LOGGER.failedToParseRequest(exception);
connection.getSourceChannel().suspendReads();
new StringWriteChannelListener(BAD_REQUEST) {
@Override
protected void writeDone(final StreamSinkChannel c) {
super.writeDone(c);
c.suspendWrites();
IoUtils.safeClose(connection);
}
@Override
protected void handleError(StreamSinkChannel channel, IOException e) {
IoUtils.safeClose(connection);
}
}.setup(connection.getSinkChannel());
}
Aggregations