001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.jabber;
018:
019: import org.apache.servicemix.jbi.jaxp.SourceMarshaler;
020: import org.jivesoftware.smack.packet.Message;
021: import org.jivesoftware.smack.packet.Packet;
022:
023: import javax.jbi.messaging.MessagingException;
024: import javax.jbi.messaging.NormalizedMessage;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.TransformerException;
027: import java.util.Date;
028: import java.util.Iterator;
029:
030: /**
031: * Marshals Jabber messages into and out of NMS messages
032: *
033: * @version $Revision: 429277 $
034: */
035: public class JabberMarshaler {
036: private SourceMarshaler sourceMarshaler;
037:
038: public JabberMarshaler() {
039: this (new SourceMarshaler());
040: }
041:
042: public JabberMarshaler(SourceMarshaler sourceMarshaler) {
043: this .sourceMarshaler = sourceMarshaler;
044: }
045:
046: /**
047: * Marshals the Jabber message into an NMS message
048: *
049: * @throws MessagingException
050: */
051: public void toNMS(NormalizedMessage normalizedMessage, Packet packet)
052: throws MessagingException {
053: addNmsProperties(normalizedMessage, packet);
054: if (packet instanceof Message) {
055: Message message = (Message) packet;
056: Source source = sourceMarshaler.asSource(message.getBody());
057: normalizedMessage.setContent(source);
058: }
059:
060: // lets add the packet to the NMS
061: normalizedMessage.setProperty(
062: "org.apache.servicemix.jabber.packet", packet);
063: }
064:
065: /**
066: * Marshals from the Jabber message to the normalized message
067: *
068: * @param message
069: * @param normalizedMessage
070: * @throws TransformerException
071: */
072: public void fromNMS(Message message,
073: NormalizedMessage normalizedMessage)
074: throws TransformerException {
075: // lets create a text message
076: String xml = messageAsString(normalizedMessage);
077: message.setBody(xml);
078: addJabberProperties(message, normalizedMessage);
079: }
080:
081: // Properties
082: //-------------------------------------------------------------------------
083: /**
084: * @deprecated use getSourceMarshaler instead
085: */
086: public SourceMarshaler getSourceMarshaller() {
087: return sourceMarshaler;
088: }
089:
090: /**
091: * @deprecated use setSourceMashaler instead
092: */
093: public void setSourceMarshaller(SourceMarshaler sourceMarshaler) {
094: this .sourceMarshaler = sourceMarshaler;
095: }
096:
097: /**
098: * @return the sourceMarshaler
099: */
100: public SourceMarshaler getSourceMarshaler() {
101: return sourceMarshaler;
102: }
103:
104: /**
105: * @param sourceMarshaler the sourceMarshaler to set
106: */
107: public void setSourceMarshaler(SourceMarshaler sourceMarshaler) {
108: this .sourceMarshaler = sourceMarshaler;
109: }
110:
111: // Implementation methods
112: //-------------------------------------------------------------------------
113:
114: /**
115: * Converts the inbound message to a String that can be sent
116: */
117: protected String messageAsString(NormalizedMessage normalizedMessage)
118: throws TransformerException {
119: return sourceMarshaler.asString(normalizedMessage.getContent());
120: }
121:
122: /**
123: * Appends properties on the NMS to the JMS Message
124: */
125: protected void addJabberProperties(Message message,
126: NormalizedMessage normalizedMessage) {
127: for (Iterator iter = normalizedMessage.getPropertyNames()
128: .iterator(); iter.hasNext();) {
129: String name = (String) iter.next();
130: Object value = normalizedMessage.getProperty(name);
131: if (shouldIncludeHeader(normalizedMessage, name, value)) {
132: message.setProperty(name, value);
133: }
134: }
135: }
136:
137: protected void addNmsProperties(
138: NormalizedMessage normalizedMessage, Packet message) {
139: Iterator iter = message.getPropertyNames();
140: while (iter.hasNext()) {
141: String name = (String) iter.next();
142: Object value = message.getProperty(name);
143: normalizedMessage.setProperty(name, value);
144: }
145: }
146:
147: /**
148: * Decides whether or not the given header should be included in the JMS message.
149: * By default this includes all suitable typed values
150: */
151: protected boolean shouldIncludeHeader(
152: NormalizedMessage normalizedMessage, String name,
153: Object value) {
154: return value instanceof String || value instanceof Number
155: || value instanceof Date;
156: }
157:
158: }
|