use of com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JBaseConfiguration method getKeyFilter.
/**
* Creates the key filter lambda which is responsible to decide how the rate limit will be performed. The key
* is the unique identifier like an IP address or a username.
*
* @param url is used to generated a unique cache key
* @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
* @param expressionParser is used to evaluate the expression if the filter key type is EXPRESSION.
* @param beanFactory used to get full access to all java beans in the SpEl
* @return should not been null. If no filter key type is matching a plain 1 is returned so that all requests uses the same key.
*/
public KeyFilter getKeyFilter(String url, RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
switch(rateLimit.getFilterKeyType()) {
case IP:
return (request) -> url + "-" + request.getRemoteAddr();
case EXPRESSION:
String expression = rateLimit.getExpression();
if (StringUtils.isEmpty(expression)) {
throw new MissingKeyFilterExpressionException();
}
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
return (request) -> {
// TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
Expression expr = expressionParser.parseExpression(rateLimit.getExpression());
final String value = expr.getValue(context, request, String.class);
return url + "-" + value;
};
}
return (request) -> url + "-" + "1";
}
use of com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JAutoConfigFailureAnalyzer method analyze.
@Override
protected FailureAnalysis analyze(Throwable rootFailure, Bucket4jGeneralException cause) {
String descriptionMessage = cause.getMessage();
String actionMessage = cause.getMessage();
if (cause instanceof JCacheNotFoundException) {
JCacheNotFoundException ex = (JCacheNotFoundException) cause;
descriptionMessage = "The cache name name defined in the property is not configured in the caching provider";
actionMessage = "Cache name: " + ex.getCacheName() + newline + "Please configure your caching provider (ehcache, hazelcast, ...)";
}
if (cause instanceof MissingKeyFilterExpressionException) {
descriptionMessage = "You've set the 'filter-key-type' to 'expression' but didn't set the property 'expression'";
actionMessage = "Please set the property 'expression' in your configuration file with a valid expression (see Spring Expression Language)" + newline;
}
return new FailureAnalysis(descriptionMessage, actionMessage, cause);
}
Aggregations