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