Coverage Report - org.apache.camel.component.jbi.JbiBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
JbiBinding
100% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.jbi;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 
 22  
 import javax.jbi.messaging.MessageExchange;
 23  
 import javax.jbi.messaging.MessageExchangeFactory;
 24  
 import javax.jbi.messaging.MessagingException;
 25  
 import javax.jbi.messaging.NormalizedMessage;
 26  
 import javax.xml.transform.Source;
 27  
 import javax.xml.transform.stream.StreamSource;
 28  
 import java.io.StringReader;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 /**
 33  
  * The binding of how Camel messages get mapped to JBI and back again
 34  
  *
 35  
  * @version $Revision: 522517 $
 36  
  */
 37  3
 public class JbiBinding {
 38  
     /**
 39  
      * Extracts the body from the given normalized message
 40  
      */
 41  
     public Object extractBodyFromJbi(JbiExchange exchange, NormalizedMessage normalizedMessage) {
 42  
         // TODO we may wish to turn this into a POJO such as a JAXB/DOM
 43  2
         return normalizedMessage.getContent();
 44  
     }
 45  
 
 46  
     public MessageExchange makeJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException {
 47  3
         MessageExchange jbiExchange = createJbiMessageExchange(camelExchange, exchangeFactory);
 48  3
         NormalizedMessage normalizedMessage = jbiExchange.getMessage("in");
 49  3
         if (normalizedMessage == null) {
 50  3
             normalizedMessage = jbiExchange.createMessage();
 51  3
             jbiExchange.setMessage(normalizedMessage, "in");
 52  
         }
 53  3
         normalizedMessage.setContent(getJbiInContent(camelExchange));
 54  3
         addJbiHeaders(jbiExchange, normalizedMessage, camelExchange);
 55  3
         return jbiExchange;
 56  
     }
 57  
 
 58  
     protected MessageExchange createJbiMessageExchange(Exchange camelExchange, MessageExchangeFactory exchangeFactory) throws MessagingException {
 59  
         // TODO we should deal with other forms of MEP
 60  3
         return exchangeFactory.createInOnlyExchange();
 61  
     }
 62  
 
 63  
     protected Source getJbiInContent(Exchange camelExchange) {
 64  
         // TODO this should be more smart
 65  3
         Object value = camelExchange.getIn().getBody();
 66  3
         if (value instanceof String) {
 67  2
             return new StreamSource(new StringReader(value.toString()));
 68  
         }
 69  1
         return camelExchange.getIn().getBody(Source.class);
 70  
     }
 71  
 
 72  
     protected void addJbiHeaders(MessageExchange jbiExchange, NormalizedMessage normalizedMessage, Exchange camelExchange) {
 73  3
         Set<Map.Entry<String, Object>> entries = camelExchange.getIn().getHeaders().entrySet();
 74  3
         for (Map.Entry<String, Object> entry : entries) {
 75  3
             normalizedMessage.setProperty(entry.getKey(), entry.getValue());
 76  3
         }
 77  3
     }
 78  
 }
 79