简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM