Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 8.0 - mime-mapping & content-type

Recently I've been digging into an issue that has lead me the "mime-mapping" element defined in the web-common_X_X.xsd and utilized in a web apps web.xml file. My goal here is to configure Tomcat to include a specific Content-Type header when returning a response to a specific servlet.

I've found previous stack overflow posts mentioning the functionality, but I'm unable to get a trivial example to work with Tomcat 8.0.33.

For this test, I've created the following servlet:

public class SimpleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IOUtils.write("Hello There!", resp.getOutputStream());
        resp.setStatus(202);
    }
}

And have the following web.xml:

<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <servlet>
    <servlet-name>SimpleServlet</servlet-name>
    <servlet-class>com.jamf.swa.SimpleServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>SimpleServlet</servlet-name>
    <url-pattern>*.swa</url-pattern>
  </servlet-mapping>

  <mime-mapping>
    <extension>swa</extension>
    <mime-type>text/rtf;charset=UTF-8</mime-type>
  </mime-mapping>

</web-app>

I've attempted this test with and without the charset included in the "mime-type" element. The "text/rtf" type is also arbitrary. I've tested others.

Once the application is started, I make the following request to /testing.swa:

curl --trace-ascii - http://localhost:8080/testing.swa
== Info:   Trying ::1...
== Info: TCP_NODELAY set
== Info: Connected to localhost (::1) port 8080 (#0)
=> Send header, 89 bytes (0x59)
0000: GET /testing.swa HTTP/1.1
001b: Host: localhost:8080
0031: User-Agent: curl/7.51.0
004a: Accept: */*
0057: 
<= Recv header, 23 bytes (0x17)
0000: HTTP/1.1 202 Accepted
<= Recv header, 27 bytes (0x1b)
0000: Server: Apache-Coyote/1.1
<= Recv header, 20 bytes (0x14)
0000: Content-Length: 12
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 15 Feb 2017 22:37:17 GMT
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 12 bytes (0xc)
0000: Hello There!
Hello There!== Info: Curl_http_done: called premature == 0
== Info: Connection #0 to host localhost left intact

As you can see, no Content-Type header included.

I can confirm that StandardContext:addMimeMapping() is correctly being called with my mapping, but I'm never seeing the mimeMappings local variable being read during my request.

I'm also not finding any documentation on Tomcat's side for using this element with v8+.

Am I'm missing something trivial? Has Tomcat moved away from supporting this functionality? Any help you can provide would be appreciated.

like image 358
Staros Avatar asked Oct 18 '22 17:10

Staros


1 Answers

Per the Tomcat mailing list, the "mime-mapping" functionality is only utilized by Tomcat's DefaultServlet. Other servlets can utilize this mechanic but it has to be explicitly built in.

The functionality I'm describing would be be accomplished by a Filter and I think that's the route I'll head.

like image 90
Staros Avatar answered Oct 20 '22 23:10

Staros