2

My Filter inspects multi-part posts and potentially rejects them before they reach the actual endpoint (jersey, outside my control). Allowing casual multipart parsing (as shown in answer below) solves the exception: Unable to process parts as no multi-part configuration has been provided

A custom CommonsMultipartResolver or the existing resolver work without the error when the property is set. However, the content gets lost after accessing / resolving it.

I could use a custom CommonsMultipartResolver and deal with the lost information as suggested here: Resolving multipart/form-data request in spring filter. However, I am hoping for a cleaner solution adding the filter, without copying the request.

2 Answers 2

2

To enable the allowCasualMultipartParsing property of the Tomcat Context, you can inject a custom TomcatServletWebServerFactory into your application (Spring Boot 2):

import org.apache.catalina.Context;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;

@Bean
public TomcatServletWebServerFactory tomcatFactory()
{
    return new TomcatServletWebServerFactory()
    {
        @Override
        protected void postProcessContext(Context context)
        {
            context.setAllowCasualMultipartParsing(true);
        }
    };
}

For Spring Boot 1, the factory class is TomcatEmbeddedServletContainerFactory.

2
  • Thank you for this approach! It answers the first part of my question and deserved recognition, but unfortunately enabling the casual parsing leads to the same issue as I described it for the custom resolver approach. The content is lost as soon as the servlet filter touches the part, e.g. using req.getPart("upload"). With this approach I would still need to copy the request. Commented Jun 27, 2022 at 6:13
  • I am voting up, because your answer helps me rule out the approach and have updated the question removing this theory. Commented Jun 27, 2022 at 6:24
1

Looking at the remainder of the question (or 'I am hoping for a cleaner solution adding the filter, without copying the request'), I seriously doubt there is one.

The request is sent over the network once only. It contains multipart data, and once this is consumed (i.e. read from the stream) how would the stream now get forwarded to the next processor with the complete data?

That's where I guess generic solutions just receive the whole data, decide where to forward it to and then send the whole data. It will not work without having stored the data inbetween, what is labelled as 'copying the request' by you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.