I need to migrate one of our custom Valve code from Tomcat 7 to Tomcat 8.5:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Container;
import org.apache.catalina.Manager;
import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
public class SessionManagerValve extends ValveBase {
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
// ...
}
public HttpSession findSession(String id, boolean updateLastAccessTime) {
try {
Container container = getContainer();
// works with Tomcat 7.x, but not with Tomcat 8.5 anymore...
Manager man = container.getManager();
Session sess = man.findSession(id);
return sess.getSession();
} catch (Exception e) {
return null;
}
}
}
Container
getManager()
Manager
Container
Context
Manager
ValveBase
<Context path="" ...>
<Valve className="SessionManagerValve" />
</Context>
Assuming your Valve
is at the Context
level (it would have to be for the code above to work) then you need:
((Context) getContainer()).getManager();