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