1 22 package org.objectweb.petals.component.common.util; 23 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.File ; 28 import java.io.OutputStream ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 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 ; 43 import javax.xml.parsers.DocumentBuilder ; 44 import javax.xml.parsers.DocumentBuilderFactory ; 45 import javax.xml.transform.TransformerException ; 46 47 import org.w3c.dom.Document ; 48 49 import com.ibm.wsdl.xml.WSDLReaderImpl; 50 import com.ibm.wsdl.xml.WSDLWriterImpl; 51 52 57 public final class WSDLHelper { 58 59 62 private WSDLHelper() { 64 } 65 66 75 public static void changeServiceAddressInWSDL(QName serviceName, 76 Definition serviceDef, String prefix) { 77 SOAPAddress address = findServiceAddress(serviceDef, serviceName); 78 if (address != null) { 79 String serviceAddress = prefix + serviceName.getLocalPart(); 80 address.setLocationURI(serviceAddress); 81 } 82 } 83 84 97 public static Document changeServiceAddressInWSDL(QName serviceName, 98 Document serviceDesc, String webServiceAddress) throws WSDLException, 99 TransformerException { 100 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 101 Document 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 doc) 109 throws WSDLException { 110 WSDLReader reader = new WSDLReaderImpl(); 111 return reader.readWSDL(null, doc); 112 } 113 114 121 public static Document createDocumentFromWSDL(File wsdlFile) { 122 Document doc = null; 123 try { 124 DocumentBuilderFactory factory = DocumentBuilderFactory 125 .newInstance(); 126 factory.setNamespaceAware(true); 127 DocumentBuilder builder = factory.newDocumentBuilder(); 128 doc = builder.parse(wsdlFile); 129 doc.normalize(); 130 } catch (Exception e) { 131 e.printStackTrace(); 132 } 133 return doc; 134 } 135 136 @SuppressWarnings ("unchecked") 137 public static SOAPAddress findServiceAddress(Definition serviceDef, 138 QName serviceName) { 139 SOAPAddress result = null; 140 if (serviceDef != null) { 141 Service service = serviceDef.getService(serviceName); 142 if (service != null) { 143 Map <String , Port> ports = service.getPorts(); 144 for (Port port : ports.values()) { 145 List <ExtensibilityElement> extensibilityElements = port 146 .getExtensibilityElements(); 147 for (ExtensibilityElement element : extensibilityElements) { 148 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 171 @SuppressWarnings ("unchecked") 172 public static String getPortNameFromWSDLDefinition(Definition serviceDef, 173 QName serviceName) { 174 String result = null; 175 Service service = serviceDef.getService(serviceName); 176 Map <String , Port> ports = service.getPorts(); 177 for (Port port : ports.values()) { 178 result = port.getName(); 179 } 180 return result; 181 } 182 183 192 public static String getPortNameFromWSDLDocument(Document serviceDesc, 193 QName serviceName) throws WSDLException, TransformerException { 194 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 195 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 196 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 197 return getPortNameFromWSDLDefinition(serviceDef, serviceName); 198 } 199 200 207 public static String getServiceAddressFromWSDLDefinition( 208 Definition serviceDef, QName serviceName) { 209 String result = null; 210 SOAPAddress address = findServiceAddress(serviceDef, serviceName); 211 if (address != null) { 212 result = address.getLocationURI(); 213 } 214 return result; 215 } 216 217 226 public static String getServiceAddressFromWSDLDocument( 227 Document serviceDesc, QName serviceName) throws WSDLException, 228 TransformerException { 229 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 230 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 231 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 232 return getServiceAddressFromWSDLDefinition(serviceDef, serviceName); 233 } 234 235 244 public static String getServiceAddressFromWSDLFile(File wsdlFile, 245 QName serviceName) throws WSDLException, TransformerException { 246 Document doc = createDocumentFromWSDL(wsdlFile); 247 return getServiceAddressFromWSDLDocument(doc, serviceName); 248 } 249 250 257 @SuppressWarnings ("unchecked") 258 public static QName [] getServiceNameFromWSDLDefinition(Definition serviceDef) { 259 QName [] result = null; 260 if (serviceDef != null) { 261 Map <String , Service> services = serviceDef.getServices(); 262 result = new QName [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 280 public static QName [] getServiceNameFromWSDLDocument(Document serviceDesc) 281 throws WSDLException, TransformerException { 282 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 283 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 284 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 285 return getServiceNameFromWSDLDefinition(serviceDef); 286 } 287 288 297 public static QName [] getServiceNameFromWSDLFile(File wsdlFile) 298 throws WSDLException, TransformerException { 299 Document doc = createDocumentFromWSDL(wsdlFile); 300 return getServiceNameFromWSDLDocument(doc); 301 } 302 303 310 public static String getTargetNSFromWSDLDefinition(Definition serviceDef) { 311 return serviceDef.getTargetNamespace(); 312 } 313 314 323 public static String getTargetNSFromWSDLDocument(Document serviceDesc) 324 throws WSDLException, TransformerException { 325 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 326 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 327 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 328 return getTargetNSFromWSDLDefinition(serviceDef); 329 } 330 331 340 public static boolean hasOperationNamed(Definition serviceDef, 341 String opName, QName serviceName) { 342 BindingOperation op = findOperationNamed(serviceDef, opName, 343 serviceName); 344 return op != null; 345 } 346 347 358 public static boolean hasOperationNamed(Document serviceDesc, 359 String opName, QName serviceName) throws WSDLException, 360 TransformerException { 361 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 362 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 363 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 364 return hasOperationNamed(serviceDef, opName, serviceName); 365 } 366 367 378 public static boolean isInOutOperation(Definition serviceDef, 379 String opName, QName 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 402 public static boolean isInOutOperation(Document serviceDesc, String opName, 403 QName serviceName) throws WSDLException, TransformerException { 404 String docString = XMLHelper.createStringFromDOMDocument(serviceDesc); 405 Document tmpDoc = XMLHelper.createDocumentFromString(docString); 406 Definition serviceDef = createDefinitionFromDocument(tmpDoc); 407 return isInOutOperation(serviceDef, opName, serviceName); 408 } 409 410 protected static Document createDocumentFromDefinition(Definition def) 411 throws WSDLException { 412 Document result = null; 413 WSDLWriter writer = new WSDLWriterImpl(); 414 OutputStream outStream = new ByteArrayOutputStream (); 415 writer.writeWSDL(def, outStream); 416 String documentToString = outStream.toString(); 417 result = XMLHelper.createDocumentFromString(documentToString); 418 return result; 419 } 420 421 @SuppressWarnings ("unchecked") 422 protected static BindingOperation findOperationNamed(Definition serviceDef, 423 String opName, QName 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 <String , Port> ports = service.getPorts(); 431 for (Port port : ports.values()) { 432 List <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 463 public static Service findService(Definition definition, QName service) { 464 Service result = null; 465 466 if (definition != null && service != null) { 467 for (Iterator iter = definition.getServices().keySet().iterator(); iter 468 .hasNext() 469 && result == null;) { 470 QName element = (QName ) iter.next(); 471 if (equals(element, service)) { 472 result = definition.getService(element); 473 } 474 475 } 476 } 477 return result; 478 } 479 480 489 public static boolean equals(QName a, QName 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 510 public static Document createLightWSDL20(QName interfaceName, QName serviceName, String endpointName) { 511 512 StringBuffer s= new StringBuffer (); 513 514 515 String 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 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 s.append(" <interface name='"+interfaceName.getLocalPart()+"'></interface>\n"); 530 531 s.append(" <binding name='"+endpointName+"'></binding>\n"); 533 534 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 ByteArrayInputStream is = new ByteArrayInputStream (s.toString().getBytes()); 544 545 Document doc = null; 546 DocumentBuilderFactory factory = DocumentBuilderFactory 547 .newInstance(); 548 factory.setNamespaceAware(true); 549 try { 550 DocumentBuilder builder = factory.newDocumentBuilder(); 551 doc= builder.parse(is); 552 } catch (Exception e) { 553 } 555 return doc; 556 } 557 558 } 559 | Popular Tags |