001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.component.jbi; 019 020 import org.apache.camel.Exchange; 021 022 import javax.jbi.messaging.MessageExchange; 023 import javax.jbi.messaging.MessageExchangeFactory; 024 import javax.jbi.messaging.MessagingException; 025 import javax.jbi.messaging.NormalizedMessage; 026 import javax.xml.transform.Source; 027 import javax.xml.transform.stream.StreamSource; 028 import java.io.StringReader; 029 import java.util.Map; 030 import java.util.Set; 031 032 /** 033 * The binding of how Camel messages get mapped to JBI and back again 034 * 035 * @version $Revision: 522517 $ 036 */ 037 public class JbiBinding { 038 /** 039 * Extracts the body from the given normalized message 040 */ 041 public Object extractBodyFromJbi(JbiExchange exchange, NormalizedMessage normalizedMessage) { 042 // TODO we may wish to turn this into a POJO such as a JAXB/DOM 043 return normalizedMessage.getContent(); 044 } 045 046 public MessageExchange makeJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException { 047 MessageExchange jbiExchange = createJbiMessageExchange(camelExchange, exchangeFactory); 048 NormalizedMessage normalizedMessage = jbiExchange.getMessage("in"); 049 if (normalizedMessage == null) { 050 normalizedMessage = jbiExchange.createMessage(); 051 jbiExchange.setMessage(normalizedMessage, "in"); 052 } 053 normalizedMessage.setContent(getJbiInContent(camelExchange)); 054 addJbiHeaders(jbiExchange, normalizedMessage, camelExchange); 055 return jbiExchange; 056 } 057 058 protected MessageExchange createJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException { 059 // TODO we should deal with other forms of MEP 060 return exchangeFactory.createInOnlyExchange(); 061 } 062 063 protected Source getJbiInContent(Exchange camelExchange) { 064 // TODO this should be more smart 065 Object value = camelExchange.getIn().getBody(); 066 if (value instanceof String) { 067 return new StreamSource(new StringReader(value.toString())); 068 } 069 return camelExchange.getIn().getBody(Source.class); 070 } 071 072 protected void addJbiHeaders(MessageExchange jbiExchange, NormalizedMessage normalizedMessage, Exchange camelExchange) { 073 Set<Map.Entry<String, Object>> entries = camelExchange.getIn().getHeaders().entrySet(); 074 for (Map.Entry<String, Object> entry : entries) { 075 normalizedMessage.setProperty(entry.getKey(), entry.getValue()); 076 } 077 } 078 } 079