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: RecordHelper.java 9243 2007-09-25 00:49:54Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.util.Set;
027:
028: import javax.jbi.messaging.MessageExchange;
029: import javax.jbi.messaging.MessagingException;
030: import javax.jbi.messaging.NormalizedMessage;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033: import javax.xml.parsers.ParserConfigurationException;
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:
040: import org.apache.commons.codec.binary.Base64;
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.Node;
046:
047: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArraySource;
048: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
049: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
050:
051: public class RecordHelper {
052:
053: protected static final transient Log log = LogFactory
054: .getLog(RecordHelper.class);
055:
056: private static final String SENDMESSAGE_NAMESPACE_URL = "http://cbesb.bostechcorp.com/soap/sendmessage/1.0";
057:
058: public static synchronized void addXmlRecord(NormalizedMessage mess) {
059: log.debug("In addXmlRecord method.");
060: Source content = mess.getContent();
061: Node doc = null;
062: DOMSource copiedContent = null;
063: if (content instanceof DOMSource)
064: doc = ((DOMSource) content).getNode();
065: else {
066: // convert into a DOMSource
067: try {
068: DOMResult dr = new DOMResult();
069: TransformerFactory tf = TransformerFactory
070: .newInstance();
071: Transformer t = tf.newTransformer();
072: t.transform(content, dr);
073: doc = dr.getNode();
074: } catch (Exception e) {
075: log.error("Exception converting content to DOMSource: "
076: + e.getMessage());
077: if (log.isDebugEnabled()) {
078: log
079: .debug(
080: "Exception converting content to DOMSource:",
081: e);
082: }
083: }
084: }
085: try {
086: copiedContent = new DOMSource(doc);
087: mess.setContent(null);
088: NormalizedMessageHandler handler = new NormalizedMessageHandler(
089: mess);
090: handler.addRecord(copiedContent);
091: handler.generateMessageContent();
092: } catch (Exception e) {
093: log.error("Exception adding dataEnvelope: "
094: + e.getMessage());
095: if (log.isDebugEnabled()) {
096: log.debug("Exception adding dataEnvelope:", e);
097: }
098: }
099: }
100:
101: public static synchronized void stripXmlRecord(
102: NormalizedMessage mess) throws MessagingException {
103: log.debug("In stripXmlRecord method.");
104: NormalizedMessageHandler h = new NormalizedMessageHandler(mess);
105: mess.setContent(h.getRecordAtIndex(0));
106: }
107:
108: public static synchronized void addSendMessage(
109: NormalizedMessage mess, MessageExchange me,
110: boolean isResponse) throws MessagingException {
111: log.debug("In addSendMessage method.");
112: try {
113: // create the content document
114: DocumentBuilderFactory factory = DocumentBuilderFactory
115: .newInstance();
116: DocumentBuilder parser = factory.newDocumentBuilder();
117: Document doc = parser.newDocument();
118: Element root = null;
119: if (isResponse)
120: root = doc.createElement("sendMessageResponse");
121: else
122: root = doc.createElement("sendMessage");
123: root.setAttribute("xmlns", SENDMESSAGE_NAMESPACE_URL);
124: doc.appendChild(root);
125:
126: // loop through the message records
127: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
128: mess);
129: for (int i = 0; i < nmh.getRecordCount(); i++) {
130: Source rec = nmh.getRecordAtIndex(i);
131:
132: if (rec instanceof StringSource) {
133: // string content
134: String textValue = ((StringSource) rec).getText();
135: Element cur = doc.createElement("messageType");
136: cur.setTextContent("STRING");
137: root.appendChild(cur);
138: cur = doc.createElement("message");
139: cur.setTextContent(textValue);
140: root.appendChild(cur);
141: } else if (rec instanceof ByteArraySource) {
142: // byte (base64) content
143: byte[] rawBytes = ((ByteArraySource) rec)
144: .getBytes();
145: byte[] encodedBytes = Base64.encodeBase64(rawBytes);
146: String encodedString = new String(encodedBytes,
147: "utf-8");
148: Element cur = doc.createElement("messageType");
149: cur.setTextContent("BASE64");
150: root.appendChild(cur);
151: cur = doc.createElement("message");
152: cur.setTextContent(encodedString);
153: root.appendChild(cur);
154: } else {
155: // convert anything else to DOM
156: DOMResult dr = new DOMResult();
157: TransformerFactory tf = TransformerFactory
158: .newInstance();
159: Transformer t = tf.newTransformer();
160: t.transform(rec, dr);
161: Document resultDoc = (Document) dr.getNode();
162: Node resultNode = resultDoc.getDocumentElement();
163: Node newNode = doc.importNode(resultNode, true);
164:
165: Element cur = doc.createElement("messageType");
166: cur.setTextContent("XML");
167: root.appendChild(cur);
168: cur = doc.createElement("message");
169: cur.appendChild(newNode);
170: root.appendChild(cur);
171: }
172: }
173: mess.setContent(new DOMSource(doc));
174:
175: // now add the properties
176: Set<String> properties = mess.getPropertyNames();
177: for (String propertyName : properties) {
178: Object value = mess.getProperty(propertyName);
179: Element cur = doc.createElement("property");
180: Element e = doc.createElement("name");
181: e.setTextContent(propertyName);
182: cur.appendChild(e);
183: e = doc.createElement("value");
184: e.setTextContent(value.toString());
185: cur.appendChild(e);
186:
187: root.appendChild(cur);
188: }
189:
190: // delete any attachments from the message
191: Set<String> attachments = mess.getAttachmentNames();
192: for (String attachment : attachments) {
193: mess.removeAttachment(attachment);
194: }
195: } catch (Exception e) {
196: log.error("Exception in addSendMessage(): "
197: + e.getMessage());
198: if (log.isDebugEnabled()) {
199: log.debug("Exception in addSendMessage():", e);
200: }
201: throw new MessagingException(e);
202: }
203: }
204:
205: public static synchronized void stripSendMessage(
206: NormalizedMessage mess, MessageExchange exchange,
207: boolean isResponse) throws MessagingException {
208: log.debug("In stripSendMessage method.");
209: NormalizedMessage newMessage = exchange.createMessage();
210: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
211: newMessage);
212:
213: // Get a DOM document from the content
214: Document doc = null;
215: Source content = mess.getContent();
216: if (content instanceof DOMSource) {
217: doc = (Document) ((DOMSource) content).getNode();
218: } else {
219: // convert into a DOMSource
220: try {
221: DOMResult dr = new DOMResult();
222: TransformerFactory tf = TransformerFactory
223: .newInstance();
224: Transformer t = tf.newTransformer();
225: t.transform(content, dr);
226: doc = (Document) dr.getNode();
227: content = new DOMSource(doc);
228: mess.setContent(content);
229: } catch (Exception e) {
230: log.error("Exception converting content to DOMSource:"
231: + e.getMessage());
232: if (log.isDebugEnabled()) {
233: log
234: .debug(
235: "Exception converting content to DOMSource:",
236: e);
237: }
238: throw new MessagingException(e);
239: }
240: }
241:
242: try {
243: Element root = (Element) doc.getFirstChild();
244:
245: // get the main element
246: String mainElementTag = null;
247: if (isResponse)
248: mainElementTag = "sendMessageResponse";
249: else
250: mainElementTag = "sendMessage";
251:
252: // Only the sendMessage element needs to be namespace qualified
253: // Check that it matches the correct namespace
254: // But only check the local name for all sub elements
255:
256: if (!root.getNamespaceURI().equals(
257: SENDMESSAGE_NAMESPACE_URL)
258: || !root.getLocalName().equals(mainElementTag)) {
259: log
260: .error("stripSendMessage: {"
261: + SENDMESSAGE_NAMESPACE_URL
262: + "}"
263: + mainElementTag
264: + " tag not found in message, leaving original message");
265: return;
266: }
267:
268: Element childElement = getFirstChildElement(root);
269:
270: while (childElement != null) {
271: if (childElement.getLocalName().equals("messageType")) {
272: String dataType = childElement.getTextContent();
273: childElement = getNextSiblingElement(childElement);
274: if (childElement != null
275: && childElement.getLocalName().equals(
276: "message")) {
277: if (dataType.equalsIgnoreCase("STRING")) {
278: // make a string record
279: StringSource ss = new StringSource(
280: childElement.getTextContent());
281: nmh.addRecord(ss);
282: } else if (dataType.equalsIgnoreCase("BASE64")) {
283: // make a binary record
284: byte[] binaryBytes = Base64
285: .decodeBase64(childElement
286: .getTextContent().getBytes(
287: "utf-8"));
288: ByteArraySource bas = new ByteArraySource(
289: binaryBytes);
290: nmh.addRecord(bas);
291: } else {
292: // make an XML record
293: try {
294: DocumentBuilderFactory dbf = DocumentBuilderFactory
295: .newInstance();
296: dbf.setNamespaceAware(true);
297: DocumentBuilder db = dbf
298: .newDocumentBuilder();
299: Document newDoc = db.newDocument();
300: Node recordNode = childElement
301: .getFirstChild();
302: while (recordNode != null
303: && !(recordNode instanceof Element)) {
304: recordNode = recordNode
305: .getNextSibling();
306: }
307: if (recordNode != null) {
308: Element newRecord = (Element) recordNode;
309: newDoc.adoptNode(newRecord);
310: newDoc.appendChild(newRecord);
311: DOMSource ds = new DOMSource(newDoc);
312: nmh.addRecord(ds);
313: }
314:
315: } catch (ParserConfigurationException e) {
316: log
317: .error("stripSendMessage - Exception creating DOM Documen"
318: + e.getMessage());
319: if (log.isDebugEnabled()) {
320: log
321: .debug(
322: "stripSendMessage - Exception creating DOM Documen:",
323: e);
324: }
325: }
326: }
327: }
328:
329: }
330:
331: if (childElement.getLocalName().equals("properties")) {
332: Node propertyNode = childElement.getFirstChild();
333: Element propertyElement = getFirstChildElement(childElement);
334: while (propertyElement != null) {
335: if (propertyElement.getLocalName().equals(
336: "property")) {
337: Element nameElement = getFirstChildElement(propertyElement);
338: if (nameElement != null
339: && nameElement.getLocalName()
340: .equals("name")) {
341: String name = nameElement
342: .getTextContent();
343: Element valueElement = getNextSiblingElement(nameElement);
344: if (valueElement != null
345: && valueElement.getLocalName()
346: .equals("value")) {
347: String value = valueElement
348: .getTextContent();
349: mess.setProperty(name, value);
350: }
351: }
352: }
353: }
354: }
355:
356: childElement = getNextSiblingElement(childElement);
357: }
358:
359: // copy the new message over the original
360: nmh.generateMessageContent();
361: mess.setContent(newMessage.getContent());
362: Set<String> attachments = newMessage.getAttachmentNames();
363: for (String name : attachments) {
364: mess
365: .addAttachment(name, newMessage
366: .getAttachment(name));
367: }
368:
369: } catch (Exception e) {
370: log.error("Exception stripping sendMessage"
371: + e.getMessage());
372: if (log.isDebugEnabled()) {
373: log.debug("Exception stripping sendMessage:", e);
374: }
375: throw new MessagingException(
376: "Exception stripping sendMessage: "
377: + e.getMessage() + "\n"
378: + ExceptionUtil.stackTraceString(e));
379: }
380: }
381:
382: private static Element getFirstChildElement(Element node) {
383: Node child = node.getFirstChild();
384: while (child != null && !(child instanceof Element)) {
385: child = child.getNextSibling();
386: }
387: return (Element) child;
388: }
389:
390: private static Element getNextSiblingElement(Element node) {
391: Node child = node.getNextSibling();
392: while (child != null && !(child instanceof Element)) {
393: child = child.getNextSibling();
394: }
395: return (Element) child;
396: }
397: }
|