Skip to content

Commit

Permalink
Use Servlet 4 mapping type if available
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and kenny5he committed Jun 21, 2020
1 parent f940a25 commit cc09050
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion spring-web/spring-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
optional(project(":spring-aop"))
optional(project(":spring-context"))
optional(project(":spring-oxm"))
optional("javax.servlet:javax.servlet-api:3.1.0")
optional("javax.servlet:javax.servlet-api") // Servlet 4 for mapping type
optional("javax.servlet.jsp:javax.servlet.jsp-api")
optional("javax.el:javax.el-api")
optional("javax.faces:javax.faces-api")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.MappingMatch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
Expand All @@ -49,6 +51,10 @@
*/
public class UrlPathHelper {

private static boolean isServlet4Present =
ClassUtils.isPresent("javax.servlet.http.HttpServletMapping",
UrlPathHelper.class.getClassLoader());

/**
* Special WebSphere request attribute, indicating the original request URI.
* Preferable over the standard Servlet 2.4 forward attribute on WebSphere,
Expand Down Expand Up @@ -154,6 +160,26 @@ protected String getDefaultEncoding() {
}


/**
* Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
* automates checking for a previously computed lookupPath saved as a
* request attribute. The attribute is only used for lookup purposes.
* @param request current HTTP request
* @param lookupPathAttributeName the request attribute to check
* @return the lookup path
* @since 5.2
* @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
*/
public String getLookupPathForRequest(HttpServletRequest request, @Nullable String lookupPathAttributeName) {
if (lookupPathAttributeName != null) {
String result = (String) request.getAttribute(lookupPathAttributeName);
if (result != null) {
return result;
}
}
return getLookupPathForRequest(request);
}

/**
* Return the mapping lookup path for the given request, within the current
* servlet mapping if applicable, else within the web application.
Expand All @@ -165,7 +191,7 @@ protected String getDefaultEncoding() {
*/
public String getLookupPathForRequest(HttpServletRequest request) {
// Always use full path within current servlet context?
if (this.alwaysUseFullPath) {
if (this.alwaysUseFullPath || skipServletPathDetermination(request)) {
return getPathWithinApplication(request);
}
// Else, use path within current servlet mapping if applicable
Expand All @@ -178,24 +204,14 @@ public String getLookupPathForRequest(HttpServletRequest request) {
}
}

/**
* Variant of {@link #getLookupPathForRequest(HttpServletRequest)} that
* automates checking for a previously computed lookupPath saved as a
* request attribute. The attribute is only used for lookup purposes.
* @param request current HTTP request
* @param lookupPathAttributeName the request attribute to check
* @return the lookup path
* @since 5.2
* @see org.springframework.web.servlet.HandlerMapping#LOOKUP_PATH
*/
public String getLookupPathForRequest(HttpServletRequest request, @Nullable String lookupPathAttributeName) {
if (lookupPathAttributeName != null) {
String result = (String) request.getAttribute(lookupPathAttributeName);
if (result != null) {
return result;
private boolean skipServletPathDetermination(HttpServletRequest request) {
if (isServlet4Present) {
if (request.getHttpServletMapping().getMappingMatch() != null) {
return !request.getHttpServletMapping().getMappingMatch().equals(MappingMatch.PATH) ||
request.getHttpServletMapping().getPattern().equals("/*");
}
}
return getLookupPathForRequest(request);
return false;
}

/**
Expand Down Expand Up @@ -658,4 +674,11 @@ public void setDefaultEncoding(String defaultEncoding) {
throw new UnsupportedOperationException();
}
};


private static class HttpServletMappingHelper {



}
}

0 comments on commit cc09050

Please sign in to comment.