title: Use servlet filter to remove a form parameter from posted data
body: <p>A vendor has been posting XML data over HTTPS within a form variable named XMLContent to my Coldfusion application server. I recently moved to a more recent version of the application server and those requests are throwing 500 server errors. It is throwing the error because a second form parameter's content is not properly urlencoded, but I don't need that parameter anyway. (I contacted the vendor to fix this but they are forcing me to pay to fix their mistake so I'm looking to fix it myself if possible.)</p>
  
  <p>How would I utilize a servlet filter to remove all but the form parameter named: XMLContent
  I have tried various attempts to explicitly remove the offending parameter "TContent" but it never gets removed.</p>
  
  <p>A snippet of the data being received:</p>
  
  <pre><code>XMLContent=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3CCheck+xmlns%3D%22http .........&amp;TContent=&lt;!--?xml version="1.0" encoding="UTF-8"?--&gt;&lt;check xmlns="http...........
  </code></pre>
  
  <p>The code I've tried:</p>
  
  <pre><code>import java.io.IOException;
  import java.util.Collections;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletRequestWrapper;
  
  
  import java.util.*;
  
  
  
  public class MultipartFilter implements Filter {
  
  // Init ----------------------------------------------------------------
  
    public FilterConfig filterConfig;
  
  // Actions -------------------------------------------------------------
  
  
  public void init(FilterConfig filterConfig) throws ServletException {
      this.filterConfig = filterConfig;
  }
  
  /**
   * Check the type request and if it is a HttpServletRequest, then parse the request.
   * @throws ServletException If parsing of the given HttpServletRequest fails.
   * @see javax.servlet.Filter#doFilter(
   *      javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
   */
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws ServletException, IOException
  {
      // Check type request.
      if (request instanceof HttpServletRequest) {
          // Cast back to HttpServletRequest.
          HttpServletRequest httpRequest = (HttpServletRequest) request;
  
          // Parse HttpServletRequest.
          HttpServletRequest parsedRequest = parseRequest(httpRequest);
  
          // Continue with filter chain.
          chain.doFilter(parsedRequest, response);
      } else {
          // Not a HttpServletRequest.
          chain.doFilter(request, response);
      }
  }
  
  /**
   * @see javax.servlet.Filter#destroy()
   */
  public void destroy() {
      this.filterConfig = null;
  }
  
  
  
  private HttpServletRequest parseRequest(HttpServletRequest request) throws ServletException {
  
      // Prepare the request parameter map.
      Map&lt;String, String[]&gt; parameterMap = new HashMap&lt;String, String[]&gt;();
  
      // Loop through form parameters.
  Enumeration&lt;String&gt; parameterNames = request.getParameterNames();
  
      while (parameterNames.hasMoreElements()) {
      String paramName = parameterNames.nextElement();
      String[] paramValues = request.getParameterValues(paramName);
  
              // Add just the XMLContent form parameter
      if (paramName.equalsIgnoreCase("xmlcontent")) {   
  
          parameterMap.put(paramName, new String[] { paramValues[0] });
  
      }
  }
  
      // Wrap the request with the parameter map which we just created and return it.
      return wrapRequest(request, parameterMap);
  }
  
  
  
  // Utility (may be refactored to public utility class) ---------------
  
  /**
   * Wrap the given HttpServletRequest with the given parameterMap.
   * @param request The HttpServletRequest of which the given parameterMap have to be wrapped in.
   * @param parameterMap The parameterMap to be wrapped in the given HttpServletRequest.
   * @return The HttpServletRequest with the parameterMap wrapped in.
   */
  private static HttpServletRequest wrapRequest(
      HttpServletRequest request, final Map&lt;String, String[]&gt; parameterMap)
  {
      return new HttpServletRequestWrapper(request) {
          public Map&lt;String, String[]&gt; getParameterMap() {
              return parameterMap;
          }
          public String[] getParameterValues(String name) {
              return parameterMap.get(name);
          }
          public String getParameter(String name) {
              String[] params = getParameterValues(name);
              return params != null &amp;&amp; params.length &gt; 0 ? params[0] : null;
          }
          public Enumeration&lt;String&gt; getParameterNames() {
              return Collections.enumeration(parameterMap.keySet());
          }
      };
    }
  }
  </code></pre>