Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Request, String> clientIdentifierFunction) {
this.clientIdentifierFunction = clientIdentifierFunction;
}

@Override
protected void initInternal() throws LifecycleException {
Expand All @@ -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=" +
Expand Down Expand Up @@ -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());
}
Expand Down