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,json", 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    @XmlAttribute @Metadata(defaultValue = "false")
046    private Boolean suppressExceptions;
047    @XmlAttribute @Metadata(defaultValue = "true")
048    private Boolean allowSimple;
049
050    public JsonPathExpression() {
051    }
052
053    public JsonPathExpression(String expression) {
054        super(expression);
055    }
056
057    public String getResultTypeName() {
058        return resultTypeName;
059    }
060
061    /**
062     * Sets the class name of the result type (type from output)
063     */
064    public void setResultTypeName(String resultTypeName) {
065        this.resultTypeName = resultTypeName;
066    }
067
068    public Class<?> getResultType() {
069        return resultType;
070    }
071
072    /**
073     * Sets the class of the result type (type from output)
074     */
075    public void setResultType(Class<?> resultType) {
076        this.resultType = resultType;
077    }
078
079    public Boolean getSuppressExceptions() {
080        return suppressExceptions;
081    }
082
083    public Boolean getAllowSimple() {
084        return allowSimple;
085    }
086
087    /**
088     * Whether to allow in inlined simple exceptions in the json path expression
089     */
090    public void setAllowSimple(Boolean allowSimple) {
091        this.allowSimple = allowSimple;
092    }
093
094    /**
095     * Whether to suppress exceptions such as PathNotFoundException.
096     */
097    public void setSuppressExceptions(Boolean suppressExceptions) {
098        this.suppressExceptions = suppressExceptions;
099    }
100
101    public String getLanguage() {
102        return "jsonpath";
103    }
104
105    @Override
106    public Expression createExpression(CamelContext camelContext) {
107        if (resultType == null && resultTypeName != null) {
108            try {
109                resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName);
110            } catch (ClassNotFoundException e) {
111                throw ObjectHelper.wrapRuntimeCamelException(e);
112            }
113        }
114        return super.createExpression(camelContext);
115    }
116
117    @Override
118    protected void configureExpression(CamelContext camelContext, Expression expression) {
119        if (resultType != null) {
120            setProperty(expression, "resultType", resultType);
121        }
122        if (suppressExceptions != null) {
123            setProperty(expression, "suppressExceptions", suppressExceptions);
124        }
125        if (allowSimple != null) {
126            setProperty(expression, "allowSimple", allowSimple);
127        }
128        super.configureExpression(camelContext, expression);
129    }
130
131    @Override
132    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
133        if (resultType != null) {
134            setProperty(predicate, "resultType", resultType);
135        }
136        if (suppressExceptions != null) {
137            setProperty(predicate, "suppressExceptions", suppressExceptions);
138        }
139        if (allowSimple != null) {
140            setProperty(predicate, "allowSimple", allowSimple);
141        }
142        super.configurePredicate(camelContext, predicate);
143    }
144
145}