1 Reply Latest reply on Sep 25, 2009 8:11 PM by John Eckhart

    SeamFilter called twice, breaks s:fileUpload

    John Eckhart Newbie

      I've been chasing down a problem where the s:fileUpload doesn't seem to work for my configuration. After some digging, I found that, for some reason, the SeamFilter is called twice per request in my app server (WebSphere 7.0.0.3). I am still trying to figure out why WebSphere is calling the SeamFilter twice, but in effect, this causes the request to be wrapped TWICE by the MultipartFilter with the second time containing no data. While I can appreciate that the app server is doing something wrong, I have little control over that behavior. Instead, I patched the MultipartFilter to check if the current request had already been wrapped by a MultipartFilter before:


      --- src.orig/main/org/jboss/seam/web/MultipartFilter.java     2009-07-29 19:42:12.000000000 +0000
      +++ src/main/org/jboss/seam/web/MultipartFilter.java     2009-09-10 11:57:26.373327345 +0000
      @@ -4,6 +4,8 @@
       import static org.jboss.seam.annotations.Install.BUILT_IN;
       
       import java.io.IOException;
      +import java.lang.reflect.Field;
      +import java.lang.reflect.Method;
       
       import javax.servlet.FilterChain;
       import javax.servlet.ServletException;
      @@ -75,7 +77,7 @@
       
             HttpServletRequest httpRequest = (HttpServletRequest) request;
       
      -      if (isMultipartRequest(httpRequest))
      +      if (isMultipartRequest(httpRequest) && !isWrappedMultipartRequest(httpRequest))
             {
                MultipartRequest multipartRequest = new MultipartRequestImpl(httpRequest, createTempFiles, 
                      maxRequestSize); 
      @@ -91,7 +93,7 @@
             }
          }
          
      -   private boolean isMultipartRequest(HttpServletRequest request)
      +   private static boolean isMultipartRequest(HttpServletRequest request)
          {
             if (!"post".equals(request.getMethod().toLowerCase()))
             {
      @@ -111,4 +113,53 @@
             
             return false;     
          }
      +
      +   // For some reason, on my app server, the Seam servlet filter is being
      +   // called twice, so let's see if this has already been wrapped
      +   private static boolean isWrappedMultipartRequest(ServletRequest request)
      +   {
      +      while (!(request instanceof MultipartRequest))
      +      {
      +         boolean found = false;
      +
      +         for (Method m : request.getClass().getMethods())
      +         {
      +            if (ServletRequest.class.isAssignableFrom(m.getReturnType())
      +                     && m.getParameterTypes().length == 0)
      +            {
      +               try
      +               {
      +                  request = (ServletRequest) m.invoke(request);
      +                  found = true;
      +                  break;
      +               }
      +               catch (Exception ex)
      +               { /* Ignore, try the next one */
      +               }
      +            }
      +         }
      +
      +         if (!found)
      +         {
      +            for (Field f : request.getClass().getDeclaredFields())
      +            {
      +               if (ServletRequest.class.isAssignableFrom(f.getType()))
      +               {
      +                  try
      +                  {
      +                     request = (ServletRequest) f.get(request);
      +                  }
      +                  catch (Exception ex)
      +                  { /* Ignore */
      +                  }
      +               }
      +            }
      +         }
      +
      +         if (!found) break;
      +      }
      +
      +      return (request instanceof MultipartRequest);
      +   }
      +
       }
      



      Is this something that I should open a JIRA for? Would this patch ever get applied?