This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 5f217e7a12 Expose clientIdentifier generation as a function and expose
a setter to modify it as needed
5f217e7a12 is described below
commit 5f217e7a124ad964b154acfcbecf0e7c1458b421
Author: Brian Matzon <[email protected]>
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 2a23433501..abd733bd26 100644
--- a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
+++ b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
@@ -21,6 +21,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 javax.servlet.ServletException;
@@ -59,6 +60,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.
@@ -162,6 +165,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 {
@@ -184,7 +196,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=" +
@@ -262,12 +274,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 72f932d3f8..bdb488247b 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: [email protected]
For additional commands, e-mail: [email protected]