Package org.codelibs.fess.helper
Class RateLimitHelper
java.lang.Object
org.codelibs.fess.helper.RateLimitHelper
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 Summary
FieldsModifier and TypeFieldDescriptionBlocked IPs with automatic expiration.protected com.google.common.cache.Cache<String, AtomicLong> Request counters per IP address. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanallowRequest(String ip) Check if the request is allowed under rate limiting rules.voidBlock an IP address for the specified duration.voidcleanup()Clean up expired entries.intGet the number of currently blocked IPs.getClientIp(jakarta.servlet.http.HttpServletRequest request) Get the client IP address from the request, considering proxy headers.intGet the Retry-After header value in seconds.intGet the number of tracked IP counters.voidinit()Initialize caches with configuration values.booleanCheck if the IP is blocked.booleanCheck if rate limiting is enabled.protected booleanisTrustedProxy(String ip) Check if the IP is a trusted proxy.booleanisWhitelisted(String ip) Check if the IP is in the whitelist.voidUnblock an IP address.
-
Field Details
-
requestCounters
Request counters per IP address. Entries automatically expire after the configured window period. -
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
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
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
Check if the IP is in the whitelist.- Parameters:
ip- the IP address to check- Returns:
- true if whitelisted
-
isBlocked
Check if the IP is blocked.- Parameters:
ip- the IP address to check- Returns:
- true if blocked
-
allowRequest
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
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 blockdurationMs- the duration in milliseconds (ignored, uses configured value)
-
unblockIp
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.
-