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.impl.ExpressionSupport;
022import org.apache.camel.spring.SpringCamelContext;
023import org.apache.camel.spring.util.RegistryBeanResolver;
024import org.springframework.context.ApplicationContext;
025import org.springframework.context.expression.BeanFactoryResolver;
026import org.springframework.context.expression.MapAccessor;
027import org.springframework.expression.BeanResolver;
028import org.springframework.expression.EvaluationContext;
029import org.springframework.expression.Expression;
030import org.springframework.expression.ParserContext;
031import org.springframework.expression.common.TemplateParserContext;
032import org.springframework.expression.spel.standard.SpelExpressionParser;
033import org.springframework.expression.spel.support.StandardEvaluationContext;
034
035/**
036 * Class responsible for evaluating <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions">
037 * Spring Expression Language (SpEL)</a> in the context of Camel.
038 */
039@SuppressWarnings("deprecation")
040public class SpelExpression extends ExpressionSupport {
041
042    private final String expressionString;
043    private final Class<?> type;
044    private final BeanResolver beanResolver;
045
046    // SpelExpressionParser is thread-safe according to the docs
047    private final SpelExpressionParser expressionParser;
048
049    public SpelExpression(String expressionString, Class<?> type) {
050        this(expressionString, type, null);
051    }
052
053    public SpelExpression(String expressionString, Class<?> type, BeanResolver beanResolver) {
054        this.expressionString = expressionString;
055        this.type = type;
056        this.beanResolver = beanResolver;
057        this.expressionParser = new SpelExpressionParser();
058    }
059
060    public static SpelExpression spel(String expression) {
061        return new SpelExpression(expression, Object.class);
062    }
063
064    public <T> T evaluate(Exchange exchange, Class<T> tClass) {
065        try {
066            Expression expression = parseExpression();
067            EvaluationContext evaluationContext = createEvaluationContext(exchange);
068            Object value = expression.getValue(evaluationContext);
069            // Let Camel handle the type conversion
070            return exchange.getContext().getTypeConverter().convertTo(tClass, value);
071        } catch (Exception e) {
072            throw new ExpressionEvaluationException(this, exchange, e);
073        }
074    }
075
076    private EvaluationContext createEvaluationContext(Exchange exchange) {
077        StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new RootObject(exchange));
078        evaluationContext.addPropertyAccessor(new MapAccessor());
079        if (beanResolver != null) {
080            evaluationContext.setBeanResolver(beanResolver);
081        } else if (exchange.getContext() instanceof SpringCamelContext) {
082            // Support references (like @foo) in expressions to beans defined in the Registry/ApplicationContext
083            ApplicationContext applicationContext = ((SpringCamelContext) exchange.getContext()).getApplicationContext();
084            evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
085        } else {
086            evaluationContext.setBeanResolver(new RegistryBeanResolver(exchange.getContext().getRegistry()));
087        }
088        return evaluationContext;
089    }
090
091    private Expression parseExpression() {
092        // Support template parsing with #{ } delimiters
093        ParserContext parserContext = new TemplateParserContext();
094        Expression expression = expressionParser.parseExpression(expressionString, parserContext);
095        return expression;
096    }
097
098    public Class<?> getType() {
099        return type;
100    }
101
102    protected String assertionFailureMessage(Exchange exchange) {
103        return expressionString;
104    }
105
106    @Override
107    public String toString() {
108        return "SpelExpression[" + expressionString + "]";
109    }
110}