Skip to content

Commit

Permalink
exclude template URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Dec 16, 2021
1 parent 1b01288 commit 87700c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.appng</groupId>
<artifactId>appng-tomcat-session</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
<description>appNG Tomcat Session</description>
<build>
<plugins>
Expand Down
Expand Up @@ -16,45 +16,68 @@
package org.appng.tomcat.session.hazelcast.v2;

import java.io.IOException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.servlet.ServletException;

import org.apache.catalina.Session;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.PersistentValve;
import org.apache.catalina.valves.ValveBase;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;

/**
* A {@link Valve} that uses {@link HazelcastManager} to store a {@link Session}
*/
public class HazelcastSessionTrackerValve extends PersistentValve {
public class HazelcastSessionTrackerValve extends ValveBase {

private final Log log = Utils.getLog(HazelcastSessionTrackerValve.class);

protected Pattern filter = Pattern.compile("^/template/.*$");

@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
if (isRequestWithoutSession(request.getDecodedRequestURI())) {
getNext().invoke(request, response);
return;
}

long start = System.currentTimeMillis();
try {
getNext().invoke(request, response);
} finally {
long start = System.currentTimeMillis();
HazelcastManager manager = (HazelcastManager) request.getContext().getManager();
Session session = request.getSessionInternal(false);
if (session != null) {
if (session.isValid()) {
log.trace(String.format("Request with session completed, saving session %s", session.getId()));
manager.commit(session);
} else {
log.trace(String.format("HTTP Session has been invalidated, removing %s", session.getId()));
manager.remove(session);
}
manager.commit(session);
}

long duration = System.currentTimeMillis() - start;
if (log.isDebugEnabled() && duration > 0) {
log.debug(String.format("handling session for %s took %sms", request.getServletPath(), duration));
log.debug(String.format("handling session %s for %s took %dms", session.getId(),
request.getServletPath(), duration));
}
}
}

protected boolean isRequestWithoutSession(String uri) {
return filter != null && filter.matcher(uri).matches();
}

public String getFilter() {
return null == filter ? null : filter.toString();
}

public void setFilter(String filter) {
if (filter == null || filter.length() == 0) {
this.filter = null;
} else {
try {
this.filter = Pattern.compile(filter);
} catch (PatternSyntaxException pse) {
log.error("ivalid pattern", pse);
}
}
}
Expand Down

0 comments on commit 87700c0

Please sign in to comment.