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: JbiWrapperHandler.java 9658 2007-10-17 17:01:53Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Vector;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.dom.DOMSource;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.jdom.Attribute;
038: import org.jdom.Document;
039: import org.jdom.Element;
040: import org.jdom.JDOMException;
041: import org.jdom.Namespace;
042: import org.jdom.input.DOMBuilder;
043: import org.jdom.output.DOMOutputter;
044:
045: import com.bostechcorp.cbesb.common.runtime.CbesbException;
046: import com.bostechcorp.cbesb.common.runtime.DataContentException;
047: import com.bostechcorp.cbesb.runtime.ccsl.lib.NormalizedMessageUtil;
048:
049: public class JbiWrapperHandler {
050:
051: public static final String JBI_WRAPPER_NS = "http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper";
052: public static final String JBI_MESSAGE = "message";
053: public static final String JBI_VERSION = "version";
054: public static final String JBI_TYPE = "type";
055: public static final String JBI_NAME = "name";
056: public static final String JBI_PART = "part";
057:
058: protected transient Log log = LogFactory.getLog(getClass());
059:
060: private QName type;
061: private String name;
062: private Vector<Source> parts;
063:
064: private Namespace jbiNamespace;
065: private HashMap<String, Namespace> namespaceUriMap;
066: private HashMap<String, Namespace> namespacePrefixMap;
067: private int namespaceCounter;
068:
069: public JbiWrapperHandler() {
070: type = null;
071: name = null;
072: parts = new Vector<Source>();
073: namespaceUriMap = new HashMap<String, Namespace>();
074: namespacePrefixMap = new HashMap<String, Namespace>();
075: jbiNamespace = Namespace.getNamespace("jbi", JBI_WRAPPER_NS);
076: }
077:
078: /**
079: * @return the name
080: */
081: public String getName() {
082: return name;
083: }
084:
085: /**
086: * @param name the name to set
087: */
088: public void setName(String name) {
089: this .name = name;
090: }
091:
092: /**
093: * @return the type
094: */
095: public QName getType() {
096: return type;
097: }
098:
099: /**
100: * @param type the type to set
101: */
102: public void setType(QName type) {
103: this .type = type;
104: }
105:
106: public void appendPart(Source src) {
107: parts.add(src);
108: }
109:
110: public int getPartCount() {
111: return parts.size();
112: }
113:
114: public Source getPartAtIndex(int index) {
115: return parts.elementAt(index);
116: }
117:
118: public static JbiWrapperHandler createFromSource(Source src)
119: throws CbesbException {
120: JbiWrapperHandler jbiWrapper = null;
121: DOMBuilder domBuilder = new DOMBuilder();
122: Document jdomDoc = domBuilder.build(NormalizedMessageUtil
123: .getDocFromSource(src));
124: // Document jdomDoc = domBuilder.build(getDocFromSource(src));
125: Element rootElement = jdomDoc.getRootElement();
126:
127: if (!JBI_WRAPPER_NS.equals(rootElement.getNamespaceURI())
128: && !JBI_MESSAGE.equals(rootElement.getName())) {
129: throw new DataContentException("Unexpected root element: {"
130: + rootElement.getNamespaceURI() + "}"
131: + rootElement.getName() + " - should be: {"
132: + JBI_WRAPPER_NS + "}" + JBI_MESSAGE);
133: }
134: jbiWrapper = new JbiWrapperHandler();
135:
136: List nsList = rootElement.getAdditionalNamespaces();
137: for (Iterator iter = nsList.iterator(); iter.hasNext();) {
138: Namespace ns = (Namespace) iter.next();
139: jbiWrapper.addNamespaceToMaps(ns);
140: }
141:
142: List attrList = rootElement.getAttributes();
143: for (Iterator iter = attrList.iterator(); iter.hasNext();) {
144: Attribute attr = (Attribute) iter.next();
145: if (JBI_TYPE.equals(attr.getName())) {
146: QName type = jbiWrapper.prefixedNameToQName(attr
147: .getValue());
148: jbiWrapper.setType(type);
149: } else if (JBI_NAME.equals(attr.getName())) {
150: jbiWrapper.setName(attr.getValue());
151: }
152: }
153:
154: List children = rootElement.getChildren(JBI_PART, Namespace
155: .getNamespace(JBI_WRAPPER_NS));
156: for (Iterator iter = children.iterator(); iter.hasNext();) {
157: Element partElem = (Element) iter.next();
158: if (partElem.getChildren().size() > 0) {
159: Element child = (Element) partElem.getChildren().get(0);
160: Document partDoc = new Document((Element) child
161: .detach());
162: DOMOutputter domOut = new DOMOutputter();
163: org.w3c.dom.Document doc;
164: try {
165: doc = domOut.output(partDoc);
166: } catch (JDOMException e) {
167: throw new DataContentException(
168: "Fail to convert from JDOM to DOMSource.",
169: CbesbException.SUBMIT_BUG, e);
170: }
171: DOMSource domSrc = new DOMSource(doc);
172: jbiWrapper.appendPart(domSrc);
173: }
174: }
175:
176: return jbiWrapper;
177: }
178:
179: public Source toSource() throws CbesbException {
180: Element rootElement = new Element(JBI_MESSAGE, jbiNamespace);
181: Document jdomDoc = new Document(rootElement);
182: rootElement.setAttribute(JBI_VERSION, "1.0");
183: rootElement.setAttribute(JBI_TYPE, QNameToPrefixedName(type));
184: if (name != null) {
185: rootElement.setAttribute(JBI_NAME, name);
186: }
187: AddNamespaceDeclarationsToRoot(rootElement);
188:
189: DOMBuilder domBuilder = new DOMBuilder();
190: for (int i = 0; i < parts.size(); i++) {
191: Source src = parts.elementAt(i);
192: Document jdomPartDoc = domBuilder
193: .build(NormalizedMessageUtil.getDocFromSource(src));
194: Element partElem = new Element(JBI_PART, jbiNamespace);
195: rootElement.addContent(partElem);
196: partElem.addContent(jdomPartDoc.detachRootElement());
197: }
198: DOMOutputter domOut = new DOMOutputter();
199: org.w3c.dom.Document doc;
200: try {
201: doc = domOut.output(jdomDoc);
202: } catch (JDOMException e) {
203: throw new DataContentException(
204: "Fail to convert from JDOM to DOMSource.",
205: CbesbException.SUBMIT_BUG, e);
206: }
207:
208: return new DOMSource(doc);
209: }
210:
211: protected QName prefixedNameToQName(String prefixedName) {
212: String prefix;
213: String localName;
214: QName qname;
215: int index = prefixedName.indexOf(':');
216: if (index > 0) {
217: prefix = prefixedName.substring(0, index);
218: localName = prefixedName.substring(index + 1);
219: } else {
220: prefix = null;
221: localName = prefixedName;
222: }
223: Namespace ns = namespacePrefixMap.get(prefix);
224: if (ns != null) {
225: qname = new QName(ns.getURI(), localName);
226: } else {
227: log.warn("Unable to resolve namespace prefix " + prefix);
228: qname = null;
229: }
230: return qname;
231: }
232:
233: protected String QNameToPrefixedName(QName qname) {
234: addNamespace(qname.getNamespaceURI());
235: Namespace ns = namespaceUriMap.get(qname.getNamespaceURI());
236: String prefix = ns.getPrefix();
237: if (prefix == null || prefix.equals("")) {
238: return qname.getLocalPart();
239: } else {
240: return prefix + ":" + qname.getLocalPart();
241: }
242:
243: }
244:
245: protected void addNamespace(String NamespaceURI) {
246: if (!namespaceUriMap.containsKey(NamespaceURI)) {
247: String prefix = "q" + namespaceCounter;
248: namespaceCounter++;
249: Namespace ns = Namespace.getNamespace(prefix, NamespaceURI);
250: addNamespaceToMaps(ns);
251: }
252: }
253:
254: protected void AddNamespaceDeclarationsToRoot(Element root) {
255: for (Iterator iter = namespaceUriMap.values().iterator(); iter
256: .hasNext();) {
257: root.addNamespaceDeclaration((Namespace) iter.next());
258: }
259: }
260:
261: private void addNamespaceToMaps(Namespace ns) {
262: namespaceUriMap.put(ns.getURI(), ns);
263: namespacePrefixMap.put(ns.getPrefix(), ns);
264: }
265:
266: // protected static org.w3c.dom.Document getDocFromSource(Source originalSource) throws CbesbException
267: // {
268: // org.w3c.dom.Node domNode;
269: // if (originalSource instanceof DOMSource) {
270: // domNode = ((DOMSource)originalSource).getNode();
271: // } else {
272: // DOMResult dr = new DOMResult();
273: // TransformerFactory tf = TransformerFactory.newInstance();
274: // try {
275: //
276: //
277: // Transformer t = tf.newTransformer();
278: // t.transform(originalSource, dr);
279: // } catch (Exception e) {
280: // throw new DataContentException("Fail to convert the source in Normalized Message into DOMSource.",
281: // "Check the source in the message to make sure it is XML formatted.", e);
282: //
283: // }
284: // domNode = dr.getNode();
285: // }
286: // if (domNode instanceof org.w3c.dom.Document) {
287: // return (org.w3c.dom.Document)domNode;
288: // } else {
289: // return ((org.w3c.dom.Element)domNode).getOwnerDocument();
290: // }
291: // }
292:
293: }
|