tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1067725 - in /tomcat/trunk/java/org/apache/catalina: AccessLog.java core/StandardEngine.java valves/AccessLogValve.java valves/JDBCAccessLogValve.java valves/RemoteIpValve.java
Date Sun, 06 Feb 2011 18:46:43 GMT
Author: markt
Date: Sun Feb  6 18:46:42 2011
New Revision: 1067725

URL: http://svn.apache.org/viewvc?rev=1067725&view=rev
Log:
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)

Modified:
    tomcat/trunk/java/org/apache/catalina/AccessLog.java
    tomcat/trunk/java/org/apache/catalina/core/StandardEngine.java
    tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java
    tomcat/trunk/java/org/apache/catalina/valves/JDBCAccessLogValve.java
    tomcat/trunk/java/org/apache/catalina/valves/RemoteIpValve.java

Modified: tomcat/trunk/java/org/apache/catalina/AccessLog.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/AccessLog.java?rev=1067725&r1=1067724&r2=1067725&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/AccessLog.java (original)
+++ tomcat/trunk/java/org/apache/catalina/AccessLog.java Sun Feb  6 18:46:42 2011
@@ -34,6 +34,35 @@ import org.apache.catalina.connector.Res
 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();
 }

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardEngine.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardEngine.java?rev=1067725&r1=1067724&r2=1067725&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardEngine.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardEngine.java Sun Feb  6 18:46:42 2011
@@ -400,6 +400,19 @@ public class StandardEngine extends Cont
         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

Modified: tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java?rev=1067725&r1=1067724&r2=1067725&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/AccessLogValve.java Sun Feb  6 18:46:42 2011
@@ -293,6 +293,11 @@ public class AccessLogValve extends Valv
      */
     protected AccessLogElement[] logElements = null;
 
+    /**
+     * @see #setRequestAttributesEnabled(boolean)
+     */
+    protected boolean requestAttributesEnabled = true;
+
     // ------------------------------------------------------------- Properties
 
     /**
@@ -303,6 +308,22 @@ public class AccessLogValve extends Valv
     }
 
     /**
+     * {@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 Valv
     /**
      * 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 Valv
     /**
      * 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 Valv
     /**
      * 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());
+            }
         }
     }
 

Modified: tomcat/trunk/java/org/apache/catalina/valves/JDBCAccessLogValve.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/JDBCAccessLogValve.java?rev=1067725&r1=1067724&r2=1067725&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/JDBCAccessLogValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/JDBCAccessLogValve.java Sun Feb  6 18:46:42
2011
@@ -210,6 +210,10 @@ public final class JDBCAccessLogValve ex
 
     private long currentTimeMillis;
 
+    /**
+     * @see #setRequestAttributesEnabled(boolean)
+     */
+    protected boolean requestAttributesEnabled = true;
 
     /**
      * The descriptive information about this implementation.
@@ -219,7 +223,23 @@ public final class JDBCAccessLogValve ex
 
 
     // ------------------------------------------------------------- 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 ex
         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();
         

Modified: tomcat/trunk/java/org/apache/catalina/valves/RemoteIpValve.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/RemoteIpValve.java?rev=1067725&r1=1067724&r2=1067725&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/RemoteIpValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/RemoteIpValve.java Sun Feb  6 18:46:42 2011
@@ -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 Valve
             }
         }
         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 {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message