Search in sources :

Example 1 with MissingKeyFilterExpressionException

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";
}
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)

Example 2 with MissingKeyFilterExpressionException

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);
}
Also used : MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) FailureAnalysis(org.springframework.boot.diagnostics.FailureAnalysis)

Aggregations

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