users@jersey.java.net

[Jersey] ManagedAsync and ContainerRequestFilter

From: Alden Quimby <aldenquimby_at_gmail.com>
Date: Sun, 15 Dec 2013 10:22:05 -0500

I am using a ContainerRequestFilter to handle authorization, and
ManagedAsync on all endpoints for scaling. My authorization filter hits a
remote service, but when the filter is run, Jersey hasn't yet added the
current thread to it's internal ExecutorService, so I'm losing the benefit
of ManagedAsync.

How can I set up my ContainerRequestFilter to be asynchronous, and run
AFTER ManagedAsync is recognized?


Here is my filter:

@Priority(Priorities.AUTHORIZATION)public class AuthorizationFilter
implements ContainerRequestFilter{
    @Inject
    private AuthorizationService authSvc;

    @Override
    public void filter(ContainerRequestContext requestContext) throws
IOException
    {
        String authToken =
requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);

        // HITS A REMOTE SERVER
        AuthorizationResponse authResponse = authSvc.authorize(authToken);

        if (!authResponse.isAuthorized())
        {
            requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED)
                    .entity("unauthorized!")
                    .build());
        }
    }}




 And here's a resource:

@Path("/stuff")@Produces(MediaType.APPLICATION_JSON)public class StuffResource{
    @GET
    @Path("/{id}")
    @ManagedAsync
    public void getById(@PathParam("id") long id, @Suspended final
AsyncResponse ar)
    {
        Stuff s;

        // HIT THE DATABASE FOR STUFF

        ar.resume(s);
    }}