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.jbi.util;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.Serializable;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import javax.activation.DataHandler;
028: import javax.activation.DataSource;
029: import javax.jbi.messaging.Fault;
030: import javax.jbi.messaging.MessageExchange;
031: import javax.jbi.messaging.MessagingException;
032: import javax.jbi.messaging.NormalizedMessage;
033: import javax.security.auth.Subject;
034: import javax.xml.parsers.ParserConfigurationException;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.TransformerException;
037: import javax.xml.transform.stream.StreamSource;
038:
039: import org.xml.sax.SAXException;
040:
041: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
042: import org.apache.servicemix.jbi.jaxp.StringSource;
043:
044: /**
045: * @author gnodet
046: * @version $Revision: 376451 $
047: */
048: public final class MessageUtil {
049:
050: private MessageUtil() {
051: }
052:
053: public static void transfer(NormalizedMessage source,
054: NormalizedMessage dest) throws MessagingException {
055: dest.setContent(source.getContent());
056: for (Iterator it = source.getPropertyNames().iterator(); it
057: .hasNext();) {
058: String name = (String) it.next();
059: dest.setProperty(name, source.getProperty(name));
060: }
061: for (Iterator it = source.getAttachmentNames().iterator(); it
062: .hasNext();) {
063: String name = (String) it.next();
064: dest.addAttachment(name, source.getAttachment(name));
065: }
066: dest.setSecuritySubject(source.getSecuritySubject());
067: }
068:
069: public static NormalizedMessage copy(NormalizedMessage source)
070: throws MessagingException {
071: if (source instanceof Fault) {
072: return new FaultImpl((Fault) source);
073: } else {
074: return new NormalizedMessageImpl(source);
075: }
076: }
077:
078: public static NormalizedMessage copyIn(MessageExchange exchange)
079: throws MessagingException {
080: return copy(exchange.getMessage("in"));
081: }
082:
083: public static NormalizedMessage copyOut(MessageExchange exchange)
084: throws MessagingException {
085: return copy(exchange.getMessage("out"));
086: }
087:
088: public static Fault copyFault(MessageExchange exchange)
089: throws MessagingException {
090: return (Fault) copy(exchange.getMessage("fault"));
091: }
092:
093: public static void transferInToIn(MessageExchange source,
094: MessageExchange dest) throws MessagingException {
095: transferToIn(source.getMessage("in"), dest);
096: }
097:
098: public static void transferOutToIn(MessageExchange source,
099: MessageExchange dest) throws MessagingException {
100: transferToIn(source.getMessage("out"), dest);
101: }
102:
103: public static void transferToIn(NormalizedMessage sourceMsg,
104: MessageExchange dest) throws MessagingException {
105: transferTo(sourceMsg, dest, "in");
106: }
107:
108: public static void transferOutToOut(MessageExchange source,
109: MessageExchange dest) throws MessagingException {
110: transferToOut(source.getMessage("out"), dest);
111: }
112:
113: public static void transferInToOut(MessageExchange source,
114: MessageExchange dest) throws MessagingException {
115: transferToOut(source.getMessage("in"), dest);
116: }
117:
118: public static void transferToOut(NormalizedMessage sourceMsg,
119: MessageExchange dest) throws MessagingException {
120: transferTo(sourceMsg, dest, "out");
121: }
122:
123: public static void transferFaultToFault(MessageExchange source,
124: MessageExchange dest) throws MessagingException {
125: transferToFault(source.getFault(), dest);
126: }
127:
128: public static void transferToFault(Fault fault, MessageExchange dest)
129: throws MessagingException {
130: transferTo(fault, dest, "fault");
131: }
132:
133: public static void transferTo(NormalizedMessage sourceMsg,
134: MessageExchange dest, String name)
135: throws MessagingException {
136: NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest
137: .createFault() : dest.createMessage();
138: transfer(sourceMsg, destMsg);
139: dest.setMessage(destMsg, name);
140: }
141:
142: public static void transferTo(MessageExchange source,
143: MessageExchange dest, String name)
144: throws MessagingException {
145: NormalizedMessage sourceMsg = source.getMessage(name);
146: NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest
147: .createFault() : dest.createMessage();
148: transfer(sourceMsg, destMsg);
149: dest.setMessage(destMsg, name);
150: }
151:
152: /**
153: * Convert the given {@link NormalizedMessage} instance's content to a re-readable {@link Source} This allows the
154: * content to be read more than once (e.g. for XPath evaluation or auditing).
155: *
156: * @param message
157: * the {@link NormalizedMessage} to convert the content for
158: * @throws MessagingException
159: */
160: public static void enableContentRereadability(
161: NormalizedMessage message) throws MessagingException {
162: if (message.getContent() instanceof StreamSource) {
163: try {
164: String content = new SourceTransformer()
165: .contentToString(message);
166: if (content != null) {
167: message.setContent(new StringSource(content));
168: }
169: } catch (TransformerException e) {
170: throw new MessagingException(
171: "Unable to convert message content into StringSource",
172: e);
173: } catch (ParserConfigurationException e) {
174: throw new MessagingException(
175: "Unable to convert message content into StringSource",
176: e);
177: } catch (IOException e) {
178: throw new MessagingException(
179: "Unable to convert message content into StringSource",
180: e);
181: } catch (SAXException e) {
182: throw new MessagingException(
183: "Unable to convert message content into StringSource",
184: e);
185: }
186: }
187: }
188:
189: public static class NormalizedMessageImpl implements
190: NormalizedMessage, Serializable {
191:
192: private static final long serialVersionUID = -5813947566001096708L;
193:
194: private Subject subject;
195: private Source content;
196: private Map properties = new HashMap();
197: private Map attachments = new HashMap();
198:
199: public NormalizedMessageImpl() {
200: }
201:
202: public NormalizedMessageImpl(NormalizedMessage message)
203: throws MessagingException {
204: try {
205: String str = new SourceTransformer()
206: .contentToString(message);
207: if (str != null) {
208: this .content = new StringSource(str);
209: }
210: for (Iterator it = message.getPropertyNames()
211: .iterator(); it.hasNext();) {
212: String name = (String) it.next();
213: this .properties
214: .put(name, message.getProperty(name));
215: }
216: for (Iterator it = message.getAttachmentNames()
217: .iterator(); it.hasNext();) {
218: String name = (String) it.next();
219: DataHandler dh = message.getAttachment(name);
220: DataSource ds = dh.getDataSource();
221: if (!(ds instanceof ByteArrayDataSource)) {
222: ByteArrayOutputStream baos = new ByteArrayOutputStream();
223: FileUtil.copyInputStream(ds.getInputStream(),
224: baos);
225: ByteArrayDataSource bads = new ByteArrayDataSource(
226: baos.toByteArray(), ds.getContentType());
227: bads.setName(ds.getName());
228: dh = new DataHandler(bads);
229: }
230: this .attachments.put(name, dh);
231: }
232: this .subject = message.getSecuritySubject();
233: } catch (MessagingException e) {
234: throw e;
235: } catch (Exception e) {
236: throw new MessagingException(e);
237: }
238: }
239:
240: public void addAttachment(String id, DataHandler data)
241: throws MessagingException {
242: this .attachments.put(id, data);
243: }
244:
245: public Source getContent() {
246: return content;
247: }
248:
249: public DataHandler getAttachment(String id) {
250: return (DataHandler) this .attachments.get(id);
251: }
252:
253: public Set getAttachmentNames() {
254: return this .attachments.keySet();
255: }
256:
257: public void removeAttachment(String id)
258: throws MessagingException {
259: this .attachments.remove(id);
260: }
261:
262: public void setContent(Source content)
263: throws MessagingException {
264: this .content = content;
265: }
266:
267: public void setProperty(String name, Object value) {
268: this .properties.put(name, value);
269: }
270:
271: public void setSecuritySubject(Subject sub) {
272: this .subject = sub;
273: }
274:
275: public Set getPropertyNames() {
276: return this .properties.keySet();
277: }
278:
279: public Object getProperty(String name) {
280: return this .properties.get(name);
281: }
282:
283: public Subject getSecuritySubject() {
284: return this .subject;
285: }
286:
287: }
288:
289: public static class FaultImpl extends NormalizedMessageImpl
290: implements Fault {
291: private static final long serialVersionUID = -6076815664102825860L;
292:
293: public FaultImpl() {
294: }
295:
296: public FaultImpl(Fault fault) throws MessagingException {
297: super(fault);
298: }
299: }
300:
301: }
|