Initial attempt to fix RemoteIp issues observed with ASF Jira
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 6 Feb 2011 18:11:52 +0000 (18:11 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sun, 6 Feb 2011 18:11:52 +0000 (18:11 +0000)
Docs, filter and changelog commits to follow once it works

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1067718 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/valves/RemoteIpValve.java

index 228f080..7fc138b 100644 (file)
@@ -430,7 +430,12 @@ public class RemoteIpValve extends ValveBase {
      * @see #setRemoteIpHeader(String)
      */
     private String remoteIpHeader = "X-Forwarded-For";
-    
+
+    /**
+     * @see #setRequestAttributesEnabled(boolean)
+     */
+    private boolean requestAttributesEnabled = true;
+
     /**
      * @see RemoteIpValve#setTrustedProxies(String)
      */
@@ -496,6 +501,15 @@ public class RemoteIpValve extends ValveBase {
     }
 
     /**
+     * @see #setRequestAttributesEnabled(boolean)
+     * @return <code>true</code> if the attributes will be logged, otherwise
+     *         <code>false</code>
+     */
+    public boolean getRequestAttributesEnabled() {
+        return requestAttributesEnabled;
+    }
+
+    /**
      * @see #setTrustedProxies(String)
      * @return Regular expression that defines the trusted proxies
      */
@@ -607,6 +621,16 @@ public class RemoteIpValve extends ValveBase {
                         + request.getRemoteAddr() + "'");
             }
         }
+        if (requestAttributesEnabled) {
+            request.setAttribute("org.apache.catalina.RemoteAddr",
+                    request.getRemoteAddr());
+            request.setAttribute("org.apache.catalina.RemoteHost",
+                    request.getRemoteHost());
+            request.setAttribute("org.apache.catalina.Protocol",
+                    request.getProtocol());
+            request.setAttribute("org.apache.catalina.ServerPort",
+                    Integer.valueOf(request.getServerPort()));
+        }
         try {
             getNext().invoke(request, response);
         } finally {
@@ -725,6 +749,28 @@ public class RemoteIpValve extends ValveBase {
     }
     
     /**
+     * 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) {
+        this.requestAttributesEnabled = requestAttributesEnabled;
+    }
+
+    /**
      * <p>
      * Regular expression defining proxies that are trusted when they appear in
      * the {@link #remoteIpHeader} header.