Polish RequestMatcher logging and toString

This commit is contained in:
Rob Winch 2013-10-07 15:45:42 -05:00
parent 76a8bbe98d
commit 0ac1176152
5 changed files with 80 additions and 4 deletions

View File

@ -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<RequestMatcher, AuthenticationEntryPoint> 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);
}

View File

@ -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<RequestMatcher> 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 + "]";
}
}

View File

@ -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<MediaType> ignoredMediaTypes) {
this.ignoredMediaTypes = ignoredMediaTypes;
}
@Override
public String toString() {
return "MediaTypeRequestMatcher [contentNegotiationStrategy="
+ contentNegotiationStrategy + ", matchingMediaTypes="
+ matchingMediaTypes + ", useEquals=" + useEquals
+ ", ignoredMediaTypes=" + ignoredMediaTypes + "]";
}
}

View File

@ -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 + "]";
}
}

View File

@ -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<RequestMatcher> 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 + "]";
}
}