Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObjectToXmppPacket |
|
| 5.0;5 |
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 | 4 | { |
31 | 8 | this.registerSourceType(String.class); |
32 | 4 | this.registerSourceType(Message.class); |
33 | 4 | setReturnClass(Message.class); |
34 | 4 | } |
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 | 0 | if (src instanceof Message) |
41 | { | |
42 | 0 | return src; |
43 | } | |
44 | ||
45 | 0 | Message result = new Message(); |
46 | ||
47 | 0 | UMOMessage msg = context.getMessage(); |
48 | 0 | if (msg.getExceptionPayload() != null) |
49 | { | |
50 | 0 | result.setError(new XMPPError(503, context.getMessage().getExceptionPayload().getMessage())); |
51 | } | |
52 | ||
53 | 0 | for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) |
54 | { | |
55 | 0 | String name = (String) iterator.next(); |
56 | 0 | if (name.equals(XmppConnector.XMPP_THREAD)) |
57 | { | |
58 | 0 | result.setThread((String) msg.getProperty(name)); |
59 | } | |
60 | 0 | else if (name.equals(XmppConnector.XMPP_SUBJECT)) |
61 | { | |
62 | 0 | result.setSubject((String) msg.getProperty(name)); |
63 | } | |
64 | 0 | else if (name.equals(XmppConnector.XMPP_FROM)) |
65 | { | |
66 | 0 | result.setFrom((String) msg.getProperty(name)); |
67 | } | |
68 | 0 | else if (name.equals(XmppConnector.XMPP_TO)) |
69 | { | |
70 | 0 | result.setTo((String) msg.getProperty(name)); |
71 | } | |
72 | else | |
73 | { | |
74 | 0 | result.setProperty(name, msg.getProperty(name)); |
75 | } | |
76 | 0 | } |
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 | 0 | result.setBody((String) src); |
81 | ||
82 | 0 | return result; |
83 | } | |
84 | } |