diff --git a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java index 0c3eaca6276f..8b63297b7933 100644 --- a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java +++ b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java @@ -22,6 +22,7 @@ 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 clientIdentifierFunction = this::getClientIdentifier; + /** * Specifies a default constructor so async support can be configured. @@ -163,6 +166,14 @@ public void setContextAware(boolean isContextAware) { 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 + */ + public void setClientIdentifierFunction(Function clientIdentifierFunction) { + this.clientIdentifierFunction = clientIdentifierFunction; + } @Override protected void initInternal() throws LifecycleException { @@ -185,7 +196,7 @@ public void invoke(Request request, Response response) throws IOException, Servl 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 +274,12 @@ public void invoke(Request request, Response response) throws IOException, Servl } } - - 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()); }