Search in sources :

Example 6 with FlashMap

use of org.springframework.web.servlet.FlashMap 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 7 with FlashMap

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

the class FlashMapManagerTests method retrieveAndUpdateMatchByParams.

@Test
public void retrieveAndUpdateMatchByParams() {
    FlashMap flashMap = new FlashMap();
    flashMap.put("key", "value");
    flashMap.addTargetRequestParam("number", "one");
    this.flashMapManager.setFlashMaps(Arrays.asList(flashMap));
    this.request.setQueryString("number=");
    FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isNull();
    assertThat(this.flashMapManager.getFlashMaps().size()).as("FlashMap should not have been removed").isEqualTo(1);
    this.request.setQueryString("number=two");
    inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isNull();
    assertThat(this.flashMapManager.getFlashMaps().size()).as("FlashMap should not have been removed").isEqualTo(1);
    this.request.setQueryString("number=one");
    inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isEqualTo(flashMap);
    assertThat(this.flashMapManager.getFlashMaps().size()).as("Input FlashMap should have been removed").isEqualTo(0);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Example 8 with FlashMap

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

the class FlashMapManagerTests method saveOutputFlashMapNormalizeTargetPath.

@Test
public void saveOutputFlashMapNormalizeTargetPath() {
    FlashMap flashMap = new FlashMap();
    flashMap.put("key", "value");
    flashMap.setTargetRequestPath(".");
    this.request.setRequestURI("/once/upon/a/time");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/once/upon/a");
    flashMap.setTargetRequestPath("./");
    this.request.setRequestURI("/once/upon/a/time");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/once/upon/a/");
    flashMap.setTargetRequestPath("..");
    this.request.setRequestURI("/once/upon/a/time");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/once/upon");
    flashMap.setTargetRequestPath("../");
    this.request.setRequestURI("/once/upon/a/time");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/once/upon/");
    flashMap.setTargetRequestPath("../../only");
    this.request.setRequestURI("/once/upon/a/time");
    this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/once/only");
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Example 9 with FlashMap

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

the class FlashMapManagerTests method retrieveAndUpdateMatchByOriginatingPath.

// SPR-8779
@Test
public void retrieveAndUpdateMatchByOriginatingPath() {
    FlashMap flashMap = new FlashMap();
    flashMap.put("key", "value");
    flashMap.setTargetRequestPath("/accounts");
    this.flashMapManager.setFlashMaps(Arrays.asList(flashMap));
    this.request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/accounts");
    this.request.setRequestURI("/mvc/accounts");
    FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isEqualTo(flashMap);
    assertThat(this.flashMapManager.getFlashMaps().size()).as("Input FlashMap should have been removed").isEqualTo(0);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Example 10 with FlashMap

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

the class FlashMapManagerTests method retrieveAndUpdateSortMultipleMatches.

@Test
public void retrieveAndUpdateSortMultipleMatches() {
    FlashMap emptyFlashMap = new FlashMap();
    FlashMap flashMapOne = new FlashMap();
    flashMapOne.put("key1", "value1");
    flashMapOne.setTargetRequestPath("/one");
    FlashMap flashMapTwo = new FlashMap();
    flashMapTwo.put("key1", "value1");
    flashMapTwo.put("key2", "value2");
    flashMapTwo.setTargetRequestPath("/one/two");
    this.flashMapManager.setFlashMaps(Arrays.asList(emptyFlashMap, flashMapOne, flashMapTwo));
    this.request.setRequestURI("/one/two");
    FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isEqualTo(flashMapTwo);
    assertThat(this.flashMapManager.getFlashMaps().size()).as("Input FlashMap should have been removed").isEqualTo(2);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Aggregations

FlashMap (org.springframework.web.servlet.FlashMap)30 Test (org.junit.jupiter.api.Test)20 SessionFlashMapManager (org.springframework.web.servlet.support.SessionFlashMapManager)5 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)5 BeforeEach (org.junit.jupiter.api.BeforeEach)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 FlashMapManager (org.springframework.web.servlet.FlashMapManager)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Cookie (javax.servlet.http.Cookie)1 JsonResponse (org.broadleafcommerce.common.web.JsonResponse)1 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)1 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)1 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)1 Test (org.junit.Test)1