Class RequestLimiter<T>

  • All Implemented Interfaces:
    Limiter

    public class RequestLimiter<T>
    extends Object
    implements Limiter
    Servlet request limiter implementation. If you implement servlet filter and you want to limit some functionality in this filter. You can add this instance to your implementation and use them in filter implementation to limit a functionality. As example you can see in messagge logger
       public class LogFilter implements Filter {
          private RequestLimiter limiter = new RequestLimiter();
          public RequestLimiter limit() { return limiter; }
     
    By this you can configure your instances
          LogFilter filter = LogFilter.instance();
           filter
               .limit()
                   .path()
                       .include("/foo/**", "POST") // - allow path pattern together with POST method
                       .include("/dummy/**" //path - allow only this pattern
                           , null //method - allow all methos
                           , null //ip - allow all incoming ips
                           , null //host - allow all incoming host names
                           , MediaType.APPLICATION_JSON_VALUE //contentType - allow only json
                           , (status) -> { return status >= 400;}) //respons status check - allow bad statuses 
                       .exclude("/bar/**" //path - disallow only this pattern
                           , "PUT" //method - disallow POT 
                           , "127.0.0.1" //ip - disallow all incoming ips
                           , null //host - disallow all incoming host names
                           , null //contentType - disallow all
                           , null) //respons status check - disallow all
                       
                       //.exclude("/dummy/**", "GET")
     ;
     
    And in LogFilter implementation you can use something like
           if (limiter.allow(request)) {
               doFilterInternal(wrapRequest(request), wrapResponse(response), chain);
           } else {
               chain.doFilter(request, response);
           }
     
    Author:
    antons
    • Constructor Detail

      • RequestLimiter

        public RequestLimiter​(T parent)
    • Method Detail

      • filter

        public T filter()
      • host

        public StringConf<RequestLimiter<T>> host()
        Host name configuration for limiter. This will apply for all requests. You can also limit this only for concrete path in path() configuration.
        Returns:
      • contentType

        public StringConf<RequestLimiter<T>> contentType()
        Content type configuration for limiter. This will apply for all requests. You can also limit this only for concrete path in path() configuration.
        Returns:
      • custom

        public RequestLimiter<T> custom​(Limiter limiter)
        You can add your custom limiter implementation.
        Parameters:
        limiter -
        Returns:
      • responseStatus

        public RequestLimiter<T> responseStatus​(ResponseStatusCheck check)
        Response status configuration for limiter. This will apply for all requests. You can also limit this only for concrete path in path() configuration.
        Returns:
      • reset

        public RequestLimiter<T> reset()
        Clears all settings for this limiter.
        Returns:
      • allow

        public boolean allow​(javax.servlet.ServletRequest request)
        Description copied from interface: Limiter
        Returns true if limiter allow filter processing
        Specified by:
        allow in interface Limiter
        Parameters:
        request - request to check
        Returns:
        true is filter should be processed.
      • allowResponseStatus

        public boolean allowResponseStatus​(javax.servlet.ServletRequest request,
                                           int status)
        Description copied from interface: Limiter
        Returns true if limiter allow filter processing after cain execution. This variant can be used after chanin prpocessing when status code is known.
        Specified by:
        allowResponseStatus in interface Limiter
        Parameters:
        request - request to check
        status - response status to check
        Returns:
        true is filter should be processed.