title: Cq5.5 applying a servlet filter to a specific path
body: <p>I'm working on a custom form handler in cq5.5 and everything is going great. I'm now working on locking down some of the security and one of my tasks is to implement a request throttling filter to the form handlers path.</p>
  
  <p>Currently I have something like</p>
  
  <pre><code>@Component(immediate = true, metatype = true)
  
  @Service(javax.servlet.Filter.class)
  
  @Properties({
    @Property(name="service.pid", value="com.xxxxxx.cq.core.filter.FormFilter",propertyPrivate=false),
    @Property(name="service.description",value="FormFilter", propertyPrivate=false),
    @Property(name="service.vendor",value="xxxxxx - Microsites", propertyPrivate=false),
    @Property(name = "filter.scope", value = "request"),
    @Property(name = "sling.filter.scope", value = "request"),
    @Property(name = "service.ranking", intValue = 100001)
  })
  
  public class FormFilter implements javax.servlet.Filter {
    private Logger LOGGER = LoggerFactory.getLogger(TrackingFilter.class.getName());
    private static final Object lock = new Object();
  
    @Override
    public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) throws IOException, ServletException {
        //my filter stuff
    }
  }
  </code></pre>
  
  <p>This works fine but I'd like to lock it down to only run at a specific path.</p>
  
  <p>thanks for any insights.</p>
  
  <p>----EDIT-----
  After doing more research I found a few posts stating that there is no way to register a filter to a specified path for the default ServletFilter handler. Basically the two solutions to this issue I've found were either create a new OSGI bundle for the filter and register it using the ExtHTTPService or Whiteboard:</p>
  
  <p><a href="http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html" rel="noreferrer">http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html</a></p>
  
  <p>OR</p>
  
  <p>Filter out the url within the filter itself. So basically add a check for the specified path in my filter.</p>
  
  <p>i.e:</p>
  
  <pre><code>  @Override
    public void doFilter(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) throws IOException, ServletException {
  
        String path = pRequest.getContextPath();
  
        if (path.contains("my/matching/path")
        {
          //my filter stuff
        }
    }
  </code></pre>
  
  <p>I would love to see if there are additional solutions to this issue, but wanted to share what I've been able to find so far, in hopes that this will either help spur more ideas or even just help someone with the same issue save some time on google searching.</p>
  
  <p>thank you,
  Brodie</p>