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     */
017    package org.apache.camel.processor;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.ExchangePattern;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Producer;
023    import org.apache.camel.impl.DefaultExchange;
024    import org.apache.camel.impl.ServiceSupport;
025    import org.apache.camel.processor.aggregate.AggregationStrategy;
026    import org.apache.camel.util.ExchangeHelper;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    import static org.apache.camel.util.ExchangeHelper.copyResultsPreservePattern;
031    /**
032     * A content enricher that enriches input data by first obtaining additional
033     * data from a <i>resource</i> represented by an endpoint <code>producer</code>
034     * and second by aggregating input data and additional data. Aggregation of
035     * input data and additional data is delegated to an {@link AggregationStrategy}
036     * object.
037     * <p/>
038     * Uses a {@link org.apache.camel.Producer} to obtain the additional data as opposed to {@link PollEnricher}
039     * that uses a {@link org.apache.camel.PollingConsumer}.
040     *
041     * @see PollEnricher
042     */
043    public class Enricher extends ServiceSupport implements Processor {
044    
045        private static final transient Log LOG = LogFactory.getLog(Enricher.class);
046        private AggregationStrategy aggregationStrategy;
047        private Producer producer;
048    
049        /**
050         * Creates a new {@link Enricher}. The default aggregation strategy is to
051         * copy the additional data obtained from the enricher's resource over the
052         * input data. When using the copy aggregation strategy the enricher
053         * degenerates to a normal transformer.
054         * 
055         * @param producer producer to resource endpoint.
056         */
057        public Enricher(Producer producer) {
058            this(defaultAggregationStrategy(), producer);
059        }
060    
061        /**
062         * Creates a new {@link Enricher}.
063         * 
064         * @param aggregationStrategy  aggregation strategy to aggregate input data and additional data.
065         * @param producer producer to resource endpoint.
066         */
067        public Enricher(AggregationStrategy aggregationStrategy, Producer producer) {
068            this.aggregationStrategy = aggregationStrategy;
069            this.producer = producer;
070        }
071    
072        /**
073         * Sets the aggregation strategy for this enricher.
074         *
075         * @param aggregationStrategy the aggregationStrategy to set
076         */
077        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
078            this.aggregationStrategy = aggregationStrategy;
079        }
080    
081        /**
082         * Sets the default aggregation strategy for this enricher.
083         */
084        public void setDefaultAggregationStrategy() {
085            this.aggregationStrategy = defaultAggregationStrategy();
086        }
087    
088        /**
089         * Enriches the input data (<code>exchange</code>) by first obtaining
090         * additional data from an endpoint represented by an endpoint
091         * <code>producer</code> and second by aggregating input data and additional
092         * data. Aggregation of input data and additional data is delegated to an
093         * {@link AggregationStrategy} object set at construction time. If the
094         * message exchange with the resource endpoint fails then no aggregation
095         * will be done and the failed exchange content is copied over to the
096         * original message exchange.
097         * 
098         * @param exchange input data.
099         */
100        public void process(Exchange exchange) throws Exception {
101            Exchange resourceExchange = createResourceExchange(exchange, ExchangePattern.InOut);
102            producer.process(resourceExchange);
103    
104            if (resourceExchange.isFailed()) {
105                // copy resource exchange onto original exchange (preserving pattern)
106                copyResultsPreservePattern(exchange, resourceExchange);
107            } else {
108                prepareResult(exchange);
109    
110                // prepare the exchanges for aggregation
111                ExchangeHelper.prepareAggregation(exchange, resourceExchange);
112                Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
113                if (aggregatedExchange != null) {
114                    // copy aggregation result onto original exchange (preserving pattern)
115                    copyResultsPreservePattern(exchange, aggregatedExchange);
116                }
117            }
118        }
119    
120        /**
121         * Creates a new {@link DefaultExchange} instance from the given
122         * <code>exchange</code>. The resulting exchange's pattern is defined by
123         * <code>pattern</code>.
124         *
125         * @param source  exchange to copy from.
126         * @param pattern exchange pattern to set.
127         * @return created exchange.
128         */
129        protected Exchange createResourceExchange(Exchange source, ExchangePattern pattern) {
130            Exchange target = source.copy();
131            target.setPattern(pattern);
132            return target;
133        }
134    
135        private static void prepareResult(Exchange exchange) {
136            if (exchange.getPattern().isOutCapable()) {
137                exchange.getOut().copyFrom(exchange.getIn());
138            }
139        }
140    
141        private static AggregationStrategy defaultAggregationStrategy() {
142            return new CopyAggregationStrategy();
143        }
144    
145        @Override
146        public String toString() {
147            return "Enrich[" + producer.getEndpoint().getEndpointUri() + "]";
148        }
149    
150        protected void doStart() throws Exception {
151            producer.start();
152        }
153    
154        protected void doStop() throws Exception {
155            producer.stop();
156        }
157    
158        private static class CopyAggregationStrategy implements AggregationStrategy {
159    
160            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
161                if (newExchange != null) {
162                    copyResultsPreservePattern(oldExchange, newExchange);
163                }
164                return oldExchange;
165            }
166    
167        }
168    
169    }