Class RateLimitHelper

java.lang.Object
org.codelibs.fess.helper.RateLimitHelper

public class RateLimitHelper extends Object
Helper class for rate limiting functionality. Implements a sliding window algorithm for request counting and manages IP-based blocking using Guava Cache for automatic expiration.
  • Field Details

    • requestCounters

      protected com.google.common.cache.Cache<String,AtomicLong> requestCounters
      Request counters per IP address. Entries automatically expire after the configured window period.
    • blockedIps

      protected com.google.common.cache.Cache<String,Boolean> blockedIps
      Blocked IPs with automatic expiration. Entries automatically expire after the configured block duration.
  • Constructor Details

    • RateLimitHelper

      public RateLimitHelper()
      Default constructor.
  • Method Details

    • init

      @PostConstruct public void init()
      Initialize caches with configuration values.
    • isEnabled

      public boolean isEnabled()
      Check if rate limiting is enabled.
      Returns:
      true if rate limiting is enabled
    • getClientIp

      public String getClientIp(jakarta.servlet.http.HttpServletRequest request)
      Get the client IP address from the request, considering proxy headers. Only trusts X-Forwarded-For/X-Real-IP headers when the request comes from a trusted proxy.
      Parameters:
      request - the HTTP request
      Returns:
      the client IP address
    • isTrustedProxy

      protected boolean isTrustedProxy(String ip)
      Check if the IP is a trusted proxy.
      Parameters:
      ip - the IP address to check
      Returns:
      true if the IP is a trusted proxy
    • isWhitelisted

      public boolean isWhitelisted(String ip)
      Check if the IP is in the whitelist.
      Parameters:
      ip - the IP address to check
      Returns:
      true if whitelisted
    • isBlocked

      public boolean isBlocked(String ip)
      Check if the IP is blocked.
      Parameters:
      ip - the IP address to check
      Returns:
      true if blocked
    • allowRequest

      public boolean allowRequest(String ip)
      Check if the request is allowed under rate limiting rules.
      Parameters:
      ip - the client IP address
      Returns:
      true if the request is allowed
    • getRetryAfterSeconds

      public int getRetryAfterSeconds()
      Get the Retry-After header value in seconds.
      Returns:
      the retry after seconds
    • blockIp

      public void blockIp(String ip, long durationMs)
      Block an IP address for the specified duration. Note: The duration is ignored as the cache uses the configured block duration.
      Parameters:
      ip - the IP address to block
      durationMs - the duration in milliseconds (ignored, uses configured value)
    • unblockIp

      public void unblockIp(String ip)
      Unblock an IP address.
      Parameters:
      ip - the IP address to unblock
    • getBlockedIpCount

      public int getBlockedIpCount()
      Get the number of currently blocked IPs. Note: This may include expired entries not yet evicted.
      Returns:
      the count of blocked IPs
    • getTrackedIpCount

      public int getTrackedIpCount()
      Get the number of tracked IP counters. Note: This may include expired entries not yet evicted.
      Returns:
      the count of tracked IPs
    • cleanup

      public void cleanup()
      Clean up expired entries. Note: Guava Cache handles expiration automatically, but this method can be called to force immediate cleanup.