3

I have some code that will run both inside Jersey request context and outside of Jersey request context (in another thread). I have a piece of data I want to access in both cases that's optional, but I need to know if I can pull it from.

Within Jersey request context it comes from a header parameter. I'm using a org.glassfish.hk2.api.ServiceLocator to access the javax.ws.rs.container.ContainerRequestContext from which I can read the header. However, this is optional, so it may or may not be present. Outside of Jersey request context I'm currently using MDC to store and read the value, but again, it's optional. (To pass the value between the two, I'm using in temporal context.) All of this stuff is working.

I want to know where to pull the value from, request context or MDC.

I know that if I try to access ContainerRequestContext outside of Jersey's org.glassfish.jersey.process.internal.RequestScope and it's null I'm not in request scope and should use MDC. But, I get unsilenceable warnings. An IllegalStateException is thrown and logged as a warning inside the service locator (which I can't silence).

One option would be to store a non-optional sentinel value in MDC, since that's always available. When I use temporal to store the optional value, I can set a sentinel. If the sentinel is present, use MDC, if not, use request context.

I think a cleaner way that I'd expect to work is just to check if I'm inside jersey's request scope. However, I can't find a way to do this, either using RequestScope or ServiceLocator. Does anyone know how to do this?

Cameron Little
  • 3,487
  • 23
  • 35

1 Answers1

0

You could register an ApplicationEventListener that calls your code, e.g.,

public class MyAppEventListener implements ApplicationEventListener {
    @Override
    public RequestEventListener onRequest(final RequestEvent event) {
        return new MyRequestListener();
    }

    public static class MyRequestListener implements RequestEventListener {
        @Override
        public void onEvent(final RequestEvent event) {
            if (event.getType() == RESOURCE_METHOD_START) {
                MyOtherCode.youAreNowInRequestScope();
            }
        }
    }
}

Then in your service's run() method (I see you've tagged this for Dropwizard):

environment.jersey().register(MyAppEventListener.class)

Presumably you'll need to tweak this to call something non-static or maybe trigger off a different request event, but I hope it's helpful.

Matthew Read
  • 1,365
  • 1
  • 30
  • 50