From 0ac1176152fe823318a40ddac8d143eea80d1577 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 7 Oct 2013 15:45:42 -0500 Subject: [PATCH] Polish RequestMatcher logging and toString --- .../DelegatingAuthenticationEntryPoint.java | 16 +++++++++++- .../security/web/util/AndRequestMatcher.java | 14 ++++++++++ .../web/util/MediaTypeRequestMatcher.java | 26 +++++++++++++++++-- .../web/util/NegatedRequestMatcher.java | 15 ++++++++++- .../security/web/util/OrRequestMatcher.java | 13 ++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java b/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java index 933714e93f..10f040b419 100644 --- a/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java +++ b/web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java @@ -22,6 +22,8 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; @@ -55,6 +57,7 @@ import org.springframework.util.Assert; * @since 3.0.2 */ public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean { + private final Log logger = LogFactory.getLog(getClass()); private final LinkedHashMap entryPoints; private AuthenticationEntryPoint defaultEntryPoint; @@ -68,12 +71,23 @@ public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPo throws IOException, ServletException { for (RequestMatcher requestMatcher : entryPoints.keySet()) { + if(logger.isDebugEnabled()) { + logger.debug("Trying to match using " + requestMatcher); + } if (requestMatcher.matches(request)) { - entryPoints.get(requestMatcher).commence(request, response, authException); + AuthenticationEntryPoint entryPoint = entryPoints.get(requestMatcher); + if(logger.isDebugEnabled()) { + logger.debug("Match found! Executing " + entryPoint); + } + entryPoint.commence(request, response, authException); return; } } + if(logger.isDebugEnabled()) { + logger.debug("No match found. Using default entry point " + defaultEntryPoint); + } + // No EntryPoint matched, use defaultEntryPoint defaultEntryPoint.commence(request, response, authException); } diff --git a/web/src/main/java/org/springframework/security/web/util/AndRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/AndRequestMatcher.java index d82a7f99b8..c635edd2d9 100644 --- a/web/src/main/java/org/springframework/security/web/util/AndRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/AndRequestMatcher.java @@ -20,6 +20,8 @@ import java.util.List; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; @@ -31,6 +33,8 @@ import org.springframework.util.Assert; * @since 3.2 */ public final class AndRequestMatcher implements RequestMatcher { + private final Log logger = LogFactory.getLog(getClass()); + private final List requestMatchers; /** @@ -57,10 +61,20 @@ public final class AndRequestMatcher implements RequestMatcher { public boolean matches(HttpServletRequest request) { for(RequestMatcher matcher : requestMatchers) { + if(logger.isDebugEnabled()) { + logger.debug("Trying to match using " + matcher); + } if(!matcher.matches(request)) { + logger.debug("Did not match"); return false; } } + logger.debug("All requestMatchers returned true"); return true; } + + @Override + public String toString() { + return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]"; + } } \ No newline at end of file diff --git a/web/src/main/java/org/springframework/security/web/util/MediaTypeRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/MediaTypeRequestMatcher.java index 58d24c226c..1063dc8011 100644 --- a/web/src/main/java/org/springframework/security/web/util/MediaTypeRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/MediaTypeRequestMatcher.java @@ -175,19 +175,33 @@ public final class MediaTypeRequestMatcher implements RequestMatcher { logger.debug("Failed to parse MediaTypes, returning false", e); return false; } + if(logger.isDebugEnabled()) { + logger.debug("httpRequestMediaTypes=" + httpRequestMediaTypes); + } for(MediaType httpRequestMediaType : httpRequestMediaTypes) { + if(logger.isDebugEnabled()) { + logger.debug("Processing " + httpRequestMediaType); + } if(shouldIgnore(httpRequestMediaType)) { + logger.debug("Ignoring"); continue; } if(useEquals) { - return matchingMediaTypes.contains(httpRequestMediaType); + boolean isEqualTo = matchingMediaTypes.contains(httpRequestMediaType); + logger.debug("isEqualTo " + isEqualTo); + return isEqualTo; } for(MediaType matchingMediaType : matchingMediaTypes) { - if(matchingMediaType.isCompatibleWith(httpRequestMediaType)) { + boolean isCompatibleWith = matchingMediaType.isCompatibleWith(httpRequestMediaType); + if(logger.isDebugEnabled()) { + logger.debug(matchingMediaType + " .isCompatibleWith " + httpRequestMediaType + " = " + isCompatibleWith); + } + if(isCompatibleWith) { return true; } } } + logger.debug("Did not match any media types"); return false; } @@ -224,4 +238,12 @@ public final class MediaTypeRequestMatcher implements RequestMatcher { public void setIgnoredMediaTypes(Set ignoredMediaTypes) { this.ignoredMediaTypes = ignoredMediaTypes; } + + @Override + public String toString() { + return "MediaTypeRequestMatcher [contentNegotiationStrategy=" + + contentNegotiationStrategy + ", matchingMediaTypes=" + + matchingMediaTypes + ", useEquals=" + useEquals + + ", ignoredMediaTypes=" + ignoredMediaTypes + "]"; + } } \ No newline at end of file diff --git a/web/src/main/java/org/springframework/security/web/util/NegatedRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/NegatedRequestMatcher.java index 14f432d59d..369158ec0b 100644 --- a/web/src/main/java/org/springframework/security/web/util/NegatedRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/NegatedRequestMatcher.java @@ -17,6 +17,8 @@ package org.springframework.security.web.util; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; /** @@ -29,6 +31,8 @@ import org.springframework.util.Assert; * @since 3.2 */ public class NegatedRequestMatcher implements RequestMatcher { + private final Log logger = LogFactory.getLog(getClass()); + private final RequestMatcher requestMatcher; /** @@ -41,6 +45,15 @@ public class NegatedRequestMatcher implements RequestMatcher { } public boolean matches(HttpServletRequest request) { - return !requestMatcher.matches(request); + boolean result = !requestMatcher.matches(request); + if(logger.isDebugEnabled()) { + logger.debug("matches = " + result); + } + return result; + } + + @Override + public String toString() { + return "NegatedRequestMatcher [requestMatcher=" + requestMatcher + "]"; } } \ No newline at end of file diff --git a/web/src/main/java/org/springframework/security/web/util/OrRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/OrRequestMatcher.java index a34d61eb59..5b84823497 100644 --- a/web/src/main/java/org/springframework/security/web/util/OrRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/OrRequestMatcher.java @@ -20,6 +20,8 @@ import java.util.List; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; @@ -31,6 +33,7 @@ import org.springframework.util.Assert; * @since 3.2 */ public final class OrRequestMatcher implements RequestMatcher { + private final Log logger = LogFactory.getLog(getClass()); private final List requestMatchers; /** @@ -57,10 +60,20 @@ public final class OrRequestMatcher implements RequestMatcher { public boolean matches(HttpServletRequest request) { for(RequestMatcher matcher : requestMatchers) { + if(logger.isDebugEnabled()) { + logger.debug("Trying to match using " + matcher); + } if(matcher.matches(request)) { + logger.debug("matched"); return true; } } + logger.debug("No matches found"); return false; } + + @Override + public String toString() { + return "OrRequestMatcher [requestMatchers=" + requestMatchers + "]"; + } } \ No newline at end of file