001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 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: DumpNormalizedMessage.java 7972 2007-06-17 07:58:47Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.StringWriter;
028: import java.util.Iterator;
029: import java.util.Set;
030:
031: import javax.activation.DataHandler;
032: import javax.activation.DataSource;
033: import javax.jbi.messaging.NormalizedMessage;
034: import javax.xml.transform.Source;
035: import javax.xml.transform.Transformer;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.dom.DOMResult;
038: import javax.xml.transform.dom.DOMSource;
039: import javax.xml.transform.stream.StreamResult;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringDataSource;
045:
046: public class DumpNormalizedMessage {
047:
048: public static String dump(NormalizedMessage m) {
049: return dumpProcessor(m, false);
050: }
051:
052: public static String dumpUnicode(NormalizedMessage m) {
053: return dumpProcessor(m, true);
054: }
055:
056: protected static transient Log logger = LogFactory
057: .getLog(DumpNormalizedMessage.class);
058:
059: public static String dumpProcessor(NormalizedMessage m,
060: boolean showUnicode) {
061: String s = "";
062:
063: if (m == null)
064: return "NormalizedMessage is null";
065:
066: /*
067: * try to make the content readable
068: */
069: Source content = m.getContent();
070: if (content == null)
071: s = s.concat("NormalizedMessage content is null\n");
072: else {
073: s = s.concat("CONTENT=" + content.toString() + "\n");
074:
075: /*
076: * If the content is StreamSource then we convert it to DOMSource to have
077: * an in-memory copy
078: */
079: if (!(content instanceof DOMSource)) {
080: try {
081: DOMResult dr = new DOMResult();
082: TransformerFactory tf = TransformerFactory
083: .newInstance();
084: Transformer t = tf.newTransformer();
085: t.transform(content, dr);
086: DOMSource ds = new DOMSource(dr.getNode());
087: m.setContent(ds);
088: content = m.getContent();
089: } catch (Exception e) {
090: logger.error("Exception in dumpProcessor(): "
091: + e.getMessage());
092: if (logger.isDebugEnabled()) {
093: logger
094: .debug("Exception in dumpProcessor():",
095: e);
096: }
097: s = s
098: .concat("Exception converting content to DOMSource: "
099: + e
100: + "\n"
101: + ExceptionUtil.stackTraceString(e)
102: + "\n");
103: }
104: }
105:
106: if (content instanceof DOMSource) {
107: try {
108: StringWriter w = new StringWriter();
109: StreamResult result = new StreamResult(w);
110: TransformerFactory tf = TransformerFactory
111: .newInstance();
112: Transformer t = tf.newTransformer();
113: t.setOutputProperty("indent", "yes");
114: t.transform(content, result);
115: if (showUnicode)
116: s = s.concat(dumpStringAsUnicode(w.toString()
117: + "\n"));
118: else
119: s = s.concat(w.toString() + "\n");
120: } catch (Exception e) {
121: logger.error("Exception in dumpProcessor(): "
122: + e.getMessage());
123: if (logger.isDebugEnabled()) {
124: logger
125: .debug("Exception in dumpProcessor():",
126: e);
127: }
128: s = s.concat("Exception printing content: " + e
129: + "\n" + ExceptionUtil.stackTraceString(e)
130: + "\n");
131: }
132: } else {
133: s = s.concat("ERROR - content is not valid\n");
134: }
135: }
136:
137: s = s.concat("\nATTACHMENTS:\n");
138: Set attachmentNames = m.getAttachmentNames();
139: for (Iterator i = attachmentNames.iterator(); i.hasNext();) {
140: try {
141: String name = (String) i.next();
142: DataHandler dh = m.getAttachment(name);
143: DataSource ds = dh.getDataSource();
144: s = s.concat(" ATT[" + name + "]: " + ds + "\n");
145: if (ds instanceof StringDataSource) {
146: String sdata = ((StringDataSource) ds).getData();
147: s = s.concat(dumpStringAsUnicode(sdata));
148: } else {
149: ByteArrayOutputStream baos = new ByteArrayOutputStream();
150: dh.writeTo(baos);
151: s = s.concat(dumpBytesAsHex(baos.toByteArray())
152: + "\n");
153: }
154: } catch (Exception e) {
155: logger.error("Exception in dumpProcessor(): "
156: + e.getMessage());
157: if (logger.isDebugEnabled()) {
158: logger.debug("Exception in dumpProcessor():", e);
159: }
160: s = s.concat("\n\nException writing attachment: " + e
161: + "\n" + ExceptionUtil.stackTraceString(e)
162: + "\n");
163: }
164: }
165:
166: s = s.concat("\nPROPERTIES:\n");
167: Set propertyNames = m.getPropertyNames();
168: for (Iterator i = propertyNames.iterator(); i.hasNext();) {
169: String name = (String) i.next();
170: s = s
171: .concat(" [" + name + "]" + m.getProperty(name)
172: + "\n");
173: }
174:
175: s = s.concat("getSecuritySubject=" + m.getSecuritySubject());
176: return s;
177: }
178:
179: public static String dumpBrief(NormalizedMessage m) {
180: String s = "";
181:
182: if (m == null)
183: return "NormalizedMessage is null";
184:
185: /*
186: * try to make the content readable
187: */
188: Source content = m.getContent();
189: if (content == null)
190: s = s.concat("NormalizedMessage content is null\n");
191: else {
192: }
193:
194: // s=s.concat("\nDATA:\n");
195: s = s.concat("\n");
196: Set attachmentNames = m.getAttachmentNames();
197: for (Iterator i = attachmentNames.iterator(); i.hasNext();) {
198: try {
199: String name = (String) i.next();
200: DataHandler dh = m.getAttachment(name);
201: DataSource ds = dh.getDataSource();
202:
203: // s=s.concat("DATA PART["+name+"]: "+ds+"\n");
204: // s=s.concat("-------------------------------------------------------------------------\n");
205: if (ds instanceof StringDataSource) {
206: String sdata = ((StringDataSource) ds).getData();
207: s = s.concat(sdata + "\n");
208: } else {
209: ByteArrayOutputStream baos = new ByteArrayOutputStream();
210: dh.writeTo(baos);
211: s = s.concat(baos.toByteArray() + "\n");
212: }
213: // s=s.concat("\n-------------------------------------------------------------------------\n");
214: } catch (Exception e) {
215: logger.error("Exception in dumpProcessor(): "
216: + e.getMessage());
217: if (logger.isDebugEnabled()) {
218: logger.debug("Exception in dumpProcessor():", e);
219: }
220: s = s.concat("\n\nException writing DATA: " + e + "\n"
221: + ExceptionUtil.stackTraceString(e) + "\n");
222: }
223: }
224: return s;
225: }
226:
227: public static String dumpStringAsUnicode(String s) {
228: int charsPerLine = 16;
229: StringBuffer out = new StringBuffer();
230: StringBuffer left = null;
231: StringBuffer right = null;
232: int position = 0;
233: for (char ch : s.toCharArray()) {
234: if (left == null)
235: left = new StringBuffer();
236: if (right == null)
237: right = new StringBuffer();
238: left.append((ch < 32) ? '.' : ch);
239: right.append(String.format("%4X ", (int) ch));
240: if (++position == charsPerLine) {
241: position = 0;
242: out.append(" " + left + " - " + right + "\n");
243: left = null;
244: right = null;
245: }
246: }
247: if (left != null) {
248: while (left.length() < charsPerLine)
249: left.append(" ");
250: out.append(" " + left + " - " + right + "\n");
251: }
252: return new String(out);
253: }
254:
255: public static String dumpBytesAsHex(byte[] bytes) {
256: int charsPerLine = 16;
257: StringBuffer out = new StringBuffer();
258: StringBuffer left = null;
259: StringBuffer right = null;
260: int position = 0;
261: for (byte b : bytes) {
262: if (left == null)
263: left = new StringBuffer();
264: if (right == null)
265: right = new StringBuffer();
266: left.append((b < 32) ? '.' : (char) b);
267: right.append(String.format("%2X ", b));
268: if (++position == charsPerLine) {
269: position = 0;
270: out.append(" " + left + " - " + right + "\n");
271: left = null;
272: right = null;
273: }
274: }
275: if (left != null) {
276: while (left.length() < charsPerLine)
277: left.append(" ");
278: out.append(" " + left + " - " + right + "\n");
279: }
280: return new String(out);
281: }
282:
283: }
|