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