Coverage Report - org.mule.providers.xmpp.XmppMessageAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppMessageAdapter
33%
8/24
30%
3/10
2.143
 
 1  
 /*
 2  
  * $Id: XmppMessageAdapter.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.xmpp;
 12  
 
 13  
 import org.mule.impl.ThreadSafeAccess;
 14  
 import org.mule.providers.AbstractMessageAdapter;
 15  
 import org.mule.umo.MessagingException;
 16  
 import org.mule.umo.provider.MessageTypeNotSupportedException;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.util.Iterator;
 20  
 
 21  
 import org.jivesoftware.smack.packet.Message;
 22  
 import org.jivesoftware.smack.packet.Packet;
 23  
 
 24  
 /**
 25  
  * <code>XmppMessageAdapter</code> wraps a Smack XMPP packet
 26  
  */
 27  
 public class XmppMessageAdapter extends AbstractMessageAdapter
 28  
 {
 29  
     public static final String DEFAULT_SUBJECT = "(no subject)";
 30  
     public static final String DEFAULT_THREAD = "(no thread)";
 31  
 
 32  
     /**
 33  
      * Serial version
 34  
      */
 35  
     private static final long serialVersionUID = -4003299444661664762L;
 36  
 
 37  
     private final Packet message;
 38  
 
 39  
     public XmppMessageAdapter(Object message) throws MessagingException
 40  4
     {
 41  4
         if (message instanceof Packet)
 42  
         {
 43  4
             this.message = (Packet)message;
 44  
 
 45  4
             for (Iterator iter = this.message.getPropertyNames(); iter.hasNext();)
 46  
             {
 47  0
                 String name = (String)iter.next();
 48  0
                 this.setProperty(name, this.message.getProperty(name));
 49  0
             }
 50  
 
 51  4
             if (this.message instanceof Message)
 52  
             {
 53  4
                 this.setProperty("subject", StringUtils.defaultIfEmpty(((Message)this.message).getSubject(),
 54  
                     DEFAULT_SUBJECT));
 55  4
                 this.setProperty("thread", StringUtils.defaultIfEmpty(((Message)this.message).getThread(),
 56  
                     DEFAULT_THREAD));
 57  
             }
 58  
         }
 59  
         else
 60  
         {
 61  0
             throw new MessageTypeNotSupportedException(message, getClass());
 62  
         }
 63  4
     }
 64  
 
 65  
     protected XmppMessageAdapter(XmppMessageAdapter template)
 66  
     {
 67  0
         super(template);
 68  0
         message = template.message;
 69  0
     }
 70  
 
 71  
     /**
 72  
      * Converts the message implementation into a String representation
 73  
      * 
 74  
      * @param encoding The encoding to use when transforming the message (if
 75  
      *            necessary). The parameter is used when converting from a byte array
 76  
      * @return String representation of the message payload
 77  
      * @throws Exception Implementation may throw an endpoint specific exception
 78  
      */
 79  
     public String getPayloadAsString(String encoding) throws Exception
 80  
     {
 81  0
         if (message instanceof Message)
 82  
         {
 83  0
             return ((Message)message).getBody();
 84  
         }
 85  
         else
 86  
         {
 87  0
             return message.toString();
 88  
         }
 89  
     }
 90  
 
 91  
     public byte[] getPayloadAsBytes() throws Exception
 92  
     {
 93  0
         if (message instanceof Message)
 94  
         {
 95  0
             return ((Message)message).getBody().getBytes();
 96  
         }
 97  
         else
 98  
         {
 99  0
             return message.toString().getBytes();
 100  
         }
 101  
     }
 102  
 
 103  
     public Object getPayload()
 104  
     {
 105  0
         return message;
 106  
     }
 107  
 
 108  
     // @Override
 109  
     public String getUniqueId()
 110  
     {
 111  0
         return message.getPacketID();
 112  
     }
 113  
 
 114  
     public ThreadSafeAccess newThreadCopy()
 115  
     {
 116  0
         return new XmppMessageAdapter(this);
 117  
     }
 118  
 
 119  
 }