Search code examples
javatomcatjersey-2.0forward

Cross-context request forwarding in tomcat results in java.lang.ClassCastException: org.glassfish.jersey.message.internal.TracingLogger


I am trying to forward a request from one webapp (using Jersey 2.10) to another ("myWebapp") by using RequestDispatcher.forward(request, response). Both webapps reside in /tomcat/webapps. In the context.xml I set <Context crossContext="true">.

@GET
@Path("")
public Response getTest(
        @Context ServletContext context,
        @Context HttpServletRequest request,
        @Context HttpServletResponse response)
        throws Exception {

        ServletContext fCtx = context.getContext("/myWebapp");
        System.out.println("ctx: "+context);
        System.out.println("forward-ctx: "+fCtx);

        RequestDispatcher dispatcher=fCtx.getRequestDispatcher("/test");
        System.out.println("rd: "+dispatcher);      

        System.out.println("request: "+request);
        System.out.println("response: "+response);

        dispatcher.forward(request, response); <- throwing ClassCastException

}

This shows the following output:

ctx: org.apache.catalina.core.ApplicationContextFacade@48a7c880
forward-ctx: org.apache.catalina.core.ApplicationContextFacade@72d0abcb
rd: org.apache.catalina.core.ApplicationDispatcher@7f5c70c3
request: org.apache.catalina.connector.RequestFacade@3829f752
response: org.apache.catalina.connector.ResponseFacade@4b2c454c

So everything should be set. But when I execute it, I get the following exception on forwarding:

java.lang.ClassCastException: org.glassfish.jersey.message.internal.TracingLogger$1 cannot be cast to org.glassfish.jersey.message.internal.TracingLogger
    at org.glassfish.jersey.message.internal.TracingLogger.getInstance(TracingLogger.java:144)
    at org.glassfish.jersey.server.internal.routing.UriRoutingContext.<init>(UriRoutingContext.java:115)
    at org.glassfish.jersey.server.ContainerRequest.<init>(ContainerRequest.java:179)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:345)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:690)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:477)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)

I am not sure, if this is not compatible with Jersey? I tried up to current version of Jersey 2.17. Is this maybe a Jersey thing which has nothing to do with the actual forwarding and how can I work around it...?

UPDATE: Interestingly, the same error is thrown no matter if the request method is GET or POST. Within another app, using exactly the same setup, the forwarding works. So I assume, its not a Jersey problem per se, but depending on my setup. Still can't figure out what it might be.


Solution

  • Changed my Jersey versions to 2.5.1 in the forwarding as well as in the finally requested webapp, to be consistent. That did the trick, pretty simple actually. Too bad I didn't recognise that, as I have had problems with different jersey versions before.

    EDIT: As it turned out, this constraint is actually quite exhausting and makes it tedious on neccessary version changes of Jersey, to keep all applications consistent. On an update to 2.22.2, even with the same versions the error did occur. The better solution now was, to change the dispatcher webapp into a simple servlet, so it does not contain any Jersey dependencies and thus will not conflict with the Jersey versions of the forwarded application.