org.springframework.web.servlet.FlashMapManager Java Examples

The following examples show how to use org.springframework.web.servlet.FlashMapManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedirectView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: RedirectView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: RequestContextUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: MockHttpServletRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 | NoSuchBeanDefinitionException ex) {
		// ignore
	}
	return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager());
}
 
Example #5
Source File: RequestContextUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: MockHttpServletRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 | NoSuchBeanDefinitionException ex) {
		// ignore
	}
	return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager());
}
 
Example #7
Source File: StandaloneMockMvcBuilder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Provide a custom FlashMapManager instance.
 * If not provided, {@code SessionFlashMapManager} is used by default.
 */
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager) {
	this.flashMapManager = flashMapManager;
	return this;
}
 
Example #8
Source File: StandaloneMockMvcBuilder.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Provide a custom FlashMapManager instance.
 * If not provided, {@code SessionFlashMapManager} is used by default.
 */
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager) {
	this.flashMapManager = flashMapManager;
	return this;
}
 
Example #9
Source File: ServletWebServerInitializer.java    From spring-fu with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("webServerFactoryCustomizerBeanPostProcessor", WebServerFactoryCustomizerBeanPostProcessor.class, WebServerFactoryCustomizerBeanPostProcessor::new);
	context.registerBean(WebMvcProperties.class, () -> this.webMvcProperties);
	context.registerBean(ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar::new);
	context.registerBean(TomcatServletWebServerFactory.class, () -> new ServletWebServerFactoryConfiguration.EmbeddedTomcat().tomcatServletWebServerFactory(
			context.getBeanProvider(TomcatConnectorCustomizer.class),
			context.getBeanProvider(TomcatContextCustomizer.class),
			context.getBeanProvider(ResolvableType.forClass(TomcatProtocolHandlerCustomizer.class))));
	ServletWebServerFactoryAutoConfiguration servletWebServerFactoryConfiguration = new ServletWebServerFactoryAutoConfiguration();
	context.registerBean(ServletWebServerFactoryCustomizer.class, () -> servletWebServerFactoryConfiguration.servletWebServerFactoryCustomizer(serverProperties));
	context.registerBean(TomcatServletWebServerFactoryCustomizer.class, () -> servletWebServerFactoryConfiguration.tomcatServletWebServerFactoryCustomizer(serverProperties));
	context.registerBean(FilterRegistrationBean.class, servletWebServerFactoryConfiguration::forwardedHeaderFilter);

	DispatcherServletAutoConfiguration.DispatcherServletConfiguration dispatcherServletConfiguration = new DispatcherServletAutoConfiguration.DispatcherServletConfiguration();
	context.registerBean(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME, DispatcherServlet.class, () -> dispatcherServletConfiguration.dispatcherServlet(webMvcProperties));
	context.registerBean(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME, DispatcherServletRegistrationBean.class, () -> new DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration().dispatcherServletRegistration(context.getBean(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME, DispatcherServlet.class), webMvcProperties, context.getBeanProvider(MultipartConfigElement.class)));

	WebMvcAutoConfiguration webMvcConfiguration = new WebMvcAutoConfiguration();
	context.registerBean(OrderedHiddenHttpMethodFilter.class, webMvcConfiguration::hiddenHttpMethodFilter);

	Supplier<WebMvcAutoConfigurationAdapter> webMvcConfigurationAdapter = new Supplier<WebMvcAutoConfigurationAdapter>() {

		private WebMvcAutoConfigurationAdapter configuration;

		@Override
		public WebMvcAutoConfigurationAdapter get() {
			if (configuration == null) {
				configuration = new WebMvcAutoConfigurationAdapter(resourceProperties, webMvcProperties, context, context.getBeanProvider(HttpMessageConverters.class), context.getBeanProvider(ResourceHandlerRegistrationCustomizer.class), context.getBeanProvider(DispatcherServletPath.class));
				return configuration;
			}
			return configuration;
		}
	};
	context.registerBean(InternalResourceViewResolver.class, () -> webMvcConfigurationAdapter.get().defaultViewResolver());
	context.registerBean(BeanNameViewResolver.class, () -> webMvcConfigurationAdapter.get().beanNameViewResolver());
	context.registerBean("viewResolver", ContentNegotiatingViewResolver.class, () -> webMvcConfigurationAdapter.get().viewResolver(context));
	context.registerBean(RequestContextFilter.class, WebMvcAutoConfigurationAdapter::requestContextFilter);
	// TODO Favicon management

	Supplier<EnableWebMvcConfiguration> enableWebMvcConfiguration = new Supplier<EnableWebMvcConfiguration>() {

		private EnableWebMvcConfiguration configuration;

		@Override
		public EnableWebMvcConfiguration get() {
			if (configuration == null) {
				configuration = new EnableWebMvcConfigurationWrapper(context.getBeanProvider(WebMvcProperties.class), context.getBeanProvider(WebMvcRegistrations.class), context);
				configuration.setApplicationContext(context);
				configuration.setServletContext(((WebApplicationContext) context).getServletContext());
				configuration.setResourceLoader(context);
			}
			return configuration;
		}
	};

	context.registerBean(FormattingConversionService.class, () -> enableWebMvcConfiguration.get().mvcConversionService());
	context.registerBean(Validator.class, () -> enableWebMvcConfiguration.get().mvcValidator());
	context.registerBean(ContentNegotiationManager.class, () -> enableWebMvcConfiguration.get().mvcContentNegotiationManager());
	context.registerBean(ResourceChainResourceHandlerRegistrationCustomizer.class, () -> new ResourceChainCustomizerConfiguration().resourceHandlerRegistrationCustomizer());
	context.registerBean(PathMatcher.class, () -> enableWebMvcConfiguration.get().mvcPathMatcher());
	context.registerBean(UrlPathHelper.class, () -> enableWebMvcConfiguration.get().mvcUrlPathHelper());
	context.registerBean(HandlerMapping.class, () ->  enableWebMvcConfiguration.get().viewControllerHandlerMapping(context.getBean(FormattingConversionService.class), context.getBean(ResourceUrlProvider.class)));
	context.registerBean(RouterFunctionMapping.class, () ->  {

		return enableWebMvcConfiguration.get().routerFunctionMapping(context.getBean(FormattingConversionService.class), context.getBean(ResourceUrlProvider.class));
	});
	context.registerBean(HandlerMapping.class, () -> enableWebMvcConfiguration.get().resourceHandlerMapping(context.getBean(ContentNegotiationManager.class), context.getBean(FormattingConversionService.class), context.getBean(ResourceUrlProvider.class)));
	context.registerBean(ResourceUrlProvider.class, () -> enableWebMvcConfiguration.get().mvcResourceUrlProvider());
	context.registerBean(HandlerMapping.class, () -> enableWebMvcConfiguration.get().defaultServletHandlerMapping());
	context.registerBean(HandlerFunctionAdapter.class, () -> enableWebMvcConfiguration.get().handlerFunctionAdapter());
	context.registerBean(HttpRequestHandlerAdapter.class, () -> enableWebMvcConfiguration.get().httpRequestHandlerAdapter());
	context.registerBean(SimpleControllerHandlerAdapter.class, () -> enableWebMvcConfiguration.get().simpleControllerHandlerAdapter());
	context.registerBean(HandlerExceptionResolver.class, () -> enableWebMvcConfiguration.get().handlerExceptionResolver(context.getBean(ContentNegotiationManager.class)));
	context.registerBean(ViewResolver.class, () -> enableWebMvcConfiguration.get().mvcViewResolver(context.getBean(ContentNegotiationManager.class)));
	context.registerBean(HandlerMappingIntrospector.class, () -> enableWebMvcConfiguration.get().mvcHandlerMappingIntrospector(), bd -> bd.setLazyInit(true));
	context.registerBean(WelcomePageHandlerMapping.class, () -> enableWebMvcConfiguration.get().welcomePageHandlerMapping(context, context.getBean(FormattingConversionService.class), context.getBean(ResourceUrlProvider.class)));
	context.registerBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, LocaleResolver.class, () -> enableWebMvcConfiguration.get().localeResolver());
	context.registerBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, ThemeResolver.class, () -> enableWebMvcConfiguration.get().themeResolver());
	context.registerBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class, () -> enableWebMvcConfiguration.get().flashMapManager());
	context.registerBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, RequestToViewNameTranslator.class, () -> enableWebMvcConfiguration.get().viewNameTranslator());
}
 
Example #10
Source File: RedirectViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
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);
	}
}
 
Example #11
Source File: StandaloneMockMvcBuilder.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Provide a custom FlashMapManager instance.
 * If not provided, {@code SessionFlashMapManager} is used by default.
 */
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager) {
	this.flashMapManager = flashMapManager;
	return this;
}
 
Example #12
Source File: RequestContextUtils.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Return the {@code FlashMapManager} instance to save flash attributes.
 * <p>As of 5.0 the convenience method {@link #saveOutputFlashMap} may be
 * used to save the "output" FlashMap.
 * @param request the current request
 * @return a {@link FlashMapManager} instance, never {@code null} within a
 * {@code DispatcherServlet}-handled request
 */
@Nullable
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
	return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);
}
 
Example #13
Source File: RequestContextUtils.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Return the {@code FlashMapManager} instance to save flash attributes.
 * <p>As of 5.0 the convenience method {@link #saveOutputFlashMap} may be
 * used to save the "output" FlashMap.
 * @param request the current request
 * @return a {@link FlashMapManager} instance, never {@code null} within a
 * {@code DispatcherServlet}-handled request
 */
@Nullable
public static FlashMapManager getFlashMapManager(HttpServletRequest request) {
	return (FlashMapManager) request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE);
}
 
Example #14
Source File: RequestContextUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #15
Source File: RequestContextUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}