You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2025/11/26 20:10:20 UTC

(tomcat) branch 11.0.x updated: Expose clientIdentifier generation as a function and expose a setter to modify it as needed

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/11.0.x by this push:
     new 5574fab1ea Expose clientIdentifier generation as a function and expose a setter to modify it as needed
5574fab1ea is described below

commit 5574fab1ea35d5134af18f0e4a15145ac823e53c
Author: Brian Matzon <br...@autorola.com>
AuthorDate: Thu Sep 25 10:37:58 2025 +0200

    Expose clientIdentifier generation as a function and expose a setter to modify it as needed
---
 .../valves/CrawlerSessionManagerValve.java         | 22 +++++++++++++++++-----
 webapps/docs/changelog.xml                         |  6 ++++++
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
index 0c3eaca627..1b44daec6e 100644
--- a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
+++ b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
@@ -22,6 +22,7 @@ import java.io.Serializable;
 import java.util.Enumeration;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
 import java.util.regex.Pattern;
 
 import jakarta.servlet.ServletException;
@@ -60,6 +61,8 @@ public class CrawlerSessionManagerValve extends ValveBase {
 
     private boolean isContextAware = true;
 
+    private Function<Request,String> clientIdentifierFunction = this::getClientIdentifier;
+
 
     /**
      * Specifies a default constructor so async support can be configured.
@@ -163,6 +166,15 @@ public class CrawlerSessionManagerValve extends ValveBase {
         this.isContextAware = isContextAware;
     }
 
+    /**
+     * Specify the clientIdentifier function that will be used to identify unique clients. The default is to use the
+     * client IP address, optionally combined with the host name and context name.
+     *
+     * @param clientIdentifierFunction The new function used to build identifiers for clients.
+     */
+    public void setClientIdentifierFunction(Function<Request,String> clientIdentifierFunction) {
+        this.clientIdentifierFunction = clientIdentifierFunction;
+    }
 
     @Override
     protected void initInternal() throws LifecycleException {
@@ -185,7 +197,7 @@ public class CrawlerSessionManagerValve extends ValveBase {
         boolean isBot = false;
         String sessionId = null;
         String clientIp = request.getRemoteAddr();
-        String clientIdentifier = getClientIdentifier(host, request.getContext(), clientIp);
+        String clientIdentifier = clientIdentifierFunction.apply(request);
 
         if (log.isTraceEnabled()) {
             log.trace(request.hashCode() + ": ClientIdentifier=" + clientIdentifier + ", RequestedSessionId=" +
@@ -263,12 +275,12 @@ public class CrawlerSessionManagerValve extends ValveBase {
         }
     }
 
-
-    private String getClientIdentifier(Host host, Context context, String clientIp) {
-        StringBuilder result = new StringBuilder(clientIp);
+    private String getClientIdentifier(Request request) {
+        StringBuilder result = new StringBuilder(request.getRemoteAddr());
         if (isHostAware) {
-            result.append('-').append(host.getName());
+            result.append('-').append(request.getHost().getName());
         }
+        Context context = request.getContext();
         if (isContextAware && context != null) {
             result.append(context.getName());
         }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index b8e35504d9..5efee5a34e 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -120,6 +120,12 @@
         <code>org.apache.catalina.Connector</code> no longer requires
         <code>org.apache.tomcat.jni.AprStatus</code> to be present. (markt)
       </fix>
+      <add>
+        Add the ability to use a custom function to generate the client
+        identifier in the <code>CrawlerSessionManagerValve</code>. This is only
+        available programmatically. Pull request <pr>902</pr> by Brian Matzon.
+        (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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