KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > NormalizedMessageImpl


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.jbi.messaging;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.Externalizable JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInput JavaDoc;
23 import java.io.ObjectOutput JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import javax.activation.DataHandler JavaDoc;
31 import javax.activation.DataSource JavaDoc;
32 import javax.jbi.messaging.Fault;
33 import javax.jbi.messaging.MessageExchange;
34 import javax.jbi.messaging.MessagingException;
35 import javax.jbi.messaging.NormalizedMessage;
36 import javax.security.auth.Subject JavaDoc;
37 import javax.xml.transform.Source JavaDoc;
38 import javax.xml.transform.TransformerException JavaDoc;
39 import javax.xml.transform.sax.SAXSource JavaDoc;
40 import javax.xml.transform.stream.StreamSource JavaDoc;
41
42 import org.apache.servicemix.client.Message;
43 import org.apache.servicemix.jbi.RuntimeJBIException;
44 import org.apache.servicemix.jbi.jaxp.BytesSource;
45 import org.apache.servicemix.jbi.jaxp.ResourceSource;
46 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
47 import org.apache.servicemix.jbi.jaxp.StringSource;
48 import org.apache.servicemix.jbi.util.ByteArrayDataSource;
49 import org.apache.servicemix.jbi.util.FileUtil;
50
51 /**
52  * Represents a JBI NormalizedMessage.
53  *
54  * @version $Revision: 426415 $
55  */

56 public class NormalizedMessageImpl implements NormalizedMessage, Externalizable JavaDoc, Message {
57     
58     private static final long serialVersionUID = 9179194301410526549L;
59     
60     protected transient MessageExchangeImpl exchange;
61     private transient Source JavaDoc content;
62     private transient Object JavaDoc body;
63     private Subject JavaDoc securitySubject;
64     private Map JavaDoc properties;
65     private Map JavaDoc attachments;
66
67     private static SourceTransformer transformer = new SourceTransformer();
68
69     /**
70      * Constructor
71      *
72      */

73     public NormalizedMessageImpl() {
74     }
75
76
77     /**
78      * Constructor
79      * @param exchange
80      */

81     public NormalizedMessageImpl(MessageExchangeImpl exchange) {
82         this.exchange = exchange;
83     }
84
85     
86
87
88     /**
89      * @return the content of the message
90      */

91     public Source JavaDoc getContent() {
92         if (content == null && body != null) {
93             try {
94                 getMarshaler().marshal(exchange, this, body);
95             }
96             catch (MessagingException e) {
97                 throw new RuntimeJBIException(e);
98             }
99         }
100         return content;
101     }
102
103     /**
104      * set the content fo the message
105      *
106      * @param source
107      */

108     public void setContent(Source JavaDoc source) {
109         this.content = source;
110     }
111
112     /**
113      * @return the security subject from the message
114      */

115     public Subject JavaDoc getSecuritySubject() {
116         return securitySubject;
117     }
118
119     /**
120      * set the security subject
121      *
122      * @param securitySubject
123      */

124     public void setSecuritySubject(Subject JavaDoc securitySubject) {
125         this.securitySubject = securitySubject;
126     }
127
128     /**
129      * get a named property
130      *
131      * @param name
132      * @return a property from the message
133      */

134     public Object JavaDoc getProperty(String JavaDoc name) {
135         if (properties != null) {
136             return properties.get(name);
137         }
138         return null;
139     }
140
141     /**
142      * @return an iterator of property names
143      */

144     public Set JavaDoc getPropertyNames() {
145         if (properties != null) {
146             return Collections.unmodifiableSet(properties.keySet());
147         }
148         return Collections.EMPTY_SET;
149     }
150
151     /**
152      * set a property
153      *
154      * @param name
155      * @param value
156      */

157     public void setProperty(String JavaDoc name, Object JavaDoc value) {
158         if (value == null) {
159             if (properties != null) {
160                 properties.remove(name);
161             }
162         } else {
163             getProperties().put(name, value);
164         }
165     }
166
167     /**
168      * Add an attachment
169      *
170      * @param id
171      * @param content
172      */

173     public void addAttachment(String JavaDoc id, DataHandler JavaDoc content) {
174         getAttachments().put(id, content.getDataSource());
175     }
176
177     /**
178      * Get a named attachement
179      *
180      * @param id
181      * @return the specified attachment
182      */

183     public DataHandler JavaDoc getAttachment(String JavaDoc id) {
184         if (attachments != null) {
185             return new DataHandler JavaDoc((DataSource JavaDoc) attachments.get(id));
186         }
187         return null;
188     }
189
190     /**
191      * @return a list of identifiers for atachments
192      */

193     public Iterator JavaDoc listAttachments() {
194         if (attachments != null) {
195             return attachments.keySet().iterator();
196         }
197         return Collections.EMPTY_LIST.iterator();
198     }
199
200     /**
201      * remove an identified attachment
202      *
203      * @param id
204      */

205     public void removeAttachment(String JavaDoc id) {
206         if (attachments != null) {
207             attachments.remove(id);
208         }
209     }
210     
211     /** Returns a list of identifiers for each attachment to the message.
212      * @return iterator over String attachment identifiers
213      */

214     public Set JavaDoc getAttachmentNames(){
215         if (attachments != null){
216             return Collections.unmodifiableSet(attachments.keySet());
217         }
218         return Collections.EMPTY_SET;
219     }
220
221
222     public String JavaDoc toString() {
223         return super.toString() + "{properties: " + getProperties() + "}";
224     }
225     
226     // Scripting helper methods to add expressive power
227
// when using languages like Groovy, Velocity etc
228
//-------------------------------------------------------------------------
229

230     public Object JavaDoc getBody() throws MessagingException {
231         if (body == null) {
232             body = getMarshaler().unmarshal(exchange, this);
233         }
234         return body;
235     }
236
237     public void setBody(Object JavaDoc body) throws MessagingException {
238         this.body = body;
239     }
240
241     public String JavaDoc getBodyText() throws TransformerException JavaDoc {
242         return transformer.toString(getContent());
243     }
244
245     public void setBodyText(String JavaDoc xml) {
246         setContent(new StringSource(xml));
247     }
248
249     public PojoMarshaler getMarshaler() {
250         return exchange.getMarshaler();
251     }
252
253     public MessageExchange getExchange() {
254         return exchange;
255     }
256
257     public Fault createFault() throws MessagingException {
258         return getExchange().createFault();
259     }
260
261
262     // Implementation methods
263
//-------------------------------------------------------------------------
264
protected Map JavaDoc getProperties() {
265         if (properties == null) {
266             properties = createPropertiesMap();
267         }
268         return properties;
269     }
270
271     protected Map JavaDoc getAttachments() {
272         if (attachments == null) {
273             attachments = createAttachmentsMap();
274         }
275         return attachments;
276     }
277
278     protected void setAttachments(Map JavaDoc attachments) {
279         this.attachments = attachments;
280     }
281
282     protected void setProperties(Map JavaDoc properties) {
283         this.properties = properties;
284     }
285
286     protected Map JavaDoc createPropertiesMap() {
287         // Normalized exchanges do not need to be thread-safe
288
return new HashMap JavaDoc();
289     }
290
291     protected Map JavaDoc createAttachmentsMap() {
292         // Normalized exchanges do not need to be thread-safe
293
return new HashMap JavaDoc();
294     }
295
296     /**
297      * Write to a Stream
298      * @param out
299      * @throws IOException
300      */

301     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
302         try {
303             convertAttachments();
304             out.writeObject(attachments);
305             out.writeObject(properties);
306             String JavaDoc src = transformer.toString(content);
307             out.writeObject(src);
308             // We have read the source
309
// so now, ensure that it can be re-read
310
if ((content instanceof StreamSource JavaDoc ||
311                     content instanceof SAXSource JavaDoc) &&
312                     !(content instanceof StringSource) &&
313                     !(content instanceof BytesSource) &&
314                     !(content instanceof ResourceSource)) {
315                 content = new StringSource(src);
316             }
317         } catch (TransformerException JavaDoc e) {
318             throw (IOException JavaDoc) new IOException JavaDoc("Could not transform content to string").initCause(e);
319         }
320     }
321
322     private void convertAttachments() throws IOException JavaDoc {
323         if (attachments != null) {
324             Map JavaDoc newAttachments = createAttachmentsMap();
325             for (Iterator JavaDoc it = attachments.keySet().iterator(); it.hasNext();) {
326                 String JavaDoc name = (String JavaDoc) it.next();
327                 DataSource JavaDoc ds = (DataSource JavaDoc) attachments.get(name);
328                 if (ds instanceof ByteArrayDataSource) {
329                     newAttachments.put(name, ds);
330                 } else {
331                     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
332                     FileUtil.copyInputStream(ds.getInputStream(), baos);
333                     ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType());
334                     bads.setName(ds.getName());
335                     newAttachments.put(name, bads);
336                 }
337             }
338             attachments = newAttachments;
339         }
340     }
341
342     /**
343      * Read from a stream
344      *
345      * @param in
346      * @throws IOException
347      * @throws ClassNotFoundException
348      */

349     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
350         attachments = (Map JavaDoc) in.readObject();
351         properties = (Map JavaDoc) in.readObject();
352         String JavaDoc src = (String JavaDoc) in.readObject();
353         if (src != null) {
354             content = new StringSource(src);
355         }
356     }
357
358 }
359
360
Popular Tags