001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: MessageExchangeLogger.java 10894 2007-12-12 22:11:32Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.util.Iterator;
030:
031: import javax.activation.DataHandler;
032: import javax.activation.DataSource;
033: import javax.jbi.messaging.Fault;
034: import javax.jbi.messaging.InOptionalOut;
035: import javax.jbi.messaging.InOut;
036: import javax.jbi.messaging.MessageExchange;
037: import javax.jbi.messaging.MessagingException;
038: import javax.jbi.messaging.NormalizedMessage;
039: import javax.jbi.messaging.RobustInOnly;
040: import javax.jbi.messaging.MessageExchange.Role;
041: import javax.xml.transform.Source;
042: import javax.xml.transform.Transformer;
043: import javax.xml.transform.TransformerFactory;
044: import javax.xml.transform.dom.DOMResult;
045: import javax.xml.transform.dom.DOMSource;
046: import javax.xml.transform.stream.StreamResult;
047: import javax.xml.transform.stream.StreamSource;
048:
049: import org.apache.commons.logging.Log;
050:
051: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArraySource;
052: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.DOMDataSource;
053: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.DataEnvelopeSource;
054: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringDataSource;
055: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
056:
057: public class MessageExchangeLogger {
058:
059: public static void outputExchange(Log log, MessageExchange exchange)
060: throws MessagingException {
061: StringBuffer buffer = new StringBuffer();
062: buffer.append("\nMessageExchange Id: "
063: + exchange.getExchangeId() + "\n");
064: buffer.append("Status: " + exchange.getStatus() + "\n");
065: if (Role.CONSUMER.equals(exchange.getRole()))
066: buffer.append("Role: Consumer\n");
067: else if (Role.PROVIDER.equals(exchange.getRole()))
068: buffer.append("Role: Provider\n");
069: buffer.append("MEP: " + exchange.getPattern() + "\n");
070: buffer.append("Interface: " + exchange.getInterfaceName()
071: + "\n");
072: buffer.append("Service: " + exchange.getService() + "\n");
073: String endpointName = null;
074: if (exchange.getEndpoint() != null) {
075: endpointName = exchange.getEndpoint().getEndpointName();
076: }
077: buffer.append("Endpoint: " + endpointName + "\n");
078: outputExchangeProperties(buffer, exchange);
079:
080: buffer.append("\n-=-=-=-=-= In Message =-=-=-=-=-\n");
081: NormalizedMessage inMsg = exchange.getMessage("in");
082: if (inMsg != null) {
083: outputMessage(buffer, inMsg);
084: } else {
085: buffer.append("NULL\n");
086: }
087:
088: if (exchange instanceof InOut
089: || exchange instanceof InOptionalOut) {
090: buffer.append("\n-=-=-=-=-= Out Message =-=-=-=-=-\n");
091: NormalizedMessage outMsg = exchange.getMessage("out");
092: if (outMsg != null) {
093: outputMessage(buffer, outMsg);
094: } else {
095: buffer.append("NULL\n");
096: }
097: }
098:
099: if (exchange instanceof RobustInOnly
100: || exchange instanceof InOut
101: || exchange instanceof InOptionalOut) {
102: Fault fault = exchange.getFault();
103: if (fault != null) {
104: buffer.append("\n-=-=-=-=-= Fault =-=-=-=-=-\n");
105: Source content = fault.getContent();
106: if (content != null) {
107: outputSourceAsXML(buffer, content);
108: } else {
109: buffer.append("Content is NULL\n");
110: }
111: }
112: }
113: log.debug(buffer);
114: }
115:
116: private static void outputExchangeProperties(StringBuffer buffer,
117: MessageExchange exchange) {
118: Iterator iter = exchange.getPropertyNames().iterator();
119: buffer.append("Exchange Properties:\n");
120: while (iter.hasNext()) {
121: String propName = (String) iter.next();
122: Object propValue = exchange.getProperty(propName);
123: buffer.append("\t" + propName + " = "
124: + propValue.toString() + "\n");
125: }
126: }
127:
128: private static void outputMessage(StringBuffer buffer,
129: NormalizedMessage message) throws MessagingException {
130: Source content = message.getContent();
131: buffer.append("Content: \n");
132: if (content != null) {
133: //Check if the content is a Source implementation
134: //that may be consumed multiple times.
135: //If its not, then assume it may only be consumed once and replace it
136: //with a DOMSource
137: if (isSourceConsumed(content)) {
138: DOMSource newSrc = convertToDOMSource(content);
139: if (newSrc != null) {
140: content = newSrc;
141: message.setContent(content);
142: }
143: }
144: outputSourceAsXML(buffer, content);
145: } else {
146: buffer.append("NULL");
147: }
148:
149: Iterator iter = message.getAttachmentNames().iterator();
150: while (iter.hasNext()) {
151: String attName = (String) iter.next();
152: buffer.append("Attachment Id: " + attName + "\n");
153: DataHandler dh = message.getAttachment(attName);
154: DataSource ds = dh.getDataSource();
155: buffer
156: .append("Content-Type: " + ds.getContentType()
157: + "\n");
158: outputAttachment(buffer, ds);
159: buffer.append("\n");
160: }
161: }
162:
163: private static boolean isSourceConsumed(Source src) {
164: if (src instanceof DOMSource || src instanceof StringSource
165: || src instanceof ByteArraySource
166: || src instanceof DataEnvelopeSource) {
167: return false;
168: }
169: return true;
170: }
171:
172: private static DOMSource convertToDOMSource(Source source) {
173: try {
174: DOMResult result = new DOMResult();
175: TransformerFactory tf = TransformerFactory.newInstance();
176: Transformer transformer = tf.newTransformer();
177: transformer.transform(source, result);
178: return new DOMSource(result.getNode());
179: } catch (Exception e) {
180: return null;
181: }
182: }
183:
184: private static void outputSourceAsXML(StringBuffer buffer,
185: Source source) {
186: try {
187: ByteArrayOutputStream baos = new ByteArrayOutputStream();
188: StreamResult result = new StreamResult(baos);
189: TransformerFactory tf = TransformerFactory.newInstance();
190: Transformer transformer = tf.newTransformer();
191: transformer.setOutputProperty("indent", "yes");
192: transformer.transform(source, result);
193: buffer.append(baos.toString() + "\n");
194: } catch (Exception e) {
195: buffer
196: .append("Unable to output DOMSource due to exception: "
197: + e.getMessage());
198: }
199: }
200:
201: private static void outputAttachment(StringBuffer buffer,
202: DataSource ds) {
203: //DataSource objects must be able to return a new
204: //InputStream multiple times, so we don't have to
205: //worry about consuming it now.
206: if (ds instanceof DOMDataSource) {
207: DOMSource src = ((DOMDataSource) ds).getSrc();
208: outputSourceAsXML(buffer, src);
209: } else if (ds instanceof StringDataSource) {
210: String data = ((StringDataSource) ds).getData();
211: buffer.append(data + "\n");
212: } else {
213: //Try checking the content type
214: String contentType = ds.getContentType();
215: if (contentType != null) {
216: if (contentType.equals("text/xml")) {
217: outputDataSourceAsXML(buffer, ds);
218: } else if (contentType.startsWith("text")) {
219: outputDataSourceAsString(buffer, ds);
220: } else {
221: outputDataSourceAsHex(buffer, ds);
222: }
223: } else {
224: outputDataSourceAsHex(buffer, ds);
225: }
226: }
227: }
228:
229: private static void outputDataSourceAsXML(StringBuffer buffer,
230: DataSource ds) {
231: try {
232: StreamSource source = new StreamSource(ds.getInputStream());
233: outputSourceAsXML(buffer, source);
234: } catch (Exception e) {
235: buffer
236: .append("Unable to output Attachment due to exception: "
237: + e.getMessage());
238: }
239: }
240:
241: private static void outputDataSourceAsString(StringBuffer buffer,
242: DataSource ds) {
243: try {
244: //Not sure how to find out what charset to use?
245: //So we will use the default and hope for the best
246: InputStream is = ds.getInputStream();
247: InputStreamReader reader = new InputStreamReader(is);
248: int c = reader.read();
249: while (c > -1) {
250: buffer.append((char) c);
251: c = reader.read();
252: }
253: } catch (Exception e) {
254: buffer
255: .append("Unable to output Attachment due to exception: "
256: + e.getMessage());
257: }
258: }
259:
260: private static void outputDataSourceAsHex(StringBuffer buffer,
261: DataSource ds) {
262: try {
263: int charsPerLine = 16;
264: StringBuffer left = null;
265: StringBuffer right = null;
266: int position = 0;
267:
268: InputStream is = ds.getInputStream();
269: int b = is.read();
270: while (b > -1) {
271: if (left == null)
272: left = new StringBuffer();
273: if (right == null)
274: right = new StringBuffer();
275: left.append((b < 32) ? '.' : (char) b);
276: right.append(String.format("%2X ", b));
277: if (++position == charsPerLine) {
278: position = 0;
279: buffer.append(" " + left + " - " + right + "\n");
280: left = null;
281: right = null;
282: }
283:
284: b = is.read();
285: }
286: if (left != null) {
287: while (left.length() < charsPerLine)
288: left.append(" ");
289: buffer.append(" " + left + " - " + right + "\n");
290: }
291: } catch (Exception e) {
292: buffer
293: .append("Unable to output Attachment due to exception: "
294: + e.getMessage());
295: }
296: }
297:
298: }
|