KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > xmpp > transformers > ObjectToXmppPacket


1 /*
2  * $Id: ObjectToXmppPacket.java 3798 2006-11-04 04:07:14Z aperepel $
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.transformers;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.jivesoftware.smack.packet.Message;
16 import org.jivesoftware.smack.packet.XMPPError;
17 import org.mule.providers.xmpp.XmppConnector;
18 import org.mule.transformers.AbstractEventAwareTransformer;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.UMOException;
21 import org.mule.umo.UMOMessage;
22 import org.mule.umo.transformer.TransformerException;
23
24 /**
25  * Creates an Xmpp message packet from a UMOMessage
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30 public class ObjectToXmppPacket extends AbstractEventAwareTransformer
31 {
32
33     /**
34      * Serial version
35      */

36     private static final long serialVersionUID = -815224066829254844L;
37
38     public ObjectToXmppPacket()
39     {
40         registerSourceType(String JavaDoc.class);
41         setReturnClass(Message.class);
42     }
43
44     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
45     {
46         Message result = null;
47         try
48         {
49             result = new Message(context.getMessageAsString(encoding));
50         }
51         catch (UMOException e)
52         {
53             throw new TransformerException(this, e);
54         }
55
56         UMOMessage msg = context.getMessage();
57         if (msg.getExceptionPayload() != null)
58         {
59             result.setError(new XMPPError(503, context.getMessage().getExceptionPayload().getMessage()));
60         }
61
62         for (Iterator JavaDoc iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
63         {
64             String JavaDoc name = (String JavaDoc)iterator.next();
65             if (name.equals(XmppConnector.XMPP_THREAD))
66             {
67                 result.setThread((String JavaDoc)msg.getProperty(name));
68             }
69             else if (name.equals(XmppConnector.XMPP_SUBJECT))
70             {
71                 result.setSubject((String JavaDoc)msg.getProperty(name));
72             }
73             else if (name.equals(XmppConnector.XMPP_FROM))
74             {
75                 result.setFrom((String JavaDoc)msg.getProperty(name));
76             }
77             else if (name.equals(XmppConnector.XMPP_TO))
78             {
79                 result.setTo((String JavaDoc)msg.getProperty(name));
80             }
81             else
82             {
83                 result.setProperty(name, msg.getProperty(name));
84             }
85         }
86         return result;
87     }
88
89 }
90
Popular Tags