KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > xmpp > XmppMessageAdapter


1 /*
2  * $Id: XmppMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
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.jivesoftware.smack.packet.Message;
14 import org.jivesoftware.smack.packet.Packet;
15 import org.mule.providers.AbstractMessageAdapter;
16 import org.mule.umo.MessagingException;
17 import org.mule.umo.provider.MessageTypeNotSupportedException;
18
19 import java.util.Iterator JavaDoc;
20
21 /**
22  * <code>XmppMessageAdapter</code> wraps an Smack XMPP packet
23  *
24  * @author Peter Braswell
25  * @version $Revision: 3982 $
26  */

27 public class XmppMessageAdapter extends AbstractMessageAdapter
28 {
29     /**
30      * Serial version
31      */

32     private static final long serialVersionUID = -4003299444661664762L;
33
34     private final Packet message;
35
36     public XmppMessageAdapter(Object JavaDoc message) throws MessagingException
37     {
38         if (message instanceof Packet)
39         {
40             this.message = (Packet)message;
41             for (Iterator JavaDoc iter = this.message.getPropertyNames(); iter.hasNext();)
42             {
43                 String JavaDoc name = (String JavaDoc)iter.next();
44                 setProperty(name, this.message.getProperty(name));
45             }
46             if (this.message instanceof Message)
47             {
48                 setProperty("subject", ((Message)this.message).getSubject());
49                 setProperty("thread", ((Message)this.message).getThread());
50             }
51         }
52         else
53         {
54             throw new MessageTypeNotSupportedException(message, getClass());
55         }
56     }
57
58     /**
59      * Converts the message implementation into a String representation
60      *
61      * @param encoding The encoding to use when transforming the message (if
62      * necessary). The parameter is used when converting from a byte array
63      * @return String representation of the message payload
64      * @throws Exception Implementation may throw an endpoint specific exception
65      */

66     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
67     {
68         if (message instanceof Message)
69         {
70             return ((Message)message).getBody();
71         }
72         else
73         {
74             return message.toString();
75         }
76     }
77
78     public byte[] getPayloadAsBytes() throws Exception JavaDoc
79     {
80         if (message instanceof Message)
81         {
82             return ((Message)message).getBody().getBytes();
83         }
84         else
85         {
86             return message.toString().getBytes();
87         }
88     }
89
90     public Object JavaDoc getPayload()
91     {
92         return message;
93     }
94
95     public String JavaDoc getUniqueId()
96     {
97         return message.getPacketID();
98     }
99 }
100
Popular Tags