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; 018 019import java.util.function.Supplier; 020import javax.xml.bind.annotation.XmlAccessType; 021import javax.xml.bind.annotation.XmlAccessorType; 022import javax.xml.bind.annotation.XmlAttribute; 023import javax.xml.bind.annotation.XmlRootElement; 024import javax.xml.bind.annotation.XmlTransient; 025 026import org.apache.camel.AggregationStrategy; 027import org.apache.camel.model.language.ExpressionDefinition; 028import org.apache.camel.spi.Metadata; 029 030/** 031 * Enriches a message with data from a secondary resource 032 * 033 * @see org.apache.camel.processor.Enricher 034 */ 035@Metadata(label = "eip,transformation") 036@XmlRootElement(name = "enrich") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class EnrichDefinition extends ExpressionNode { 039 @XmlAttribute(name = "strategyRef") 040 private String aggregationStrategyRef; 041 @XmlAttribute(name = "strategyMethodName") 042 private String aggregationStrategyMethodName; 043 @XmlAttribute(name = "strategyMethodAllowNull") 044 private Boolean aggregationStrategyMethodAllowNull; 045 @XmlAttribute 046 private Boolean aggregateOnException; 047 @XmlTransient 048 private AggregationStrategy aggregationStrategy; 049 @XmlAttribute 050 private Boolean shareUnitOfWork; 051 @XmlAttribute 052 private Integer cacheSize; 053 @XmlAttribute 054 private Boolean ignoreInvalidEndpoint; 055 056 public EnrichDefinition() { 057 this(null); 058 } 059 060 public EnrichDefinition(AggregationStrategy aggregationStrategy) { 061 this.aggregationStrategy = aggregationStrategy; 062 } 063 064 @Override 065 public String toString() { 066 return "Enrich[" + getExpression() + "]"; 067 } 068 069 @Override 070 public String getShortName() { 071 return "enrich"; 072 } 073 074 @Override 075 public String getLabel() { 076 return "enrich[" + getExpression() + "]"; 077 } 078 079 // Fluent API 080 // ------------------------------------------------------------------------- 081 082 /** 083 * Sets the AggregationStrategy to be used to merge the reply from the 084 * external service, into a single outgoing message. By default Camel will 085 * use the reply from the external service as outgoing message. 086 */ 087 public EnrichDefinition aggregationStrategy(AggregationStrategy aggregationStrategy) { 088 setAggregationStrategy(aggregationStrategy); 089 return this; 090 } 091 092 /** 093 * Sets the AggregationStrategy to be used to merge the reply from the 094 * external service, into a single outgoing message. By default Camel will 095 * use the reply from the external service as outgoing message. 096 */ 097 public EnrichDefinition aggregationStrategy(Supplier<AggregationStrategy> aggregationStrategy) { 098 setAggregationStrategy(aggregationStrategy.get()); 099 return this; 100 } 101 102 /** 103 * Refers to an AggregationStrategy to be used to merge the reply from the 104 * external service, into a single outgoing message. By default Camel will 105 * use the reply from the external service as outgoing message. 106 */ 107 public EnrichDefinition aggregationStrategyRef(String aggregationStrategyRef) { 108 setAggregationStrategyRef(aggregationStrategyRef); 109 return this; 110 } 111 112 /** 113 * This option can be used to explicit declare the method name to use, when 114 * using POJOs as the AggregationStrategy. 115 */ 116 public EnrichDefinition aggregationStrategyMethodName(String aggregationStrategyMethodName) { 117 setAggregationStrategyMethodName(aggregationStrategyMethodName); 118 return this; 119 } 120 121 /** 122 * If this option is false then the aggregate method is not used if there 123 * was no data to enrich. If this option is true then null values is used as 124 * the oldExchange (when no data to enrich), when using POJOs as the 125 * AggregationStrategy. 126 */ 127 public EnrichDefinition aggregationStrategyMethodAllowNull(boolean aggregationStrategyMethodAllowNull) { 128 setAggregationStrategyMethodAllowNull(aggregationStrategyMethodAllowNull); 129 return this; 130 } 131 132 /** 133 * If this option is false then the aggregate method is not used if there 134 * was an exception thrown while trying to retrieve the data to enrich from 135 * the resource. Setting this option to true allows end users to control 136 * what to do if there was an exception in the aggregate method. For example 137 * to suppress the exception or set a custom message body etc. 138 */ 139 public EnrichDefinition aggregateOnException(boolean aggregateOnException) { 140 setAggregateOnException(aggregateOnException); 141 return this; 142 } 143 144 /** 145 * Shares the {@link org.apache.camel.spi.UnitOfWork} with the parent and 146 * the resource exchange. Enrich will by default not share unit of work 147 * between the parent exchange and the resource exchange. This means the 148 * resource exchange has its own individual unit of work. 149 */ 150 public EnrichDefinition shareUnitOfWork() { 151 setShareUnitOfWork(true); 152 return this; 153 } 154 155 /** 156 * Sets the maximum size used by the 157 * {@link org.apache.camel.spi.ProducerCache} which is used to cache and 158 * reuse producer when uris are reused. 159 * 160 * @param cacheSize the cache size, use <tt>0</tt> for default cache size, 161 * or <tt>-1</tt> to turn cache off. 162 * @return the builder 163 */ 164 public EnrichDefinition cacheSize(int cacheSize) { 165 setCacheSize(cacheSize); 166 return this; 167 } 168 169 /** 170 * Ignore the invalidate endpoint exception when try to create a producer 171 * with that endpoint 172 * 173 * @return the builder 174 */ 175 public EnrichDefinition ignoreInvalidEndpoint() { 176 setIgnoreInvalidEndpoint(true); 177 return this; 178 } 179 180 // Properties 181 // ------------------------------------------------------------------------- 182 183 /** 184 * Expression that computes the endpoint uri to use as the resource endpoint 185 * to enrich from 186 */ 187 @Override 188 public void setExpression(ExpressionDefinition expression) { 189 // override to include javadoc what the expression is used for 190 super.setExpression(expression); 191 } 192 193 public String getAggregationStrategyRef() { 194 return aggregationStrategyRef; 195 } 196 197 public void setAggregationStrategyRef(String aggregationStrategyRef) { 198 this.aggregationStrategyRef = aggregationStrategyRef; 199 } 200 201 public String getAggregationStrategyMethodName() { 202 return aggregationStrategyMethodName; 203 } 204 205 public void setAggregationStrategyMethodName(String aggregationStrategyMethodName) { 206 this.aggregationStrategyMethodName = aggregationStrategyMethodName; 207 } 208 209 public Boolean getAggregationStrategyMethodAllowNull() { 210 return aggregationStrategyMethodAllowNull; 211 } 212 213 public void setAggregationStrategyMethodAllowNull(Boolean aggregationStrategyMethodAllowNull) { 214 this.aggregationStrategyMethodAllowNull = aggregationStrategyMethodAllowNull; 215 } 216 217 public AggregationStrategy getAggregationStrategy() { 218 return aggregationStrategy; 219 } 220 221 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) { 222 this.aggregationStrategy = aggregationStrategy; 223 } 224 225 public Boolean getAggregateOnException() { 226 return aggregateOnException; 227 } 228 229 public void setAggregateOnException(Boolean aggregateOnException) { 230 this.aggregateOnException = aggregateOnException; 231 } 232 233 public Boolean getShareUnitOfWork() { 234 return shareUnitOfWork; 235 } 236 237 public void setShareUnitOfWork(Boolean shareUnitOfWork) { 238 this.shareUnitOfWork = shareUnitOfWork; 239 } 240 241 public Integer getCacheSize() { 242 return cacheSize; 243 } 244 245 public void setCacheSize(Integer cacheSize) { 246 this.cacheSize = cacheSize; 247 } 248 249 public Boolean getIgnoreInvalidEndpoint() { 250 return ignoreInvalidEndpoint; 251 } 252 253 public void setIgnoreInvalidEndpoint(Boolean ignoreInvalidEndpoint) { 254 this.ignoreInvalidEndpoint = ignoreInvalidEndpoint; 255 } 256}