Search in sources :

Example 1 with FlashMap

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

use of org.springframework.web.servlet.FlashMap in project tutorials by eugenp.

the class TodoControllerWithSessionAttributesTest method whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo.

@Test
public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception {
    FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form").param("description", "newtodo")).andExpect(status().is3xxRedirection()).andReturn().getFlashMap();
    MvcResult result = mockMvc.perform(get("/sessionattributes/form").sessionAttrs(flashMap)).andExpect(status().isOk()).andExpect(model().attributeExists("todo")).andReturn();
    TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo");
    assertEquals("newtodo", item.getDescription());
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with FlashMap

use of org.springframework.web.servlet.FlashMap in project motech by motech.

the class FlashMapInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    Object flashMapObj = RequestContextUtils.getOutputFlashMap(request);
    if (flashMapObj != null && !(flashMapObj instanceof FlashMap)) {
        // we need to create a FlashMap using the webapp classLoader
        FlashMap flashMapCopy = new FlashMap();
        flashMapCopy.putAll((Map<? extends String, ?>) flashMapObj);
        // then put it in the request
        SessionFlashMapManager sessionFlashMapManager = new SessionFlashMapManager();
        sessionFlashMapManager.saveOutputFlashMap(flashMapCopy, request, response);
    }
    return true;
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager)

Example 4 with FlashMap

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

the class ExceptionHandlerExceptionResolverTests method resolveRedirectAttributesAtArgument.

// SPR-14651
@Test
void resolveRedirectAttributesAtArgument() throws Exception {
    IllegalArgumentException ex = new IllegalArgumentException();
    HandlerMethod handlerMethod = new HandlerMethod(new RedirectAttributesController(), "handle");
    this.resolver.afterPropertiesSet();
    ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
    assertThat(mav).isNotNull();
    assertThat(mav.getViewName()).isEqualTo("redirect:/");
    FlashMap flashMap = (FlashMap) this.request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
    assertThat((Object) flashMap).as("output FlashMap should exist").isNotNull();
    assertThat(flashMap.get("exceptionClassName")).isEqualTo("IllegalArgumentException");
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 5 with FlashMap

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

the class AbstractFlashMapManager method retrieveAndUpdate.

@Override
@Nullable
public final FlashMap retrieveAndUpdate(HttpServletRequest request, HttpServletResponse response) {
    List<FlashMap> allFlashMaps = retrieveFlashMaps(request);
    if (CollectionUtils.isEmpty(allFlashMaps)) {
        return null;
    }
    List<FlashMap> mapsToRemove = getExpiredFlashMaps(allFlashMaps);
    FlashMap match = getMatchingFlashMap(allFlashMaps, request);
    if (match != null) {
        mapsToRemove.add(match);
    }
    if (!mapsToRemove.isEmpty()) {
        Object mutex = getFlashMapsMutex(request);
        if (mutex != null) {
            synchronized (mutex) {
                allFlashMaps = retrieveFlashMaps(request);
                if (allFlashMaps != null) {
                    allFlashMaps.removeAll(mapsToRemove);
                    updateFlashMaps(allFlashMaps, request, response);
                }
            }
        } else {
            allFlashMaps.removeAll(mapsToRemove);
            updateFlashMaps(allFlashMaps, request, response);
        }
    }
    return match;
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Nullable(org.springframework.lang.Nullable)

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