How to handle SizeLimitExceededException from CommonsMultipartResolver in Spring WebFlow?

apache-commons-fileupload, spring-webflow, spring-webflow-2

Solution

In your `SimpleMappingExceptionResolver` you should be able to override the `resolveException` method, determine the exception type being caught and handle appropriately.

I've found some old code in our project that seems to be a solution to a similar exception;

public class GeneralMappingExceptionResolver extends SimpleMappingExceptionResolver {

 @Override
 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {

    if(exception instanceof MaxUploadSizeExceededException) {
        MaxUploadSizeExceededException maxe = (MaxUploadSizeExceededException)exception;
        String errorMessage = "Max filesize exceeded, please ensure filesize is too large.");
        HashMap<String, Object> model = new HashMap<String, Object>(2);
        model.put("errorMessage", errorMessage);
        return new ModelAndView("verification/psv/consent", model);
    } else {
        return super.resolveException(request, response, handler, exception); // Do whatever default behaviour is (ie throw to error page).
    }
}

Note that the "verification/psv/consent" is the flow where this exception would have been thrown from and where it needs to return to. We only have the one page that has a file upload.

Obviously the errorMessage is just a parameter passed into the view so will need to be handled and displayed like an error message. You may also need to re-populate any other form fields that were submitted. Hopefully this is a point in the right direction though.

Problem

I have the following situation. I have a CommonsMultipartResolver bean configured the following way. ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2100000" /> ``` And I have a few file upload fields in a Spring Web Flow view state jsp. Everything works fine if the file is under the limit, but if the file exceeds the limit of 2MB-s I have to add a validation error to the binding result on my form. My problem is that the multipart file resolver throws a org.apache.commons.fileupload.FileUploadBase.SizeL imitExceededException exception when the file limit is exceeded and I can't find a way to catch this in Spring Web Flow and add my FieldError to the form. I tried using the on-exception attribute of the transition tag, but if I understand correctly it only works for exceptions that are thrown within Spring Web Flow. I've also tried to use SimpleMappingExceptionResolver in spring mvc, but I do not want to redirect to a page, I want to handle this exception. I also found this: https://jira.springsource.org/browse/SWF-158 But it's from version 1.0 and I'm assuming that this has been incorporated since or that a better way was found to handle these situations. Any ideas on how to deal with this would be greatly appreciated. Thanks.

Original source