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;
}
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());
}
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;
}
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");
}
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;
}
Aggregations