Coverage Report - org.mule.transport.xmpp.transformers.ObjectToXmppPacket
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToXmppPacket
0%
0/24
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: ObjectToXmppPacket.java 19250 2010-08-30 16:53:14Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.xmpp.transformers;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.transformer.DataType;
 15  
 import org.mule.transformer.AbstractMessageTransformer;
 16  
 import org.mule.transformer.types.DataTypeFactory;
 17  
 import org.mule.transport.xmpp.XmppConnector;
 18  
 
 19  
 import org.jivesoftware.smack.packet.Message;
 20  
 import org.jivesoftware.smack.packet.XMPPError;
 21  
 
 22  
 /**
 23  
  * Creates an Xmpp message packet from a MuleMessage
 24  
  */
 25  
 public class ObjectToXmppPacket extends AbstractMessageTransformer
 26  
 {
 27  
     public ObjectToXmppPacket()
 28  0
     {
 29  0
         DataType<Message> messageDataType = DataTypeFactory.create(Message.class);
 30  
 
 31  0
         registerSourceType(DataTypeFactory.STRING);
 32  0
         registerSourceType(messageDataType);
 33  0
         setReturnDataType(messageDataType);
 34  0
     }
 35  
 
 36  
     @Override
 37  
     public Object transformMessage(MuleMessage muleMessage, String outputEncoding)
 38  
     {
 39  0
         Object src = muleMessage.getPayload();
 40  
         
 41  
         // Make the transformer match its wiki documentation: we accept Messages and Strings.
 42  
         // No special treatment for Messages is needed
 43  0
         if (src instanceof Message)
 44  
         {
 45  0
             return src;
 46  
         }
 47  
         
 48  0
         Message result = new Message();
 49  
 
 50  0
         if (muleMessage.getExceptionPayload() != null)
 51  
         {
 52  0
             result.setError(
 53  
                 new XMPPError(XMPPError.Condition.service_unavailable,
 54  
                     muleMessage.getExceptionPayload().getMessage()));
 55  
         }
 56  
 
 57  0
         for (String propertyName : muleMessage.getOutboundPropertyNames())
 58  
         {
 59  0
             if (propertyName.equals(XmppConnector.XMPP_THREAD))
 60  
             {
 61  0
                 result.setThread((String) muleMessage.getOutboundProperty(propertyName));
 62  
             }
 63  0
             else if (propertyName.equals(XmppConnector.XMPP_SUBJECT))
 64  
             {
 65  0
                 result.setSubject((String) muleMessage.getOutboundProperty(propertyName));
 66  
             }
 67  0
             else if (propertyName.equals(XmppConnector.XMPP_FROM))
 68  
             {
 69  0
                 result.setFrom((String) muleMessage.getOutboundProperty(propertyName));
 70  
             }
 71  0
             else if (propertyName.equals(XmppConnector.XMPP_TO))
 72  
             {
 73  0
                 result.setTo((String) muleMessage.getOutboundProperty(propertyName));
 74  
             }
 75  
             else
 76  
             {
 77  0
                 result.setProperty(propertyName, muleMessage.<Object>getOutboundProperty(propertyName));
 78  
             }
 79  
         }
 80  
 
 81  
         // copy the payload. Since it can only be a String (other objects wouldn't be passed in through
 82  
         // AbstractTransformer) the following is safe.
 83  0
         result.setBody((String) src);
 84  
         
 85  0
         return result;
 86  
     }
 87  
 }