SPR-6464 Add target request matching logic to DefaultFlashMapManager.
This commit is contained in:
@@ -53,7 +53,6 @@ import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
@@ -815,7 +814,7 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
}
|
||||
}
|
||||
|
||||
boolean flashInitialized = this.flashMapManager.requestStarted(request);
|
||||
this.flashMapManager.requestStarted(request);
|
||||
|
||||
// Make framework objects available to handlers and view objects.
|
||||
request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
|
||||
@@ -827,9 +826,8 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
doDispatch(request, response);
|
||||
}
|
||||
finally {
|
||||
if (flashInitialized) {
|
||||
this.flashMapManager.requestCompleted(request);
|
||||
}
|
||||
this.flashMapManager.requestCompleted(request);
|
||||
|
||||
// Restore the original attribute snapshot, in case of an include.
|
||||
if (attributesSnapshot != null) {
|
||||
restoreAttributesAfterInclude(request, attributesSnapshot);
|
||||
|
||||
@@ -20,98 +20,90 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* Stores attributes that need to be made available in the next request.
|
||||
* A FlashMap provides a way for one request to store attributes intended for
|
||||
* use in another. This is most commonly needed when redirecting from one URL
|
||||
* to another -- e.g. the Post/Redirect/Get pattern. A FlashMap is saved before
|
||||
* the redirect (typically in the session) and is made available after the
|
||||
* redirect and removed immediately.
|
||||
*
|
||||
* <p>A FlashMap can be set up with a request path and request parameters to
|
||||
* help identify the target request. Without this information, a FlashMap is
|
||||
* made available to the next request, which may or may not be the intended
|
||||
* result. Before a redirect, the target URL is known and when using the
|
||||
* {@code org.springframework.web.servlet.view.RedirectView}, FlashMap
|
||||
* instances are automatically updated with redirect URL information.
|
||||
*
|
||||
* <p>Annotated controllers will usually not access a FlashMap directly.. TODO
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
*
|
||||
* @see FlashMapManager
|
||||
*/
|
||||
public class FlashMap extends HashMap<String, Object> implements Comparable<FlashMap> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String expectedRequestUri;
|
||||
private String targetRequestPath;
|
||||
|
||||
private final Map<String, String> expectedRequestParameters = new LinkedHashMap<String, String>();
|
||||
private final Map<String, String> targetRequestParams = new LinkedHashMap<String, String>();
|
||||
|
||||
private long expirationStartTime;
|
||||
|
||||
private int timeToLive;
|
||||
|
||||
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
||||
private final int createdBy;
|
||||
|
||||
/**
|
||||
* Provide a URL to identify the target request for this FlashMap.
|
||||
* Only the path of the provided URL will be used for matching purposes.
|
||||
* If the URL is absolute or has a query string, the URL path is
|
||||
* extracted. Or if the URL is relative, it is appended to the current
|
||||
* request URI and normalized.
|
||||
*
|
||||
* @param request the current request, used to normalize relative URLs
|
||||
* @param url an absolute URL, a URL path, or a relative URL, never {@code null}
|
||||
* Create a new instance.
|
||||
*/
|
||||
public FlashMap setExpectedRequestUri(HttpServletRequest request, String url) {
|
||||
Assert.notNull(url, "Expected URL must not be null");
|
||||
String path = extractRequestUri(url);
|
||||
this.expectedRequestUri = path.startsWith("/") ? path : normalizeRelativePath(request, path);
|
||||
return this;
|
||||
}
|
||||
|
||||
private String extractRequestUri(String url) {
|
||||
int index = url.indexOf("?");
|
||||
if (index != -1) {
|
||||
url = url.substring(0, index);
|
||||
}
|
||||
index = url.indexOf("://");
|
||||
if (index != -1) {
|
||||
int pathBegin = url.indexOf("/", index + 3);
|
||||
url = (pathBegin != -1 ) ? url.substring(pathBegin) : "";
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private String normalizeRelativePath(HttpServletRequest request, String relativeUrl) {
|
||||
String requestUri = this.urlPathHelper.getRequestUri(request);
|
||||
relativeUrl = requestUri.substring(0, requestUri.lastIndexOf('/') + 1) + relativeUrl;
|
||||
return StringUtils.cleanPath(relativeUrl);
|
||||
public FlashMap() {
|
||||
this.createdBy = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a request parameter pair to help identify the request this FlashMap
|
||||
* should be made available to. If expected parameters are not set, the
|
||||
* FlashMap instance will match to requests with any parameters.
|
||||
*
|
||||
* @param name the name of the expected parameter (never {@code null})
|
||||
* @param value the value for the expected parameter (never {@code null})
|
||||
* Create a new instance with an id uniquely identifying the creator of
|
||||
* this FlashMap.
|
||||
*/
|
||||
public FlashMap setExpectedRequestParam(String name, String value) {
|
||||
this.expectedRequestParameters.put(name, value.toString());
|
||||
return this;
|
||||
public FlashMap(int createdBy) {
|
||||
this.createdBy = createdBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide request parameter pairs to help identify the request this FlashMap
|
||||
* should be made available to. If expected parameters are not set, the
|
||||
* FlashMap instance will match to requests with any parameters.
|
||||
*
|
||||
* <p>Although the provided map contain any Object values, only non-"simple"
|
||||
* value types as defined in {@link BeanUtils#isSimpleValueType} are used.
|
||||
*
|
||||
* Provide a URL path to help identify the target request for this FlashMap.
|
||||
* The path may be absolute (e.g. /application/resource) or relative to the
|
||||
* current request (e.g. ../resource).
|
||||
* @param path the URI path, never {@code null}
|
||||
*/
|
||||
public void setTargetRequestPath(String path) {
|
||||
Assert.notNull(path, "Expected path must not be null");
|
||||
this.targetRequestPath = path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the URL path of the target request, or {@code null} if none.
|
||||
*/
|
||||
public String getTargetRequestPath() {
|
||||
return targetRequestPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide request parameter pairs to identify the request for this FlashMap.
|
||||
* If not set, the FlashMap will match to requests with any parameters.
|
||||
* Only simple value types, as defined in {@link BeanUtils#isSimpleValueType},
|
||||
* are used.
|
||||
* @param params a Map with the names and values of expected parameters.
|
||||
*/
|
||||
public FlashMap setExpectedRequestParams(Map<String, ?> params) {
|
||||
public FlashMap addTargetRequestParams(Map<String, ?> params) {
|
||||
if (params != null) {
|
||||
for (String name : params.keySet()) {
|
||||
Object value = params.get(name);
|
||||
if ((value != null) && BeanUtils.isSimpleValueType(value.getClass())) {
|
||||
this.expectedRequestParameters.put(name, value.toString());
|
||||
this.targetRequestParams.put(name, value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,37 +111,24 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this FlashMap matches to the given request by checking
|
||||
* expectations provided via {@link #setExpectedRequestUri} and
|
||||
* {@link #setExpectedRequestParams}.
|
||||
* Provide a request parameter to identify the request for this FlashMap.
|
||||
* If not set, the FlashMap will match to requests with any parameters.
|
||||
*
|
||||
* @param request the current request
|
||||
*
|
||||
* @return "true" if the expectations match or there are no expectations.
|
||||
* @param name the name of the expected parameter (never {@code null})
|
||||
* @param value the value for the expected parameter (never {@code null})
|
||||
*/
|
||||
public boolean matches(HttpServletRequest request) {
|
||||
if (this.expectedRequestUri != null) {
|
||||
String requestUri = this.urlPathHelper.getRequestUri(request);
|
||||
if (!matchPathsIgnoreTrailingSlash(requestUri, this.expectedRequestUri)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (this.expectedRequestParameters != null) {
|
||||
for (Map.Entry<String, String> entry : this.expectedRequestParameters.entrySet()) {
|
||||
if (!entry.getValue().equals(request.getParameter(entry.getKey()))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
public FlashMap addTargetRequestParam(String name, String value) {
|
||||
this.targetRequestParams.put(name, value.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
private boolean matchPathsIgnoreTrailingSlash(String path1, String path2) {
|
||||
path1 = path1.endsWith("/") ? path1.substring(0, path1.length() - 1) : path1;
|
||||
path2 = path2.endsWith("/") ? path2.substring(0, path2.length() - 1) : path2;
|
||||
return path1.equals(path2);
|
||||
/**
|
||||
* Return the parameters identifying the target request, or an empty Map.
|
||||
*/
|
||||
public Map<String, String> getTargetRequestParams() {
|
||||
return targetRequestParams;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start the expiration period for this instance. After the given number of
|
||||
* seconds calls to {@link #isExpired()} will return "true".
|
||||
@@ -174,21 +153,24 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two FlashMap instances. One instance is preferred over the other
|
||||
* if it has an expected URL path or if it has a greater number of expected
|
||||
* request parameters.
|
||||
*
|
||||
* <p>It is expected that both instances have been matched against the
|
||||
* current request via {@link FlashMap#matches}.
|
||||
* Whether the given id matches the id of the creator of this FlashMap.
|
||||
*/
|
||||
public boolean isCreatedBy(int createdBy) {
|
||||
return this.createdBy == createdBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two FlashMaps and select the one that has a target URL path or
|
||||
* has more target request parameters.
|
||||
*/
|
||||
public int compareTo(FlashMap other) {
|
||||
int thisUrlPath = (this.expectedRequestUri != null) ? 1 : 0;
|
||||
int otherUrlPath = (other.expectedRequestUri != null) ? 1 : 0;
|
||||
int thisUrlPath = (this.targetRequestPath != null) ? 1 : 0;
|
||||
int otherUrlPath = (other.targetRequestPath != null) ? 1 : 0;
|
||||
if (thisUrlPath != otherUrlPath) {
|
||||
return otherUrlPath - thisUrlPath;
|
||||
}
|
||||
else {
|
||||
return other.expectedRequestParameters.size() - this.expectedRequestParameters.size();
|
||||
return other.targetRequestParams.size() - this.targetRequestParams.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,8 +178,8 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
result.append("[Attributes=").append(super.toString());
|
||||
result.append(", expecteRequestUri=").append(this.expectedRequestUri);
|
||||
result.append(", expectedRequestParameters=" + this.expectedRequestParameters.toString()).append("]");
|
||||
result.append(", expecteRequestUri=").append(this.targetRequestPath);
|
||||
result.append(", expectedRequestParameters=" + this.targetRequestParams.toString()).append("]");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,18 +16,20 @@
|
||||
|
||||
package org.springframework.web.servlet;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
|
||||
/**
|
||||
* A strategy interface for maintaining {@link FlashMap} instances in some
|
||||
* underlying storage until the next request.
|
||||
* A strategy interface for storing and retrieving {@code FlashMap} instances.
|
||||
* See {@link FlashMap} for a general overview of using flash attributes.
|
||||
*
|
||||
* <p>The most common use case for using flash storage is a redirect.
|
||||
* For example creating a resource in a POST request and then redirecting
|
||||
* to the page that shows the resource. Flash storage may be used to
|
||||
* pass along a success message.
|
||||
* <p>A FlashMapManager is invoked at the beginning and at the end of a request.
|
||||
* For each request, it exposes an "input" FlashMap with attributes passed from
|
||||
* a previous request (if any) and an "output" FlashMap with attributes to pass
|
||||
* to a subsequent request. Both FlashMap instances are exposed via request
|
||||
* attributes and can be accessed through methods in
|
||||
* {@code org.springframework.web.servlet.support.RequestContextUtils}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
@@ -37,41 +39,42 @@ import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
public interface FlashMapManager {
|
||||
|
||||
/**
|
||||
* Request attribute holding the read-only Map with flash attributes saved
|
||||
* during the previous request.
|
||||
* @see RequestContextUtils#getInputFlashMap(HttpServletRequest)
|
||||
* Name of request attribute that holds a read-only {@link Map} with
|
||||
* "input" flash attributes from a previous request (if any).
|
||||
* @see org.springframework.web.servlet.support.RequestContextUtils#getInputFlashMap(HttpServletRequest)
|
||||
*/
|
||||
public static final String INPUT_FLASH_MAP_ATTRIBUTE = FlashMapManager.class.getName() + ".INPUT_FLASH_MAP";
|
||||
|
||||
/**
|
||||
* Request attribute holding the {@link FlashMap} to add attributes to during
|
||||
* the current request.
|
||||
* @see RequestContextUtils#getOutputFlashMap(HttpServletRequest)
|
||||
* Name of request attribute that holds the "output" {@link FlashMap} with
|
||||
* attributes to pass to a subsequent request.
|
||||
* @see org.springframework.web.servlet.support.RequestContextUtils#getOutputFlashMap(HttpServletRequest)
|
||||
*/
|
||||
public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = FlashMapManager.class.getName() + ".OUTPUT_FLASH_MAP";
|
||||
|
||||
/**
|
||||
* Perform flash storage tasks at the start of a new request:
|
||||
* <ul>
|
||||
* <li>Create a FlashMap and make it available under the request attribute
|
||||
* {@link #OUTPUT_FLASH_MAP_ATTRIBUTE}.
|
||||
* <li>Locate the FlashMap saved during the previous request and make it
|
||||
* available under the request attribute {@link #INPUT_FLASH_MAP_ATTRIBUTE}.
|
||||
* Performs the following tasks unless the {@link #OUTPUT_FLASH_MAP_ATTRIBUTE}
|
||||
* request attribute exists:
|
||||
* <ol>
|
||||
* <li>Find the "input" FlashMap from a previous request (if any), expose it
|
||||
* under the request attribute {@link #INPUT_FLASH_MAP_ATTRIBUTE}, and
|
||||
* remove it from underlying storage.
|
||||
* <li>Create the "output" FlashMap where the current request can save
|
||||
* flash attributes and expose it under the request attribute
|
||||
* {@link #OUTPUT_FLASH_MAP_ATTRIBUTE}.
|
||||
* <li>Remove expired FlashMap instances.
|
||||
* </ul>
|
||||
* <p>If the {@link #OUTPUT_FLASH_MAP_ATTRIBUTE} request attribute exists
|
||||
* return "false" immediately.
|
||||
* </ol>
|
||||
*
|
||||
* @param request the current request
|
||||
* @return "true" if flash storage tasks were performed; "false" otherwise.
|
||||
*/
|
||||
boolean requestStarted(HttpServletRequest request);
|
||||
void requestStarted(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* Access the FlashMap with attributes added during the current request and
|
||||
* if it is not empty, save it in the underlying storage.
|
||||
* <p>If the call to {@link #requestStarted} returned "false", this
|
||||
* method is not invoked.
|
||||
* Save the "output" FlashMap in underlying storage, start its expiration
|
||||
* period, and decode/normalize its target request path.
|
||||
*
|
||||
* <p>The "output" FlashMap is not saved if it is empty or if it was not
|
||||
* created by this FlashMapManager.
|
||||
*/
|
||||
void requestCompleted(HttpServletRequest request);
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ public final class ParamsRequestCondition extends AbstractRequestCondition<Param
|
||||
private static Collection<ParamExpression> parseExpressions(String... params) {
|
||||
Set<ParamExpression> expressions = new LinkedHashSet<ParamExpression>();
|
||||
if (params != null) {
|
||||
for (String header : params) {
|
||||
expressions.add(new ParamExpression(header));
|
||||
for (String param : params) {
|
||||
expressions.add(new ParamExpression(param));
|
||||
}
|
||||
}
|
||||
return expressions;
|
||||
|
||||
@@ -28,12 +28,13 @@ import javax.servlet.http.HttpSession;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.FlashMap;
|
||||
import org.springframework.web.servlet.FlashMapManager;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* A {@link FlashMapManager} that saves and retrieves FlashMap instances in the
|
||||
* HTTP session.
|
||||
* A {@link FlashMapManager} that stores FlashMap instances in the HTTP session.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
@@ -46,10 +47,11 @@ public class DefaultFlashMapManager implements FlashMapManager {
|
||||
|
||||
private int flashTimeout = 180;
|
||||
|
||||
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
|
||||
/**
|
||||
* The amount of time in seconds after a request has completed processing
|
||||
* and before a FlashMap is considered expired.
|
||||
* The default value is 180.
|
||||
* The amount of time in seconds after a FlashMap is saved (after request
|
||||
* completion) before it is considered expired. The default value is 180.
|
||||
*/
|
||||
public void setFlashMapTimeout(int flashTimeout) {
|
||||
this.flashTimeout = flashTimeout;
|
||||
@@ -57,126 +59,153 @@ public class DefaultFlashMapManager implements FlashMapManager {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>This method never creates an HTTP session. The new FlashMap created
|
||||
* for the current request is exposed as a request attribute only and is
|
||||
* not saved in the session until {@link #requestCompleted} is called.
|
||||
* <p>This method never causes the HTTP session to be created.
|
||||
*/
|
||||
public boolean requestStarted(HttpServletRequest request) {
|
||||
public void requestStarted(HttpServletRequest request) {
|
||||
if (request.getAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE) != null) {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
FlashMap outputFlashMap = new FlashMap();
|
||||
request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, outputFlashMap);
|
||||
|
||||
Map<String, ?> inputFlashMap = getFlashMap(request);
|
||||
Map<String, ?> inputFlashMap = lookupFlashMap(request);
|
||||
if (inputFlashMap != null) {
|
||||
request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, inputFlashMap);
|
||||
}
|
||||
|
||||
|
||||
FlashMap outputFlashMap = new FlashMap(this.hashCode());
|
||||
request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, outputFlashMap);
|
||||
|
||||
removeExpiredFlashMaps(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the flash attributes saved during the previous request if any.
|
||||
*
|
||||
* @return a read-only Map; or {@code null} if not found.
|
||||
* Look up the "input" FlashMap by matching the target request path and
|
||||
* the target request parameters configured in each available FlashMap
|
||||
* to the current request.
|
||||
*/
|
||||
private Map<String, ?> getFlashMap(HttpServletRequest request) {
|
||||
List<FlashMap> allMaps = retrieveFlashMaps(request, false);
|
||||
if (CollectionUtils.isEmpty(allMaps)) {
|
||||
private Map<String, ?> lookupFlashMap(HttpServletRequest request) {
|
||||
List<FlashMap> allFlashMaps = retrieveFlashMaps(request, false);
|
||||
if (CollectionUtils.isEmpty(allFlashMaps)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Looking up previous FlashMap among available FlashMaps: " + allMaps);
|
||||
logger.debug("Retrieved flash maps: " + allFlashMaps);
|
||||
}
|
||||
|
||||
List<FlashMap> matches = new ArrayList<FlashMap>();
|
||||
for (FlashMap flashMap : allMaps) {
|
||||
if (flashMap.matches(request)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Matched " + flashMap);
|
||||
}
|
||||
matches.add(flashMap);
|
||||
List<FlashMap> result = new ArrayList<FlashMap>();
|
||||
for (FlashMap flashMap : allFlashMaps) {
|
||||
if (isFlashMapForRequest(flashMap, request)) {
|
||||
result.add(flashMap);
|
||||
}
|
||||
}
|
||||
|
||||
if (!matches.isEmpty()) {
|
||||
Collections.sort(matches);
|
||||
FlashMap match = matches.remove(0);
|
||||
allMaps.remove(match);
|
||||
if (!result.isEmpty()) {
|
||||
Collections.sort(result);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Matching flash maps: " + result);
|
||||
}
|
||||
FlashMap match = result.remove(0);
|
||||
allFlashMaps.remove(match);
|
||||
return Collections.unmodifiableMap(match);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of all FlashMap instances from the HTTP session.
|
||||
* Compares the target request path and the target request parameters in the
|
||||
* given FlashMap and returns "true" if they match. If the FlashMap does not
|
||||
* have target request information, it matches any request.
|
||||
*/
|
||||
protected boolean isFlashMapForRequest(FlashMap flashMap, HttpServletRequest request) {
|
||||
if (flashMap.getTargetRequestPath() != null) {
|
||||
String requestUri = this.urlPathHelper.getRequestUri(request);
|
||||
if (!requestUri.equals(flashMap.getTargetRequestPath())
|
||||
&& !requestUri.equals(flashMap.getTargetRequestPath() + "/")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (flashMap.getTargetRequestParams() != null) {
|
||||
for (Map.Entry<String, String> entry : flashMap.getTargetRequestParams().entrySet()) {
|
||||
if (!entry.getValue().equals(request.getParameter(entry.getKey()))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all available FlashMap instances from the HTTP session.
|
||||
* @param request the current request
|
||||
* @param allowCreate whether to create and the FlashMap container if not found
|
||||
* @return a Map with all stored FlashMap instances; or {@code null}
|
||||
* @param allowCreate whether to create and save the FlashMap in the session
|
||||
* @return a Map with all FlashMap instances; or {@code null}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<FlashMap> retrieveFlashMaps(HttpServletRequest request, boolean allowCreate) {
|
||||
protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request, boolean allowCreate) {
|
||||
HttpSession session = request.getSession(allowCreate);
|
||||
if (session == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<FlashMap> allMaps = (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE);
|
||||
if (allMaps == null && allowCreate) {
|
||||
synchronized (DefaultFlashMapManager.class) {
|
||||
allMaps = (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE);
|
||||
if (allMaps == null) {
|
||||
allMaps = new CopyOnWriteArrayList<FlashMap>();
|
||||
session.setAttribute(FLASH_MAPS_SESSION_ATTRIBUTE, allMaps);
|
||||
}
|
||||
List<FlashMap> allFlashMaps = (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE);
|
||||
if (allFlashMaps == null && allowCreate) {
|
||||
synchronized (this) {
|
||||
allFlashMaps = (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE);
|
||||
if (allFlashMaps == null) {
|
||||
allFlashMaps = new CopyOnWriteArrayList<FlashMap>();
|
||||
session.setAttribute(FLASH_MAPS_SESSION_ATTRIBUTE, allFlashMaps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allMaps;
|
||||
}
|
||||
|
||||
private void removeExpiredFlashMaps(HttpServletRequest request) {
|
||||
List<FlashMap> allMaps = retrieveFlashMaps(request, false);
|
||||
if (allMaps != null && !allMaps.isEmpty()) {
|
||||
List<FlashMap> expiredMaps = new ArrayList<FlashMap>();
|
||||
for (FlashMap flashMap : allMaps) {
|
||||
if (flashMap.isExpired()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Removing expired FlashMap: " + flashMap);
|
||||
}
|
||||
expiredMaps.add(flashMap);
|
||||
}
|
||||
}
|
||||
allMaps.removeAll(expiredMaps);
|
||||
}
|
||||
return allFlashMaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The HTTP session is not created if the current FlashMap instance is empty.
|
||||
* Iterate available FlashMap instances and remove the ones that have expired.
|
||||
*/
|
||||
private void removeExpiredFlashMaps(HttpServletRequest request) {
|
||||
List<FlashMap> allMaps = retrieveFlashMaps(request, false);
|
||||
if (CollectionUtils.isEmpty(allMaps)) {
|
||||
return;
|
||||
}
|
||||
List<FlashMap> expiredMaps = new ArrayList<FlashMap>();
|
||||
for (FlashMap flashMap : allMaps) {
|
||||
if (flashMap.isExpired()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Removing expired FlashMap: " + flashMap);
|
||||
}
|
||||
expiredMaps.add(flashMap);
|
||||
}
|
||||
}
|
||||
allMaps.removeAll(expiredMaps);
|
||||
}
|
||||
|
||||
public void requestCompleted(HttpServletRequest request) {
|
||||
FlashMap flashMap = (FlashMap) request.getAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE);
|
||||
if (flashMap == null) {
|
||||
throw new IllegalStateException(
|
||||
"Did not find a FlashMap exposed as the request attribute " + OUTPUT_FLASH_MAP_ATTRIBUTE);
|
||||
}
|
||||
|
||||
if (!flashMap.isEmpty()) {
|
||||
if (!flashMap.isEmpty() && flashMap.isCreatedBy(this.hashCode())) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Saving FlashMap=" + flashMap);
|
||||
}
|
||||
List<FlashMap> allFlashMaps = retrieveFlashMaps(request, true);
|
||||
decodeAndNormalizeTargetPath(flashMap, request);
|
||||
flashMap.startExpirationPeriod(this.flashTimeout);
|
||||
allFlashMaps.add(flashMap);
|
||||
retrieveFlashMaps(request, true).add(flashMap);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the target request path in the given FlashMap is decoded and also
|
||||
* normalized (if it is relative) against the current request URL.
|
||||
*/
|
||||
private void decodeAndNormalizeTargetPath(FlashMap flashMap, HttpServletRequest request) {
|
||||
String path = flashMap.getTargetRequestPath();
|
||||
if (path != null) {
|
||||
path = urlPathHelper.decodeRequestString(request, path);
|
||||
if (path.charAt(0) != '/') {
|
||||
String requestUri = this.urlPathHelper.getRequestUri(request);
|
||||
path = requestUri.substring(0, requestUri.lastIndexOf('/') + 1) + path;
|
||||
path = StringUtils.cleanPath(path);
|
||||
}
|
||||
flashMap.setTargetRequestPath(path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.FlashMap;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.SmartView;
|
||||
@@ -261,24 +262,27 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
|
||||
enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
|
||||
}
|
||||
|
||||
UriTemplate uriTemplate = createUriTemplate(targetUrl, enc);
|
||||
if (uriTemplate.getVariableNames().size() > 0) {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
vars.putAll(getCurrentUriVars(request));
|
||||
vars.putAll(model);
|
||||
targetUrl = new StringBuilder(uriTemplate.expand(vars).toString());
|
||||
model = removeKeys(model, uriTemplate.getVariableNames());
|
||||
if (StringUtils.hasText(targetUrl)) {
|
||||
UriTemplate uriTemplate = createUriTemplate(targetUrl, enc);
|
||||
if (uriTemplate.getVariableNames().size() > 0) {
|
||||
Map<String, Object> vars = new HashMap<String, Object>();
|
||||
vars.putAll(getCurrentUriVars(request));
|
||||
vars.putAll(model);
|
||||
targetUrl = new StringBuilder(uriTemplate.expand(vars).toString());
|
||||
model = removeKeys(model, uriTemplate.getVariableNames());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
|
||||
if (!CollectionUtils.isEmpty(flashMap)) {
|
||||
flashMap.setExpectedRequestUri(request, targetUrl.toString());
|
||||
String targetPath = WebUtils.extractUrlPath(targetUrl.toString());
|
||||
flashMap.setTargetRequestPath(targetPath);
|
||||
}
|
||||
|
||||
if (this.exposeModelAttributes) {
|
||||
appendQueryProperties(targetUrl, model, enc);
|
||||
if (!CollectionUtils.isEmpty(flashMap)) {
|
||||
flashMap.setExpectedRequestParams(model);
|
||||
flashMap.addTargetRequestParams(model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user