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.builder;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.ExchangePattern;
025    import org.apache.camel.Message;
026    import org.apache.camel.impl.DefaultExchange;
027    
028    public final class ExchangeBuilder {
029        private CamelContext context;
030        private ExchangePattern pattern;
031        private Object body;
032        private Map<String, Object> headers = new HashMap<String, Object>();
033        private Map<String, Object> properties = new HashMap<String, Object>();
034    
035        public ExchangeBuilder(CamelContext context) {
036            this.context = context;
037        }
038    
039        /**
040         * Create the exchange by setting the camel context
041         * @param context the camel context 
042         * @return exchange builder
043         */
044        public static ExchangeBuilder anExchange(CamelContext context) {
045            return new ExchangeBuilder(context);
046        }
047    
048        /**
049         * Set the in message body on the exchange
050         * @param body
051         * @return exchange builder
052         */
053        public ExchangeBuilder withBody(Object body) {
054            this.body = body;
055            return this;
056        }
057    
058        /**
059         * Set the message header of the in message on the exchange
060         * @param key the key of the header
061         * @param value the value of the header
062         * @return exchange builder
063         */
064        public ExchangeBuilder withHeader(String key, Object value) {
065            headers.put(key, value);
066            return this;
067        }
068    
069        /**
070         * Set the message exchange pattern on the exchange
071         * @param pattern exchange pattern
072         * @return exchange builder
073         */
074        public ExchangeBuilder withPattern(ExchangePattern pattern) {
075            this.pattern = pattern;
076            return this;
077        }
078        
079        /**
080         * Set the exchange property
081         * @param key the key of the exchange property
082         * @param value the value of the exchange property
083         * @return exchange builder
084         */
085        public ExchangeBuilder withProperty(String key, Object value) {
086            properties.put(key, value);
087            return this;
088        }
089    
090        /**
091         * Build up the exchange from the exchange builder
092         * @return exchange 
093         */
094        public Exchange build() {
095            Exchange exchange = new DefaultExchange(context);
096            Message message = exchange.getIn();
097            message.setBody(body);
098            if (headers.size() > 0) {
099                message.setHeaders(headers);
100            }
101            // setup the properties on the exchange
102            for (Map.Entry<String, Object> entry : properties.entrySet()) {
103                exchange.setProperty(entry.getKey(), entry.getValue());
104            }
105            if (pattern != null) {
106                exchange.setPattern(pattern);
107            }
108    
109            return exchange;
110        }
111    }