title: Dropwizard Request Filters With URI Pattern
body: <p>I am building a RESTful application in Dropwizard. While connecting to a database, I want to set up a <code>UserNotFoundFilter</code> that implements <code>ContainerRequestFilter</code> so that incoming requests go through this filter first.</p>
  
  <p>The idea is that I would like to have this specific filter only mapped to certain URI patterns. For instance, I want the filter to only apply to <code>/users/*</code> and not anything else. Is there a way to do this without resorting to custom annotations and implementation with <code>DynamicFeature</code> ?</p>
  
  <pre><code>@Provider
  public class UserNotFoundFilter implements ContainerRequestFilter {
      @Context
      UriInfo uriInfo;
  
      @Override
      public void filter(ContainerRequestContext requestContext) throws IOException {
          MultivaluedMap pathParams = uriInfo.getPathParameters(); // Should contain (uid: 1) pair for /users/1
          boolean userExists = // check against the database using above pathparam pair to see if user exists
          if (!userExists) 
              throw new WebApplicationException("User does not exist", Status.NOT_FOUND);
          // let the request through as user exists
      }
  }
  </code></pre>
  
  <p>My UserResource class</p>
  
  <pre><code>public class UserResource {
      @GET
      @Path("/users/{uid}")
      @Produces(MediaType.APPLICATION_JSON)
      public User getUser(@PathParam("uid") String uid) {
          // Now I don't need to do the check here !
          // boolean userExists = check against database using uid path param
          // if (!userExists)
          //     throw new WebApplicationException("User does not exist", Status.NOT_FOUND);
          return database.getUser(uid);
      }
  }
  </code></pre>
  
  <p>My ItemResource class</p>
  
  <pre><code>public class ItemResource {
      @GET
      @Path("/items/{uid}")
      @Produces(MediaType.APPLICATION_JSON)
      public Item getItem(@PathParam("uid") String uid) {
          return database.getItem(uid);
      }
  }
  </code></pre>
  
  <p>What I'm trying to do</p>
  
  <pre><code>public class MyApplication extends Application&lt;MyConfiguration&gt; {
      // ...
      @Override
      public void run(MyConfiguration config, Environment environment) throws Exception {
          // ... do other things, register resources
          // this pseudocode, the UserNotFoundFilter only applies to URIs of the kind /users/*
          environment.jersey().register(new UserNotFoundFilter()).forUriPattern("/users/*");
  </code></pre>
  
  <p>I appreciate any example code snippets.</p>