title: Is there an Interceptor in Jersey similar to that of Spring's HandlerInterceptor
body: <p>I need an Interceptor in Jersey 2.x which gives references to request, response and the Method which matched the Path to a Web Service.</p>
  
  <p>Something similar to <strong>HandlerInterceptor</strong> of Spring.</p>
  
  <p>Requirements:</p>
  
  <ol>
  <li><strong>Need Annotations used on Class</strong> - Perform the below checks, Only if the corresponding Method which needs to be invoked by jersey is NOT Annotated with a custom annotation.</li>
  <li><strong>Request</strong> - To get/set attributes and get Session Object to validate user.</li>
  <li><strong>Response</strong> - To re-direct the call in case any validation fails even before invoking the corresponding Methos.</li>
  </ol>
  
  <p>Spring equivalent code:</p>
  
  <pre><code>public class WebServiceInterceptor implements HandlerInterceptor {
       @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  
          try {
              SkipWebServiceIntercept skipWebService = handler.getClass().getAnnotation(SkipWebServiceIntercept.class);
  
              if (skipWebService != null) {
                  return Boolean.TRUE;
              }
  
              ...
  
              if(securityFails)
              {
                  if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
                      response.setCharacterEncoding("utf-8");
                      response.setContentType("application/json");
                      PrintWriter out = response.getWriter();
                      String json;
                      ...
                      out.println(json);
                      return Boolean.FALSE;
                  }
                  else {
                      response.sendRedirect(redirectUrl);
                  }
              }
              else
              {
                  return Boolean.TRUE;
              }
          }catch (Exception e) {
              log.error("Exception in preHandle, redirecting to Login page", e);
              return LoginUtil.redirectToLogin(request, response);
          }
      }
  }
  </code></pre>
  
  <p>I found references of</p>
  
  <ol>
  <li><strong>ReaderInterceptor</strong> - This provides just Annotations of the Class. No access to request/response.</li>
  <li><strong>ContainerRequestFilter</strong> - Provides Request Object but no Annotations/Response.</li>
  <li><strong>ContainerResponseFilter</strong> - Gives Request &amp; Response. But after the web-service/Method is invoked.</li>
  </ol>
  
  <p>Is there anyother way this can be achieved without using Filters. because I need this processing to happen ONLY if the corresponding web-service is present.
  Filter's with /* on the other hand would always perform these validations even when the resource was not found.</p>
  
  <p><strong>Edit:</strong>
  Thanks to @peeskillet <a href="https://stackoverflow.com/a/35736830/1925997">answer</a> This is how I implemented it.</p>
  
  <pre><code>@Provider
  public class ResourceInterceptor implements DynamicFeature {
  
      @Override
      public void configure(ResourceInfo resourceInfo, FeatureContext context) {
  
          System.out.println("Resource Interceptor called");
  
          if (resourceInfo.getResourceClass() != null
                  &amp;&amp; resourceInfo.getResourceClass().getAnnotation(SkipWebServiceIntercept.class) != null)
              return;
  
          context.register(LoginFilter.class);
      }
  
  }
  
  
  @Slf4j
  public class LoginFilter implements ContainerRequestFilter {
  
      @Context
      private HttpServletRequest      request;
  
      @Context
      private ServletContext          servletContext;
  
      @Override
      public void filter(ContainerRequestContext requestContext) throws IOException {
          try {
  
  
  WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  
  CookieAuthenticator cookieAuthenticator = springContext.getBean(CookieAuthenticator.class);
  
  HttpSession session = request.getSession(true);
  
  ...
  </code></pre>
  
  <blockquote>
    <p>// Sending JSON/Object back as response</p>
  </blockquote>
  
  <pre><code>...
  String json = gson.toJson(resposeJson);
  
  Response response = new ResponseBuilderImpl().encoding(StandardCharsets.UTF_8.name())
                          .type(MediaType.APPLICATION_JSON).entity(json).build();
  requestContext.abortWith(response);
  ...
  </code></pre>
  
  <blockquote>
    <p>// or Sending URL back</p>
  </blockquote>
  
  <pre><code>...
  URI uri = new URI(baseUrl + redirectUrl + "?refback=" + url);
  requestContext.abortWith(Response.temporaryRedirect(uri).build());
  ...
  </code></pre>
  
  <p>Both the approaches worked perfectly, similar to HandlerInterceptor of Spring.</p>