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