Search in sources :

Example 1 with Condition

use of com.giffing.bucket4j.spring.boot.starter.context.Condition 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;
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression)

Example 2 with Condition

use of com.giffing.bucket4j.spring.boot.starter.context.Condition 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;
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression)

Example 3 with Condition

use of com.giffing.bucket4j.spring.boot.starter.context.Condition 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";
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Expression(org.springframework.expression.Expression)

Aggregations

Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)3 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)3 BandWidthConfig (com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig)3 Condition (com.giffing.bucket4j.spring.boot.starter.context.Condition)3 FilterConfiguration (com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration)3 KeyFilter (com.giffing.bucket4j.spring.boot.starter.context.KeyFilter)3 RateLimitCheck (com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck)3 Bucket4JConfiguration (com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration)3 RateLimit (com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit)3 JCacheNotFoundException (com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException)3 MissingKeyFilterExpressionException (com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException)3 Bandwidth (io.github.bucket4j.Bandwidth)3 Bucket (io.github.bucket4j.Bucket)3 Bucket4j (io.github.bucket4j.Bucket4j)3 ConfigurationBuilder (io.github.bucket4j.ConfigurationBuilder)3 ConsumptionProbe (io.github.bucket4j.ConsumptionProbe)3 GridBucketState (io.github.bucket4j.grid.GridBucketState)3 ProxyManager (io.github.bucket4j.grid.ProxyManager)3 JCache (io.github.bucket4j.grid.jcache.JCache)3 Duration (java.time.Duration)3