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