use of com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JBaseConfiguration method executeCondition.
/**
* Creates the lambda for the execute condition which will be evaluated on each request.
*
* @param rateLimit the {@link RateLimit} configuration which holds the execute condition string
* @param expressionParser is used to evaluate the execution expression
* @param beanFactory used to get full access to all java beans in the SpEl
* @return the lamdba condition which will be evaluated lazy - null if there is no condition available.
*/
public Condition executeCondition(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
if (rateLimit.getExecuteCondition() != null) {
return (request) -> {
Expression expr = expressionParser.parseExpression(rateLimit.getExecuteCondition());
Boolean value = expr.getValue(context, request, Boolean.class);
return value;
};
}
return null;
}
use of com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JBaseConfiguration method skipCondition.
/**
* Creates the lambda for the skip condition which will be evaluated on each request
*
* @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
* @param expressionParser is used to evaluate the skip expression
* @param beanFactory used to get full access to all java beans in the SpEl
* @return the lamdba condition which will be evaluated lazy - null if there is no condition available.
*/
public Condition skipCondition(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
if (rateLimit.getSkipCondition() != null) {
return (request) -> {
Expression expr = expressionParser.parseExpression(rateLimit.getSkipCondition());
Boolean value = expr.getValue(context, request, Boolean.class);
return value;
};
}
return null;
}
use of com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit 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";
}
Aggregations