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.bpmscript.jbi.component;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.Serializable;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import javax.activation.DataHandler;
027: import javax.activation.DataSource;
028: import javax.jbi.messaging.Fault;
029: import javax.jbi.messaging.MessageExchange;
030: import javax.jbi.messaging.MessagingException;
031: import javax.jbi.messaging.NormalizedMessage;
032: import javax.security.auth.Subject;
033: import javax.xml.transform.Source;
034:
035: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
036: import org.apache.servicemix.jbi.jaxp.StringSource;
037: import org.apache.servicemix.jbi.util.ByteArrayDataSource;
038: import org.apache.servicemix.jbi.util.FileUtil;
039:
040: /**
041: * @author gnodet
042: * @version $Revision: 1.2 $
043: */
044: public class MessageUtil {
045:
046: public static void transfer(NormalizedMessage source,
047: NormalizedMessage dest) throws Exception {
048: dest.setContent(source.getContent());
049: for (Iterator it = source.getPropertyNames().iterator(); it
050: .hasNext();) {
051: String name = (String) it.next();
052: dest.setProperty(name, source.getProperty(name));
053: }
054: for (Iterator it = source.getAttachmentNames().iterator(); it
055: .hasNext();) {
056: String name = (String) it.next();
057: dest.addAttachment(name, source.getAttachment(name));
058: }
059: dest.setSecuritySubject(source.getSecuritySubject());
060: }
061:
062: public static NormalizedMessage copy(NormalizedMessage source)
063: throws Exception {
064: if (source instanceof Fault) {
065: return new FaultImpl((Fault) source);
066: } else {
067: return new NormalizedMessageImpl(source);
068: }
069: }
070:
071: public static NormalizedMessage copyIn(MessageExchange exchange)
072: throws Exception {
073: return copy(exchange.getMessage("in"));
074: }
075:
076: public static NormalizedMessage copyOut(MessageExchange exchange)
077: throws Exception {
078: return copy(exchange.getMessage("out"));
079: }
080:
081: public static Fault copyFault(MessageExchange exchange)
082: throws Exception {
083: return (Fault) copy(exchange.getMessage("fault"));
084: }
085:
086: public static void transferInToIn(MessageExchange source,
087: MessageExchange dest) throws Exception {
088: transferToIn(source.getMessage("in"), dest);
089: }
090:
091: public static void transferOutToIn(MessageExchange source,
092: MessageExchange dest) throws Exception {
093: transferToIn(source.getMessage("out"), dest);
094: }
095:
096: public static void transferToIn(NormalizedMessage sourceMsg,
097: MessageExchange dest) throws Exception {
098: transferTo(sourceMsg, dest, "in");
099: }
100:
101: public static void transferOutToOut(MessageExchange source,
102: MessageExchange dest) throws Exception {
103: transferToOut(source.getMessage("out"), dest);
104: }
105:
106: public static void transferInToOut(MessageExchange source,
107: MessageExchange dest) throws Exception {
108: transferToOut(source.getMessage("in"), dest);
109: }
110:
111: public static void transferToOut(NormalizedMessage sourceMsg,
112: MessageExchange dest) throws Exception {
113: transferTo(sourceMsg, dest, "out");
114: }
115:
116: public static void transferFaultToFault(MessageExchange source,
117: MessageExchange dest) throws Exception {
118: transferToFault(source.getFault(), dest);
119: }
120:
121: public static void transferToFault(Fault fault, MessageExchange dest)
122: throws Exception {
123: transferTo(fault, dest, "fault");
124: }
125:
126: public static void transferTo(NormalizedMessage sourceMsg,
127: MessageExchange dest, String name) throws Exception {
128: NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest
129: .createFault() : dest.createMessage();
130: transfer(sourceMsg, destMsg);
131: dest.setMessage(destMsg, name);
132: }
133:
134: public static void transferTo(MessageExchange source,
135: MessageExchange dest, String name) throws Exception {
136: NormalizedMessage sourceMsg = source.getMessage(name);
137: NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest
138: .createFault() : dest.createMessage();
139: transfer(sourceMsg, destMsg);
140: dest.setMessage(destMsg, name);
141: }
142:
143: private static class NormalizedMessageImpl implements
144: NormalizedMessage, Serializable {
145:
146: private static final long serialVersionUID = -5813947566001096708L;
147:
148: private Subject subject;
149: private Source content;
150: private Map properties = new HashMap();
151: private Map attachments = new HashMap();
152:
153: public NormalizedMessageImpl(NormalizedMessage message)
154: throws Exception {
155: String str = new SourceTransformer()
156: .contentToString(message);
157: if (str != null) {
158: this .content = new StringSource(str);
159: }
160: for (Iterator it = message.getPropertyNames().iterator(); it
161: .hasNext();) {
162: String name = (String) it.next();
163: this .properties.put(name, message.getProperty(name));
164: }
165: for (Iterator it = message.getAttachmentNames().iterator(); it
166: .hasNext();) {
167: String name = (String) it.next();
168: DataHandler dh = message.getAttachment(name);
169: DataSource ds = dh.getDataSource();
170: if (ds instanceof ByteArrayDataSource == false) {
171: ByteArrayOutputStream baos = new ByteArrayOutputStream();
172: FileUtil.copyInputStream(ds.getInputStream(), baos);
173: ByteArrayDataSource bads = new ByteArrayDataSource(
174: baos.toByteArray(), ds.getContentType());
175: bads.setName(ds.getName());
176: dh = new DataHandler(bads);
177: }
178: this .attachments.put(name, dh);
179: }
180: this .subject = message.getSecuritySubject();
181: }
182:
183: public void addAttachment(String id, DataHandler content)
184: throws MessagingException {
185: this .attachments.put(id, content);
186: }
187:
188: public Source getContent() {
189: return content;
190: }
191:
192: public DataHandler getAttachment(String id) {
193: return (DataHandler) this .attachments.get(id);
194: }
195:
196: public Set getAttachmentNames() {
197: return this .attachments.keySet();
198: }
199:
200: public void removeAttachment(String id)
201: throws MessagingException {
202: this .attachments.remove(id);
203: }
204:
205: public void setContent(Source content)
206: throws MessagingException {
207: this .content = content;
208: }
209:
210: public void setProperty(String name, Object value) {
211: this .properties.put(name, value);
212: }
213:
214: public void setSecuritySubject(Subject subject) {
215: this .subject = subject;
216: }
217:
218: public Set getPropertyNames() {
219: return this .properties.keySet();
220: }
221:
222: public Object getProperty(String name) {
223: return this .properties.get(name);
224: }
225:
226: public Subject getSecuritySubject() {
227: return this .subject;
228: }
229:
230: }
231:
232: private static class FaultImpl extends NormalizedMessageImpl
233: implements Fault {
234: private static final long serialVersionUID = -6076815664102825860L;
235:
236: public FaultImpl(Fault fault) throws Exception {
237: super(fault);
238: }
239: }
240:
241: }
|