tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Greco <>
Subject Rewriting entire request /body in Servlet Filter not working as expected
Date Sun, 04 Oct 2015 18:03:57 GMT
First time post here.

Using :
Tomcat 8.0.26
JDK1.8.0 update 51
Apache MyFaces 2.2.8.
Maven build of webapp war file
Chrome  45.0.2454.101 m 64 bit
Windows 7 64 bit

Trying to rewrite the entire request body in a filter using a http request
wrapper.  Not entirely sure if this is the right approach or even possible
but researching similar examples it seems this approach should work.
Created a simple test case with a one page test app, expecting the
index.xhtml page's text value of  "Welcome" to be replaced with the text
"NEW TEXT HERE" found in the filter code, but I only get "Welcome" back
when the page is rendered in the browser.

Below is what I have in the index page, the filter and the web.xml and the
pom.xml.  The reason I can see is that the getInputStream() or getReader()
calls are never called by the FacesServlet in the extended
HttpServletRequestWrapper.  I did override the getParameter function to put
some simple debug output in there to ensure the wrapped request was
actually used, so I know it's definitely getting into this class but not
calling the methods that would return it the new body content.

I even tried to re-read the entire request in the getParametersMap() from
examples I found on the web, but this didn't seen to do the job either.  It
seems that the request body is somehow cached somewhere / somehow and not

Thanks in advance for any insight on this.

index page code (index.xhtml) :
<!DOCTYPE html>
<html lang="en"
<head jsf:id="head">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Test Webapp</title>
<body jsf:id="body">

Filter code :
package com.testwebapp.reqrewrite.filter;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class RewriteBodyTestFilter implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
String newReqBody = new String("<div><h1>NEW TEXT HERE</h1></div>");
HttpServletRequest req = (HttpServletRequest)request;
RewriteBodyRequestWrapper rewriteBodyRequestWrapper = new
RewriteBodyRequestWrapper(req, newReqBody);
chain.doFilter(rewriteBodyRequestWrapper, response);

public void destroy() {
class RewriteBodyRequestWrapper extends HttpServletRequestWrapper {

private byte[] buffer;

public RewriteBodyRequestWrapper(HttpServletRequest req, String reqBody)
throws IOException {
System.out.println("entering RewriteBodyRequestWrapper() constructor");
this.buffer = reqBody.getBytes();

public ServletInputStream getInputStream() {
System.out.println("entering RewriteBodyRequestWrapper().getInputStream()");
try {
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
BufferedServletInputStream bsis = new BufferedServletInputStream(bais);
return bsis;
} catch (Exception ex) {
throw new RuntimeException(ex);

public BufferedReader getReader() {
System.out.println("entering RewriteBodyRequestWrapper().getReader()");
InputStreamReader isr = new InputStreamReader(getInputStream());
return new BufferedReader(isr);

class BufferedServletInputStream extends ServletInputStream  {

ByteArrayInputStream bais;

public BufferedServletInputStream(ByteArrayInputStream bais) {
this.bais = bais;

public int available() {
return bais.available();

public boolean isFinished() {
return bais.available() == 0;

public boolean isReady() {
return true;

public void setReadListener(ReadListener readListener) {

public int read() {

public int read(byte[] buf) {
return, 0, buf.length);

public int read(byte[] buf, int off, int len) {
return, off, len);


web.xml code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="" xmlns:xsi=""
metadata-complete="true" version="3.0">

<display-name>Test Webapp Req Rewrite</display-name>

<!-- Change to "Production" when you are ready to deploy -->

<!-- JSF mapping -->

<!-- Map these files with JSF -->

<!-- Session timeout -->

<!-- Welcome page -->

<!-- Filters -->

<!-- Filter mappings / chain ordering -->


pom.xml :
<project xmlns="" xmlns:xsi=""
  <name>reqrewrite Maven Webapp</name>

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message