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