title: Servlet Filter modify header value with servlet request wrapper not working
body: <p>I am attempting to change the Content-Type header in a request and change it to "application/json" before it reaches my spring rest controller. I have created a servlet request wrapper to change the values, but when the request reaches the controller it is still "text/plain". The logging shows that the header value has been changed before hitting doFilter();</p>
  
  <p>Here is my class extending HttpServletRequestWrapper</p>
  
  <pre><code>class HttpServletRequestWritableWrapper extends HttpServletRequestWrapper {
  private final Logger logger = org.slf4j.LoggerFactory.getLogger(HttpServletRequestWritableWrapper.class);
  private final ByteArrayInputStream decryptedBody;
  
  HttpServletRequestWritableWrapper(HttpServletRequest request, byte[] decryptedData) {
      super(request);
      decryptedBody = new ByteArrayInputStream(decryptedData);
  }
  
  
  
  @Override
  public String getHeader(String name) {
      String headerValue = super.getHeader(name);
      if("Accept".equalsIgnoreCase(name))
      {
          logger.debug("Accept header changing :");
          return headerValue.replaceAll(
                  MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE
          );
      }
      else if ("Content-Type".equalsIgnoreCase(name))
      {
          logger.debug("Content type change: ");
          return headerValue.replaceAll(MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_JSON_VALUE);
      }
      return headerValue;
  }
  
  @Override
  public Enumeration&lt;String&gt; getHeaderNames() {
      return super.getHeaderNames();
  }
  
  @Override
  public String getContentType() {
      String contentTypeValue = super.getContentType();
      if (MediaType.TEXT_PLAIN_VALUE.equalsIgnoreCase(contentTypeValue)) {
          logger.debug("Changing on getContentType():");
          return MediaType.APPLICATION_JSON_VALUE;
      }
      return contentTypeValue;
  }
  
  
  @Override
  public BufferedReader getReader() throws UnsupportedEncodingException {
      return new BufferedReader(new InputStreamReader(decryptedBody, UTF_8));
  }
  
  @Override
  public ServletInputStream getInputStream() throws IOException {
      return new ServletInputStream() {
          @Override
          public int read() {
              return decryptedBody.read();
          }
      };
  }
  </code></pre>
  
  <p>And here is my filter: </p>
  
  <pre><code>@WebFilter(displayName = "EncryptionFilter", urlPatterns = "/*")
  public class EncryptionFilter implements Filter {
  private final Logger logger = org.slf4j.LoggerFactory.getLogger(EncryptionFilter.class);
  
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
  
  }
  
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      byte[] data = "{\"currentClientVersion\":{\"majorElement\":\"1\",\"minorElement\":\"2\"}}".getBytes();
  
      logger.debug("data string " + data.toString());
  
      logger.debug("Content-type before: " + servletRequest.getContentType());
  
      HttpServletRequest request = (HttpServletRequest) servletRequest;
      HttpServletRequestWritableWrapper requestWrapper = new HttpServletRequestWritableWrapper(request, data);
      //logger.debug("Accept Header: " + requestWrapper.getHeader("Accept"));
      //logger.debug("Content-Type: " + requestWrapper.getHeader("Content-Type"));
      //logger.debug("Contenttype" + requestWrapper.getContentType());
  
      filterChain.doFilter(requestWrapper, servletResponse);
  
  
  }
  
  @Override
  public void destroy() {
  
  }
  
  
  }
  </code></pre>