View Javadoc

1   /*
2    * $Id: XmppPacketToObject.java 20321 2010-11-24 15:21:24Z dfeist $
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.transformers;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.PropertyScope;
15  import org.mule.transformer.AbstractMessageTransformer;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.transport.xmpp.XmppConnector;
18  import org.mule.util.StringUtils;
19  
20  import org.jivesoftware.smack.packet.Message;
21  
22  public class XmppPacketToObject extends AbstractMessageTransformer
23  {
24      public XmppPacketToObject()
25      {
26          registerSourceType(DataTypeFactory.create(Message.class));
27          setReturnDataType(DataTypeFactory.STRING);
28      }
29  
30      @Override
31      public Object transformMessage(MuleMessage muleMessage, String outputEncoding)
32      {
33          Message xmppMessage = (Message) muleMessage.getPayload();
34          copySubject(xmppMessage, muleMessage);
35          copyThread(xmppMessage, muleMessage);
36          copyProperties(xmppMessage, muleMessage);
37          return xmppMessage.getBody();
38      }
39  
40      private void copySubject(Message xmppMessage, MuleMessage muleMessage)
41      {
42          String subject = xmppMessage.getSubject();
43          if (StringUtils.isNotEmpty(subject))
44          {
45              muleMessage.setProperty(XmppConnector.XMPP_SUBJECT, subject, PropertyScope.INBOUND);
46          }
47      }
48  
49      private void copyThread(Message xmppMessage, MuleMessage muleMessage)
50      {
51          String thread = xmppMessage.getThread();
52          if (StringUtils.isNotEmpty(thread))
53          {
54              muleMessage.setOutboundProperty(XmppConnector.XMPP_THREAD, thread);
55          }
56      }
57  
58      private void copyProperties(Message xmppMessage, MuleMessage muleMessage)
59      {
60          for (String propertyName : xmppMessage.getPropertyNames())
61          {
62              muleMessage.setOutboundProperty(propertyName, xmppMessage.getProperty(propertyName));
63          }
64      }
65  }