Minor cleanup on Ant / Regex Request Matchers

- Removed duplicative code for transforming String into HttpMethod
 - Removed an unnecessary array initialization
This commit is contained in:
Nick McKinney 2020-12-11 17:40:55 -05:00 committed by Eleftheria Stein-Kousathana
parent 6be25df1db
commit 5306d4c4d5
3 changed files with 4 additions and 33 deletions

View File

@ -87,7 +87,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
* @return the object that is chained after creating the {@link RequestMatcher} * @return the object that is chained after creating the {@link RequestMatcher}
*/ */
public C antMatchers(HttpMethod method) { public C antMatchers(HttpMethod method) {
return antMatchers(method, new String[] { "/**" }); return antMatchers(method, "/**");
} }
/** /**

View File

@ -141,7 +141,7 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria
@Override @Override
public boolean matches(HttpServletRequest request) { public boolean matches(HttpServletRequest request) {
if (this.httpMethod != null && StringUtils.hasText(request.getMethod()) if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
&& this.httpMethod != valueOf(request.getMethod())) { && this.httpMethod != HttpMethod.resolve(request.getMethod())) {
return false; return false;
} }
if (this.pattern.equals(MATCH_ALL)) { if (this.pattern.equals(MATCH_ALL)) {
@ -211,21 +211,6 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria
return sb.toString(); return sb.toString();
} }
/**
* Provides a save way of obtaining the HttpMethod from a String. If the method is
* invalid, returns null.
* @param method the HTTP method to use.
* @return the HttpMethod or null if method is invalid.
*/
private static HttpMethod valueOf(String method) {
try {
return HttpMethod.valueOf(method);
}
catch (IllegalArgumentException ex) {
return null;
}
}
private interface Matcher { private interface Matcher {
boolean matches(String path); boolean matches(String path);

View File

@ -81,7 +81,8 @@ public final class RegexRequestMatcher implements RequestMatcher {
*/ */
@Override @Override
public boolean matches(HttpServletRequest request) { public boolean matches(HttpServletRequest request) {
if (this.httpMethod != null && request.getMethod() != null && this.httpMethod != valueOf(request.getMethod())) { if (this.httpMethod != null && request.getMethod() != null
&& this.httpMethod != HttpMethod.resolve(request.getMethod())) {
return false; return false;
} }
String url = request.getServletPath(); String url = request.getServletPath();
@ -101,21 +102,6 @@ public final class RegexRequestMatcher implements RequestMatcher {
return this.pattern.matcher(url).matches(); return this.pattern.matcher(url).matches();
} }
/**
* Provides a save way of obtaining the HttpMethod from a String. If the method is
* invalid, returns null.
* @param method the HTTP method to use.
* @return the HttpMethod or null if method is invalid.
*/
private static HttpMethod valueOf(String method) {
try {
return HttpMethod.valueOf(method);
}
catch (IllegalArgumentException ex) {
return null;
}
}
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();