1 16 package org.apache.axis2.wsdl.builder.wsdl4j; 17 18 import com.ibm.wsdl.extensions.soap.SOAPConstants; 19 import org.apache.axis2.wsdl.builder.WSDLComponentFactory; 20 import org.apache.crimson.tree.XmlDocument; 21 import org.apache.wsdl.*; 22 import org.apache.wsdl.extensions.DefaultExtensibilityElement; 23 import org.apache.wsdl.extensions.ExtensionConstants; 24 import org.apache.wsdl.extensions.ExtensionFactory; 25 import org.apache.wsdl.impl.WSDLProcessingException; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 29 import javax.wsdl.*; 30 import javax.wsdl.extensions.ExtensibilityElement; 31 import javax.wsdl.extensions.UnknownExtensibilityElement; 32 import javax.wsdl.extensions.schema.Schema; 33 import javax.wsdl.extensions.soap.SOAPAddress; 34 import javax.wsdl.extensions.soap.SOAPBinding; 35 import javax.wsdl.extensions.soap.SOAPBody; 36 import javax.wsdl.extensions.soap.SOAPOperation; 37 import javax.xml.namespace.QName ; 38 import java.util.Iterator ; 39 import java.util.LinkedList ; 40 import java.util.List ; 41 import java.util.Map ; 42 43 46 public class WSDLPump { 47 48 private static final String BOUND_INTERFACE_NAME = "BoundInterface"; 49 50 private WSDLDescription womDefinition; 51 52 private Definition wsdl4jParsedDefinition; 53 54 private WSDLComponentFactory wsdlComponenetFactory; 55 56 57 private List resolvedMultipartMessageList = new LinkedList (); 58 59 public WSDLPump(WSDLDescription womDefinition, 60 Definition wsdl4jParsedDefinition) { 61 this(womDefinition, wsdl4jParsedDefinition, womDefinition); 62 } 63 64 public WSDLPump(WSDLDescription womDefinition, 65 Definition wsdl4jParsedDefinition, 66 WSDLComponentFactory wsdlComponentFactory) { 67 this.womDefinition = womDefinition; 68 this.wsdl4jParsedDefinition = wsdl4jParsedDefinition; 69 this.wsdlComponenetFactory = wsdlComponentFactory; 70 } 71 72 public void pump() { 73 if (null != this.wsdl4jParsedDefinition && null != this.womDefinition) { 74 this.populateDefinition(this.womDefinition, 75 this.wsdl4jParsedDefinition); 76 } else { 77 throw new WSDLProcessingException("Properties not set properly"); 78 } 79 80 } 81 82 private void populateDefinition(WSDLDescription wsdlDefinition, 83 Definition wsdl4JDefinition) { 84 wsdlDefinition.setWSDL1DefinitionName(wsdl4JDefinition.getQName()); 86 wsdlDefinition 87 .setTargetNameSpace(wsdl4JDefinition.getTargetNamespace()); 88 wsdlDefinition.setNamespaces(wsdl4JDefinition.getNamespaces()); 89 this.copyExtensibleElements(wsdl4JDefinition.getExtensibilityElements(), wsdlDefinition); 90 91 103 106 Types wsdl4jTypes = wsdl4JDefinition.getTypes(); 107 if (null != wsdl4jTypes) { 108 WSDLTypes wsdlTypes = this.wsdlComponenetFactory.createTypes(); 109 110 111 this.copyExtensibleElements(wsdl4jTypes.getExtensibilityElements(), 112 wsdlTypes); 113 114 this.womDefinition.setTypes(wsdlTypes); 115 } 116 117 122 Iterator portTypeIterator = wsdl4JDefinition.getPortTypes().values() 123 .iterator(); 124 WSDLInterface wsdlInterface; 125 PortType portType; 126 while (portTypeIterator.hasNext()) { 127 wsdlInterface = this.wsdlComponenetFactory.createInterface(); 128 portType = (PortType) portTypeIterator.next(); 129 this.populateInterfaces(wsdlInterface, portType); 130 this.copyExtensibilityAttribute(portType.getExtensionAttributes(), 131 wsdlInterface); 132 wsdlDefinition.addInterface(wsdlInterface); 133 } 134 135 140 Iterator bindingIterator = wsdl4JDefinition.getBindings().values() 141 .iterator(); 142 WSDLBinding wsdlBinding; 143 Binding wsdl4jBinding; 144 while (bindingIterator.hasNext()) { 145 wsdlBinding = this.wsdlComponenetFactory.createBinding(); 146 wsdl4jBinding = (Binding) bindingIterator.next(); 147 this.populateBindings(wsdlBinding, wsdl4jBinding); 148 this.copyExtensibleElements(wsdl4jBinding.getExtensibilityElements(), 149 wsdlBinding); 150 wsdlDefinition.addBinding(wsdlBinding); 151 152 } 153 154 156 Iterator serviceIterator = wsdl4JDefinition.getServices().values() 157 .iterator(); 158 WSDLService wsdlService; 159 Service wsdl4jService; 160 while (serviceIterator.hasNext()) { 161 wsdlService = this.wsdlComponenetFactory.createService(); 162 wsdl4jService = (Service) serviceIterator.next(); 163 this.populateServices(wsdlService, wsdl4jService); 164 this.copyExtensibleElements(wsdl4jService.getExtensibilityElements(), 165 wsdlService); 166 wsdlDefinition.addService(wsdlService); 167 } 168 169 } 170 171 175 181 private void populateInterfaces(WSDLInterface wsdlInterface, 184 PortType wsdl4jPortType) { 185 186 wsdlInterface.setName(wsdl4jPortType.getQName()); 190 Iterator wsdl4JOperationsIterator = 191 wsdl4jPortType.getOperations().iterator(); 192 WSDLOperation wsdloperation; 193 Operation wsdl4jOperation; 194 while (wsdl4JOperationsIterator.hasNext()) { 195 wsdloperation = this.wsdlComponenetFactory.createOperation(); 196 wsdl4jOperation = (Operation) wsdl4JOperationsIterator.next(); 197 this.populateOperations(wsdloperation, 198 wsdl4jOperation, 199 wsdl4jPortType.getQName().getNamespaceURI()); 200 this.copyExtensibleElements(wsdl4jOperation.getExtensibilityElements(), wsdloperation); 201 wsdlInterface.setOperation(wsdloperation); 202 } 203 } 204 205 208 private void populateBindings(WSDLBinding wsdlBinding, Binding wsdl4JBinding) { 209 wsdlBinding.setName(wsdl4JBinding.getQName()); 211 QName interfaceName = wsdl4JBinding.getPortType().getQName(); 212 WSDLInterface wsdlInterface = 213 this.womDefinition.getInterface(interfaceName); 214 215 if (null == wsdlInterface) 217 throw new WSDLProcessingException( 218 "Interface/PortType not found for the Binding :" 219 + wsdlBinding.getName()); 220 wsdlBinding.setBoundInterface(wsdlInterface); 221 Iterator bindingoperationsIterator = 222 wsdl4JBinding.getBindingOperations().iterator(); 223 WSDLBindingOperation wsdlBindingOperation; 224 BindingOperation wsdl4jBindingOperation; 225 while (bindingoperationsIterator.hasNext()) { 226 wsdlBindingOperation = 227 this.wsdlComponenetFactory.createWSDLBindingOperation(); 228 wsdl4jBindingOperation = 229 (BindingOperation)bindingoperationsIterator.next(); 230 this.populateBindingOperation(wsdlBindingOperation, 231 wsdl4jBindingOperation, 232 wsdl4JBinding.getQName().getNamespaceURI()); 233 wsdlBindingOperation.setOperation( 234 wsdlInterface.getOperation(wsdl4jBindingOperation.getOperation().getName()) 235 ); 236 this.copyExtensibleElements( 237 wsdl4jBindingOperation.getExtensibilityElements(), 238 wsdlBindingOperation); 239 wsdlBinding.addBindingOperation(wsdlBindingOperation); 240 } 241 242 } 243 244 public void populateServices(WSDLService wsdlService, Service wsdl4jService) { 245 wsdlService.setName(wsdl4jService.getQName()); 246 Iterator wsdl4jportsIterator = 247 wsdl4jService.getPorts().values().iterator(); 248 wsdlService.setServiceInterface(this.getBoundInterface(wsdlService)); 249 WSDLEndpoint wsdlEndpoint; 250 Port wsdl4jPort; 251 while (wsdl4jportsIterator.hasNext()) { 252 wsdlEndpoint = this.wsdlComponenetFactory.createEndpoint(); 253 wsdl4jPort = (Port) wsdl4jportsIterator.next(); 254 this.populatePorts(wsdlEndpoint, 255 wsdl4jPort, 256 wsdl4jService.getQName().getNamespaceURI()); 257 this.copyExtensibleElements(wsdl4jPort.getExtensibilityElements(), 258 wsdlEndpoint); 259 wsdlService.setEndpoint(wsdlEndpoint); 260 } 261 262 } 263 264 public void populateOperations(WSDLOperation wsdlOperation, 267 Operation wsdl4jOperation, String nameSpaceOfTheOperation) { 268 wsdlOperation.setName(new QName (nameSpaceOfTheOperation, 270 wsdl4jOperation.getName())); 271 272 Input wsdl4jInputMessage = wsdl4jOperation.getInput(); 276 277 if(null != wsdl4jInputMessage){ 278 MessageReference wsdlInputMessage = this.wsdlComponenetFactory 279 .createMessageReference(); 280 wsdlInputMessage.setDirection(WSDLConstants.WSDL_MESSAGE_DIRECTION_IN); 281 wsdlInputMessage.setMessageLabel(WSDLConstants.MESSAGE_LABEL_IN_VALUE); 282 283 Message message = wsdl4jInputMessage.getMessage(); 284 if(null != message){ 285 wsdlInputMessage.setElement(this.generateReferenceQname(message)); 286 this.copyExtensibleElements( 287 (message).getExtensibilityElements(), 288 wsdlInputMessage 289 ); 290 } 291 this.copyExtensibilityAttribute(wsdl4jInputMessage.getExtensionAttributes(), 292 wsdlInputMessage); 293 wsdlOperation.setInputMessage(wsdlInputMessage); 294 } 295 296 Output wsdl4jOutputMessage = wsdl4jOperation.getOutput(); 298 if(null != wsdl4jOutputMessage){ 299 MessageReference wsdlOutputMessage = 300 this.wsdlComponenetFactory.createMessageReference(); 301 wsdlOutputMessage.setDirection(WSDLConstants.WSDL_MESSAGE_DIRECTION_OUT); 302 wsdlOutputMessage.setMessageLabel(WSDLConstants.MESSAGE_LABEL_OUT_VALUE); 303 304 Message outputMessage = wsdl4jOutputMessage.getMessage(); 305 if(null != outputMessage){ 306 wsdlOutputMessage.setElement(this.generateReferenceQname(outputMessage)); 307 this.copyExtensibleElements( 308 (outputMessage).getExtensibilityElements(), 309 wsdlOutputMessage 310 ); 311 } 312 this.copyExtensibilityAttribute(wsdl4jOutputMessage.getExtensionAttributes(), 313 wsdlOutputMessage); 314 wsdlOperation.setOutputMessage(wsdlOutputMessage); 315 } 316 317 Map faults = wsdl4jOperation.getFaults(); 318 Iterator faultKeyIterator = faults.keySet().iterator(); 319 WSDLFaultReference faultReference = null; 320 321 while(faultKeyIterator.hasNext()){ 322 323 Fault fault = (Fault)faults.get(faultKeyIterator.next()); 324 faultReference = wsdlComponenetFactory.createFaultReference(); 325 faultReference.setDirection(WSDLConstants.WSDL_MESSAGE_DIRECTION_OUT); 326 Message faultMessage = fault.getMessage(); 327 if(null != faultMessage){ 328 faultReference.setRef(this.generateReferenceQname(faultMessage)); 329 } 330 wsdlOperation.addOutFault(faultReference); 331 this.copyExtensibilityAttribute(fault.getExtensionAttributes(), faultReference); 332 334 } 335 336 wsdlOperation.setMessageExchangePattern(WSDL11MEPFinder 338 .getMEP(wsdl4jOperation)); 339 340 } 341 private QName generateReferenceQname(Message wsdl4jMessage){ 342 QName referenceQName = null; 343 if (wsdl4jMessage.getParts().size() > 1){ 344 346 Iterator multipartListIterator = this.resolvedMultipartMessageList.iterator(); 348 boolean multipartAlreadyResolved = false; 349 while(multipartListIterator.hasNext() && !multipartAlreadyResolved){ 350 QName temp = (QName )multipartListIterator.next(); 351 multipartAlreadyResolved = wsdl4jMessage.getQName().equals(temp); 352 } 353 if(multipartAlreadyResolved){ 354 referenceQName = wsdl4jMessage.getQName(); 359 }else{ 360 Map parts = wsdl4jMessage.getParts(); 363 Element element = null; 364 WSDLTypes types = womDefinition.getTypes(); 365 if(null == types){ 366 XmlDocument newDoc = new XmlDocument(); 367 368 Element schemaElement = newDoc.createElement("schema"); types =wsdlComponenetFactory.createTypes(); 370 ExtensionFactory extensionFactory = wsdlComponenetFactory.createExtensionFactory(); 371 org.apache.wsdl.extensions.Schema typesElement = (org.apache.wsdl.extensions.Schema)extensionFactory.getExtensionElement(ExtensionConstants.SCHEMA); 372 typesElement.setElelment(schemaElement); 373 types.addExtensibilityElement(typesElement); 374 this.womDefinition.setTypes(types); 375 } 376 Iterator schemaEIIterator = types.getExtensibilityElements().iterator(); 377 while(schemaEIIterator.hasNext()){ 378 WSDLExtensibilityElement temp = (WSDLExtensibilityElement)schemaEIIterator.next(); 379 if(ExtensionConstants.SCHEMA.equals(temp.getType())){ 380 element = ((org.apache.wsdl.extensions.Schema)temp).getElelment(); 381 break; 382 } 383 } 384 385 Document doc = element.getOwnerDocument(); 386 String name = wsdl4jMessage.getQName().getLocalPart(); 387 Element newElement = doc.createElement("complexType"); 388 newElement.setAttribute("name", name); 389 390 Element cmplxContent = doc.createElement("complexContent"); 391 Element child; 392 Iterator iterator = parts.keySet().iterator(); 393 while(iterator.hasNext()){ 394 Part part = (Part)parts.get(iterator.next()); 395 QName elementName = part.getElementName(); 396 if(null == elementName){ 397 elementName = part.getTypeName(); 398 } 399 400 401 child = doc.createElement("element"); 402 child.setAttribute("name", "var"+elementName.getLocalPart()); 403 child.setAttribute("type", elementName.getNamespaceURI()+":"+elementName.getLocalPart()); 404 cmplxContent.appendChild(child); 405 } 406 407 408 newElement.appendChild(cmplxContent); 409 410 element.appendChild(newElement); 411 referenceQName = wsdl4jMessage.getQName(); 414 419 this.resolvedMultipartMessageList.add(wsdl4jMessage.getQName()); 420 421 } 422 }else{ 423 Iterator outputIterator = 425 wsdl4jMessage.getParts().values().iterator(); 426 if (outputIterator.hasNext()) { 427 Part outPart = ((Part) outputIterator.next()); 428 QName typeName; 429 if (null != (typeName = outPart.getTypeName())) { 430 referenceQName = typeName; 431 } else { 432 referenceQName = outPart.getElementName(); 433 } 434 } 435 } 436 return referenceQName; 437 } 438 439 private void populateBindingOperation( 440 WSDLBindingOperation wsdlBindingOperation, 441 BindingOperation wsdl4jBindingOperation, 442 String nameSpaceOfTheBindingOperation) { 443 444 wsdlBindingOperation.setName(new QName (nameSpaceOfTheBindingOperation, 445 wsdl4jBindingOperation.getName())); 446 BindingInput wsdl4jInputBinding = 447 wsdl4jBindingOperation.getBindingInput(); 448 if(null != wsdl4jInputBinding){ 449 WSDLBindingMessageReference wsdlInputBinding = 450 this.wsdlComponenetFactory.createWSDLBindingMessageReference(); 451 wsdlInputBinding.setDirection(WSDLConstants.WSDL_MESSAGE_DIRECTION_IN); 452 this.copyExtensibleElements(wsdl4jInputBinding.getExtensibilityElements(), 453 wsdlInputBinding); 454 wsdlBindingOperation.setInput(wsdlInputBinding); 455 } 456 457 BindingOutput wsdl4jOutputBinding = wsdl4jBindingOperation 458 .getBindingOutput(); 459 if(null != wsdl4jOutputBinding){ 460 WSDLBindingMessageReference wsdlOutputBinding = this.wsdlComponenetFactory 461 .createWSDLBindingMessageReference(); 462 wsdlOutputBinding.setDirection(WSDLConstants.WSDL_MESSAGE_DIRECTION_OUT); 463 464 this.copyExtensibleElements(wsdl4jOutputBinding.getExtensibilityElements(), 465 wsdlOutputBinding); 466 wsdlBindingOperation.setOutput(wsdlOutputBinding); 467 } 468 469 470 Map bindingFaults = wsdl4jBindingOperation.getBindingFaults(); 471 Iterator keyIterator = bindingFaults.keySet().iterator(); 472 while(keyIterator.hasNext()){ 473 BindingFault bindingFault = (BindingFault)bindingFaults.get(keyIterator.next()); 474 WSDLBindingFault womBindingFault = this.wsdlComponenetFactory.createBindingFault(); 475 this.copyExtensibleElements(bindingFault.getExtensibilityElements(), womBindingFault); 476 wsdlBindingOperation.addOutFault(womBindingFault); 477 } 478 479 } 480 481 public void populatePorts(WSDLEndpoint wsdlEndpoint, Port wsdl4jPort, 482 String targetNamspace) { 483 wsdlEndpoint.setName(new QName (targetNamspace, wsdl4jPort.getName())); 484 485 wsdlEndpoint.setBinding(this.womDefinition.getBinding(wsdl4jPort 486 .getBinding().getQName())); 487 488 } 489 490 504 private WSDLInterface getBoundInterface(WSDLService service) { 505 506 if (0 == this.womDefinition.getWsdlInterfaces().size()) 508 throw new WSDLProcessingException( 509 "There are no " 510 + "Interfaces/PortTypes identified in the current partially built" 511 + "WOM"); 512 513 if (1 == this.womDefinition.getWsdlInterfaces().size()) 517 return (WSDLInterface) this.womDefinition.getWsdlInterfaces() 518 .values().iterator().next(); 519 520 WSDLInterface newBoundInterface = this.womDefinition.createInterface(); 526 newBoundInterface.setName(new QName (service.getNamespace(), service 527 .getName().getLocalPart() 528 + BOUND_INTERFACE_NAME)); 529 Iterator interfaceIterator = this.womDefinition.getWsdlInterfaces() 530 .values().iterator(); 531 while (interfaceIterator.hasNext()) { 532 newBoundInterface 533 .addSuperInterface((WSDLInterface) interfaceIterator.next()); 534 } 535 return newBoundInterface; 536 } 537 538 545 private void copyExtensibleElements(List wsdl4jExtensibleElements, 546 Component component) { 547 Iterator iterator = wsdl4jExtensibleElements.iterator(); 548 ExtensionFactory extensionFactory = this.wsdlComponenetFactory 549 .createExtensionFactory(); 550 while (iterator.hasNext()) { 551 552 ExtensibilityElement wsdl4jElement = (ExtensibilityElement) iterator 553 .next(); 554 555 if (wsdl4jElement instanceof UnknownExtensibilityElement) { 556 UnknownExtensibilityElement unknown = (UnknownExtensibilityElement) (wsdl4jElement); 557 DefaultExtensibilityElement extensibilityElement = (DefaultExtensibilityElement) extensionFactory 558 .getExtensionElement(wsdl4jElement.getElementType()); 559 extensibilityElement.setElement(unknown.getElement()); 560 Boolean required = unknown.getRequired(); 561 if (null != required) { 562 extensibilityElement.setRequired(required.booleanValue()); 563 } 564 component.addExtensibilityElement(extensibilityElement); 565 } else if (wsdl4jElement instanceof SOAPAddress) { 566 SOAPAddress soapAddress = (SOAPAddress) wsdl4jElement; 567 org.apache.wsdl.extensions.SOAPAddress extensibilityElement = (org.apache.wsdl.extensions.SOAPAddress) extensionFactory 568 .getExtensionElement(soapAddress.getElementType()); 569 extensibilityElement.setLocationURI(soapAddress 570 .getLocationURI()); 571 Boolean required = soapAddress.getRequired(); 572 if (null != required) { 573 extensibilityElement.setRequired(required.booleanValue()); 574 } 575 component.addExtensibilityElement(extensibilityElement); 576 }else if(wsdl4jElement instanceof Schema) { 577 Schema schema = (Schema)wsdl4jElement; 578 org.apache.wsdl.extensions.Schema extensibilityElement = (org.apache.wsdl.extensions.Schema)extensionFactory.getExtensionElement(schema.getElementType()); 579 extensibilityElement.setElelment(schema.getElement()); 580 Boolean required = schema.getRequired(); 581 if(null != required){ 582 extensibilityElement.setRequired(required.booleanValue()); 583 } 584 component.addExtensibilityElement(extensibilityElement); 585 }else if(SOAPConstants.Q_ELEM_SOAP_OPERATION.equals(wsdl4jElement.getElementType())){ 586 SOAPOperation soapOperation = (SOAPOperation)wsdl4jElement; 587 org.apache.wsdl.extensions.SOAPOperation extensibilityElement = (org.apache.wsdl.extensions.SOAPOperation)extensionFactory.getExtensionElement(soapOperation.getElementType()); 588 extensibilityElement.setSoapAction(soapOperation.getSoapActionURI()); 589 extensibilityElement.setStyle(soapOperation.getStyle()); 590 Boolean required = soapOperation.getRequired(); 591 if(null != required){ 592 extensibilityElement.setRequired(required.booleanValue()); 593 } 594 component.addExtensibilityElement(extensibilityElement); 595 }else if(SOAPConstants.Q_ELEM_SOAP_BODY.equals(wsdl4jElement.getElementType())){ 596 SOAPBody soapBody = (SOAPBody)wsdl4jElement; 597 org.apache.wsdl.extensions.SOAPBody extensibilityElement = (org.apache.wsdl.extensions.SOAPBody)extensionFactory.getExtensionElement(soapBody.getElementType()); 598 extensibilityElement.setNamespaceURI(soapBody.getNamespaceURI()); 599 extensibilityElement.setUse(soapBody.getUse()); 600 Boolean required = soapBody.getRequired(); 601 if(null != required){ 602 extensibilityElement.setRequired(required.booleanValue()); 603 } 604 component.addExtensibilityElement(extensibilityElement); 605 }else if(SOAPConstants.Q_ELEM_SOAP_BINDING.equals(wsdl4jElement.getElementType())){ 606 SOAPBinding soapBinding = (SOAPBinding)wsdl4jElement; 607 org.apache.wsdl.extensions.SOAPBinding extensibilityElement = (org.apache.wsdl.extensions.SOAPBinding)extensionFactory.getExtensionElement(soapBinding.getElementType()); 608 extensibilityElement.setTransportURI(soapBinding.getTransportURI()); 609 extensibilityElement.setStyle(soapBinding.getStyle()); 610 Boolean required = soapBinding.getRequired(); 611 if(null != required){ 612 extensibilityElement.setRequired(required.booleanValue()); 613 } 614 component.addExtensibilityElement(extensibilityElement); 615 }else { 616 } 619 } 620 } 621 622 629 private void copyExtensibilityAttribute(Map wsdl4jExtensibilityAttributes, 630 Component component) { 631 Iterator iterator = wsdl4jExtensibilityAttributes.keySet().iterator(); 632 while (iterator.hasNext()) { 633 QName attributeName = (QName ) iterator.next(); 634 QName value = (QName ) wsdl4jExtensibilityAttributes 635 .get(attributeName); 636 WSDLExtensibilityAttribute attribute = this.wsdlComponenetFactory 637 .createWSDLExtensibilityAttribute(); 638 attribute.setKey(attributeName); 639 attribute.setValue(value); 640 component.addExtensibleAttributes(attribute); 641 } 642 } 643 644 } | Popular Tags |