Rainer Montag Rainer Montag - 2 years ago 70
Java Question

Tomcat 8.5.X: Migrate Valve javacode from Tomcat 7

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;
}
}
}


The
Container
interface does not provide the
getManager()
method anymore. According to the Tomcat migration guide, accessing the
Manager
has moved from
Container
to
Context
.

Can anybody give me a hint how to access the
Manager
from within the
ValveBase
object?

Thanks in advance,
Rainer

EDIT: The Context configuration is - as correctly noted in the answer -

<Context path="" ...>
<Valve className="SessionManagerValve" />
</Context>

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download