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