use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestWsWebSocketContainer method doMaxMessageSize.
private void doMaxMessageSize(String path, long size, boolean expectOpen) 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");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
Session s = connectToEchoServer(wsContainer, new EndpointA(), path);
// One for the client, one for the server
validateBackgroundProcessCount(2);
StringBuilder msg = new StringBuilder();
for (long i = 0; i < size; i++) {
msg.append('x');
}
s.getBasicRemote().sendText(msg.toString());
// Wait for up to 5 seconds for the client session to open
boolean open = s.isOpen();
int count = 0;
while (open != expectOpen && count < 50) {
Thread.sleep(100);
count++;
open = s.isOpen();
}
Assert.assertEquals(Boolean.valueOf(expectOpen), Boolean.valueOf(s.isOpen()));
// Close the session if it is expected to be open
if (expectOpen) {
s.close();
}
// Ensure both server and client have shutdown
validateBackgroundProcessCount(0);
}
use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestWsWebSocketContainer method testGetOpenSessions.
@Test
public void testGetOpenSessions() 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");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
EndpointA endpointA = new EndpointA();
Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
EndpointB endpointB = new EndpointB();
Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
Set<Session> setA = s3a.getOpenSessions();
Assert.assertEquals(3, setA.size());
Assert.assertTrue(setA.remove(s1a));
Assert.assertTrue(setA.remove(s2a));
Assert.assertTrue(setA.remove(s3a));
s1a.close();
setA = s3a.getOpenSessions();
Assert.assertEquals(2, setA.size());
Assert.assertFalse(setA.remove(s1a));
Assert.assertTrue(setA.remove(s2a));
Assert.assertTrue(setA.remove(s3a));
Set<Session> setB = s1b.getOpenSessions();
Assert.assertEquals(2, setB.size());
Assert.assertTrue(setB.remove(s1b));
Assert.assertTrue(setB.remove(s2b));
// Close sessions explicitly as Gump reports a session remains open at
// the end of this test
s2a.close();
s3a.close();
s1b.close();
s2b.close();
}
use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestWsWebSocketContainer method doTestPerMessageDeflateClient.
private void doTestPerMessageDeflateClient(String msg, int count) 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");
tomcat.start();
Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
List<Extension> extensions = new ArrayList<>(1);
extensions.add(perMessageDeflate);
ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
CountDownLatch latch = new CountDownLatch(count);
BasicText handler = new BasicText(latch, msg);
wsSession.addMessageHandler(handler);
for (int i = 0; i < count; i++) {
wsSession.getBasicRemote().sendText(msg);
}
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
((WsWebSocketContainer) wsContainer).destroy();
}
use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestWsWebSocketContainerTimeoutServer method doTestWriteTimeoutServer.
private void doTestWriteTimeoutServer(boolean setTimeoutOnContainer) throws Exception {
/*
* Note: There are all sorts of horrible uses of statics in this test
* because the API uses classes and the tests really need access
* to the instances which simply isn't possible.
*/
timeoutOnContainer = setTimeoutOnContainer;
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(ConstantTxConfig.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + ConstantTxConfig.PATH));
wsSession.addMessageHandler(new BlockingBinaryHandler());
int loops = 0;
while (loops < 15) {
Thread.sleep(1000);
if (!ConstantTxEndpoint.getRunning()) {
break;
}
loops++;
}
// Close the client session, primarily to allow the
// BackgroundProcessManager to shut down.
wsSession.close();
// Check the right exception was thrown
Assert.assertNotNull(ConstantTxEndpoint.getException());
Assert.assertEquals(ExecutionException.class, ConstantTxEndpoint.getException().getClass());
Assert.assertNotNull(ConstantTxEndpoint.getException().getCause());
Assert.assertEquals(SocketTimeoutException.class, ConstantTxEndpoint.getException().getCause().getClass());
// Check correct time passed
Assert.assertTrue(ConstantTxEndpoint.getTimeout() >= TIMEOUT_MS);
// Check the timeout wasn't too long
Assert.assertTrue(ConstantTxEndpoint.getTimeout() < TIMEOUT_MS * 2);
}
use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.
the class TestWebSocketFrameClient method testConnectToDigestEndpoint.
@Test
public void testConnectToDigestEndpoint() throws Exception {
Tomcat tomcat = getTomcatInstance();
Context ctx = tomcat.addContext(URI_PROTECTED, null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
SecurityCollection collection = new SecurityCollection();
collection.addPatternDecoded("/*");
tomcat.addUser(USER, PWD);
tomcat.addRole(USER, ROLE);
SecurityConstraint sc = new SecurityConstraint();
sc.addAuthRole(ROLE);
sc.addCollection(collection);
ctx.addConstraint(sc);
LoginConfig lc = new LoginConfig();
lc.setAuthMethod("DIGEST");
ctx.setLoginConfig(lc);
AuthenticatorBase digestAuthenticator = new org.apache.catalina.authenticator.DigestAuthenticator();
ctx.getPipeline().addValve(digestAuthenticator);
tomcat.start();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, USER);
clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD, PWD);
echoTester(URI_PROTECTED, clientEndpointConfig);
}
Aggregations