Java Code Examples for org.apache.catalina.Context#addMimeMapping()

The following examples show how to use org.apache.catalina.Context#addMimeMapping() . 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: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Static version of {@link #initWebappDefaults(String)}
 * @param ctx   The context to set the defaults for
 */
public static void initWebappDefaults(Context ctx) {
    // Default servlet
    Wrapper servlet = addServlet(
            ctx, "default", "org.apache.catalina.servlets.DefaultServlet");
    servlet.setLoadOnStartup(1);
    servlet.setOverridable(true);

    // JSP servlet (by class name - to avoid loading all deps)
    servlet = addServlet(
            ctx, "jsp", "org.apache.jasper.servlet.JspServlet");
    servlet.addInitParameter("fork", "false");
    servlet.setLoadOnStartup(3);
    servlet.setOverridable(true);

    // Servlet mappings
    ctx.addServletMappingDecoded("/", "default");
    ctx.addServletMappingDecoded("*.jsp", "jsp");
    ctx.addServletMappingDecoded("*.jspx", "jsp");

    // Sessions
    ctx.setSessionTimeout(30);

    // MIME mappings
    for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) {
        ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++],
                DEFAULT_MIME_MAPPINGS[i++]);
    }

    // Welcome files
    ctx.addWelcomeFile("index.html");
    ctx.addWelcomeFile("index.htm");
    ctx.addWelcomeFile("index.jsp");
}
 
Example 2
Source File: Tomcat.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #initWebappDefaults(String)}
 * @param ctx   The context to set the defaults for
 */
public static void initWebappDefaults(Context ctx) {
    // Default servlet 
    Wrapper servlet = addServlet(
            ctx, "default", "org.apache.catalina.servlets.DefaultServlet");
    servlet.setLoadOnStartup(1);
    servlet.setOverridable(true);

    // JSP servlet (by class name - to avoid loading all deps)
    servlet = addServlet(
            ctx, "jsp", "org.apache.jasper.servlet.JspServlet");
    servlet.addInitParameter("fork", "false");
    servlet.setLoadOnStartup(3);
    servlet.setOverridable(true);

    // Servlet mappings
    ctx.addServletMapping("/", "default");
    ctx.addServletMapping("*.jsp", "jsp");
    ctx.addServletMapping("*.jspx", "jsp");

    // Sessions
    ctx.setSessionTimeout(30);
    
    // MIME mappings
    for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) {
        ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++],
                DEFAULT_MIME_MAPPINGS[i++]);
    }
    
    // Welcome files
    ctx.addWelcomeFile("index.html");
    ctx.addWelcomeFile("index.htm");
    ctx.addWelcomeFile("index.jsp");
}
 
Example 3
Source File: Tomcat.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Static version of {@link #initWebappDefaults(String)}
 * @param ctx   The context to set the defaults for
 */
public static void initWebappDefaults(Context ctx) {
    // Default servlet 
    Wrapper servlet = addServlet(
            ctx, "default", "org.apache.catalina.servlets.DefaultServlet");
    servlet.setLoadOnStartup(1);
    servlet.setOverridable(true);

    // JSP servlet (by class name - to avoid loading all deps)
    servlet = addServlet(
            ctx, "jsp", "org.apache.jasper.servlet.JspServlet");
    servlet.addInitParameter("fork", "false");
    servlet.setLoadOnStartup(3);
    servlet.setOverridable(true);

    // Servlet mappings
    ctx.addServletMapping("/", "default");
    ctx.addServletMapping("*.jsp", "jsp");
    ctx.addServletMapping("*.jspx", "jsp");

    // Sessions
    ctx.setSessionTimeout(30);
    
    // MIME mappings
    for (int i = 0; i < DEFAULT_MIME_MAPPINGS.length;) {
        ctx.addMimeMapping(DEFAULT_MIME_MAPPINGS[i++],
                DEFAULT_MIME_MAPPINGS[i++]);
    }
    
    // Welcome files
    ctx.addWelcomeFile("index.html");
    ctx.addWelcomeFile("index.htm");
    ctx.addWelcomeFile("index.jsp");
}
 
Example 4
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testGzippedFile() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    File gzipIndex = new File(appDir, "index.html.gz");
    long gzipSize = gzipIndex.length();

    File index = new File(appDir, "index.html");
    long indexSize = index.length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            "org.apache.catalina.servlets.DefaultServlet");
    defaultServlet.addInitParameter("gzip", "true");
    defaultServlet.addInitParameter("fileEncoding", "ISO-8859-1");
    ctxt.addServletMappingDecoded("/", "default");

    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestCompressedClient gzipClient = new TestCompressedClient(getPort());

    gzipClient.reset();
    gzipClient.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
            "Host: localhost" + CRLF +
            "Connection: Close" + CRLF +
            "Accept-Encoding: gzip, br" + CRLF + CRLF });
    gzipClient.connect();
    gzipClient.processRequest();
    Assert.assertTrue(gzipClient.isResponse200());
    List<String> responseHeaders = gzipClient.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: gzip"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + gzipSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));

    gzipClient.reset();
    gzipClient.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
            "Host: localhost" + CRLF +
            "Connection: Close" + CRLF+ CRLF });
    gzipClient.connect();
    gzipClient.processRequest();
    Assert.assertTrue(gzipClient.isResponse200());
    responseHeaders = gzipClient.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Type: text/html"));
    Assert.assertFalse(responseHeaders.contains("Content-Encoding: gzip"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + indexSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));
}
 
Example 5
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBrotliCompressedFile() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    long brSize = new File(appDir, "index.html.br").length();
    long indexSize = new File(appDir, "index.html").length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            "org.apache.catalina.servlets.DefaultServlet");
    defaultServlet.addInitParameter("precompressed", "true");
    defaultServlet.addInitParameter("fileEncoding", "ISO-8859-1");

    ctxt.addServletMappingDecoded("/", "default");
    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestCompressedClient client = new TestCompressedClient(getPort());

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: br, gzip" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    List<String> responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: br"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + brSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF+ CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Type: text/html"));
    Assert.assertFalse(responseHeaders.contains("Content-Encoding"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + indexSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));
}
 
Example 6
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testCustomCompressedFile() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    long brSize = new File(appDir, "index.html.br").length();
    long gzSize = new File(appDir, "index.html.gz").length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            DefaultServlet.class.getName());
    defaultServlet.addInitParameter("precompressed", "gzip=.gz,custom=.br");

    ctxt.addServletMappingDecoded("/", "default");
    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestCompressedClient client = new TestCompressedClient(getPort());

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: br, gzip ; q = 0.5 , custom" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    List<String> responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: custom"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + brSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: br;q=1,gzip,custom" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: gzip"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + gzSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));
}
 
Example 7
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testIdentityAndStarAcceptEncodings() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    long brSize = new File(appDir, "index.html.br").length();
    long indexSize = new File(appDir, "index.html").length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            DefaultServlet.class.getName());
    defaultServlet.addInitParameter("precompressed", "br=.br,gzip=.gz");
    defaultServlet.addInitParameter("fileEncoding", "ISO-8859-1");

    ctxt.addServletMappingDecoded("/", "default");
    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestCompressedClient client = new TestCompressedClient(getPort());

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: gzip;q=0.9,*" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    List<String> responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: br"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + brSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));

    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: gzip;q=0.9,br;q=0,identity," + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    responseHeaders = client.getResponseHeaders();
    Assert.assertFalse(responseHeaders.contains("Content-Encoding"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + indexSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));
}
 
Example 8
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testBrotliPreference() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    long brSize = new File(appDir, "index.html.br").length();

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            DefaultServlet.class.getName());
    defaultServlet.addInitParameter("precompressed", "true");

    ctxt.addServletMappingDecoded("/", "default");
    ctxt.addMimeMapping("html", "text/html");

    tomcat.start();

    TestCompressedClient client = new TestCompressedClient(getPort());

    // Firefox 45 Accept-Encoding
    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: gzip, deflate, br" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    List<String> responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: br"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + brSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));

    // Chrome 50 Accept-Encoding
    client.reset();
    client.setRequest(new String[] {
            "GET /index.html HTTP/1.1" + CRLF +
                    "Host: localhost" + CRLF +
                    "Connection: Close" + CRLF +
                    "Accept-Encoding: gzip, deflate, sdch, br" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse200());
    responseHeaders = client.getResponseHeaders();
    Assert.assertTrue(responseHeaders.contains("Content-Encoding: br"));
    Assert.assertTrue(responseHeaders.contains("Content-Length: " + brSize));
    Assert.assertTrue(responseHeaders.contains("vary: accept-encoding"));
}
 
Example 9
Source File: TestDefaultServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testCustomErrorPage() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    // app dir is relative to server home
    Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
    Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default",
            DefaultServlet.class.getName());
    defaultServlet.addInitParameter("fileEncoding", "ISO-8859-1");

    ctxt.addServletMappingDecoded("/", "default");
    ctxt.addMimeMapping("html", "text/html");
    ErrorPage ep = new ErrorPage();
    ep.setErrorCode(404);
    ep.setLocation("/404.html");
    ctxt.addErrorPage(ep);

    tomcat.start();

    TestCustomErrorClient client =
            new TestCustomErrorClient(tomcat.getConnector().getLocalPort());

    client.reset();
    client.setRequest(new String[] {
            "GET /MyApp/missing HTTP/1.0" +CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse404());
    Assert.assertEquals("It is 404.html", client.getResponseBody());

    SimpleDateFormat format = new SimpleDateFormat(
            "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    String tomorrow = format.format(new Date(System.currentTimeMillis()
            + 24 * 60 * 60 * 1000));

    // https://bz.apache.org/bugzilla/show_bug.cgi?id=50413
    //
    client.reset();
    client.setRequest(new String[] {
            "GET /MyApp/missing HTTP/1.1" + CRLF +
            "Host: localhost" + CRLF +
            "Connection: close" + CRLF +
            "If-Modified-Since: " + tomorrow + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse404());
    Assert.assertEquals("It is 404.html", client.getResponseBody());

    // https://bz.apache.org/bugzilla/show_bug.cgi?id=50413#c6
    //
    client.reset();
    client.setRequest(new String[] {
            "GET /MyApp/missing HTTP/1.1" + CRLF +
            "Host: localhost" + CRLF +
            "Connection: close" + CRLF +
            "Range: bytes=0-100" + CRLF + CRLF });
    client.connect();
    client.processRequest();
    Assert.assertTrue(client.isResponse404());
    Assert.assertEquals("It is 404.html", client.getResponseBody());
}