A rewrite rule like: RewriteRule ^/path/.*$ - [T=text/xml] fails with a NullPointerException because the contentTypeMB field in the Request object has not initialized. Request.setContentType should initialize contentTypeMB, or the rewrite valve should call getContentType first. The former is preferable in case there is no content type in the request. This always happens with the HTTP/1.1 connector. With the AJP connector the exeception occurs if the request does not contain a Content-Type header. Backtrace: java.lang.NullPointerException: Cannot invoke "org.apache.tomcat.util.buf.MessageBytes.setString(String)" because "this.contentTypeMB" is null at org.apache.coyote.Request.setContentType(Request.java:465) at org.apache.catalina.connector.Request.setContentType(Request.java:1057) at org.apache.catalina.valves.rewrite.RewriteValve.invoke(RewriteValve.java:448) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833)
This should be setting the content-type on the response rather than the request.
Fixed in: - 10.1.x for 10.1.0-M12 onwards - 10.0.x for 10.0.18 onwards - 9.0.x for 9.0.60 onwards - 8.5.x for 8.5.77 onwards
The use case here was to fix up the content-type in a request before it reaches he application (the client code was sending the wrong type). If this RewriteRule mechanism is in fact designed to set the response type, how can the request type be changed? I can write a custom valve but the rewrite mechanism seemed better.
The portable way would be to use a Filter and wrap the HttpServletRequest.