org.apache.catalina.servlets.DefaultServlet Java Examples

The following examples show how to use org.apache.catalina.servlets.DefaultServlet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestWsRemoteEndpointImplServer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientDropsConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();

    tomcat.start();

    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);

    Session session = wsContainer.connectToServer(client, uri);
    // Break point A required on following line
    session.close();
}
 
Example #2
Source File: TestWebSocketFrameClient.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectToRootEndpoint() 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.addServletMapping("/", "default");
    Context ctx2 = tomcat.addContext("/foo", null);
    ctx2.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx2, "default", new DefaultServlet());
    ctx2.addServletMapping("/", "default");

    tomcat.start();

    echoTester("");
    echoTester("/");
    // FIXME: The ws client doesn't handle any response other than the upgrade,
    // which may or may not be allowed. In that case, the server will return
    // a redirect to the root of the webapp to avoid possible broken relative
    // paths.
    // echoTester("/foo");
    echoTester("/foo/");
}
 
Example #3
Source File: TestWebSocketFrameClient.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectToRootEndpoint() 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.addServletMapping("/", "default");
    Context ctx2 = tomcat.addContext("/foo", null);
    ctx2.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx2, "default", new DefaultServlet());
    ctx2.addServletMapping("/", "default");

    tomcat.start();

    echoTester("");
    echoTester("/");
    // FIXME: The ws client doesn't handle any response other than the upgrade,
    // which may or may not be allowed. In that case, the server will return
    // a redirect to the root of the webapp to avoid possible broken relative
    // paths.
    // echoTester("/foo");
    echoTester("/foo/");
}
 
Example #4
Source File: TestWsRemoteEndpointImplServer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientDropsConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();

    tomcat.start();

    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);

    Session session = wsContainer.connectToServer(client, uri);
    // Break point A required on following line
    session.close();
}
 
Example #5
Source File: TestWebSocketFrameClient.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testConnectToRootEndpoint() 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");
    Context ctx2 = tomcat.addContext("/foo", null);
    ctx2.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx2, "default", new DefaultServlet());
    ctx2.addServletMappingDecoded("/", "default");

    tomcat.start();

    echoTester("",null);
    echoTester("/",null);
    echoTester("/foo",null);
    echoTester("/foo/",null);
}
 
Example #6
Source File: TestWsServerContainer.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug54807() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example #7
Source File: TestWsWebSocketContainer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void doTestPerMessageDefalteClient(String msg, int count) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Must have a real docBase - just use temp
    Context ctx =
        tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
    List<Extension> extensions = new ArrayList<Extension>(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();
}
 
Example #8
Source File: TestWsPingPongMessages.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPingPongMessages() 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.addServletMapping("/", "default");

    tomcat.start();

    WebSocketContainer wsContainer = ContainerProvider
            .getWebSocketContainer();

    tomcat.start();

    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
                    .create().build(), new URI("ws://localhost:"
                    + getPort() + TesterEchoServer.Config.PATH_ASYNC));

    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties()
            .get("endpoint");
    tep.setLatch(latch);

    PongMessageHandler handler = new PongMessageHandler(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendPing(applicationData);

    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Assert.assertArrayEquals(applicationData.array(),
            (handler.getMessages().peek()).getApplicationData().array());
}
 
Example #9
Source File: TestPojoEndpointBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnOpenPojoMethod() throws Exception {
    // Set up utility classes
    OnOpenServerEndpoint server = new OnOpenServerEndpoint();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(OnOpenServerEndpoint.class);

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();


    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + "/");

    Session session = wsContainer.connectToServer(client, uri);

    client.waitForClose(5);
    Assert.assertTrue(session.isOpen());
}
 
Example #10
Source File: TestDefaultInstanceManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void run() {
    try {
        Object test = new DefaultServlet();
        for (int i = 0; i < 200000; i++) {
            im.newInstance(test);
            im.destroyInstance(test);
        }
    } catch (NamingException | IllegalAccessException | InvocationTargetException ne) {
        ne.printStackTrace();
    }
}
 
Example #11
Source File: TomcatAutoInitializer.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public void onStartup(final Set<Class<?>> c, final ServletContext ctx) {
    final Configuration builder = Configuration.class.cast(ctx.getAttribute("meecrowave.configuration"));
    if (!builder.isTomcatAutoSetup()) {
        return;
    }

    final ServletRegistration.Dynamic def = ctx.addServlet("default", DefaultServlet.class);
    def.setInitParameter("listings", "false");
    def.setInitParameter("debug", "0");
    def.setLoadOnStartup(1);
    def.addMapping("/");

    try {
        final String jsp = "org.apache.jasper.servlet.JspServlet";
        TomcatAutoInitializer.class.getClassLoader().loadClass(jsp);
        final ServletRegistration.Dynamic jspDef = ctx.addServlet("jsp", jsp);
        if (jspDef != null) {
            jspDef.setInitParameter("fork", "false");
            jspDef.setInitParameter("xpoweredBy", "false");
            jspDef.setInitParameter("development", Boolean.toString(builder.isTomcatJspDevelopment()));
            jspDef.setLoadOnStartup(3);
            jspDef.addMapping("*.jsp");
            jspDef.addMapping("*.jspx");
        }
    } catch (final NoClassDefFoundError | ClassNotFoundException e) {
        // not there, skip
    }
}
 
Example #12
Source File: TestPojoEndpointBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug54716() throws Exception {
    TestUtil.generateMask();
    // Set up utility classes
    Bug54716 server = new Bug54716();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(Bug54716.class);

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();


    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + "/");

    wsContainer.connectToServer(client, uri);

    // Server should close the connection after the exception on open.
    boolean closed = client.waitForClose(5);
    Assert.assertTrue("Server failed to close connection", closed);
}
 
Example #13
Source File: TestPojoEndpointBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug54716() throws Exception {
    TestUtil.generateMask();
    // Set up utility classes
    Bug54716 server = new Bug54716();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(Bug54716.class);

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();


    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + "/");

    wsContainer.connectToServer(client, uri);

    // Server should close the connection after the exception on open.
    boolean closed = client.waitForClose(5);
    Assert.assertTrue("Server failed to close connection", closed);
}
 
Example #14
Source File: TestWsServerContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug58232() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());

    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/echoBasic");

    try (Session session = wsContainer.connectToServer(client, uri);) {
        CountDownLatch latch = new CountDownLatch(1);
        BasicText handler = new BasicText(latch);
        session.addMessageHandler(handler);
        session.getBasicRemote().sendText("echoBasic");

        boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
        Assert.assertTrue(latchResult);

        Queue<String> messages = handler.getMessages();
        Assert.assertEquals(1, messages.size());
        for (String message : messages) {
            Assert.assertEquals("echoBasic", message);
        }
    }
}
 
Example #15
Source File: TestPojoEndpointBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnOpenPojoMethod() throws Exception {
    // Set up utility classes
    OnOpenServerEndpoint server = new OnOpenServerEndpoint();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(OnOpenServerEndpoint.class);

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();


    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + "/");

    Session session = wsContainer.connectToServer(client, uri);

    client.waitForClose(5);
    Assert.assertTrue(session.isOpen());
}
 
Example #16
Source File: TestWsPingPongMessages.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testPingPongMessages() 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.addServletMapping("/", "default");

    tomcat.start();

    WebSocketContainer wsContainer = ContainerProvider
            .getWebSocketContainer();

    tomcat.start();

    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder
                    .create().build(), new URI("ws://localhost:"
                    + getPort() + TesterEchoServer.Config.PATH_ASYNC));

    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties()
            .get("endpoint");
    tep.setLatch(latch);

    PongMessageHandler handler = new PongMessageHandler(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendPing(applicationData);

    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Assert.assertArrayEquals(applicationData.array(),
            (handler.getMessages().peek()).getApplicationData().array());
}
 
Example #17
Source File: TestWsSessionSuspendResume.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void test() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(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() + Config.PATH));

    final CountDownLatch latch = new CountDownLatch(2);
    wsSession.addMessageHandler(new MessageHandler.Whole<String>() {

        @Override
        public void onMessage(String message) {
            Assert.assertTrue("[echo, echo, echo]".equals(message));
            latch.countDown();
        }
    });
    for (int i = 0; i < 8; i++) {
        wsSession.getAsyncRemote().sendText("echo");
    }

    boolean latchResult = latch.await(30, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);

    wsSession.close();
}
 
Example #18
Source File: TestEncodingDecoding.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testUnsupportedObject() throws Exception{
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgramaticServerEndpointConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);

    // This should fail
    Object msg1 = new Object();
    try {
        session.getBasicRemote().sendObject(msg1);
        Assert.fail("No exception thrown ");
    } catch (EncodeException e) {
        // Expected
    } catch (Throwable t) {
        Assert.fail("Wrong exception type");
    } finally {
        session.close();
    }
}
 
Example #19
Source File: TestShutdown.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testShutdownBufferedMessages() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(EchoBufferedConfig.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() + "/test"));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");

    int count = 0;
    while (count < 10 && EchoBufferedEndpoint.messageCount.get() == 0) {
        Thread.sleep(200);
        count++;
    }
    Assert.assertNotEquals("Message not received by server",
            EchoBufferedEndpoint.messageCount.get(), 0);

    tomcat.stop();

    Assert.assertTrue("Latch expired waiting for message", latch.await(10, TimeUnit.SECONDS));
}
 
Example #20
Source File: TestWsServerContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug54807() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example #21
Source File: TestConnectionLimit.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testSingleMachine() 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.getConnector().setAttribute("maxConnections", "-1");

    tomcat.start();

    URI uri = new URI("ws://localhost:" + getPort() +
            TesterEchoServer.Config.PATH_ASYNC);
    AtomicInteger counter = new AtomicInteger(0);

    int threadCount = 50;

    Thread[] threads = new ConnectionThread[threadCount];

    for (int i = 0; i < threadCount; i++) {
        threads[i] = new ConnectionThread(counter, uri);
        threads[i].start();
    }

    // Wait for the threads to die
    for (Thread thread : threads) {
        thread.join();
    }

    System.out.println("Maximum connection count was " + counter.get());
}
 
Example #22
Source File: TestWsWebSocketContainer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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.addServletMapping("/", "default");

    tomcat.start();

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();

    Session s = connectToEchoServer(wsContainer, new EndpointA(), path);

    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 session to close
    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()));
}
 
Example #23
Source File: TestWebSocket.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUpgrade() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(new ApplicationListener(
            TesterEchoServer.Config.class.getName(), false));

    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    WebSocketClient client= new WebSocketClient(getPort());

    // Send the WebSocket handshake
    client.writer.write("GET " + TesterEchoServer.Config.PATH_BASIC + " HTTP/1.1" + CRLF);
    client.writer.write("Host: foo" + CRLF);
    client.writer.write("Connection: upgrade" + CRLF);
    client.writer.write("Sec-WebSocket-Version: 13" + CRLF);
    client.writer.write("Sec-WebSocket-Key: TODO" + CRLF);
    client.writer.write(CRLF);
    client.writer.flush();

    // Make sure we got an error response
    // No upgrade means it is not treated an as upgrade request so a 404 is
    // generated when the request reaches the Default Servlet.s
    String responseLine = client.reader.readLine();
    assertTrue(responseLine.startsWith("HTTP/1.1 404"));

    // Finished with the socket
    client.close();
}
 
Example #24
Source File: TestClose.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private Tomcat startServer(
        final Class<? extends WsContextListener> configClass)
        throws LifecycleException {

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(configClass.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();
    return tomcat;
}
 
Example #25
Source File: WebSocketBaseTest.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected Tomcat startServer(
        final Class<? extends WsContextListener> configClass)
        throws LifecycleException {

    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(configClass.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");

    tomcat.start();
    return tomcat;
}
 
Example #26
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testConnectToServerEndpoint() 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();
    // Set this artificially small to trigger
    // https://bz.apache.org/bugzilla/show_bug.cgi?id=57054
    wsContainer.setDefaultMaxBinaryMessageBufferSize(64);
    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class,
            ClientEndpointConfig.Builder.create().build(),
            new URI("ws://" + getHostName() + ":" + getPort() +
                    TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);

    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);

    Assert.assertTrue(latchResult);

    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(MESSAGE_STRING_1, messages.peek());

    ((WsWebSocketContainer) wsContainer).destroy();
}
 
Example #27
Source File: TestWsServerContainer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug54807() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example #28
Source File: TestWsWebSocketContainer.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectToServerEndpoint() 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.addServletMapping("/", "default");

    tomcat.start();

    WebSocketContainer wsContainer =
            ContainerProvider.getWebSocketContainer();
    // Set this artificially small to trigger
    // https://bz.apache.org/bugzilla/show_bug.cgi?id=57054
    wsContainer.setDefaultMaxBinaryMessageBufferSize(64);
    Session wsSession = wsContainer.connectToServer(
            TesterProgrammaticEndpoint.class,
            ClientEndpointConfig.Builder.create().build(),
            new URI("ws://" + getHostName() + ":" + getPort() +
                    TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);

    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);

    Assert.assertTrue(latchResult);

    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(MESSAGE_STRING_1, messages.peek());

    ((WsWebSocketContainer) wsContainer).destroy();
}
 
Example #29
Source File: TestEncodingDecoding.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsupportedObject() throws Exception{
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgramaticServerEndpointConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");

    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();

    tomcat.start();

    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);

    // This should fail
    Object msg1 = new Object();
    try {
        session.getBasicRemote().sendObject(msg1);
        Assert.fail("No exception thrown ");
    } catch (EncodeException e) {
        // Expected
    } catch (Throwable t) {
        Assert.fail("Wrong exception type");
    } finally {
        session.close();
    }
}
 
Example #30
Source File: TestWsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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();
}