Java 类org.springframework.web.servlet.FlashMapManager 实例源码

项目:spring4-understanding    文件:RedirectView.java   
/**
 * Convert model to request parameters and redirect to the given URL.
 * @see #appendQueryProperties
 * @see #sendRedirect
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    String targetUrl = createTargetUrl(model, request);
    targetUrl = updateTargetUrl(targetUrl, model, request, response);

    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    if (!CollectionUtils.isEmpty(flashMap)) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(targetUrl).build();
        flashMap.setTargetRequestPath(uriComponents.getPath());
        flashMap.addTargetRequestParams(uriComponents.getQueryParams());
        FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
        if (flashMapManager == null) {
            throw new IllegalStateException("FlashMapManager not found despite output FlashMap having been set");
        }
        flashMapManager.saveOutputFlashMap(flashMap, request, response);
    }

    sendRedirect(request, response, targetUrl, this.http10Compatible);
}
项目:class-guard    文件:RedirectView.java   
/**
 * Convert model to request parameters and redirect to the given URL.
 * @see #appendQueryProperties
 * @see #sendRedirect
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    String targetUrl = createTargetUrl(model, request);
    targetUrl = updateTargetUrl(targetUrl, model, request, response);

    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    if (!CollectionUtils.isEmpty(flashMap)) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(targetUrl).build();
        flashMap.setTargetRequestPath(uriComponents.getPath());
        flashMap.addTargetRequestParams(uriComponents.getQueryParams());
        FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
        if (flashMapManager == null) {
            throw new IllegalStateException("FlashMapManager not found despite output FlashMap having been set");
        }
        flashMapManager.saveOutputFlashMap(flashMap, request, response);
    }

    sendRedirect(request, response, targetUrl, this.http10Compatible);
}
项目:spring4-understanding    文件:RedirectViewTests.java   
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative,
        final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception {

    class TestRedirectView extends RedirectView {

        public boolean queryPropertiesCalled = false;

        /**
         * Test whether this callback method is called with correct args
         */
        @Override
        protected Map<String, Object> queryProperties(Map<String, Object> model) {
            // They may not be the same model instance, but they're still equal
            assertTrue("Map and model must be equal.", map.equals(model));
            this.queryPropertiesCalled = true;
            return super.queryProperties(model);
        }
    }

    TestRedirectView rv = new TestRedirectView();
    rv.setUrl(url);
    rv.setContextRelative(contextRelative);
    rv.setExposeModelAttributes(exposeModelAttributes);

    HttpServletRequest request = mock(HttpServletRequest.class, "request");
    if (exposeModelAttributes) {
        given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
    }
    if (contextRelative) {
        expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
        given(request.getContextPath()).willReturn("/context");
    }

    given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());

    FlashMapManager flashMapManager = new SessionFlashMapManager();
    given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);

    HttpServletResponse response = mock(HttpServletResponse.class, "response");
    given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
    response.sendRedirect(expectedUrlForEncoding);

    rv.render(map, request, response);
    if (exposeModelAttributes) {
        assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
    }
}
项目:spring4-understanding    文件:StandaloneMockMvcBuilder.java   
/**
 * Provide a custom FlashMapManager instance.
 * If not provided, {@code SessionFlashMapManager} is used by default.
 */
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager) {
    this.flashMapManager = flashMapManager;
    return this;
}
项目:pungwecms    文件:Utils.java   
public static void saveFlashMap(FlashMap flashMap) {
    FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(getRequest());
    flashMapManager.saveOutputFlashMap(flashMap, getRequest(), getResponse());
}
项目:class-guard    文件:StandaloneMockMvcBuilder.java   
/**
 * Provide a custom FlashMapManager instance.
 * If not provided, {@code SessionFlashMapManager} is used by default.
 */
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager) {
    this.flashMapManager = flashMapManager;
    return this;
}
项目:class-guard    文件:RedirectViewTests.java   
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative,
        final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception {

    class TestRedirectView extends RedirectView {

        public boolean queryPropertiesCalled = false;

        /**
         * Test whether this callback method is called with correct args
         */
        @Override
        protected Map<String, Object> queryProperties(Map<String, Object> model) {
            // They may not be the same model instance, but they're still equal
            assertTrue("Map and model must be equal.", map.equals(model));
            this.queryPropertiesCalled = true;
            return super.queryProperties(model);
        }
    }

    TestRedirectView rv = new TestRedirectView();
    rv.setUrl(url);
    rv.setContextRelative(contextRelative);
    rv.setExposeModelAttributes(exposeModelAttributes);

    HttpServletRequest request = mock(HttpServletRequest.class, "request");
    if (exposeModelAttributes) {
        given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING);
    }
    if (contextRelative) {
        expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
        given(request.getContextPath()).willReturn("/context");
    }

    given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());

    FlashMapManager flashMapManager = new SessionFlashMapManager();
    given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);

    HttpServletResponse response = mock(HttpServletResponse.class, "response");
    given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
    response.sendRedirect(expectedUrlForEncoding);

    rv.render(map, request, response);
    if (exposeModelAttributes) {
        assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
    }
}
项目:bandwidth-on-demand    文件:CsrfHandlerInterceptor.java   
private void addInfoMessage(HttpServletRequest request, HttpServletResponse response) {
  FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
  flashMap.put(MessageManager.INFO_MESSAGES_KEY, "Your POST request has been ignored because your session timed out.");
  FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
  flashMapManager.saveOutputFlashMap(flashMap, request, response);
}
项目:spring4-understanding    文件:RequestContextUtils.java   
/**
 * Return the FlashMapManager instance to save flash attributes with
 * before a redirect.
 * @param request the current request
 * @return a {@link FlashMapManager} instance (never {@code null} within a DispatcherServlet request)
 */
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
    return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);
}
项目:feilong-spring    文件:FlashMapUtil.java   
/**
 * Save output flash map.
 *
 * @param flashMap
 *            the flash map
 * @param request
 *            the request
 * @param response
 *            the response
 */
public static void saveOutputFlashMap(FlashMap flashMap,HttpServletRequest request,HttpServletResponse response){
    FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
    flashMapManager.saveOutputFlashMap(flashMap, request, response);
}
项目:class-guard    文件:RequestContextUtils.java   
/**
 * Return the FlashMapManager instance to save flash attributes with
 * before a redirect.
 * @param request the current request
 * @return a {@link FlashMapManager} instance (never {@code null} within a DispatcherServlet request)
 */
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
    return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);
}