View Javadoc

1   /*
2    * $Id: ObjectToXmppPacket.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.transformers;
12  
13  import org.mule.providers.xmpp.XmppConnector;
14  import org.mule.transformers.AbstractEventAwareTransformer;
15  import org.mule.umo.UMOEventContext;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.transformer.TransformerException;
18  
19  import java.util.Iterator;
20  
21  import org.jivesoftware.smack.packet.Message;
22  import org.jivesoftware.smack.packet.XMPPError;
23  
24  /**
25   * Creates an Xmpp message packet from a UMOMessage
26   */
27  public class ObjectToXmppPacket extends AbstractEventAwareTransformer
28  {
29      public ObjectToXmppPacket()
30      {
31          this.registerSourceType(String.class);
32          this.registerSourceType(Message.class);
33          setReturnClass(Message.class);
34      }
35  
36      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
37      {
38          // Make the transformer match its wiki documentation: we accept Messages and Strings. 
39          // No special treatment for Messages is needed
40          if (src instanceof Message)
41          {
42              return src;
43          }
44          
45          Message result = new Message();
46  
47          UMOMessage msg = context.getMessage();
48          if (msg.getExceptionPayload() != null)
49          {
50              result.setError(new XMPPError(503, context.getMessage().getExceptionPayload().getMessage()));
51          }
52  
53          for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();)
54          {
55              String name = (String) iterator.next();
56              if (name.equals(XmppConnector.XMPP_THREAD))
57              {
58                  result.setThread((String) msg.getProperty(name));
59              }
60              else if (name.equals(XmppConnector.XMPP_SUBJECT))
61              {
62                  result.setSubject((String) msg.getProperty(name));
63              }
64              else if (name.equals(XmppConnector.XMPP_FROM))
65              {
66                  result.setFrom((String) msg.getProperty(name));
67              }
68              else if (name.equals(XmppConnector.XMPP_TO))
69              {
70                  result.setTo((String) msg.getProperty(name));
71              }
72              else
73              {
74                  result.setProperty(name, msg.getProperty(name));
75              }
76          }
77  
78          // copy the payload. Since it can only be a String (other objects wouldn't be passed in through
79          // AbstractTransformer) the following is safe.
80          result.setBody((String) src);
81          
82          return result;
83      }
84  }