title: Jersey: Is it possible to override return type of Jersey filter()?
body: <p>Here's my use case:</p>
  
  <p>I have implemented a Jersey filter <code>public void filter(ContainerRequestContext requestContext){ //do stuff }</code> that will intercept all incoming requests to any Jersey endpoints I have registered. Based on some validation I do of the incoming request headers, I have been able to reject a request via the following:</p>
  
  <pre><code>Response response = Response.status(status).entity(errorMsg).build();
  requestContext.abortWith(response);
  </code></pre>
  
  <p>This is how I have been doing it and it has worked for me. However, now I need to access certain information in the requestContext within the actual requested endpoint itself. I know this can be done like so:</p>
  
  <pre><code>@Path("/path")
  @GET
  public ReturnType endpoint(@Context ContainerRequestContext requestContext,
                             @CookieParam("cookieId") String cookieValue,
                             @HeaderParam("headerId") String headerValue, etc..){
      //do stuff
  }
  </code></pre>
  
  <p>The problem is, if I do the above^ to access the request information on the endpoint, I can no longer abort the request in the filter if the filter validation fails because I get the following error:</p>
  
  <pre><code>java.lang.IllegalStateException: The request cannot be aborted as it is
  already in the response processing phase.
  </code></pre>
  
  <p>It seems that if you define any kind of parameters on your Jersey endpoints that can be accessed within the ContainerRequestContext, it will not allow you to abort the filter chain within the filter.</p>
  
  <p>One work around (that I haven't tried yet but assuming will work), would be to do this: Instead of attempting to abort the request within the filter if the filter validation fails, I can just add a custom header to the request. Then, on the endpoint itself, I can do a check to see if that header is set. If it's set, I know the filter validation failed, and then I can build a <code>Response</code> object and return that. If it's not set, I know the filter validation succeeded, and I can go ahead and process the request.</p>
  
  <p>However, to avoid having to do this check manually on every single Jersey endpoint I have (hence the point of filters), it would be nice to just stop the request at the filter and return/abort the response right there. I thought that was the whole point of filters in the first place. However, the return type of the Jersey filter method is <code>void</code>, and I can't seem to override that.</p>
  
  <p>Anyone have a solution to my dilemna?</p>