001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Wsdl11WrapperHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl11wrapper;
030:
031: import org.w3c.dom.*;
032:
033: import java.util.*;
034:
035: import javax.wsdl.*;
036: import javax.wsdl.extensions.*;
037: import javax.wsdl.factory.*;
038: import javax.wsdl.xml.*;
039:
040: import javax.xml.namespace.QName;
041: import javax.xml.parsers.*;
042: import javax.xml.transform.*;
043: import javax.xml.transform.dom.*;
044: import javax.xml.transform.stream.*;
045:
046: /**
047: * DOCUMENT ME!
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class Wsdl11WrapperHelper {
052: /**
053: *
054: */
055: private Definition mDefinition;
056:
057: /**
058: *
059: */
060: private Document mDoc;
061:
062: /**
063: *
064: */
065: public static final String WRAPPER_DEFAULT_NAMESPACE = "http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper";
066:
067: /**
068: *
069: */
070: public static final String WRAPPER_MESSAGE = "jbi:documentroot";
071:
072: /**
073: *
074: */
075: public static final String WSDL11 = "http://schemas.xmlsoap.org/wsdl/";
076:
077: /**
078: * Creates a new Wsdl11WrapperHelper object.
079: *
080: */
081: public Wsdl11WrapperHelper() {
082: ;
083: }
084:
085: /**
086: * Creates a new Wsdl11WrapperHelper object.
087: *
088: * @param wsdlfile
089: * @param inputfile
090: */
091: public Wsdl11WrapperHelper(String wsdlfile, String inputfile) {
092: try {
093: javax.wsdl.factory.WSDLFactory mFactory = WSDLFactory
094: .newInstance();
095: javax.wsdl.xml.WSDLReader mReader = mFactory
096: .newWSDLReader();
097: mDefinition = mReader.readWSDL(null, wsdlfile);
098:
099: DocumentBuilderFactory docfac = DocumentBuilderFactory
100: .newInstance();
101: docfac.setNamespaceAware(true);
102: mDoc = docfac.newDocumentBuilder().parse(inputfile);
103: } catch (Exception e) {
104: e.printStackTrace();
105: }
106: }
107:
108: /**
109: * Creates a new Wsdl11WrapperHelper object.
110: *
111: * @param wsdldef
112: */
113: public Wsdl11WrapperHelper(Object wsdldef) {
114: mDefinition = (Definition) wsdldef;
115: }
116:
117: private Document getDocFromSource(Source src)
118: throws Wsdl11WrapperHelperException {
119: Document normaldoc = null;
120: try {
121: DOMResult result = new DOMResult();
122: TransformerFactory fac = TransformerFactory.newInstance();
123: Transformer trans = fac.newTransformer();
124: trans.transform(src, result);
125: Node node = result.getNode();
126:
127: if (node.getNodeType() == Node.DOCUMENT_NODE) {
128: normaldoc = (Document) node;
129: } else if (node.getNodeType() == Node.ELEMENT_NODE) {
130: normaldoc = ((Element) (node)).getOwnerDocument();
131: }
132: } catch (Exception e) {
133: e.printStackTrace();
134: throw new Wsdl11WrapperHelperException(
135: "Cannot retrieve the DOM from Source", e);
136: }
137: return normaldoc;
138: }
139:
140: public void parse(String wsdlfile)
141: throws Wsdl11WrapperHelperException {
142: try {
143: javax.wsdl.factory.WSDLFactory mFactory = WSDLFactory
144: .newInstance();
145: javax.wsdl.xml.WSDLReader mReader = mFactory
146: .newWSDLReader();
147: mDefinition = mReader.readWSDL(null, wsdlfile);
148: } catch (Exception e) {
149: throw new Wsdl11WrapperHelperException(
150: "Cannot parse Wsdl 1.1 file", e);
151: }
152:
153: }
154:
155: public boolean isWsdl11() throws Wsdl11WrapperHelperException {
156: try {
157: String xmlns = mDefinition.getNamespace("");
158: if (xmlns.trim().equals(WSDL11)) {
159: return true;
160: }
161: } catch (Exception e) {
162: throw new Wsdl11WrapperHelperException(
163: "Cannot get version", e);
164: }
165: return false;
166: }
167:
168: public Document wrapMessage(Source src, QName service,
169: String endpoint, String operation, boolean input)
170: throws Wsdl11WrapperHelperException {
171: Document tempdoc = getDocFromSource(src);
172: return wrapMessage(tempdoc, service, endpoint, operation, input);
173: }
174:
175: /**
176: * DOCUMENT ME!
177: *
178: * @param doc
179: * @param service
180: * @param enpoint
181: * @param operation
182: * @param input
183: *
184: * @return
185: */
186: public Document wrapMessage(Document doc, QName service,
187: String enpoint, String operation, boolean input)
188: throws Wsdl11WrapperHelperException {
189: Document d = null;
190:
191: if (doc == null) {
192: doc = mDoc;
193: }
194:
195: try {
196: WrapperBuilder builder = HelperFactory.createBuilder();
197: Message msg = getMessage(service, enpoint, operation, input);
198:
199: builder.initialize(null, msg, operation);
200:
201: Map parts = msg.getParts();
202: Iterator iter = parts.values().iterator();
203:
204: while (iter.hasNext()) {
205: Part part = (Part) iter.next();
206:
207: Element partelement = findpart(doc, part);
208:
209: if (partelement != null) {
210: builder.addPart(part.getName(), partelement);
211: }
212: }
213:
214: d = builder.getResult();
215: } catch (Exception e) {
216: e.printStackTrace();
217: throw new Wsdl11WrapperHelperException(
218: "Cannot create Wsdl 1.1 Wrapper", e);
219: }
220:
221: return d;
222: }
223:
224: public Document unwrapFault(Source wrappeddoc, QName service,
225: String endpoint, String operation)
226: throws Wsdl11WrapperHelperException {
227: return unwrapFault(getDocFromSource(wrappeddoc), service,
228: endpoint, operation);
229: }
230:
231: public Document wrapFault(Source doc, QName service,
232: String endpoint, String operation, String fault)
233: throws Wsdl11WrapperHelperException {
234: return wrapFault(getDocFromSource(doc), service, endpoint,
235: operation, fault);
236: }
237:
238: /**
239: *
240: *
241: * @param doc
242: * @param service
243: * @param endpoint
244: * @param operation
245: * @param fault
246: *
247: * @return
248: */
249: public Document wrapFault(Document doc, QName service,
250: String endpoint, String operation, String fault)
251: throws Wsdl11WrapperHelperException {
252: Document d = null;
253:
254: try {
255: WrapperBuilder builder = HelperFactory.createBuilder();
256: Message msg = getFaultMessage(service, endpoint, operation,
257: fault);
258:
259: builder.initialize(null, msg, operation);
260:
261: Map parts = msg.getParts();
262: Iterator iter = parts.values().iterator();
263:
264: while (iter.hasNext()) {
265: Part part = (Part) iter.next();
266:
267: Element partelement = findpart(doc, part);
268:
269: if (partelement != null) {
270: builder.addPart(part.getName(), partelement);
271: }
272: }
273:
274: d = builder.getResult();
275: } catch (Exception e) {
276: e.printStackTrace();
277: throw new Wsdl11WrapperHelperException(
278: "Cannot create Wsdl 1.1 wrapper for Fault", e);
279: }
280:
281: return d;
282: }
283:
284: public Document unwrapMessage(Source wrappeddoc, QName service,
285: String endpoint, String operation, boolean input)
286: throws Wsdl11WrapperHelperException {
287: return unwrapMessage(getDocFromSource(wrappeddoc), service,
288: endpoint, operation, input);
289: }
290:
291: /**
292: *
293: *
294: * @param wrappeddoc
295: * @param service
296: * @param endpoint
297: * @param operation
298: *
299: * @return
300: */
301: public Document unwrapFault(Document wrappeddoc, QName service,
302: String endpoint, String operation)
303: throws Wsdl11WrapperHelperException {
304: Document d = null;
305:
306: try {
307: d = DocumentBuilderFactory.newInstance()
308: .newDocumentBuilder().newDocument();
309:
310: WrapperParser parser = HelperFactory.createParser();
311:
312: parser.parse(wrappeddoc, mDefinition);
313:
314: String msgname = parser.getMessageName();
315: QName msgtype = parser.getMessageType();
316:
317: Message msg = mDefinition.getMessage(msgtype);
318: Map parts = msg.getParts();
319: Iterator iter = parts.values().iterator();
320: Element docroot = null;
321:
322: if (parts.size() > 1) {
323: docroot = d.createElementNS(WRAPPER_DEFAULT_NAMESPACE,
324: WRAPPER_MESSAGE);
325: }
326:
327: while (iter.hasNext()) {
328: Part part = (Part) iter.next();
329:
330: if (parser.hasPart(part.getName())) {
331: NodeList partnodes = parser.getPartNodes(part
332: .getName());
333:
334: for (int i = 0; i < partnodes.getLength(); i++) {
335: Node temp = (Node) partnodes.item(i);
336:
337: if ((temp == null)
338: || (temp.getLocalName() == null)) {
339: continue;
340: }
341:
342: Node node = d.importNode(temp, true);
343:
344: if (docroot != null) {
345: docroot.appendChild(node);
346: } else {
347: d.appendChild(node);
348: }
349: }
350: }
351: }
352:
353: d.normalize();
354: } catch (Exception e) {
355: e.printStackTrace();
356: throw new Wsdl11WrapperHelperException(
357: "Cannot un-wrap fault message", e);
358: }
359:
360: return d;
361: }
362:
363: /**
364: *
365: *
366: * @param wrappeddoc
367: * @param service
368: * @param endpoint
369: * @param operation
370: * @param input
371: *
372: * @return
373: */
374: public Document unwrapMessage(Document wrappeddoc, QName service,
375: String endpoint, String operation, boolean input)
376: throws Wsdl11WrapperHelperException {
377: Document d = null;
378:
379: if (wrappeddoc == null) {
380: wrappeddoc = mDoc;
381: }
382:
383: try {
384: d = DocumentBuilderFactory.newInstance()
385: .newDocumentBuilder().newDocument();
386:
387: WrapperParser parser = HelperFactory.createParser();
388: Message msg = getMessage(service, endpoint, operation,
389: input);
390: parser.parse(wrappeddoc, msg);
391:
392: Map parts = msg.getParts();
393: Iterator iter = parts.values().iterator();
394: Element docroot = null;
395:
396: if (parts.size() > 1) {
397: docroot = d.createElementNS(WRAPPER_DEFAULT_NAMESPACE,
398: WRAPPER_MESSAGE);
399: }
400:
401: while (iter.hasNext()) {
402: Part part = (Part) iter.next();
403:
404: if (parser.hasPart(part.getName())) {
405: NodeList partnodes = parser.getPartNodes(part
406: .getName());
407:
408: for (int i = 0; i < partnodes.getLength(); i++) {
409: Node temp = (Node) partnodes.item(i);
410:
411: if ((temp == null)
412: || (temp.getLocalName() == null)) {
413: continue;
414: }
415:
416: Node node = d.importNode(temp, true);
417:
418: if (docroot != null) {
419: docroot.appendChild(node);
420: } else {
421: d.appendChild(node);
422: }
423: }
424: }
425: }
426:
427: d.normalize();
428: } catch (Exception e) {
429: e.printStackTrace();
430: throw new Wsdl11WrapperHelperException(
431: "Cannot un-wrap output message", e);
432: }
433:
434: return d;
435: }
436:
437: /**
438: * DOCUMENT ME!
439: *
440: * @param doc
441: * @param part
442: *
443: * @return
444: */
445: private Element findpart(Document doc, Part part) {
446: Element ele = null;
447:
448: try {
449: QName partelement = part.getElementName();
450: QName parttype = part.getTypeName();
451: NodeList nl = null;
452: if (partelement != null) {
453: nl = doc.getElementsByTagNameNS("*", partelement
454: .getLocalPart());
455: }
456:
457: if (parttype != null) {
458: nl = doc.getElementsByTagNameNS("*", part.getName());
459: }
460:
461: if ((nl != null) && (nl.getLength() > 0)) {
462: ele = (Element) nl.item(0);
463: }
464: } catch (Exception e) {
465: e.printStackTrace();
466: }
467:
468: return ele;
469: }
470:
471: /**
472: * DOCUMENT ME!
473: *
474: * @param service
475: * @param portstring
476: * @param oper
477: * @param input
478: *
479: * @return
480: */
481: public Message getMessage(QName service, String portstring,
482: String oper, boolean input) {
483: Message msg = null;
484:
485: try {
486: Service ser = mDefinition.getService(service);
487:
488: //Port port = ser.getPort(\");
489: Port port = ser.getPort(portstring);
490: List l = port.getBinding().getPortType().getOperations();
491:
492: Iterator iter = l.iterator();
493:
494: while (iter.hasNext()) {
495: Operation operation = (Operation) iter.next();
496:
497: if (operation.getName().equals(oper)) {
498: if (input) {
499: return operation.getInput().getMessage();
500: } else {
501: return operation.getOutput().getMessage();
502: }
503: }
504: }
505: } catch (Exception e) {
506: e.printStackTrace();
507: }
508:
509: return null;
510: }
511:
512: /**
513: * DOCUMENT ME!
514: *
515: * @param service
516: * @param portstring
517: * @param oper
518: * @param fault
519: *
520: * @return
521: */
522: public Message getFaultMessage(QName service, String portstring,
523: String oper, String fault) {
524: Message msg = null;
525:
526: try {
527: Service ser = mDefinition.getService(service);
528: Port port = ser.getPort(portstring);
529: List l = port.getBinding().getPortType().getOperations();
530:
531: Iterator iter = l.iterator();
532:
533: while (iter.hasNext()) {
534: Operation operation = (Operation) iter.next();
535:
536: if (operation.getName().equals(oper)) {
537: if (fault != null) {
538: return operation.getFault(fault).getMessage();
539: } else {
540: Map faults = operation.getFaults();
541: Iterator faultiter = faults.values().iterator();
542: Fault defaultflt = (Fault) faultiter.next();
543:
544: return defaultflt.getMessage();
545: }
546: }
547: }
548: } catch (Exception e) {
549: e.printStackTrace();
550: }
551:
552: return null;
553: }
554:
555: /**
556: * DOCUMENT ME!
557: *
558: * @param args
559: */
560: /*
561: public static void main(String [] args)
562: {
563: if (args.length != 2)
564: {
565: System.out.println("Takes wsdl file name and the document to wrap");
566: System.exit(1);
567: }
568:
569: Wsdl11WrapperHelper wrapper = new Wsdl11WrapperHelper(args[0], args[1]);
570: QName service =
571: new QName("http://sun.com/jbi/binding/soap/stockquote/wsdl",
572: "QuoteService");
573:
574: Document d = wrapper.wrapMessage(null, service, "QuotePort", "getQuote", true);
575:
576: try
577: {
578: TransformerFactory tFactory = TransformerFactory.newInstance();
579: Transformer trans = tFactory.newTransformer();
580: StreamResult result = new StreamResult(System.out);
581: trans.transform(new DOMSource(d), result);
582: }
583: catch (Exception e)
584: {
585: e.printStackTrace();
586: }
587: Document d1 =
588: wrapper.unwrapMessage(null, service, "QuotePort", "getQuote", false);
589:
590: try
591: {
592: TransformerFactory tFactory = TransformerFactory.newInstance();
593: Transformer trans = tFactory.newTransformer();
594: StreamResult result = new StreamResult(System.out);
595: trans.transform(new DOMSource(d1), result);
596: }
597: catch (Exception e)
598: {
599: e.printStackTrace();
600: }
601: }
602: */
603: }
|