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: NativeAxiomBuilder.java 9709 2007-10-23 19:52:09Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: import javax.activation.DataHandler;
030: import javax.jbi.messaging.MessagingException;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.dom.DOMSource;
033:
034: import org.apache.axiom.om.OMAttribute;
035: import org.apache.axiom.om.OMElement;
036: import org.apache.axiom.om.OMFactory;
037: import org.apache.axiom.om.OMNode;
038: import org.apache.axiom.om.OMText;
039: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.w3c.dom.Document;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.Node;
045:
046: /**
047: * Specialed IAxiomBuilder used when the Source is a DOMSource using
048: * the Axiom DOM implementation. This allows for high performance
049: * because no conversion is needed.
050: * There seems to be a bug in the Axiom DOM implementation that is
051: * preventing MTOM from working properly, so another IAxiomBuilder
052: * should be used when MTOM is required until the Axiom bug is resolved.
053: */
054: public class NativeAxiomBuilder implements IAxiomBuilder {
055:
056: protected final transient Log logger = LogFactory
057: .getLog(getClass());
058:
059: private boolean useMTOM = false;
060: private boolean useSwA = false;
061:
062: private OMElement root = null;
063:
064: private OMFactory factory = DOOMAbstractFactory.getOMFactory(); // OMAbstractFactory.getOMFactory();
065:
066: private HashMap<String, DataHandler> inputAttachments;
067: private HashMap<String, DataHandler> outputAttachments;
068:
069: /* (non-Javadoc)
070: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IAxiomBuilder#getOMTree(javax.xml.transform.Source, java.util.HashMap, boolean, boolean)
071: */
072: public OMElement getOMTree(Source source,
073: HashMap<String, DataHandler> attachments, boolean useMTOM,
074: boolean useSwA) throws MessagingException {
075: this .inputAttachments = attachments;
076: this .outputAttachments = new HashMap<String, DataHandler>();
077: this .useMTOM = useMTOM;
078: this .useSwA = useSwA;
079:
080: if (source instanceof DOMSource) {
081: Node node = ((DOMSource) source).getNode();
082: Element rootElement;
083: if (node instanceof Document) {
084: rootElement = ((Document) node).getDocumentElement();
085: } else {
086: rootElement = (Element) node;
087: }
088:
089: if (rootElement instanceof OMElement) {
090: root = (OMElement) rootElement;
091: if (useMTOM || useSwA) {
092: processAttachments(root);
093: }
094: return root;
095:
096: } else {
097: throw new MessagingException(
098: "NativeAxiomBuilder requires Source to be an instance of DOMSource using Axiom DOM implementation.");
099: }
100:
101: } else {
102: throw new MessagingException(
103: "NativeAxiomBuilder requires Source to be an instance of DOMSource using Axiom DOM implementation.");
104: }
105: }
106:
107: /* (non-Javadoc)
108: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IAxiomBuilder#getOutputAttachments()
109: */
110: public HashMap<String, DataHandler> getOutputAttachments() {
111:
112: return null;
113: }
114:
115: private void processAttachments(OMElement omElement) {
116: //Check all attributes for SwA references
117: for (Iterator iter = omElement.getAllAttributes(); iter
118: .hasNext();) {
119: OMAttribute attr = (OMAttribute) iter.next();
120: DataHandler dh = getAttachment(attr.getAttributeValue());
121: if (dh != null) {
122: outputAttachments.put(attr.getAttributeValue(), dh);
123: }
124: }
125: for (Iterator iter = omElement.getChildren(); iter.hasNext();) {
126: OMNode node = (OMNode) iter.next();
127: if (node.getType() == OMNode.ELEMENT_NODE) {
128: processAttachments((OMElement) node);
129: } else if (node.getType() == OMNode.TEXT_NODE) {
130: OMText omText = (OMText) node;
131: String text = omText.getText();
132: //Check if this is an SwA reference
133: DataHandler dh = getAttachment(text);
134: if (dh != null) {
135: outputAttachments.put(text, dh);
136: if (useMTOM) {
137: OMText newNode = factory.createOMText(dh, true);
138: node.insertSiblingBefore(newNode);
139: node.detach();
140: }
141: }
142: }
143: }
144: }
145:
146: private DataHandler getAttachment(String contentId) {
147: if (inputAttachments.containsKey(contentId)) {
148: return inputAttachments.get(contentId);
149: } else if (contentId.startsWith("cid:")
150: && inputAttachments.containsKey(contentId.substring(4))) {
151: return inputAttachments.get(contentId.substring(4));
152: }
153: return null;
154: }
155: }
|