View Javadoc

1   /*
2    * $Id: XmppMuleMessageFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.DefaultMuleMessage;
14  import org.mule.api.MuleContext;
15  import org.mule.transport.AbstractMuleMessageFactory;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.jivesoftware.smack.packet.Message;
21  import org.jivesoftware.smack.packet.Packet;
22  
23  public class XmppMuleMessageFactory extends AbstractMuleMessageFactory
24  {
25      public XmppMuleMessageFactory(MuleContext context)
26      {
27          super(context);
28      }
29  
30      @Override
31      protected Class<?>[] getSupportedTransportMessageTypes()
32      {
33          return new Class[] { Packet.class };
34      }
35  
36      @Override
37      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
38      {
39          return transportMessage;
40      }
41  
42      @Override
43      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
44      {
45          super.addProperties(message, transportMessage);
46          
47          Packet packet = (Packet) transportMessage;
48  
49          message.setUniqueId(packet.getPacketID());
50          
51          Map<String, Object> properties = new HashMap<String, Object>();
52          addXmppPacketProperties(packet, properties);
53  
54          if (packet instanceof Message)
55          {
56              Message xmppMessage = (Message) packet;
57              addXmppMessageProperties(xmppMessage, properties);
58          }
59          
60          message.addInboundProperties(properties);
61      }
62  
63      private void addXmppPacketProperties(Packet packet, Map<String, Object> properties)
64      {
65          for (String key : packet.getPropertyNames())
66          {
67              properties.put(key, packet.getProperty(key));
68          }        
69      }
70  
71      private void addXmppMessageProperties(Message message, Map<String, Object> properties)
72      {
73          properties.put(XmppConnector.XMPP_SUBJECT, message.getSubject());
74          properties.put(XmppConnector.XMPP_THREAD, message.getThread());
75      }
76  }
77  
78