KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > util > MessageUtil


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.util;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.activation.DataHandler JavaDoc;
27 import javax.activation.DataSource JavaDoc;
28 import javax.jbi.messaging.Fault;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.NormalizedMessage;
32 import javax.security.auth.Subject JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34
35 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
36 import org.apache.servicemix.jbi.jaxp.StringSource;
37 import org.apache.servicemix.jbi.util.ByteArrayDataSource;
38 import org.apache.servicemix.jbi.util.FileUtil;
39
40 /**
41  * @author gnodet
42  * @version $Revision: 376451 $
43  */

44 public class MessageUtil {
45
46     public static void transfer(NormalizedMessage source, NormalizedMessage dest) throws Exception JavaDoc {
47         dest.setContent(source.getContent());
48         for (Iterator JavaDoc it = source.getPropertyNames().iterator(); it.hasNext();) {
49             String JavaDoc name = (String JavaDoc) it.next();
50             dest.setProperty(name, source.getProperty(name));
51         }
52         for (Iterator JavaDoc it = source.getAttachmentNames().iterator(); it.hasNext();) {
53             String JavaDoc name = (String JavaDoc) it.next();
54             dest.addAttachment(name, source.getAttachment(name));
55         }
56         dest.setSecuritySubject(source.getSecuritySubject());
57     }
58     
59     public static NormalizedMessage copy(NormalizedMessage source) throws Exception JavaDoc {
60         if (source instanceof Fault) {
61             return new FaultImpl((Fault) source);
62         } else {
63             return new NormalizedMessageImpl(source);
64         }
65     }
66     
67     public static NormalizedMessage copyIn(MessageExchange exchange) throws Exception JavaDoc {
68         return copy(exchange.getMessage("in"));
69     }
70     
71     public static NormalizedMessage copyOut(MessageExchange exchange) throws Exception JavaDoc {
72         return copy(exchange.getMessage("out"));
73     }
74     
75     public static Fault copyFault(MessageExchange exchange) throws Exception JavaDoc {
76         return (Fault) copy(exchange.getMessage("fault"));
77     }
78     
79     public static void transferInToIn(MessageExchange source, MessageExchange dest) throws Exception JavaDoc {
80         transferToIn(source.getMessage("in"), dest);
81     }
82     
83     public static void transferOutToIn(MessageExchange source, MessageExchange dest) throws Exception JavaDoc {
84         transferToIn(source.getMessage("out"), dest);
85     }
86     
87     public static void transferToIn(NormalizedMessage sourceMsg, MessageExchange dest) throws Exception JavaDoc {
88         transferTo(sourceMsg, dest, "in");
89     }
90     
91     public static void transferOutToOut(MessageExchange source, MessageExchange dest) throws Exception JavaDoc {
92         transferToOut(source.getMessage("out"), dest);
93     }
94     
95     public static void transferInToOut(MessageExchange source, MessageExchange dest) throws Exception JavaDoc {
96         transferToOut(source.getMessage("in"), dest);
97     }
98     
99     public static void transferToOut(NormalizedMessage sourceMsg, MessageExchange dest) throws Exception JavaDoc {
100         transferTo(sourceMsg, dest, "out");
101     }
102     
103     public static void transferFaultToFault(MessageExchange source, MessageExchange dest) throws Exception JavaDoc {
104         transferToFault(source.getFault(), dest);
105     }
106     
107     public static void transferToFault(Fault fault, MessageExchange dest) throws Exception JavaDoc {
108         transferTo(fault, dest, "fault");
109     }
110     
111     public static void transferTo(NormalizedMessage sourceMsg, MessageExchange dest, String JavaDoc name) throws Exception JavaDoc {
112         NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
113         transfer(sourceMsg, destMsg);
114         dest.setMessage(destMsg, name);
115     }
116     
117     public static void transferTo(MessageExchange source, MessageExchange dest, String JavaDoc name) throws Exception JavaDoc {
118         NormalizedMessage sourceMsg = source.getMessage(name);
119         NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
120         transfer(sourceMsg, destMsg);
121         dest.setMessage(destMsg, name);
122     }
123     
124     private static class NormalizedMessageImpl implements NormalizedMessage, Serializable JavaDoc {
125
126         private static final long serialVersionUID = -5813947566001096708L;
127         
128         private Subject JavaDoc subject;
129         private Source JavaDoc content;
130         private Map JavaDoc properties = new HashMap JavaDoc();
131         private Map JavaDoc attachments = new HashMap JavaDoc();
132         
133         public NormalizedMessageImpl(NormalizedMessage message) throws Exception JavaDoc {
134             String JavaDoc str = new SourceTransformer().contentToString(message);
135             if (str != null) {
136                 this.content = new StringSource(str);
137             }
138             for (Iterator JavaDoc it = message.getPropertyNames().iterator(); it.hasNext();) {
139                 String JavaDoc name = (String JavaDoc) it.next();
140                 this.properties.put(name, message.getProperty(name));
141             }
142             for (Iterator JavaDoc it = message.getAttachmentNames().iterator(); it.hasNext();) {
143                 String JavaDoc name = (String JavaDoc) it.next();
144                 DataHandler JavaDoc dh = message.getAttachment(name);
145                 DataSource JavaDoc ds = dh.getDataSource();
146                 if (ds instanceof ByteArrayDataSource == false) {
147                     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
148                     FileUtil.copyInputStream(ds.getInputStream(), baos);
149                     ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType());
150                     bads.setName(ds.getName());
151                     dh = new DataHandler JavaDoc(bads);
152                 }
153                 this.attachments.put(name, dh);
154             }
155             this.subject = message.getSecuritySubject();
156         }
157         
158         public void addAttachment(String JavaDoc id, DataHandler JavaDoc content) throws MessagingException {
159             this.attachments.put(id, content);
160         }
161
162         public Source JavaDoc getContent() {
163             return content;
164         }
165
166         public DataHandler JavaDoc getAttachment(String JavaDoc id) {
167             return (DataHandler JavaDoc) this.attachments.get(id);
168         }
169
170         public Set JavaDoc getAttachmentNames() {
171             return this.attachments.keySet();
172         }
173
174         public void removeAttachment(String JavaDoc id) throws MessagingException {
175             this.attachments.remove(id);
176         }
177
178         public void setContent(Source JavaDoc content) throws MessagingException {
179             this.content = content;
180         }
181
182         public void setProperty(String JavaDoc name, Object JavaDoc value) {
183             this.properties.put(name, value);
184         }
185
186         public void setSecuritySubject(Subject JavaDoc subject) {
187             this.subject = subject;
188         }
189
190         public Set JavaDoc getPropertyNames() {
191             return this.properties.keySet();
192         }
193
194         public Object JavaDoc getProperty(String JavaDoc name) {
195             return this.properties.get(name);
196         }
197
198         public Subject JavaDoc getSecuritySubject() {
199             return this.subject;
200         }
201         
202     }
203     
204     private static class FaultImpl extends NormalizedMessageImpl implements Fault {
205         private static final long serialVersionUID = -6076815664102825860L;
206
207         public FaultImpl(Fault fault) throws Exception JavaDoc {
208             super(fault);
209         }
210     }
211     
212 }
213
Popular Tags