1 17 package org.apache.servicemix.components.jabber; 18 19 import org.apache.servicemix.jbi.jaxp.SourceMarshaler; 20 import org.jivesoftware.smack.packet.Message; 21 import org.jivesoftware.smack.packet.Packet; 22 23 import javax.jbi.messaging.MessagingException; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.TransformerException ; 27 import java.util.Date ; 28 import java.util.Iterator ; 29 30 35 public class JabberMarshaler { 36 private SourceMarshaler sourceMarshaler; 37 38 public JabberMarshaler() { 39 this(new SourceMarshaler()); 40 } 41 42 public JabberMarshaler(SourceMarshaler sourceMarshaler) { 43 this.sourceMarshaler = sourceMarshaler; 44 } 45 46 51 public void toNMS(NormalizedMessage normalizedMessage, Packet packet) throws MessagingException { 52 addNmsProperties(normalizedMessage, packet); 53 if (packet instanceof Message) { 54 Message message = (Message) packet; 55 Source source = sourceMarshaler.asSource(message.getBody()); 56 normalizedMessage.setContent(source); 57 } 58 59 normalizedMessage.setProperty("org.apache.servicemix.jabber.packet", packet); 61 } 62 63 70 public void fromNMS(Message message, NormalizedMessage normalizedMessage) throws TransformerException { 71 String xml = messageAsString(normalizedMessage); 73 message.setBody(xml); 74 addJabberProperties(message, normalizedMessage); 75 } 76 77 82 public SourceMarshaler getSourceMarshaller() { 83 return sourceMarshaler; 84 } 85 86 89 public void setSourceMarshaller(SourceMarshaler sourceMarshaler) { 90 this.sourceMarshaler = sourceMarshaler; 91 } 92 93 96 public SourceMarshaler getSourceMarshaler() { 97 return sourceMarshaler; 98 } 99 100 103 public void setSourceMarshaler(SourceMarshaler sourceMarshaler) { 104 this.sourceMarshaler = sourceMarshaler; 105 } 106 107 110 113 protected String messageAsString(NormalizedMessage normalizedMessage) throws TransformerException { 114 return sourceMarshaler.asString(normalizedMessage.getContent()); 115 } 116 117 120 protected void addJabberProperties(Message message, NormalizedMessage normalizedMessage) { 121 for (Iterator iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) { 122 String name = (String ) iter.next(); 123 Object value = normalizedMessage.getProperty(name); 124 if (shouldIncludeHeader(normalizedMessage, name, value)) { 125 message.setProperty(name, value); 126 } 127 } 128 } 129 130 protected void addNmsProperties(NormalizedMessage normalizedMessage, Packet message) { 131 Iterator iter = message.getPropertyNames(); 132 while (iter.hasNext()) { 133 String name = (String ) iter.next(); 134 Object value = message.getProperty(name); 135 normalizedMessage.setProperty(name, value); 136 } 137 } 138 139 143 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 144 return value instanceof String || value instanceof Number || value instanceof Date ; 145 } 146 147 } 148 | Popular Tags |