From 5a2a45b67dfbaafa44d0e95ce112c6c23e937ca1 Mon Sep 17 00:00:00 2001 From: markt Date: Sun, 6 Feb 2011 18:46:42 +0000 Subject: [PATCH] Move attribute name definitions to AccessLog interface AccessLog implementations need to be aware of attributes since they aren't always set (e.g. if request is rejected early in the connector) git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1067725 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/AccessLog.java | 56 +++++++++++++++++ java/org/apache/catalina/core/StandardEngine.java | 13 ++++ .../org/apache/catalina/valves/AccessLogValve.java | 73 +++++++++++++++++++--- .../apache/catalina/valves/JDBCAccessLogValve.java | 49 +++++++++++++-- java/org/apache/catalina/valves/RemoteIpValve.java | 9 +-- 5 files changed, 183 insertions(+), 17 deletions(-) diff --git a/java/org/apache/catalina/AccessLog.java b/java/org/apache/catalina/AccessLog.java index 83fedc703..0d7ff950f 100644 --- a/java/org/apache/catalina/AccessLog.java +++ b/java/org/apache/catalina/AccessLog.java @@ -34,6 +34,35 @@ import org.apache.catalina.connector.Response; public interface AccessLog { /** + * Name of request attribute used to override the remote address recorded by + * the AccessLog. + */ + public static final String REMOTE_ADDR_ATTRIBUTE = + "org.apache.catalina.AccessLog.RemoteAddr"; + + /** + * Name of request attribute used to override remote host name recorded by + * the AccessLog. + */ + public static final String REMOTE_HOST_ATTRIBUTE = + "org.apache.catalina.AccessLog.RemoteHost"; + + /** + * Name of request attribute used to override the protocol recorded by the + * AccessLog. + */ + public static final String PROTOCOL_ATTRIBUTE = + "org.apache.catalina.AccessLog.Protocol"; + + /** + * Name of request attribute used to override the server port recorded by + * the AccessLog. + */ + public static final String SERVER_PORT_ATTRIBUTE = + "org.apache.catalina.AccessLog.ServerPort"; + + + /** * Add the request/response to the access log using the specified processing * time. * @@ -43,4 +72,31 @@ public interface AccessLog { * milliseconds (use 0 if not known) */ public void log(Request request, Response response, long time); + + /** + * Should this valve set request attributes for IP address, Hostname, + * protocol and port used for the request? This are typically used in + * conjunction with the {@link AccessLogValve} which will otherwise log the + * original values. Default is true. + * + * The attributes set are: + * + * + * @param requestAttributesEnabled true causes the attributes + * to be set, false disables + * the setting of the attributes. + */ + public void setRequestAttributesEnabled(boolean requestAttributesEnabled); + + /** + * @see #setRequestAttributesEnabled(boolean) + * @return true if the attributes will be logged, otherwise + * false + */ + public boolean getRequestAttributesEnabled(); } diff --git a/java/org/apache/catalina/core/StandardEngine.java b/java/org/apache/catalina/core/StandardEngine.java index 954502835..30ece97fe 100644 --- a/java/org/apache/catalina/core/StandardEngine.java +++ b/java/org/apache/catalina/core/StandardEngine.java @@ -400,6 +400,19 @@ public class StandardEngine extends ContainerBase implements Engine { public void log(Request request, Response response, long time) { // NOOP } + + @Override + public void setRequestAttributesEnabled( + boolean requestAttributesEnabled) { + // NOOP + + } + + @Override + public boolean getRequestAttributesEnabled() { + // NOOP + return false; + } } protected static final class AccessLogListener diff --git a/java/org/apache/catalina/valves/AccessLogValve.java b/java/org/apache/catalina/valves/AccessLogValve.java index e5e426a04..e72fdd1a0 100644 --- a/java/org/apache/catalina/valves/AccessLogValve.java +++ b/java/org/apache/catalina/valves/AccessLogValve.java @@ -293,6 +293,11 @@ public class AccessLogValve extends ValveBase implements AccessLog { */ protected AccessLogElement[] logElements = null; + /** + * @see #setRequestAttributesEnabled(boolean) + */ + protected boolean requestAttributesEnabled = true; + // ------------------------------------------------------------- Properties /** @@ -303,6 +308,22 @@ public class AccessLogValve extends ValveBase implements AccessLog { } /** + * {@inheritDoc} + */ + @Override + public void setRequestAttributesEnabled(boolean requestAttributesEnabled) { + this.requestAttributesEnabled = requestAttributesEnabled; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean getRequestAttributesEnabled() { + return requestAttributesEnabled; + } + + /** * @param enabled * The enabled to set. */ @@ -880,22 +901,40 @@ public class AccessLogValve extends ValveBase implements AccessLog { /** * write remote IP address - %a */ - protected static class RemoteAddrElement implements AccessLogElement { + protected class RemoteAddrElement implements AccessLogElement { @Override public void addElement(StringBuilder buf, Date date, Request request, Response response, long time) { - buf.append(request.getRemoteAddr()); + if (requestAttributesEnabled) { + Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE); + if (addr == null) { + buf.append(request.getRemoteAddr()); + } else { + buf.append(addr); + } + } else { + buf.append(request.getRemoteAddr()); + } } } /** * write remote host name - %h */ - protected static class HostElement implements AccessLogElement { + protected class HostElement implements AccessLogElement { @Override public void addElement(StringBuilder buf, Date date, Request request, Response response, long time) { - buf.append(request.getRemoteHost()); + if (requestAttributesEnabled) { + Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE); + if (host == null) { + buf.append(request.getRemoteHost()); + } else { + buf.append(host); + } + } else { + buf.append(request.getRemoteHost()); + } } } @@ -913,11 +952,20 @@ public class AccessLogValve extends ValveBase implements AccessLog { /** * write request protocol - %H */ - protected static class ProtocolElement implements AccessLogElement { + protected class ProtocolElement implements AccessLogElement { @Override public void addElement(StringBuilder buf, Date date, Request request, Response response, long time) { - buf.append(request.getProtocol()); + if (requestAttributesEnabled) { + Object proto = request.getAttribute(PROTOCOL_ATTRIBUTE); + if (proto == null) { + buf.append(request.getProtocol()); + } else { + buf.append(proto); + } + } else { + buf.append(request.getProtocol()); + } } } @@ -1011,11 +1059,20 @@ public class AccessLogValve extends ValveBase implements AccessLog { /** * write local port on which this request was received - %p */ - protected static class LocalPortElement implements AccessLogElement { + protected class LocalPortElement implements AccessLogElement { @Override public void addElement(StringBuilder buf, Date date, Request request, Response response, long time) { - buf.append(request.getServerPort()); + if (requestAttributesEnabled) { + Object port = request.getAttribute(SERVER_PORT_ATTRIBUTE); + if (port == null) { + buf.append(request.getServerPort()); + } else { + buf.append(port); + } + } else { + buf.append(request.getServerPort()); + } } } diff --git a/java/org/apache/catalina/valves/JDBCAccessLogValve.java b/java/org/apache/catalina/valves/JDBCAccessLogValve.java index 75539ed87..2ba0069fa 100644 --- a/java/org/apache/catalina/valves/JDBCAccessLogValve.java +++ b/java/org/apache/catalina/valves/JDBCAccessLogValve.java @@ -210,6 +210,10 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { private long currentTimeMillis; + /** + * @see #setRequestAttributesEnabled(boolean) + */ + protected boolean requestAttributesEnabled = true; /** * The descriptive information about this implementation. @@ -219,7 +223,23 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { // ------------------------------------------------------------- Properties - + + /** + * {@inheritDoc} + */ + @Override + public void setRequestAttributesEnabled(boolean requestAttributesEnabled) { + this.requestAttributesEnabled = requestAttributesEnabled; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean getRequestAttributesEnabled() { + return requestAttributesEnabled; + } + /** * Return the username to use to connect to the database. * @@ -451,10 +471,29 @@ public final class JDBCAccessLogValve extends ValveBase implements AccessLog { final String EMPTY = "" ; String remoteHost; - if(resolveHosts) - remoteHost = request.getRemoteHost(); - else - remoteHost = request.getRemoteAddr(); + if(resolveHosts) { + if (requestAttributesEnabled) { + Object host = request.getAttribute(REMOTE_HOST_ATTRIBUTE); + if (host == null) { + remoteHost = request.getRemoteHost(); + } else { + remoteHost = (String) host; + } + } else { + remoteHost = request.getRemoteHost(); + } + } else { + if (requestAttributesEnabled) { + Object addr = request.getAttribute(REMOTE_ADDR_ATTRIBUTE); + if (addr == null) { + remoteHost = request.getRemoteAddr(); + } else { + remoteHost = (String) addr; + } + } else { + remoteHost = request.getRemoteAddr(); + } + } String user = request.getRemoteUser(); String query=request.getRequestURI(); diff --git a/java/org/apache/catalina/valves/RemoteIpValve.java b/java/org/apache/catalina/valves/RemoteIpValve.java index 7fc138ba7..1ea461586 100644 --- a/java/org/apache/catalina/valves/RemoteIpValve.java +++ b/java/org/apache/catalina/valves/RemoteIpValve.java @@ -26,6 +26,7 @@ import java.util.regex.Pattern; import javax.servlet.ServletException; +import org.apache.catalina.AccessLog; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.juli.logging.Log; @@ -622,13 +623,13 @@ public class RemoteIpValve extends ValveBase { } } if (requestAttributesEnabled) { - request.setAttribute("org.apache.catalina.RemoteAddr", + request.setAttribute(AccessLog.REMOTE_ADDR_ATTRIBUTE, request.getRemoteAddr()); - request.setAttribute("org.apache.catalina.RemoteHost", + request.setAttribute(AccessLog.REMOTE_HOST_ATTRIBUTE, request.getRemoteHost()); - request.setAttribute("org.apache.catalina.Protocol", + request.setAttribute(AccessLog.PROTOCOL_ATTRIBUTE, request.getProtocol()); - request.setAttribute("org.apache.catalina.ServerPort", + request.setAttribute(AccessLog.SERVER_PORT_ATTRIBUTE, Integer.valueOf(request.getServerPort())); } try { -- 2.11.0