1

I'm currently running into an issue where my HTTP Request is being sent into the "processing phase" before the asynchronous request that deems if authentication is valid is completed.

Here is an example of the filter causing the issue:

@Provider
public class AuthenticationFilter implements ContainerRequestFilter {

    private static final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED).build();
    private static final Response INTERNAL_SERVER_ERROR = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

    @Context
    private ResourceInfo resourceInfo;

    @Override
    public void filter(final ContainerRequestContext context) throws IOException {
        System.out.println("Filter called");
        Method method = resourceInfo.getResourceMethod();

        if(method.isAnnotationPresent(NoAuthorizationRequired.class)) {
            return;
        }

        if(method.isAnnotationPresent(AuthorizationRequired.class)) {

            AuthorizationRequest request = (new AuthorizationRequest(false) {
                @Override
                public void onCompleted(ParallelResult superResult) {
                    AuthorizationResult result = (AuthorizationResult)superResult;
                    if(result.successful()) {
                        System.out.println("Authentication completed -- Process resource");
                    } else {
                        context.abortWith(ACCESS_DENIED);
                    }
                }
            });

            request.setTask(new AuthorizationTask(request));

            Worker.work(request);

        } else {
            System.err.println("[SEVERE] IMPLEMENTATION FAULT. Authorization annotation not found for method: " + method.getName());
            context.abortWith(INTERNAL_SERVER_ERROR);
        }

    }

}

How can I make it so the request will not enter "processing phase" until the asynchornous request is completed. (Polls mydatabase).

Hobbyist
  • 14,260
  • 8
  • 37
  • 89
  • [`CountDownLatch`](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html) maybe? – Paul Samsotha Apr 24 '16 at 13:12
  • @peeskillet - Anything that would cause thread blocking goes against the entire purpose of asynchronous handling. I might as-well just process the query on the main thread if I was to use CountDownLatch. NOTE: There's no logic to work around this, and if the method completes execution without an "abort" being called, the "processing phase" is entered. – Hobbyist Apr 24 '16 at 13:13
  • If you need to wait until your async processing finishes, wouldn't that pretty much be the same as synchronous, as far as the filter -> resource processing goes? If I'm understanding you correctly, isn't that what you mean. Or maybe I'm not understanding your question correctly – Paul Samsotha Apr 24 '16 at 13:15
  • @peeskillet - The difference is that I'm looking for a way to pass off the context to the processing thread, instead of waiting on the networking thread for the processing thread to finish working. This entire API is async friendly, until it comes to filters, which seem to not have any async implementations. One drawn out database call could cause many REST request timeouts. – Hobbyist Apr 24 '16 at 13:16
  • Ah I get what you're saying. – Paul Samsotha Apr 24 '16 at 13:24
  • @Hobbyist any update on this? Were you able to figure a way out for async filters? – polavishnu Oct 13 '17 at 10:29
  • +polavishnu This was so long ago, I don't remember. This may help you https://stackoverflow.com/questions/20556200/jersey-async-containerrequestfilter – Hobbyist Oct 16 '17 at 17:44

0 Answers0