use of javax.websocket.Session in project tomcat by apache.
the class TestWebSocketFrameClient method testConnectToServerEndpoint.
@Test
public void testConnectToServerEndpoint() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
System.out.println("Sent Hello message, waiting for data");
// Ignore the latch result as the message count test below will tell us
// if the right number of messages arrived
handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
for (String message : messages) {
Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
}
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWebSocketFrameClientSSL method testConnectToServerEndpoint.
@Test
public void testConnectToServerEndpoint() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
TesterSupport.initSsl(tomcat);
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
clientEndpointConfig.getUserProperties().put(Constants.SSL_TRUSTSTORE_PROPERTY, "test/org/apache/tomcat/util/net/ca.jks");
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
System.out.println("Sent Hello message, waiting for data");
// Ignore the latch result as the message count test below will tell us
// if the right number of messages arrived
handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
for (String message : messages) {
Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
}
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWebSocketFrameClientSSL method testBug56032.
@Test
public void testBug56032() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
TesterSupport.initSsl(tomcat);
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
clientEndpointConfig.getUserProperties().put(Constants.SSL_TRUSTSTORE_PROPERTY, "test/org/apache/tomcat/util/net/ca.jks");
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
// Process incoming messages very slowly
MessageHandler handler = new SleepingText(5000);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
// Wait long enough for the buffers to fill and the send to timeout
int count = 0;
int limit = TesterFirehoseServer.WAIT_TIME_MILLIS / 100;
System.out.println("Waiting for server to report an error");
while (TesterFirehoseServer.Endpoint.getErrorCount() == 0 && count < limit) {
Thread.sleep(100);
count++;
}
if (TesterFirehoseServer.Endpoint.getErrorCount() == 0) {
Assert.fail("No error reported by Endpoint when timeout was expected");
}
// Wait up to another 10 seconds for the connection to be closed -
// should be a lot faster.
System.out.println("Waiting for connection to be closed");
count = 0;
limit = (TesterFirehoseServer.SEND_TIME_OUT_MILLIS * 2) / 100;
while (TesterFirehoseServer.Endpoint.getOpenConnectionCount() != 0 && count < limit) {
Thread.sleep(100);
count++;
}
int openConnectionCount = TesterFirehoseServer.Endpoint.getOpenConnectionCount();
if (openConnectionCount != 0) {
Assert.fail("There are [" + openConnectionCount + "] connections still open");
}
// Close the client session.
wsSession.close();
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWsRemoteEndpoint method doTestWriter.
private void doTestWriter(Class<?> clazz, boolean useWriter, String testMessage) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Session wsSession;
URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC);
if (Endpoint.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz;
wsSession = wsContainer.connectToServer(endpointClazz, Builder.create().build(), uri);
} else {
wsSession = wsContainer.connectToServer(clazz, uri);
}
CountDownLatch latch = new CountDownLatch(1);
TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
tep.setLatch(latch);
AsyncHandler<?> handler;
if (useWriter) {
handler = new AsyncText(latch);
} else {
handler = new AsyncBinary(latch);
}
wsSession.addMessageHandler(handler);
if (useWriter) {
Writer w = wsSession.getBasicRemote().getSendWriter();
for (int i = 0; i < 8; i++) {
w.write(testMessage);
}
w.close();
} else {
OutputStream s = wsSession.getBasicRemote().getSendStream();
for (int i = 0; i < 8; i++) {
s.write(testMessage.getBytes(StandardCharsets.UTF_8));
}
s.close();
}
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
List<String> results = new ArrayList<>();
if (useWriter) {
@SuppressWarnings("unchecked") List<String> messages = (List<String>) handler.getMessages();
results.addAll(messages);
} else {
// Take advantage of the fact that the message uses characters that
// are represented as a single UTF-8 byte so won't be split across
// binary messages
@SuppressWarnings("unchecked") List<ByteBuffer> messages = (List<ByteBuffer>) handler.getMessages();
for (ByteBuffer message : messages) {
byte[] bytes = new byte[message.limit()];
message.get(bytes);
results.add(new String(bytes, StandardCharsets.UTF_8));
}
}
int offset = 0;
int i = 0;
for (String result : results) {
if (testMessage.length() == 0) {
Assert.assertEquals(0, result.length());
} else {
// First may be a fragment
Assert.assertEquals(SEQUENCE.substring(offset, S_LEN), result.substring(0, S_LEN - offset));
i = S_LEN - offset;
while (i + S_LEN < result.length()) {
if (!SEQUENCE.equals(result.substring(i, i + S_LEN))) {
Assert.fail();
}
i += S_LEN;
}
offset = result.length() - i;
if (!SEQUENCE.substring(0, offset).equals(result.substring(i))) {
Assert.fail();
}
}
}
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWsRemoteEndpoint method doTestWriterError.
private void doTestWriterError(Class<?> clazz) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Session wsSession;
URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_WRITER_ERROR);
if (Endpoint.class.isAssignableFrom(clazz)) {
@SuppressWarnings("unchecked") Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz;
wsSession = wsContainer.connectToServer(endpointClazz, Builder.create().build(), uri);
} else {
wsSession = wsContainer.connectToServer(clazz, uri);
}
CountDownLatch latch = new CountDownLatch(1);
TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
tep.setLatch(latch);
AsyncHandler<?> handler;
handler = new AsyncText(latch);
wsSession.addMessageHandler(handler);
// This should trigger the error
wsSession.getBasicRemote().sendText("Start");
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
@SuppressWarnings("unchecked") List<String> messages = (List<String>) handler.getMessages();
Assert.assertEquals(0, messages.size());
}
Aggregations