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 messages with data polled from a secondary resource
033 *
034 * @see org.apache.camel.processor.Enricher
035 */
036@Metadata(label = "eip,transformation")
037@XmlRootElement(name = "pollEnrich")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class PollEnrichDefinition extends ExpressionNode {
040    @XmlAttribute
041    @Metadata(defaultValue = "-1")
042    private String timeout;
043    @XmlAttribute(name = "strategyRef")
044    private String aggregationStrategyRef;
045    @XmlAttribute(name = "strategyMethodName")
046    private String aggregationStrategyMethodName;
047    @XmlAttribute(name = "strategyMethodAllowNull")
048    private String aggregationStrategyMethodAllowNull;
049    @XmlAttribute
050    private String aggregateOnException;
051    @XmlTransient
052    private AggregationStrategy aggregationStrategy;
053    @XmlAttribute
054    private String cacheSize;
055    @XmlAttribute
056    private String ignoreInvalidEndpoint;
057
058    public PollEnrichDefinition() {
059    }
060
061    public PollEnrichDefinition(AggregationStrategy aggregationStrategy, long timeout) {
062        this.aggregationStrategy = aggregationStrategy;
063        this.timeout = Long.toString(timeout);
064    }
065
066    @Override
067    public String toString() {
068        return "PollEnrich[" + getExpression() + "]";
069    }
070
071    @Override
072    public String getShortName() {
073        return "pollEnrich";
074    }
075
076    @Override
077    public String getLabel() {
078        return "pollEnrich[" + getExpression() + "]";
079    }
080
081    // Fluent API
082    // -------------------------------------------------------------------------
083
084    /**
085     * Timeout in millis when polling from the external service.
086     * <p/>
087     * The timeout has influence about the poll enrich behavior. It basically
088     * operations in three different modes:
089     * <ul>
090     * <li>negative value - Waits until a message is available and then returns
091     * it. Warning that this method could block indefinitely if no messages are
092     * available.</li>
093     * <li>0 - Attempts to receive a message exchange immediately without
094     * waiting and returning <tt>null</tt> if a message exchange is not
095     * available yet.</li>
096     * <li>positive value - Attempts to receive a message exchange, waiting up
097     * to the given timeout to expire if a message is not yet available. Returns
098     * <tt>null</tt> if timed out</li>
099     * </ul>
100     * The default value is -1 and therefore the method could block
101     * indefinitely, and therefore its recommended to use a timeout value
102     */
103    public PollEnrichDefinition timeout(long timeout) {
104        setTimeout(Long.toString(timeout));
105        return this;
106    }
107
108    /**
109     * Sets the AggregationStrategy to be used to merge the reply from the
110     * external service, into a single outgoing message. By default Camel will
111     * use the reply from the external service as outgoing message.
112     */
113    public PollEnrichDefinition aggregationStrategy(AggregationStrategy aggregationStrategy) {
114        setAggregationStrategy(aggregationStrategy);
115        return this;
116    }
117
118    /**
119     * Sets the AggregationStrategy to be used to merge the reply from the
120     * external service, into a single outgoing message. By default Camel will
121     * use the reply from the external service as outgoing message.
122     */
123    public PollEnrichDefinition aggregationStrategy(Supplier<AggregationStrategy> aggregationStrategy) {
124        setAggregationStrategy(aggregationStrategy.get());
125        return this;
126    }
127
128    /**
129     * Refers to an AggregationStrategy to be used to merge the reply from the
130     * external service, into a single outgoing message. By default Camel will
131     * use the reply from the external service as outgoing message.
132     */
133    public PollEnrichDefinition aggregationStrategyRef(String aggregationStrategyRef) {
134        setAggregationStrategyRef(aggregationStrategyRef);
135        return this;
136    }
137
138    /**
139     * This option can be used to explicit declare the method name to use, when
140     * using POJOs as the AggregationStrategy.
141     */
142    public PollEnrichDefinition aggregationStrategyMethodName(String aggregationStrategyMethodName) {
143        setAggregationStrategyMethodName(aggregationStrategyMethodName);
144        return this;
145    }
146
147    /**
148     * If this option is false then the aggregate method is not used if there
149     * was no data to enrich. If this option is true then null values is used as
150     * the oldExchange (when no data to enrich), when using POJOs as the
151     * AggregationStrategy.
152     */
153    public PollEnrichDefinition aggregationStrategyMethodAllowNull(boolean aggregationStrategyMethodAllowNull) {
154        setAggregationStrategyMethodAllowNull(Boolean.toString(aggregationStrategyMethodAllowNull));
155        return this;
156    }
157
158    /**
159     * If this option is false then the aggregate method is not used if there
160     * was an exception thrown while trying to retrieve the data to enrich from
161     * the resource. Setting this option to true allows end users to control
162     * what to do if there was an exception in the aggregate method. For example
163     * to suppress the exception or set a custom message body etc.
164     */
165    public PollEnrichDefinition aggregateOnException(boolean aggregateOnException) {
166        setAggregateOnException(Boolean.toString(aggregateOnException));
167        return this;
168    }
169
170    /**
171     * Sets the maximum size used by the
172     * {@link org.apache.camel.spi.ConsumerCache} which is used to cache and
173     * reuse consumers when uris are reused.
174     *
175     * @param cacheSize the cache size, use <tt>0</tt> for default cache size,
176     *            or <tt>-1</tt> to turn cache off.
177     * @return the builder
178     */
179    public PollEnrichDefinition cacheSize(int cacheSize) {
180        setCacheSize(Integer.toString(cacheSize));
181        return this;
182    }
183
184    /**
185     * Ignore the invalidate endpoint exception when try to create a producer
186     * with that endpoint
187     *
188     * @return the builder
189     */
190    public PollEnrichDefinition ignoreInvalidEndpoint() {
191        setIgnoreInvalidEndpoint(Boolean.toString(true));
192        return this;
193    }
194
195    // Properties
196    // -------------------------------------------------------------------------
197
198    /**
199     * Expression that computes the endpoint uri to use as the resource endpoint
200     * to enrich from
201     */
202    @Override
203    public void setExpression(ExpressionDefinition expression) {
204        // override to include javadoc what the expression is used for
205        super.setExpression(expression);
206    }
207
208    public String getTimeout() {
209        return timeout;
210    }
211
212    public void setTimeout(String timeout) {
213        this.timeout = timeout;
214    }
215
216    public String getAggregationStrategyRef() {
217        return aggregationStrategyRef;
218    }
219
220    public void setAggregationStrategyRef(String aggregationStrategyRef) {
221        this.aggregationStrategyRef = aggregationStrategyRef;
222    }
223
224    public String getAggregationStrategyMethodName() {
225        return aggregationStrategyMethodName;
226    }
227
228    public void setAggregationStrategyMethodName(String aggregationStrategyMethodName) {
229        this.aggregationStrategyMethodName = aggregationStrategyMethodName;
230    }
231
232    public String getAggregationStrategyMethodAllowNull() {
233        return aggregationStrategyMethodAllowNull;
234    }
235
236    public void setAggregationStrategyMethodAllowNull(String aggregationStrategyMethodAllowNull) {
237        this.aggregationStrategyMethodAllowNull = aggregationStrategyMethodAllowNull;
238    }
239
240    public AggregationStrategy getAggregationStrategy() {
241        return aggregationStrategy;
242    }
243
244    public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
245        this.aggregationStrategy = aggregationStrategy;
246    }
247
248    public String getAggregateOnException() {
249        return aggregateOnException;
250    }
251
252    public void setAggregateOnException(String aggregateOnException) {
253        this.aggregateOnException = aggregateOnException;
254    }
255
256    public String getCacheSize() {
257        return cacheSize;
258    }
259
260    public void setCacheSize(String cacheSize) {
261        this.cacheSize = cacheSize;
262    }
263
264    public String getIgnoreInvalidEndpoint() {
265        return ignoreInvalidEndpoint;
266    }
267
268    public void setIgnoreInvalidEndpoint(String ignoreInvalidEndpoint) {
269        this.ignoreInvalidEndpoint = ignoreInvalidEndpoint;
270    }
271}