/** * 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"; }
/** * Create an {@link EvaluationContext}. * @param caches the current caches * @param method the method * @param args the method arguments * @param target the target object * @param targetClass the target class * @param result the return value (can be {@code null}) or * {@link #NO_RESULT} if there is no return at this time * @return the evaluation context */ public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches, Method method, Object[] args, Object target, Class<?> targetClass, Object result, BeanFactory beanFactory) { CacheExpressionRootObject rootObject = new CacheExpressionRootObject( caches, method, args, target, targetClass); Method targetMethod = getTargetMethod(targetClass, method); CacheEvaluationContext evaluationContext = new CacheEvaluationContext( rootObject, targetMethod, args, getParameterNameDiscoverer()); if (result == RESULT_UNAVAILABLE) { evaluationContext.addUnavailableVariable(RESULT_VARIABLE); } else if (result != NO_RESULT) { evaluationContext.setVariable(RESULT_VARIABLE, result); } if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return evaluationContext; }
private ConversionService conversionService(ApplicationContext applicationContext) { // TODO should be also able to resolve a url to a bean/method BeanFactoryResolver beanResolver = new BeanFactoryResolver(applicationContext) { @Override public Object resolve(EvaluationContext context, String beanName) throws AccessException { return super.resolve(context, (beanName.startsWith("bean:") ? beanName.substring("bean:".length()) : beanName)); } }; ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean(); Set<Converter<?, ?>> cons = new HashSet<Converter<?, ?>>(); cons.add(new DtoCheckConverter(beanResolver)); cons.add(new DtoOperationConverter(beanResolver)); factoryBean.setConverters(cons); factoryBean.afterPropertiesSet(); return factoryBean.getObject(); }
/** * 创建SpEL执行上下文 * * @param rootObject * SpEL表达式根对象 * @param context * Beetl上下文对象 * @return SpEL表达式执行上下文 */ private EvaluationContext createEvaluationContext(Object rootObject, Context beetlContext) { StandardEvaluationContext context = new StandardEvaluationContext(rootObject); // 允许使用#context访问Beetl上下文 context.setVariable("context", beetlContext); // 允许使用#global访问Beetl上下文的全局变量 context.setVariable("global", beetlContext.globalVar); // 注册WebRender定义的全局变量 context.setVariable("ctxPath", beetlContext.getGlobal("ctxPath")); context.setVariable("servlet", beetlContext.getGlobal("servlet")); context.setVariable("parameter", beetlContext.getGlobal("parameter")); context.setVariable("request", beetlContext.getGlobal("request")); context.setVariable("session", beetlContext.getGlobal("session")); // 允许使用属性格式访问Map context.addPropertyAccessor(new MapAccessor()); // 允许访问Spring容器Bean context.setBeanResolver(new BeanFactoryResolver(applicationContext)); return context; }
/** * 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; }
/** * 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; }
public SpringDataFetcher setApplicationContext(ApplicationContext context) { this.context = context; // setup the bean factory resolver beanFactoryResolver = new BeanFactoryResolver(context); return this; }
private EvaluationContext createEvaluationContext(PageContext pageContext) { StandardEvaluationContext context = new StandardEvaluationContext(); context.addPropertyAccessor(new JspPropertyAccessor(pageContext)); context.addPropertyAccessor(new MapAccessor()); context.addPropertyAccessor(new EnvironmentAccessor()); context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext())); ConversionService conversionService = getConversionService(pageContext); if (conversionService != null) { context.setTypeConverter(new StandardTypeConverter(conversionService)); } return context; }
/** * Create the suitable {@link EvaluationContext} for the specified event handling * on the specified method. */ public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass, Method method, Object[] args, BeanFactory beanFactory) { Method targetMethod = getTargetMethod(targetClass, method); EventExpressionRootObject root = new EventExpressionRootObject(event, args); MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext( root, targetMethod, args, getParameterNameDiscoverer()); if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return evaluationContext; }
private int getBrokerCount() { LocalEntityManagerFactoryBean entityManagerFactory = applicationContext.getBean("&entityManagerFactory", LocalEntityManagerFactoryBean.class); //uses Spring EL so we don't need to reference the classes StandardEvaluationContext context = new StandardEvaluationContext(entityManagerFactory); context.setBeanResolver(new BeanFactoryResolver(applicationContext)); SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("nativeEntityManagerFactory.brokerFactory.openBrokers"); List<?> brokers = expression.getValue(context, List.class); return brokers.size(); }
private EvaluationContext createEvaluationContext(Exchange exchange) { StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange)); if (exchange.getContext() instanceof SpringCamelContext) { // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext(); evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext)); } return evaluationContext; }
/** * JAVADOC Method Level Comments * * @throws Exception JAVADOC. */ @Override public void afterPropertiesSet() throws Exception { if (!resolvers.containsKey("bean")) { resolvers.put("bean", new BeanFactoryResolver(beanFactory)); } if (!resolvers.containsKey("class")) { resolvers.put("class", new ClassResolver()); } }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context.addPropertyAccessor(new BeanFactoryAccessor()); context.setBeanResolver(new BeanFactoryResolver(applicationContext)); context.setRootObject(applicationContext); }
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context.addPropertyAccessor(new BeanFactoryAccessor()); context.setBeanResolver(new BeanFactoryResolver(applicationContext)); context.setRootObject(applicationContext); }
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactoryResolver = new BeanFactoryResolver(beanFactory); }
/** * Helper method for creating an {@code ExpressionSelector}. * * @param expr * The expression to parse. * @param beanFactory * The {@link org.springframework.beans.factory.BeanFactory} to use to resolve references in the expression. * * @return A new {@link reactor.bus.selector.Selector} */ public static Selector expressionSelector(String expr, BeanFactory beanFactory) { StandardEvaluationContext evalCtx = new StandardEvaluationContext(); if(null != beanFactory) { evalCtx.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return expressionSelector(expr, evalCtx); }