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.RuntimeCamelException;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * To use JsonPath in Camel expressions or predicates.
033 */
034@Metadata(firstVersion = "2.13.0", label = "language,json", title = "JsonPath")
035@XmlRootElement(name = "jsonpath")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class JsonPathExpression extends ExpressionDefinition {
038
039    @XmlAttribute(name = "resultType")
040    private String resultTypeName;
041    @XmlTransient
042    private Class<?> resultType;
043    @XmlAttribute
044    @Metadata(defaultValue = "false")
045    private Boolean suppressExceptions;
046    @XmlAttribute
047    @Metadata(defaultValue = "true")
048    private Boolean allowSimple;
049    @XmlAttribute
050    @Metadata(defaultValue = "true")
051    private Boolean allowEasyPredicate;
052    @XmlAttribute
053    @Metadata(defaultValue = "false")
054    private Boolean writeAsString;
055    @XmlAttribute
056    private String headerName;
057
058    public JsonPathExpression() {
059    }
060
061    public JsonPathExpression(String expression) {
062        super(expression);
063    }
064
065    public String getResultTypeName() {
066        return resultTypeName;
067    }
068
069    /**
070     * Sets the class name of the result type (type from output)
071     */
072    public void setResultTypeName(String resultTypeName) {
073        this.resultTypeName = resultTypeName;
074    }
075
076    public Class<?> getResultType() {
077        return resultType;
078    }
079
080    /**
081     * Sets the class of the result type (type from output)
082     */
083    public void setResultType(Class<?> resultType) {
084        this.resultType = resultType;
085    }
086
087    public Boolean getSuppressExceptions() {
088        return suppressExceptions;
089    }
090
091    public Boolean getAllowSimple() {
092        return allowSimple;
093    }
094
095    /**
096     * Whether to allow in inlined simple exceptions in the JsonPath expression
097     */
098    public void setAllowSimple(Boolean allowSimple) {
099        this.allowSimple = allowSimple;
100    }
101
102    public Boolean getAllowEasyPredicate() {
103        return allowEasyPredicate;
104    }
105
106    /**
107     * Whether to allow using the easy predicate parser to pre-parse predicates.
108     */
109    public void setAllowEasyPredicate(Boolean allowEasyPredicate) {
110        this.allowEasyPredicate = allowEasyPredicate;
111    }
112
113    /**
114     * Whether to suppress exceptions such as PathNotFoundException.
115     */
116    public void setSuppressExceptions(Boolean suppressExceptions) {
117        this.suppressExceptions = suppressExceptions;
118    }
119
120    public Boolean getWriteAsString() {
121        return writeAsString;
122    }
123
124    /**
125     * Whether to write the output of each row/element as a JSON String value
126     * instead of a Map/POJO value.
127     */
128    public void setWriteAsString(Boolean writeAsString) {
129        this.writeAsString = writeAsString;
130    }
131
132    public String getHeaderName() {
133        return headerName;
134    }
135
136    /**
137     * Name of header to use as input, instead of the message body
138     */
139    public void setHeaderName(String headerName) {
140        this.headerName = headerName;
141    }
142
143    @Override
144    public String getLanguage() {
145        return "jsonpath";
146    }
147
148    @Override
149    public Expression createExpression(CamelContext camelContext) {
150        if (resultType == null && resultTypeName != null) {
151            try {
152                resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName);
153            } catch (ClassNotFoundException e) {
154                throw RuntimeCamelException.wrapRuntimeCamelException(e);
155            }
156        }
157        return super.createExpression(camelContext);
158    }
159
160    @Override
161    protected void configureExpression(CamelContext camelContext, Expression expression) {
162        if (resultType != null) {
163            setProperty(camelContext, expression, "resultType", resultType);
164        }
165        if (suppressExceptions != null) {
166            setProperty(camelContext, expression, "suppressExceptions", suppressExceptions);
167        }
168        if (allowSimple != null) {
169            setProperty(camelContext, expression, "allowSimple", allowSimple);
170        }
171        if (allowEasyPredicate != null) {
172            setProperty(camelContext, expression, "allowEasyPredicate", allowEasyPredicate);
173        }
174        if (writeAsString != null) {
175            setProperty(camelContext, expression, "writeAsString", writeAsString);
176        }
177        if (headerName != null) {
178            setProperty(camelContext, expression, "headerName", headerName);
179        }
180        super.configureExpression(camelContext, expression);
181    }
182
183    @Override
184    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
185        if (resultType != null) {
186            setProperty(camelContext, predicate, "resultType", resultType);
187        }
188        if (suppressExceptions != null) {
189            setProperty(camelContext, predicate, "suppressExceptions", suppressExceptions);
190        }
191        if (allowSimple != null) {
192            setProperty(camelContext, predicate, "allowSimple", allowSimple);
193        }
194        if (allowEasyPredicate != null) {
195            setProperty(camelContext, predicate, "allowEasyPredicate", allowEasyPredicate);
196        }
197        if (writeAsString != null) {
198            setProperty(camelContext, predicate, "writeAsString", writeAsString);
199        }
200        if (headerName != null) {
201            setProperty(camelContext, predicate, "headerName", headerName);
202        }
203        super.configurePredicate(camelContext, predicate);
204    }
205
206}