Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Filtering multipart content

Lance Semmens

2007-03-22

Replies:

Can someone tell me the best way to deal with multipart parameters in a
Filter without affecting struts?

In my filter, request.getParameterMap() returns an empty map because the
multipart content has not yet been parsed. I had a look in the struts
code and saw references to a MultipartRequestWrapper and a
CommonsMultipartRequestHandler. From what I can see, the requestHandler
parses the request and calls MultipartRequestWrapper.setParameter().

So... in my filter I do the following:

protected void doFilter(HttpServletRequest request, HttpServletResponse
response, FilterChain chain) throws IOException, ServletException {
  boolean isMultipart = isMultipart(request);
  Map parameterMap;
  if (isMultipart) {
    // wrap the request so that
CommonsMultipartRequestHandler.handleRequest()
    // can call set parameter. the wrapped request is then passed
down the filter chain
    request = new MultipartRequestWrapper(request);
    MultipartRequestHandler multipartHandler = new
CommonsMultipartRequestHandler();
    ModuleConfig moduleConfig = (ModuleConfig)
servletContext.getAttribute(Globals.MODULE_KEY);
    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
    multipartHandler.handleRequest(request);
    request.removeAttribute(Globals.MODULE_KEY);
    parameterMap = multipartHandler.getTextElements();
  } else {
    parameterMap = request.getParameterMap();
  }

  // do some stuff with the parameters

  chain.doFilter(request, response);
}

The problem now is that when I upload a file, struts thinks the file is
null. request.getParameter() works for normal text parameters on the
multipart form.
I have also tried parsing the request and sending the non-wrapped
request down the chain but struts does not re-parse the request if i do
this.

I am using struts 1.3.5

Thanks,
Lance.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.