View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.xmpp;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.transport.AbstractMuleMessageFactory;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import org.jivesoftware.smack.packet.Message;
17  import org.jivesoftware.smack.packet.Packet;
18  
19  public class XmppMuleMessageFactory extends AbstractMuleMessageFactory
20  {
21      public XmppMuleMessageFactory(MuleContext context)
22      {
23          super(context);
24      }
25  
26      @Override
27      protected Class<?>[] getSupportedTransportMessageTypes()
28      {
29          return new Class[] { Packet.class };
30      }
31  
32      @Override
33      protected Object extractPayload(Object transportMessage, String encoding) throws Exception
34      {
35          return transportMessage;
36      }
37  
38      @Override
39      protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
40      {
41          super.addProperties(message, transportMessage);
42          
43          Packet packet = (Packet) transportMessage;
44  
45          message.setUniqueId(packet.getPacketID());
46          
47          Map<String, Object> properties = new HashMap<String, Object>();
48          addXmppPacketProperties(packet, properties);
49  
50          if (packet instanceof Message)
51          {
52              Message xmppMessage = (Message) packet;
53              addXmppMessageProperties(xmppMessage, properties);
54          }
55          
56          message.addInboundProperties(properties);
57      }
58  
59      private void addXmppPacketProperties(Packet packet, Map<String, Object> properties)
60      {
61          for (String key : packet.getPropertyNames())
62          {
63              properties.put(key, packet.getProperty(key));
64          }        
65      }
66  
67      private void addXmppMessageProperties(Message message, Map<String, Object> properties)
68      {
69          properties.put(XmppConnector.XMPP_SUBJECT, message.getSubject());
70          properties.put(XmppConnector.XMPP_THREAD, message.getThread());
71      }
72  }
73  
74