Search in sources :

Example 1 with MultipartResolver

use of org.springframework.web.multipart.MultipartResolver in project spring-framework by spring-projects.

the class DispatcherServletTests method dispatcherServletContextRefresh.

@Test
public void dispatcherServletContextRefresh() throws ServletException {
    MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.init(new MockServletConfig(servletContext, "empty"));
    ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
    ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertThat(contextBean.getServletContext()).isSameAs(servletContext);
    assertThat(configBean.getServletConfig()).isSameAs(servlet.getServletConfig());
    MultipartResolver multipartResolver = servlet.getMultipartResolver();
    assertThat(multipartResolver).isNotNull();
    ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh();
    ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
    ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertThat(contextBean2.getServletContext()).isSameAs(servletContext);
    assertThat(configBean2.getServletConfig()).isSameAs(servlet.getServletConfig());
    assertThat(contextBean != contextBean2).isTrue();
    assertThat(configBean != configBean2).isTrue();
    MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
    assertThat(multipartResolver != multipartResolver2).isTrue();
    servlet.destroy();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ServletContextAwareBean(org.springframework.web.context.ServletContextAwareBean) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MultipartResolver(org.springframework.web.multipart.MultipartResolver) ServletConfigAwareBean(org.springframework.web.context.ServletConfigAwareBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 2 with MultipartResolver

use of org.springframework.web.multipart.MultipartResolver in project spring-framework by spring-projects.

the class MultipartFilter method doFilterInternal.

/**
 * Check for a multipart request via this filter's MultipartResolver,
 * and wrap the original request with a MultipartHttpServletRequest if appropriate.
 * <p>All later elements in the filter chain, most importantly servlets, benefit
 * from proper parameter extraction in the multipart case, and are able to cast to
 * MultipartHttpServletRequest if they need to.
 */
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    MultipartResolver multipartResolver = lookupMultipartResolver(request);
    HttpServletRequest processedRequest = request;
    if (multipartResolver.isMultipart(processedRequest)) {
        if (logger.isTraceEnabled()) {
            logger.trace("Resolving multipart request");
        }
        processedRequest = multipartResolver.resolveMultipart(processedRequest);
    } else {
        // A regular request...
        if (logger.isTraceEnabled()) {
            logger.trace("Not a multipart request");
        }
    }
    try {
        filterChain.doFilter(processedRequest, response);
    } finally {
        if (processedRequest instanceof MultipartHttpServletRequest) {
            multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
        }
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) MultipartResolver(org.springframework.web.multipart.MultipartResolver) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 3 with MultipartResolver

use of org.springframework.web.multipart.MultipartResolver in project spring-boot by spring-projects.

the class MultipartAutoConfigurationTests method webServerWithCustomMultipartResolver.

@Test
void webServerWithCustomMultipartResolver() {
    this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithCustomMultipartResolver.class, BaseConfiguration.class);
    MultipartResolver multipartResolver = this.context.getBean(MultipartResolver.class);
    assertThat(multipartResolver).isNotInstanceOf(StandardServletMultipartResolver.class);
    assertThat(this.context.getBeansOfType(MultipartConfigElement.class)).hasSize(1);
}
Also used : AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) StandardServletMultipartResolver(org.springframework.web.multipart.support.StandardServletMultipartResolver) MultipartResolver(org.springframework.web.multipart.MultipartResolver) Test(org.junit.jupiter.api.Test)

Example 4 with MultipartResolver

use of org.springframework.web.multipart.MultipartResolver in project grails-core by grails.

the class DefaultUrlMappingInfo method tryMultipartParams.

private Enumeration<String> tryMultipartParams(HttpServletRequest request, Enumeration<String> originalParams) {
    Enumeration<String> paramNames = originalParams;
    boolean disabled = isMultipartDisabled();
    if (!disabled) {
        MultipartResolver resolver = getMultipartResolver();
        if (resolver != null && resolver.isMultipart(request)) {
            MultipartHttpServletRequest resolvedMultipartRequest = getResolvedRequest(request, resolver);
            paramNames = resolvedMultipartRequest.getParameterNames();
        }
    }
    return paramNames;
}
Also used : MultipartResolver(org.springframework.web.multipart.MultipartResolver) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 5 with MultipartResolver

use of org.springframework.web.multipart.MultipartResolver in project spring-integration by spring-projects.

the class HttpRequestHandlingEndpointSupport method onInit.

/**
 * Locates the {@link MultipartResolver} bean based on the default name defined by the
 * {@link DispatcherServlet#MULTIPART_RESOLVER_BEAN_NAME} constant if available.
 * Sets up default converters if no converters set, or {@link #setMergeWithDefaultConverters(boolean)}
 * was called with true after the converters were set.
 */
@Override
protected void onInit() throws Exception {
    super.onInit();
    BeanFactory beanFactory = getBeanFactory();
    if (this.multipartResolver == null && beanFactory != null) {
        try {
            MultipartResolver multipartResolver = beanFactory.getBean(DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class);
            if (logger.isDebugEnabled()) {
                logger.debug("Using MultipartResolver [" + multipartResolver + "]");
            }
            this.multipartResolver = multipartResolver;
        } catch (NoSuchBeanDefinitionException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to locate MultipartResolver with name '" + DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME + "': no multipart request handling will be supported.");
            }
        }
    }
    if (this.messageConverters.size() == 0 || (this.mergeWithDefaultConverters && !this.convertersMerged)) {
        this.messageConverters.addAll(this.defaultMessageConverters);
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) MultipartResolver(org.springframework.web.multipart.MultipartResolver) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Aggregations

MultipartResolver (org.springframework.web.multipart.MultipartResolver)6 Test (org.junit.jupiter.api.Test)3 ServletConfigAwareBean (org.springframework.web.context.ServletConfigAwareBean)2 ServletContextAwareBean (org.springframework.web.context.ServletContextAwareBean)2 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)2 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)2 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 StandardServletMultipartResolver (org.springframework.web.multipart.support.StandardServletMultipartResolver)1