KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jabber > JabberMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * Marshals Jabber messages into and out of NMS messages
32  *
33  * @version $Revision: 429277 $
34  */

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     /**
47      * Marshals the Jabber message into an NMS message
48      *
49      * @throws MessagingException
50      */

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 JavaDoc source = sourceMarshaler.asSource(message.getBody());
56             normalizedMessage.setContent(source);
57         }
58
59         // lets add the packet to the NMS
60
normalizedMessage.setProperty("org.apache.servicemix.jabber.packet", packet);
61     }
62
63     /**
64      * Marshals from the Jabber message to the normalized message
65      *
66      * @param message
67      * @param normalizedMessage
68      * @throws TransformerException
69      */

70     public void fromNMS(Message message, NormalizedMessage normalizedMessage) throws TransformerException JavaDoc {
71         // lets create a text message
72
String JavaDoc xml = messageAsString(normalizedMessage);
73         message.setBody(xml);
74         addJabberProperties(message, normalizedMessage);
75     }
76
77     // Properties
78
//-------------------------------------------------------------------------
79
/**
80      * @deprecated use getSourceMarshaler instead
81      */

82     public SourceMarshaler getSourceMarshaller() {
83         return sourceMarshaler;
84     }
85
86     /**
87      * @deprecated use setSourceMashaler instead
88      */

89     public void setSourceMarshaller(SourceMarshaler sourceMarshaler) {
90         this.sourceMarshaler = sourceMarshaler;
91     }
92
93     /**
94      * @return the sourceMarshaler
95      */

96     public SourceMarshaler getSourceMarshaler() {
97         return sourceMarshaler;
98     }
99
100     /**
101      * @param sourceMarshaler the sourceMarshaler to set
102      */

103     public void setSourceMarshaler(SourceMarshaler sourceMarshaler) {
104         this.sourceMarshaler = sourceMarshaler;
105     }
106
107     // Implementation methods
108
//-------------------------------------------------------------------------
109

110     /**
111      * Converts the inbound message to a String that can be sent
112      */

113     protected String JavaDoc messageAsString(NormalizedMessage normalizedMessage) throws TransformerException JavaDoc {
114         return sourceMarshaler.asString(normalizedMessage.getContent());
115     }
116
117     /**
118      * Appends properties on the NMS to the JMS Message
119      */

120     protected void addJabberProperties(Message message, NormalizedMessage normalizedMessage) {
121         for (Iterator JavaDoc iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) {
122             String JavaDoc name = (String JavaDoc) iter.next();
123             Object JavaDoc 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 JavaDoc iter = message.getPropertyNames();
132         while (iter.hasNext()) {
133             String JavaDoc name = (String JavaDoc) iter.next();
134             Object JavaDoc value = message.getProperty(name);
135             normalizedMessage.setProperty(name, value);
136         }
137     }
138
139     /**
140      * Decides whether or not the given header should be included in the JMS message.
141      * By default this includes all suitable typed values
142      */

143     protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String JavaDoc name, Object JavaDoc value) {
144         return value instanceof String JavaDoc || value instanceof Number JavaDoc || value instanceof Date JavaDoc;
145     }
146
147 }
148
Popular Tags