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.component.jbi; 018 019 import org.apache.camel.Exchange; 020 021 import javax.jbi.messaging.MessageExchange; 022 import javax.jbi.messaging.MessageExchangeFactory; 023 import javax.jbi.messaging.MessagingException; 024 import javax.jbi.messaging.NormalizedMessage; 025 import javax.xml.transform.Source; 026 import javax.xml.transform.stream.StreamSource; 027 import java.io.StringReader; 028 import java.util.Map; 029 import java.util.Set; 030 031 /** 032 * The binding of how Camel messages get mapped to JBI and back again 033 * 034 * @version $Revision: 563665 $ 035 */ 036 public class JbiBinding { 037 /** 038 * Extracts the body from the given normalized message 039 */ 040 public Object extractBodyFromJbi(JbiExchange exchange, NormalizedMessage normalizedMessage) { 041 // TODO we may wish to turn this into a POJO such as a JAXB/DOM 042 return normalizedMessage.getContent(); 043 } 044 045 public MessageExchange makeJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException { 046 MessageExchange jbiExchange = createJbiMessageExchange(camelExchange, exchangeFactory); 047 NormalizedMessage normalizedMessage = jbiExchange.getMessage("in"); 048 if (normalizedMessage == null) { 049 normalizedMessage = jbiExchange.createMessage(); 050 jbiExchange.setMessage(normalizedMessage, "in"); 051 } 052 normalizedMessage.setContent(getJbiInContent(camelExchange)); 053 addJbiHeaders(jbiExchange, normalizedMessage, camelExchange); 054 return jbiExchange; 055 } 056 057 protected MessageExchange createJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException { 058 // TODO we should deal with other forms of MEP 059 return exchangeFactory.createInOnlyExchange(); 060 } 061 062 protected Source getJbiInContent(Exchange camelExchange) { 063 // TODO this should be more smart 064 Object value = camelExchange.getIn().getBody(); 065 if (value instanceof String) { 066 return new StreamSource(new StringReader(value.toString())); 067 } 068 return camelExchange.getIn().getBody(Source.class); 069 } 070 071 protected void addJbiHeaders(MessageExchange jbiExchange, NormalizedMessage normalizedMessage, Exchange camelExchange) { 072 Set<Map.Entry<String, Object>> entries = camelExchange.getIn().getHeaders().entrySet(); 073 for (Map.Entry<String, Object> entry : entries) { 074 normalizedMessage.setProperty(entry.getKey(), entry.getValue()); 075 } 076 } 077 } 078