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