001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.language.spel;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.ExpressionEvaluationException;
021import org.apache.camel.spring.SpringCamelContext;
022import org.apache.camel.spring.util.RegistryBeanResolver;
023import org.springframework.context.ApplicationContext;
024import org.springframework.context.expression.BeanFactoryResolver;
025import org.springframework.expression.BeanResolver;
026import org.springframework.expression.EvaluationContext;
027import org.springframework.expression.Expression;
028import org.springframework.expression.ParserContext;
029import org.springframework.expression.common.TemplateParserContext;
030import org.springframework.expression.spel.standard.SpelExpressionParser;
031import org.springframework.expression.spel.support.StandardEvaluationContext;
032
033/**
034 * Class responsible for evaluating <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions">
035 * Spring Expression Language (SpEL)</a> in the context of Camel.
036 */
037public class SpelExpression extends org.apache.camel.support.ExpressionSupport {
038
039    private final String expressionString;
040    private final Class<?> type;
041    private final BeanResolver beanResolver;
042
043    // SpelExpressionParser is thread-safe according to the docs
044    private final SpelExpressionParser expressionParser;
045
046    public SpelExpression(String expressionString, Class<?> type) {
047        this(expressionString, type, null);
048    }
049
050    public SpelExpression(String expressionString, Class<?> type, BeanResolver beanResolver) {
051        this.expressionString = expressionString;
052        this.type = type;
053        this.beanResolver = beanResolver;
054        this.expressionParser = new SpelExpressionParser();
055    }
056
057    public static SpelExpression spel(String expression) {
058        return new SpelExpression(expression, Object.class);
059    }
060
061    public <T> T evaluate(Exchange exchange, Class<T> tClass) {
062        try {
063            Expression expression = parseExpression();
064            EvaluationContext evaluationContext = createEvaluationContext(exchange);
065            Object value = expression.getValue(evaluationContext);
066            // Let Camel handle the type conversion
067            return exchange.getContext().getTypeConverter().convertTo(tClass, value);
068        } catch (Exception e) {
069            throw new ExpressionEvaluationException(this, exchange, e);
070        }
071    }
072
073    private EvaluationContext createEvaluationContext(Exchange exchange) {
074        StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
075        if (beanResolver != null) {
076            evaluationContext.setBeanResolver(beanResolver);
077        } else if (exchange.getContext() instanceof SpringCamelContext) {
078            // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
079            ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext();
080            evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
081        } else {
082            evaluationContext.setBeanResolver(new RegistryBeanResolver(exchange.getContext().getRegistry()));
083        }
084        return evaluationContext;
085    }
086
087    private Expression parseExpression() {
088        // Support template parsing with #{ } delimiters
089        ParserContext parserContext = new TemplateParserContext();
090        Expression expression = expressionParser.parseExpression(expressionString, parserContext);
091        return expression;
092    }
093
094    public Class<?> getType() {
095        return type;
096    }
097
098    protected String assertionFailureMessage(Exchange exchange) {
099        return expressionString;
100    }
101
102    @Override
103    public String toString() {
104        return "SpelExpression[" + expressionString + "]";
105    }
106}