Move attribute name definitions to AccessLog interface
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 6 Feb 2011 18:46:42 +0000 (18:46 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 6 Feb 2011 18:46:42 +0000 (18:46 +0000)
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
java/org/apache/catalina/core/StandardEngine.java
java/org/apache/catalina/valves/AccessLogValve.java
java/org/apache/catalina/valves/JDBCAccessLogValve.java
java/org/apache/catalina/valves/RemoteIpValve.java

index 83fedc7..0d7ff95 100644 (file)
@@ -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 <code>true</code>.
+     * 
+     * The attributes set are:
+     * <ul>
+     * <li>org.apache.catalina.RemoteAddr</li>
+     * <li>org.apache.catalina.RemoteHost</li>
+     * <li>org.apache.catalina.Protocol</li>
+     * <li>org.apache.catalina.ServerPost</li>
+     * </ul>
+     * 
+     * @param requestAttributesEnabled  <code>true</code> causes the attributes
+     *                                  to be set, <code>false</code> disables
+     *                                  the setting of the attributes. 
+     */
+    public void setRequestAttributesEnabled(boolean requestAttributesEnabled);
+    
+    /**
+     * @see #setRequestAttributesEnabled(boolean)
+     * @return <code>true</code> if the attributes will be logged, otherwise
+     *         <code>false</code>
+     */
+    public boolean getRequestAttributesEnabled();
 }
index 9545028..30ece97 100644 (file)
@@ -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
index e5e426a..e72fdd1 100644 (file)
@@ -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());
+            }
         }
     }
 
index 75539ed..2ba0069 100644 (file)
@@ -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();
         
index 7fc138b..1ea4615 100644 (file)
@@ -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 {