Search in sources :

Example 66 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat70 by apache.

the class TestPojoEndpointBase method testBug54716.

@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);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 67 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat70 by apache.

the class TestCloseBug58624 method testOnErrorNotCalledWhenClosingConnection.

@Test
public void testOnErrorNotCalledWhenClosingConnection() throws Throwable {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624ServerConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Bug58624ClientEndpoint client = new Bug58624ClientEndpoint();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624ServerConfig.PATH);
    Session session = wsContainer.connectToServer(client, uri);
    // Wait for session to open on the server
    int count = 0;
    while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() == 0) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertNotEquals(0, Bug58624ServerEndpoint.getOpenSessionCount());
    // Now close the session
    session.close();
    // Wait for session to close on the server
    count = 0;
    while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() > 0) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertEquals(0, Bug58624ServerEndpoint.getOpenSessionCount());
    // Ensure no errors were reported on the server
    Assert.assertEquals(0, Bug58624ServerEndpoint.getErrorCount());
    if (client.getError() != null) {
        throw client.getError();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) ClientEndpoint(javax.websocket.ClientEndpoint) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 68 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat70 by apache.

the class TestWebSocket method testNoConnection.

@Test
public void testNoConnection() 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("Upgrade: websocket" + 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
    String responseLine = client.reader.readLine();
    Assert.assertTrue(responseLine.startsWith("HTTP/1.1 400"));
    // Finished with the socket
    client.close();
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterEchoServer(org.apache.tomcat.websocket.TesterEchoServer) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 69 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project cxf by apache.

the class StatsServer method main.

public static void main(final String[] args) throws Exception {
    // Register and map the dispatcher servlet
    final File base = new File(System.getProperty("java.io.tmpdir"));
    final Tomcat server = new Tomcat();
    server.setPort(8686);
    server.setBaseDir(base.getAbsolutePath());
    final StandardContext context = (StandardContext) server.addWebapp("/", base.getAbsolutePath());
    context.setConfigFile(StatsServer.class.getResource("/META-INF/context.xml"));
    context.addApplicationListener(ContextLoaderListener.class.getName());
    context.setAddWebinfClassesResources(true);
    context.setResources(resourcesFrom(context, "target/classes"));
    context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
    final Wrapper cxfServlet = Tomcat.addServlet(context, "cxfServlet", new CXFServlet());
    cxfServlet.setAsyncSupported(true);
    context.addServletMappingDecoded("/rest/*", "cxfServlet");
    final Context staticContext = server.addWebapp("/static", base.getAbsolutePath());
    Tomcat.addServlet(staticContext, "cxfStaticServlet", new DefaultServlet());
    staticContext.addServletMappingDecoded("/static/*", "cxfStaticServlet");
    staticContext.setResources(resourcesFrom(staticContext, "target/classes/web-ui"));
    staticContext.setParentClassLoader(Thread.currentThread().getContextClassLoader());
    server.start();
    server.getServer().await();
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) CXFServlet(org.apache.cxf.transport.servlet.CXFServlet) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) File(java.io.File)

Example 70 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project Payara by payara.

the class StandardContext method setDirectoryListing.

/**
 * Enables or disables directory listings on this <tt>Context</tt>.
 */
public void setDirectoryListing(boolean directoryListing) {
    this.directoryListing = directoryListing;
    Wrapper wrapper = (Wrapper) findChild(org.apache.catalina.core.Constants.DEFAULT_SERVLET_NAME);
    if (wrapper != null) {
        Servlet servlet = ((StandardWrapper) wrapper).getServlet();
        if (servlet instanceof DefaultServlet) {
            ((DefaultServlet) servlet).setListings(directoryListing);
        }
    }
}
Also used : Wrapper(org.apache.catalina.Wrapper) Servlet(javax.servlet.Servlet) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) DefaultServlet(org.apache.catalina.servlets.DefaultServlet)

Aggregations

DefaultServlet (org.apache.catalina.servlets.DefaultServlet)93 Context (org.apache.catalina.Context)91 Tomcat (org.apache.catalina.startup.Tomcat)91 Test (org.junit.Test)70 URI (java.net.URI)59 Session (jakarta.websocket.Session)39 WebSocketContainer (jakarta.websocket.WebSocketContainer)36 Session (javax.websocket.Session)30 WebSocketContainer (javax.websocket.WebSocketContainer)27 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)27 CountDownLatch (java.util.concurrent.CountDownLatch)24 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)24 ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)23 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)19 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)19 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)18 Endpoint (jakarta.websocket.Endpoint)15 ServerEndpoint (jakarta.websocket.server.ServerEndpoint)15 ServerEndpoint (javax.websocket.server.ServerEndpoint)14 Endpoint (javax.websocket.Endpoint)13