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