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.model.language;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.Expression;
027import org.apache.camel.Predicate;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.util.ObjectHelper;
030
031/**
032 * For JSonPath expressions and predicates
033 *
034 * @version 
035 */
036@Metadata(label = "language", title = "JSonPath")
037@XmlRootElement(name = "jsonpath")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class JsonPathExpression extends ExpressionDefinition {
040
041    @XmlAttribute(name = "resultType")
042    private String resultTypeName;
043    @XmlTransient
044    private Class<?> resultType;
045
046    public JsonPathExpression() {
047    }
048
049    public JsonPathExpression(String expression) {
050        super(expression);
051    }
052
053    public String getResultTypeName() {
054        return resultTypeName;
055    }
056
057    /**
058     * Sets the class name of the result type (type from output)
059     */
060    public void setResultTypeName(String resultTypeName) {
061        this.resultTypeName = resultTypeName;
062    }
063
064    public Class<?> getResultType() {
065        return resultType;
066    }
067
068    /**
069     * Sets the class of the result type (type from output)
070     */
071    public void setResultType(Class<?> resultType) {
072        this.resultType = resultType;
073    }
074
075    public String getLanguage() {
076        return "jsonpath";
077    }
078
079    @Override
080    public Expression createExpression(CamelContext camelContext) {
081        if (resultType == null && resultTypeName != null) {
082            try {
083                resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName);
084            } catch (ClassNotFoundException e) {
085                throw ObjectHelper.wrapRuntimeCamelException(e);
086            }
087        }
088        return super.createExpression(camelContext);
089    }
090
091    @Override
092    protected void configureExpression(CamelContext camelContext, Expression expression) {
093        if (resultType != null) {
094            setProperty(expression, "resultType", resultType);
095        }
096        super.configureExpression(camelContext, expression);
097    }
098
099    @Override
100    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
101        if (resultType != null) {
102            setProperty(predicate, "resultType", resultType);
103        }
104        super.configurePredicate(camelContext, predicate);
105    }
106
107}