View Javadoc

1   /*
2    * $Id: XmppMessageAdapter.java 10489 2008-01-23 17:53:38Z dfeist $
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.transport.xmpp;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.api.ThreadSafeAccess;
15  import org.mule.api.transport.MessageTypeNotSupportedException;
16  import org.mule.transport.AbstractMessageAdapter;
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      {
41          if (message instanceof Packet)
42          {
43              this.message = (Packet)message;
44  
45              for (Iterator iter = this.message.getPropertyNames(); iter.hasNext();)
46              {
47                  String name = (String)iter.next();
48                  this.setProperty(name, this.message.getProperty(name));
49              }
50  
51              if (this.message instanceof Message)
52              {
53                  this.setProperty("subject", StringUtils.defaultIfEmpty(((Message)this.message).getSubject(),
54                      DEFAULT_SUBJECT));
55                  this.setProperty("thread", StringUtils.defaultIfEmpty(((Message)this.message).getThread(),
56                      DEFAULT_THREAD));
57              }
58          }
59          else
60          {
61              throw new MessageTypeNotSupportedException(message, getClass());
62          }
63      }
64  
65      protected XmppMessageAdapter(XmppMessageAdapter template)
66      {
67          super(template);
68          message = template.message;
69      }
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          if (message instanceof Message)
82          {
83              return ((Message)message).getBody();
84          }
85          else
86          {
87              return message.toString();
88          }
89      }
90  
91      public byte[] getPayloadAsBytes() throws Exception
92      {
93          if (message instanceof Message)
94          {
95              return ((Message)message).getBody().getBytes();
96          }
97          else
98          {
99              return message.toString().getBytes();
100         }
101     }
102 
103     public Object getPayload()
104     {
105         return message;
106     }
107 
108     // @Override
109     public String getUniqueId()
110     {
111         return message.getPacketID();
112     }
113 
114     public ThreadSafeAccess newThreadCopy()
115     {
116         return new XmppMessageAdapter(this);
117     }
118 
119 }