Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are resteasy interceptors thread safe between preProcess and postProcess?

If I have a single class that implements both pre and post process will I be able to store things on the object between the preProcess call and postProcess call?

So really would this be legal?

@ServerInterceptor
@Provider
public class MyInterceptor implements PreProcessInterceptor, PostProcessInterceptor {

    private String url;

    @Override
    public ServerResponse preProcess(HttpRequest request, ResourceMethod resourceMethod) throws Failure, WebApplicationException {
        url = request.getUri().getRequestUri().toString();
        return null;
    }

    @Override
    public void postProcess(ServerResponse response) {
        System.out.println(url);
    }
}
like image 432
Tom Avatar asked Nov 02 '22 07:11

Tom


1 Answers

OK, I've ran experiments and the answer seems to be that in rest easy 2.01GA running in JBoss jboss-as-7.1.1.Final I get different instances for the preProcess and the postProcess.

So the answer to "would this be legal?" is NO.

So as a workaround I'm including the context of the HttpServletRequest and storing the state as a request attribute:

@ServerInterceptor
@Provider
public class MyInterceptor implements PreProcessInterceptor, PostProcessInterceptor {

    private static final String ATTRIBUTE_NAME = MyInterceptor.class.getName();

    @Context
    HttpServletRequest servletRequest;

    @Override
    public ServerResponse preProcess(HttpRequest request, ResourceMethod resourceMethod) throws Failure, WebApplicationException {
        String url = request.getUri().getRequestUri().toString();
        servletRequest.setAttribute(ATTRIBUTE_NAME, url);
        return null;
    }

    @Override
    public void postProcess(ServerResponse response) {
        String url = servletRequest.getAttribute(ATTRIBUTE_NAME);
        System.out.println(url);
    }
}

I found for my use this wasn't adequate as the postProcess isn't called when there's an error (401, 500 etc.), I ended up using a javax.servlet.Filter

like image 109
Tom Avatar answered Nov 11 '22 13:11

Tom