KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > WSDLHelper


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: XMLHelper.java 17:06:54 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.util;
23
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.wsdl.BindingOperation;
34 import javax.wsdl.Definition;
35 import javax.wsdl.Port;
36 import javax.wsdl.Service;
37 import javax.wsdl.WSDLException;
38 import javax.wsdl.extensions.ExtensibilityElement;
39 import javax.wsdl.extensions.soap.SOAPAddress;
40 import javax.wsdl.xml.WSDLReader;
41 import javax.wsdl.xml.WSDLWriter;
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.parsers.DocumentBuilder JavaDoc;
44 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
45 import javax.xml.transform.TransformerException JavaDoc;
46
47 import org.w3c.dom.Document JavaDoc;
48
49 import com.ibm.wsdl.xml.WSDLReaderImpl;
50 import com.ibm.wsdl.xml.WSDLWriterImpl;
51
52 /**
53  * Util class to manipulate WSDL Document or Definition
54  *
55  * @author wjoseph - eBMWebsourcing
56  */

57 public final class WSDLHelper {
58
59     /**
60      * Namespace for the soap 1.1
61      */

62     // private static final String WSDL_NS = "http://schemas.xmlsoap.org/wsdl/";
63
private WSDLHelper() {
64     }
65
66     /**
67      * Change the location attribute of the service node to
68      * "/axis2/services/serviceName"
69      *
70      * @param serviceName
71      * Name of the service
72      * @param serviceDesc
73      * Dom representation of the service description
74      */

75     public static void changeServiceAddressInWSDL(QName JavaDoc serviceName,
76         Definition serviceDef, String JavaDoc prefix) {
77         SOAPAddress address = findServiceAddress(serviceDef, serviceName);
78         if (address != null) {
79             String JavaDoc serviceAddress = prefix + serviceName.getLocalPart();
80             address.setLocationURI(serviceAddress);
81         }
82     }
83
84     /**
85      * Change the location attribute of the service node to
86      * the given address
87      *
88      * @param serviceName
89      * Name of the service
90      * @param serviceDesc
91      * Dom representation of the service description
92      * @param webServiceAddress
93      * new service address
94      * @throws WSDLException
95      * @throws TransformerException
96      */

97     public static Document JavaDoc changeServiceAddressInWSDL(QName JavaDoc serviceName,
98         Document JavaDoc serviceDesc, String JavaDoc webServiceAddress) throws WSDLException,
99         TransformerException JavaDoc {
100         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
101         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
102         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
103         changeServiceAddressInWSDL(serviceName, serviceDef, webServiceAddress);
104         serviceDesc = createDocumentFromDefinition(serviceDef);
105         return serviceDesc;
106     }
107
108     public static Definition createDefinitionFromDocument(Document JavaDoc doc)
109         throws WSDLException {
110         WSDLReader reader = new WSDLReaderImpl();
111         return reader.readWSDL(null, doc);
112     }
113
114     /**
115      * Create a dom document from a File
116      *
117      * @param wsdlFile
118      * file
119      * @return dom document
120      */

121     public static Document JavaDoc createDocumentFromWSDL(File JavaDoc wsdlFile) {
122         Document JavaDoc doc = null;
123         try {
124             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
125                 .newInstance();
126             factory.setNamespaceAware(true);
127             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
128             doc = builder.parse(wsdlFile);
129             doc.normalize();
130         } catch (Exception JavaDoc e) {
131             e.printStackTrace();
132         }
133         return doc;
134     }
135
136     @SuppressWarnings JavaDoc("unchecked")
137     public static SOAPAddress findServiceAddress(Definition serviceDef,
138         QName JavaDoc serviceName) {
139         SOAPAddress result = null;
140         if (serviceDef != null) {
141             Service service = serviceDef.getService(serviceName);
142             if (service != null) {
143                 Map JavaDoc<String JavaDoc, Port> ports = service.getPorts();
144                 for (Port port : ports.values()) {
145                     List JavaDoc<ExtensibilityElement> extensibilityElements = port
146                         .getExtensibilityElements();
147                     for (ExtensibilityElement element : extensibilityElements) {
148                         // The SOAP binding is an extensibility element of
149
// Port
150
if (element instanceof SOAPAddress) {
151                             result = (SOAPAddress) element;
152                             break;
153                         }
154                     }
155                     if (result != null) {
156                         break;
157                     }
158                 }
159             }
160         }
161         return result;
162     }
163
164     /**
165      * Returns the service's port name contained in a WSDL 1.0 document
166      *
167      * @param serviceDesc
168      * The service description document
169      * @return The service port name attribute or null if not found
170      */

171     @SuppressWarnings JavaDoc("unchecked")
172     public static String JavaDoc getPortNameFromWSDLDefinition(Definition serviceDef,
173         QName JavaDoc serviceName) {
174         String JavaDoc result = null;
175         Service service = serviceDef.getService(serviceName);
176         Map JavaDoc<String JavaDoc, Port> ports = service.getPorts();
177         for (Port port : ports.values()) {
178             result = port.getName();
179         }
180         return result;
181     }
182
183     /**
184      * Returns the service's port name contained in a WSDL 1.0 document
185      *
186      * @param serviceDesc
187      * The service description document
188      * @return The service port name attribute or null if not found
189      * @throws WSDLException
190      * @throws TransformerException
191      */

192     public static String JavaDoc getPortNameFromWSDLDocument(Document JavaDoc serviceDesc,
193         QName JavaDoc serviceName) throws WSDLException, TransformerException JavaDoc {
194         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
195         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
196         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
197         return getPortNameFromWSDLDefinition(serviceDef, serviceName);
198     }
199
200     /**
201      * Returns the service address contained in a WSDL 1.0 document
202      *
203      * @param serviceDesc
204      * The service description document
205      * @return The service address attribute or null if not found
206      */

207     public static String JavaDoc getServiceAddressFromWSDLDefinition(
208         Definition serviceDef, QName JavaDoc serviceName) {
209         String JavaDoc result = null;
210         SOAPAddress address = findServiceAddress(serviceDef, serviceName);
211         if (address != null) {
212             result = address.getLocationURI();
213         }
214         return result;
215     }
216
217     /**
218      * Returns the service address contained in a WSDL 1.0 document
219      *
220      * @param serviceDesc
221      * The service description document
222      * @return The service address attribute or null if not found
223      * @throws WSDLException
224      * @throws TransformerException
225      */

226     public static String JavaDoc getServiceAddressFromWSDLDocument(
227         Document JavaDoc serviceDesc, QName JavaDoc serviceName) throws WSDLException,
228         TransformerException JavaDoc {
229         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
230         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
231         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
232         return getServiceAddressFromWSDLDefinition(serviceDef, serviceName);
233     }
234
235     /**
236      * Returns the service address contained in a WSDL 1.0 file
237      *
238      * @param wsdlFile
239      * The WSDL file
240      * @return The service address attribute or null if not found
241      * @throws WSDLException
242      * @throws TransformerException
243      */

244     public static String JavaDoc getServiceAddressFromWSDLFile(File JavaDoc wsdlFile,
245         QName JavaDoc serviceName) throws WSDLException, TransformerException JavaDoc {
246         Document JavaDoc doc = createDocumentFromWSDL(wsdlFile);
247         return getServiceAddressFromWSDLDocument(doc, serviceName);
248     }
249
250     /**
251      * Returns the service name contained in a WSDL 1.1 dom Document
252      *
253      * @param serviceDef
254      * The dom Document
255      * @return The service name attribute or null if not found
256      */

257     @SuppressWarnings JavaDoc("unchecked")
258     public static QName JavaDoc[] getServiceNameFromWSDLDefinition(Definition serviceDef) {
259         QName JavaDoc[] result = null;
260         if (serviceDef != null) {
261             Map JavaDoc<String JavaDoc, Service> services = serviceDef.getServices();
262             result = new QName JavaDoc[services.size()];
263             int i = 0;
264             for (Service service : services.values()) {
265                 result[i++] = service.getQName();
266             }
267         }
268         return result;
269     }
270
271     /**
272      * Returns the service name contained in a WSDL 1.1 dom Document
273      *
274      * @param serviceDef
275      * The dom Document
276      * @return The service name attribute or null if not found
277      * @throws WSDLException
278      * @throws TransformerException
279      */

280     public static QName JavaDoc[] getServiceNameFromWSDLDocument(Document JavaDoc serviceDesc)
281         throws WSDLException, TransformerException JavaDoc {
282         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
283         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
284         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
285         return getServiceNameFromWSDLDefinition(serviceDef);
286     }
287
288     /**
289      * Returns the service name contained in a WSDL 1.0 file
290      *
291      * @param wsdlFile
292      * The WSDL file
293      * @return The service name attribute or null if not found
294      * @throws WSDLException
295      * @throws TransformerException
296      */

297     public static QName JavaDoc[] getServiceNameFromWSDLFile(File JavaDoc wsdlFile)
298         throws WSDLException, TransformerException JavaDoc {
299         Document JavaDoc doc = createDocumentFromWSDL(wsdlFile);
300         return getServiceNameFromWSDLDocument(doc);
301     }
302
303     /**
304      * Returns WSDL 1.0 document target namespace
305      *
306      * @param serviceDesc
307      * The service description document
308      * @return The target namespace or null if not found
309      */

310     public static String JavaDoc getTargetNSFromWSDLDefinition(Definition serviceDef) {
311         return serviceDef.getTargetNamespace();
312     }
313
314     /**
315      * Returns WSDL 1.0 document target namespace
316      *
317      * @param serviceDesc
318      * The service description document
319      * @return The target namespace or null if not found
320      * @throws WSDLException
321      * @throws TransformerException
322      */

323     public static String JavaDoc getTargetNSFromWSDLDocument(Document JavaDoc serviceDesc)
324         throws WSDLException, TransformerException JavaDoc {
325         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
326         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
327         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
328         return getTargetNSFromWSDLDefinition(serviceDef);
329     }
330
331     /**
332      * Search the service description for an operation
333      *
334      * @param serviceDesc
335      * dom Document representation of the service description
336      * @param opName
337      * Name of the operation to search
338      * @return true if the operation has been found or else false
339      */

340     public static boolean hasOperationNamed(Definition serviceDef,
341         String JavaDoc opName, QName JavaDoc serviceName) {
342         BindingOperation op = findOperationNamed(serviceDef, opName,
343             serviceName);
344         return op != null;
345     }
346
347     /**
348      * Search the service description for an operation
349      *
350      * @param serviceDesc
351      * dom Document representation of the service description
352      * @param opName
353      * Name of the operation to search
354      * @return true if the operation has been found or else false
355      * @throws WSDLException
356      * @throws TransformerException
357      */

358     public static boolean hasOperationNamed(Document JavaDoc serviceDesc,
359         String JavaDoc opName, QName JavaDoc serviceName) throws WSDLException,
360         TransformerException JavaDoc {
361         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
362         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
363         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
364         return hasOperationNamed(serviceDef, opName, serviceName);
365     }
366
367     /**
368      * Search the service description for an operation. If the operation is
369      * found , checks if it has a child node named 'output'
370      *
371      * @param serviceDesc
372      * dom Document representation of the service description
373      * @param opName
374      * Name of the operation to search
375      * @return true if the operation has been found and has the output child
376      * node or else false
377      */

378     public static boolean isInOutOperation(Definition serviceDef,
379         String JavaDoc opName, QName JavaDoc serviceName) {
380         boolean inOut = false;
381         BindingOperation op = findOperationNamed(serviceDef, opName,
382             serviceName);
383         if (op != null) {
384             inOut = op.getBindingOutput() != null;
385         }
386         return inOut;
387     }
388
389     /**
390      * Search the service description for an operation. If the operation is
391      * found , checks if it has a child node named 'output'
392      *
393      * @param serviceDesc
394      * dom Document representation of the service description
395      * @param opName
396      * Name of the operation to search
397      * @return true if the operation has been found and has the output child
398      * node or else false
399      * @throws WSDLException
400      * @throws TransformerException
401      */

402     public static boolean isInOutOperation(Document JavaDoc serviceDesc, String JavaDoc opName,
403         QName JavaDoc serviceName) throws WSDLException, TransformerException JavaDoc {
404         String JavaDoc docString = XMLHelper.createStringFromDOMDocument(serviceDesc);
405         Document JavaDoc tmpDoc = XMLHelper.createDocumentFromString(docString);
406         Definition serviceDef = createDefinitionFromDocument(tmpDoc);
407         return isInOutOperation(serviceDef, opName, serviceName);
408     }
409
410     protected static Document JavaDoc createDocumentFromDefinition(Definition def)
411         throws WSDLException {
412         Document JavaDoc result = null;
413         WSDLWriter writer = new WSDLWriterImpl();
414         OutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
415         writer.writeWSDL(def, outStream);
416         String JavaDoc documentToString = outStream.toString();
417         result = XMLHelper.createDocumentFromString(documentToString);
418         return result;
419     }
420
421     @SuppressWarnings JavaDoc("unchecked")
422     protected static BindingOperation findOperationNamed(Definition serviceDef,
423         String JavaDoc opName, QName JavaDoc serviceName) {
424         BindingOperation result = null;
425         boolean found = false;
426         if (serviceDef != null) {
427
428             Service service = findService(serviceDef,serviceName);
429             if (service != null) {
430                 Map JavaDoc<String JavaDoc, Port> ports = service.getPorts();
431                 for (Port port : ports.values()) {
432                     List JavaDoc<BindingOperation> ops = port.getBinding()
433                         .getBindingOperations();
434                     for (BindingOperation op : ops) {
435                         if (op.getName().equals(opName)) {
436                             result = op;
437                             found = true;
438                             break;
439                         }
440                     }
441                     if (found) {
442                         break;
443                     }
444                 }
445             }
446         }
447         return result;
448     }
449
450     /**
451      * Return the Service definition for this service. This method is comparable
452      * to definition.getService(qname_service), but due to some variation in
453      * QName structure, we perform manually a equals(qname_service, qname) to
454      * retreive the service description
455      *
456      * @param definition
457      * wsdl definition, can be null
458      * @param service
459      * service to retrieve its definition, can be null
460      * @return null if definition or service are null, or service not found in
461      * description
462      */

463     public static Service findService(Definition definition, QName JavaDoc service) {
464         Service result = null;
465
466         if (definition != null && service != null) {
467             for (Iterator JavaDoc iter = definition.getServices().keySet().iterator(); iter
468                 .hasNext()
469                 && result == null;) {
470                 QName JavaDoc element = (QName JavaDoc) iter.next();
471                 if (equals(element, service)) {
472                     result = definition.getService(element);
473                 }
474
475             }
476         }
477         return result;
478     }
479
480     /**
481      * Perform a qname.toString equality
482      *
483      * @param a
484      * can be null
485      * @param b
486      * can be null
487      * @return a.toString equals b.toString, or a=b=null
488      */

489     public static boolean equals(QName JavaDoc a, QName JavaDoc b) {
490         boolean result = true;
491
492         if (a != null && b != null) {
493             result = a.toString().equals(b.toString());
494         } else {
495             result = a == null && b == null;
496         }
497         return result;
498     }
499     
500     /**
501      * Create a light WSDL2.0 document from the given parameters.
502      * All parameters are required, otherwise a NullPointerException
503      * will be thrown.
504      * Namespaces are managed.
505      * @param interfaceName must be non null
506      * @param serviceName must be non null
507      * @param endpointName must be non null
508      * @return a WSDL2.0 light document
509      */

510     public static Document JavaDoc createLightWSDL20(QName JavaDoc interfaceName, QName JavaDoc serviceName, String JavaDoc endpointName) {
511
512         StringBuffer JavaDoc s= new StringBuffer JavaDoc();
513
514         
515         String JavaDoc itfns=null;
516         if(! StringHelper.isEmpty(interfaceName.getNamespaceURI())){
517             itfns=interfaceName.getNamespaceURI();
518         }else{
519             itfns="http://org.objectweb.petals/xml/ns/defaulttns";
520         }
521         
522         // write definition xmlns block
523
s.append("<?xml version='1.0' encoding='UTF-8'?>\n");
524         s.append(" <description targetNamespace='"+itfns+"'\n");
525         s.append(" xmlns:tns='"+itfns+"'\n");
526         s.append(" xmlns='http://www.w3.org/2006/01/wsdl'>\n");
527         
528         // write interface block
529
s.append(" <interface name='"+interfaceName.getLocalPart()+"'></interface>\n");
530
531         // write binding block
532
s.append(" <binding name='"+endpointName+"'></binding>\n");
533
534         // write service block (with interface and endpoint part)
535
s.append(" <service name='"+serviceName.getLocalPart()+"'\n");
536         s.append(" interface='tns:"+interfaceName.getLocalPart()+"'>\n");
537         s.append(" <endpoint binding='tns:"+endpointName+"'></endpoint>\n");
538
539         s.append(" </service>\n");
540         s.append(" </description>");
541
542         // construct the document
543
ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(s.toString().getBytes());
544
545         Document JavaDoc doc = null;
546         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
547         .newInstance();
548         factory.setNamespaceAware(true);
549         try {
550             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
551             doc= builder.parse(is);
552         } catch (Exception JavaDoc e) {
553             // this kind of exception should never be thrown...
554
}
555         return doc;
556     }
557
558 }
559
Popular Tags