Shared read-only instances of UrlPathHelper
UrlPathHelper is often created and used without customizations or with the same customizations. This commit introduces re-usable, instances. Effectively a backport of commit 23233c. Closes gh-25690
This commit is contained in:
@@ -76,20 +76,11 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
|
||||
}
|
||||
|
||||
|
||||
private final UrlPathHelper pathHelper;
|
||||
|
||||
private boolean removeOnly;
|
||||
|
||||
private boolean relativeRedirects;
|
||||
|
||||
|
||||
public ForwardedHeaderFilter() {
|
||||
this.pathHelper = new UrlPathHelper();
|
||||
this.pathHelper.setUrlDecode(false);
|
||||
this.pathHelper.setRemoveSemicolonContent(false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
|
||||
* removed only and the information in them ignored.
|
||||
@@ -145,7 +136,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
|
||||
filterChain.doFilter(theRequest, response);
|
||||
}
|
||||
else {
|
||||
HttpServletRequest theRequest = new ForwardedHeaderExtractingRequest(request, this.pathHelper);
|
||||
HttpServletRequest theRequest = new ForwardedHeaderExtractingRequest(request);
|
||||
HttpServletResponse theResponse = (this.relativeRedirects ?
|
||||
RelativeRedirectResponseWrapper.wrapIfNecessary(response, HttpStatus.SEE_OTHER) :
|
||||
new ForwardedHeaderExtractingResponse(response, theRequest));
|
||||
@@ -221,7 +212,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
|
||||
|
||||
private final String requestUrl;
|
||||
|
||||
public ForwardedHeaderExtractingRequest(HttpServletRequest request, UrlPathHelper pathHelper) {
|
||||
public ForwardedHeaderExtractingRequest(HttpServletRequest request) {
|
||||
super(request);
|
||||
|
||||
HttpRequest httpRequest = new ServletServerHttpRequest(request);
|
||||
@@ -235,7 +226,7 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
|
||||
|
||||
String prefix = getForwardedPrefix(request);
|
||||
this.contextPath = (prefix != null ? prefix : request.getContextPath());
|
||||
this.requestUri = this.contextPath + pathHelper.getPathWithinApplication(request);
|
||||
this.requestUri = this.contextPath + UrlPathHelper.rawPathInstance.getPathWithinApplication(request);
|
||||
this.requestUrl = this.scheme + "://" + this.host + (port == -1 ? "" : ":" + port) + this.requestUri;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -69,6 +70,8 @@ public class UrlPathHelper {
|
||||
|
||||
private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
|
||||
|
||||
private boolean readOnly = false;
|
||||
|
||||
|
||||
/**
|
||||
* Whether URL lookups should always use the full path within the current
|
||||
@@ -80,6 +83,7 @@ public class UrlPathHelper {
|
||||
* <p>By default this is set to "false".
|
||||
*/
|
||||
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
|
||||
checkReadOnly();
|
||||
this.alwaysUseFullPath = alwaysUseFullPath;
|
||||
}
|
||||
|
||||
@@ -102,6 +106,7 @@ public class UrlPathHelper {
|
||||
* @see java.net.URLDecoder#decode(String, String)
|
||||
*/
|
||||
public void setUrlDecode(boolean urlDecode) {
|
||||
checkReadOnly();
|
||||
this.urlDecode = urlDecode;
|
||||
}
|
||||
|
||||
@@ -118,6 +123,7 @@ public class UrlPathHelper {
|
||||
* <p>Default is "true".
|
||||
*/
|
||||
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
|
||||
checkReadOnly();
|
||||
this.removeSemicolonContent = removeSemicolonContent;
|
||||
}
|
||||
|
||||
@@ -125,6 +131,7 @@ public class UrlPathHelper {
|
||||
* Whether configured to remove ";" (semicolon) content from the request URI.
|
||||
*/
|
||||
public boolean shouldRemoveSemicolonContent() {
|
||||
checkReadOnly();
|
||||
return this.removeSemicolonContent;
|
||||
}
|
||||
|
||||
@@ -142,6 +149,7 @@ public class UrlPathHelper {
|
||||
* @see WebUtils#DEFAULT_CHARACTER_ENCODING
|
||||
*/
|
||||
public void setDefaultEncoding(String defaultEncoding) {
|
||||
checkReadOnly();
|
||||
this.defaultEncoding = defaultEncoding;
|
||||
}
|
||||
|
||||
@@ -152,6 +160,17 @@ public class UrlPathHelper {
|
||||
return this.defaultEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch to read-only mode where further configuration changes are not allowed.
|
||||
*/
|
||||
private void setReadOnly() {
|
||||
this.readOnly = true;
|
||||
}
|
||||
|
||||
private void checkReadOnly() {
|
||||
Assert.isTrue(!this.readOnly, "This instance cannot be modified");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the mapping lookup path for the given request, within the current
|
||||
@@ -605,4 +624,39 @@ public class UrlPathHelper {
|
||||
return !flagToUse;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shared, read-only instance with defaults. The following apply:
|
||||
* <ul>
|
||||
* <li>{@code alwaysUseFullPath=false}
|
||||
* <li>{@code urlDecode=true}
|
||||
* <li>{@code removeSemicolon=true}
|
||||
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
|
||||
* </ul>
|
||||
*/
|
||||
public static final UrlPathHelper defaultInstance = new UrlPathHelper();
|
||||
|
||||
static {
|
||||
defaultInstance.setReadOnly();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shared, read-only instance for the full, encoded path. The following apply:
|
||||
* <ul>
|
||||
* <li>{@code alwaysUseFullPath=true}
|
||||
* <li>{@code urlDecode=false}
|
||||
* <li>{@code removeSemicolon=false}
|
||||
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
|
||||
* </ul>
|
||||
*/
|
||||
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
|
||||
|
||||
static {
|
||||
rawPathInstance.setAlwaysUseFullPath(true);
|
||||
rawPathInstance.setUrlDecode(false);
|
||||
rawPathInstance.setRemoveSemicolonContent(false);
|
||||
rawPathInstance.setReadOnly();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user