title: Java Jersey 2 implementing filters
body: <p>I want to include a filter so i can check basic auth headers for each request</p>
  
  <pre><code>import java.io.IOException;
  import javax.ws.rs.client.ClientRequestContext;
  import javax.ws.rs.client.ClientRequestFilter;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.ext.Provider;
  import javax.ws.rs.container.PreMatching;
  
  @Provider
  @PreMatching 
  public class CheckRequestFilter implements ClientRequestFilter {
  
      @Override
      public void filter(ClientRequestContext requestContext) throws IOException {
  
          ServiceProvider sp = new ServiceProvider();
  
  
          /*if (sp.authenticateSP(requestContext.getHeaderString("authorization"))     == false){ */
              requestContext.abortWith(
                          Response.status(Response.Status.BAD_REQUEST)
                  .entity("User cannot be authenticated")
                          .build());
  
  }}
  //}
  </code></pre>
  
  <p>So far that's what i've come up with following the documentation here <a href="https://jersey.java.net/documentation/latest/user-guide.html#filters-and-interceptors" rel="nofollow">https://jersey.java.net/documentation/latest/user-guide.html#filters-and-interceptors</a></p>
  
  <p>My problems is that i don't know how to include a filter so i can test it (i.e copy pasting the first example is not working) and if my approach is correct.</p>
  
  <p>I want to limit it for certain requests in the future but for now i just want to to work.</p>