2

I'm writing an abstract Tomcat servlet, Foo and I want to make it so that whenever a method is called in FooImplement, it runs a method from Foo.

So before every method call in FooImplement I want to run checkAccess defined in Foo. If the user does not have access, the method is not invoked and the request is redirected elsewhere. If the user does have access, the method is invoked.

I thought I could use a Proxy as seen here but the problem is I cannot pass tomcat the proxy instance because that would involve an impossible constructor of

public Foo() {
    return (Foo) Proxy.newProxyInstance(
    this.getClass().getClassLoader(),
    new Class[]{this.getClass()},
    new FooInvocationHandler(this);
}

Does anyone know of a way I can do this?

Community
  • 1
  • 1
DontTurnAround
  • 684
  • 1
  • 7
  • 20
  • Can't you use a ServletFilter? – Jaumzera May 05 '16 at 00:09
  • @Jaumzera the filter runs on doGet and doPost. I want to run my checkAccess method when the implementations of doGet and doPost call other methods. The reason for this is access is defined using an annotation on the method (e.g. `deleteUser`). Therefore I can't filter on that high of a level I don't think. – DontTurnAround May 05 '16 at 00:21
  • @Jaumzera I tried implementing something like this and I can't see how it could be done. – DontTurnAround May 05 '16 at 00:42
  • Hey @DontTurnAround, have you seen this one? http://stackoverflow.com/questions/576918/how-do-i-intercept-a-method-invocation-with-standard-java-features-no-aspectj-e – Jaumzera May 09 '16 at 16:48
  • I don't think that works @Jaumzera because there's no way to make Tomcat use an instance of the proxy instead of the Servlet. – DontTurnAround May 10 '16 at 04:47
  • So try to implement your own version of HttpServlet to do what you want. You can put your code inside META-INF/services in order to make Tomcat use it instead of the original source. Take a look at http://stackoverflow.com/questions/7692497/tomcat-wont-load-my-meta-inf-services-javax-servlet-servletcontainerinitializ and https://docs.oracle.com/javase/tutorial/ext/basics/spi.html. – Jaumzera May 10 '16 at 12:09

0 Answers0