Search in sources :

Example 1 with FlashMapManager

use of org.springframework.web.servlet.FlashMapManager in project spring-framework by spring-projects.

the class MockHttpServletRequestBuilder method buildRequest.

/**
	 * Build a {@link MockHttpServletRequest}.
	 */
@Override
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
    MockHttpServletRequest request = createServletRequest(servletContext);
    request.setAsyncSupported(true);
    request.setMethod(this.method);
    String requestUri = this.url.getRawPath();
    request.setRequestURI(requestUri);
    if (this.url.getScheme() != null) {
        request.setScheme(this.url.getScheme());
    }
    if (this.url.getHost() != null) {
        request.setServerName(this.url.getHost());
    }
    if (this.url.getPort() != -1) {
        request.setServerPort(this.url.getPort());
    }
    updatePathRequestProperties(request, requestUri);
    if (this.secure != null) {
        request.setSecure(this.secure);
    }
    if (this.principal != null) {
        request.setUserPrincipal(this.principal);
    }
    if (this.session != null) {
        request.setSession(this.session);
    }
    request.setCharacterEncoding(this.characterEncoding);
    request.setContent(this.content);
    request.setContentType(this.contentType);
    for (String name : this.headers.keySet()) {
        for (Object value : this.headers.get(name)) {
            request.addHeader(name, value);
        }
    }
    if (this.url.getRawQuery() != null) {
        request.setQueryString(this.url.getRawQuery());
    }
    addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
    for (String name : this.parameters.keySet()) {
        for (String value : this.parameters.get(name)) {
            request.addParameter(name, value);
        }
    }
    if (this.content != null && this.content.length > 0) {
        String requestContentType = request.getContentType();
        if (requestContentType != null) {
            MediaType mediaType = MediaType.parseMediaType(requestContentType);
            if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
                addRequestParams(request, parseFormData(mediaType));
            }
        }
    }
    if (!ObjectUtils.isEmpty(this.cookies)) {
        request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
    }
    if (!ObjectUtils.isEmpty(this.locales)) {
        request.setPreferredLocales(this.locales);
    }
    for (String name : this.requestAttributes.keySet()) {
        request.setAttribute(name, this.requestAttributes.get(name));
    }
    for (String name : this.sessionAttributes.keySet()) {
        request.getSession().setAttribute(name, this.sessionAttributes.get(name));
    }
    FlashMap flashMap = new FlashMap();
    flashMap.putAll(this.flashAttributes);
    FlashMapManager flashMapManager = getFlashMapManager(request);
    flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
    return request;
}
Also used : Cookie(javax.servlet.http.Cookie) FlashMap(org.springframework.web.servlet.FlashMap) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) FlashMapManager(org.springframework.web.servlet.FlashMapManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MediaType(org.springframework.http.MediaType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 2 with FlashMapManager

use of org.springframework.web.servlet.FlashMapManager in project spring-framework by spring-projects.

the class RequestContextUtils method saveOutputFlashMap.

/**
 * Convenience method that retrieves the {@link #getOutputFlashMap "output"
 * FlashMap}, updates it with the path and query params of the target URL,
 * and then saves it using the {@link #getFlashMapManager FlashMapManager}.
 * @param location the target URL for the redirect
 * @param request the current request
 * @param response the current response
 * @since 5.0
 */
public static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response) {
    FlashMap flashMap = getOutputFlashMap(request);
    if (CollectionUtils.isEmpty(flashMap)) {
        return;
    }
    UriComponents uriComponents = UriComponentsBuilder.fromUriString(location).build();
    flashMap.setTargetRequestPath(uriComponents.getPath());
    flashMap.addTargetRequestParams(uriComponents.getQueryParams());
    FlashMapManager manager = getFlashMapManager(request);
    Assert.state(manager != null, "No FlashMapManager. Is this a DispatcherServlet handled request?");
    manager.saveOutputFlashMap(flashMap, request, response);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) UriComponents(org.springframework.web.util.UriComponents) FlashMapManager(org.springframework.web.servlet.FlashMapManager)

Example 3 with FlashMapManager

use of org.springframework.web.servlet.FlashMapManager in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method defaultFlashMapManagerConfiguration.

@Test
public void defaultFlashMapManagerConfiguration() {
    ApplicationContext context = initContext(WebConfig.class);
    FlashMapManager flashMapManager = context.getBean(FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class);
    assertThat(flashMapManager).isNotNull();
    assertThat(flashMapManager).isInstanceOf(SessionFlashMapManager.class);
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) FlashMapManager(org.springframework.web.servlet.FlashMapManager) Test(org.junit.jupiter.api.Test)

Example 4 with FlashMapManager

use of org.springframework.web.servlet.FlashMapManager in project spring-framework by spring-projects.

the class MockHttpServletRequestBuilder method getFlashMapManager.

private FlashMapManager getFlashMapManager(MockHttpServletRequest request) {
    FlashMapManager flashMapManager = null;
    try {
        ServletContext servletContext = request.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        flashMapManager = wac.getBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class);
    } catch (IllegalStateException ex) {
    // ignore
    } catch (NoSuchBeanDefinitionException ex) {
    // ignore
    }
    return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager());
}
Also used : SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) FlashMapManager(org.springframework.web.servlet.FlashMapManager) ServletContext(javax.servlet.ServletContext) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

FlashMapManager (org.springframework.web.servlet.FlashMapManager)4 SessionFlashMapManager (org.springframework.web.servlet.support.SessionFlashMapManager)3 FlashMap (org.springframework.web.servlet.FlashMap)2 ServletContext (javax.servlet.ServletContext)1 Cookie (javax.servlet.http.Cookie)1 Test (org.junit.jupiter.api.Test)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ApplicationContext (org.springframework.context.ApplicationContext)1 MediaType (org.springframework.http.MediaType)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1 UriComponents (org.springframework.web.util.UriComponents)1