1 package org.objectweb.celtix.tools.processors.wsdl2.internal; 2 3 import java.util.Collection ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.logging.Logger ; 9 10 import javax.wsdl.Binding; 11 import javax.wsdl.BindingOperation; 12 import javax.wsdl.BindingOutput; 13 import javax.wsdl.Definition; 14 import javax.wsdl.Message; 15 import javax.wsdl.Operation; 16 import javax.wsdl.Part; 17 import javax.wsdl.Port; 18 import javax.wsdl.PortType; 19 import javax.wsdl.Service; 20 import javax.wsdl.extensions.http.HTTPAddress; 21 import javax.wsdl.extensions.http.HTTPBinding; 22 import javax.wsdl.extensions.mime.MIMEMultipartRelated; 23 import javax.wsdl.extensions.soap.SOAPAddress; 24 import javax.wsdl.extensions.soap.SOAPBinding; 25 import javax.wsdl.extensions.soap.SOAPBody; 26 import javax.wsdl.extensions.soap.SOAPHeader; 27 import javax.wsdl.extensions.soap.SOAPOperation; 28 import javax.xml.namespace.QName ; 29 30 import org.objectweb.celtix.common.logging.LogUtils; 31 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 32 import org.objectweb.celtix.tools.common.ToolConstants; 33 import org.objectweb.celtix.tools.common.ToolException; 34 import org.objectweb.celtix.tools.common.model.JavaAnnotation; 35 import org.objectweb.celtix.tools.common.model.JavaInterface; 36 import org.objectweb.celtix.tools.common.model.JavaMethod; 37 import org.objectweb.celtix.tools.common.model.JavaModel; 38 import org.objectweb.celtix.tools.common.model.JavaParameter; 39 import org.objectweb.celtix.tools.common.model.JavaPort; 40 import org.objectweb.celtix.tools.common.model.JavaServiceClass; 41 import org.objectweb.celtix.tools.common.model.JavaType; 42 import org.objectweb.celtix.tools.common.toolspec.parser.CommandLineParser; 43 import org.objectweb.celtix.tools.extensions.jaxws.CustomizationParser; 44 import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBinding; 45 import org.objectweb.celtix.tools.extensions.jms.JMSAddress; 46 import org.objectweb.celtix.tools.utils.ProcessorUtil; 47 48 public class ServiceProcessor extends AbstractProcessor { 49 50 private static final Logger LOG = LogUtils.getL7dLogger(CommandLineParser.class); 51 52 private String soapOPAction = "SOAPACTION"; 53 54 private String soapOPStyle = "STYLE"; 55 56 private Definition definition; 57 58 private BindingType bindingType; 59 60 private final int inHEADER = 1; 61 62 private final int outHEADER = 2; 63 64 private final int resultHeader = 3; 65 66 private final int noHEADER = 0; 67 68 private Object bindingObj; 69 70 public ServiceProcessor(ProcessorEnvironment penv) { 71 super(penv); 72 } 73 74 public ServiceProcessor(ProcessorEnvironment penv, Definition def) { 75 super(penv); 76 this.definition = def; 77 } 78 79 public void process(JavaModel model) throws ToolException { 80 81 Collection services = definition.getServices().values(); 82 if (services.size() == 0) { 83 Iterator bindingIte = definition.getBindings().values().iterator(); 84 while (bindingIte.hasNext()) { 85 Binding binding = (Binding)bindingIte.next(); 86 Iterator bopIte = binding.getBindingOperations().iterator(); 87 while (bopIte.hasNext()) { 88 BindingOperation bop = (BindingOperation)bopIte.next(); 89 processOperation(model, bop, binding); 90 } 91 } 92 return; 93 } 94 Iterator ite = services.iterator(); 95 while (ite.hasNext()) { 96 Service service = (Service)ite.next(); 97 processService(model, service); 98 } 99 } 100 101 private boolean isNameCollision(String packageName, String className) { 102 return collector.containTypesClass(packageName, className) 103 || collector.containSeiClass(packageName, className) 104 || collector.containExceptionClass(packageName, className); 105 } 106 107 private void processService(JavaModel model, Service service) throws ToolException { 108 JavaServiceClass sclz = new JavaServiceClass(model); 109 String name = ProcessorUtil.mangleNameToClassName(service.getQName().getLocalPart()); 110 String namespace = service.getQName().getNamespaceURI(); 111 String packageName = ProcessorUtil.parsePackageName(namespace, env.mapPackageName(namespace)); 112 113 while (isNameCollision(packageName, name)) { 114 name = name + "_Service"; 115 } 116 117 sclz.setName(name); 118 sclz.setNamespace(namespace); 119 sclz.setPackageName(packageName); 120 121 Map ports = service.getPorts(); 122 123 for (Iterator ite = ports.values().iterator(); ite.hasNext();) { 124 Port port = (Port)ite.next(); 125 JavaPort javaport = processPort(model, port); 126 sclz.addPort(javaport); 127 } 128 model.addServiceClass(name, sclz); 129 } 130 131 private JavaPort processPort(JavaModel model, Port port) throws ToolException { 132 JavaPort jport = new JavaPort(port.getName()); 133 Binding binding = port.getBinding(); 134 jport.setBindingAdress(getPortAddress(port)); 136 jport.setBindingName(binding.getQName().getLocalPart()); 137 138 String namespace = binding.getPortType().getQName().getNamespaceURI(); 139 String packageName = ProcessorUtil.parsePackageName(namespace, env.mapPackageName(namespace)); 140 jport.setPackageName(packageName); 141 142 String portType = binding.getPortType().getQName().getLocalPart(); 143 jport.setPortType(portType); 144 jport.setInterfaceClass(ProcessorUtil.mangleNameToClassName(portType)); 145 146 bindingType = getBindingType(binding); 147 148 if (bindingType == null) { 149 org.objectweb.celtix.common.i18n.Message msg = 150 new org.objectweb.celtix.common.i18n.Message("BINDING_SPECIFY_ONE_PROTOCOL", 151 LOG, 152 binding.getQName()); 153 throw new ToolException(msg); 154 } 155 156 if (isSoapBinding()) { 157 SOAPBinding spbd = (SOAPBinding)this.bindingObj; 158 jport.setStyle(getSoapStyle(spbd.getStyle())); 159 jport.setTransURI(spbd.getTransportURI()); 160 } 161 162 165 166 Iterator ite = binding.getBindingOperations().iterator(); 167 while (ite.hasNext()) { 168 BindingOperation bop = (BindingOperation)ite.next(); 169 processOperation(model, bop, binding); 170 } 171 return jport; 172 } 173 174 private javax.jws.soap.SOAPBinding.Style getSoapStyle(String soapStyle) { 175 if ("".equals(soapStyle)) { 176 return null; 177 } else if ("RPC".equalsIgnoreCase(soapStyle)) { 178 return javax.jws.soap.SOAPBinding.Style.RPC; 179 } else { 180 return javax.jws.soap.SOAPBinding.Style.DOCUMENT; 181 } 182 } 183 184 private javax.jws.soap.SOAPBinding.Use getSoapUse(String soapUse) { 185 186 if ("".equals(soapUse)) { 187 return null; 188 } else if ("ENCODED".equalsIgnoreCase(soapUse)) { 189 return javax.jws.soap.SOAPBinding.Use.ENCODED; 190 } else { 191 return javax.jws.soap.SOAPBinding.Use.LITERAL; 192 } 193 194 } 195 196 197 198 private void processOperation(JavaModel model, BindingOperation bop, Binding binding) 199 throws ToolException { 200 String portType = ProcessorUtil 201 .mangleNameToClassName(binding.getPortType().getQName().getLocalPart()); 202 JavaInterface jf = model.getInterfaces().get(portType); 203 doCustomizeBinding(model, jf, binding); 205 if (isSoapBinding()) { 206 SOAPBinding soapBinding = (SOAPBinding)bindingObj; 207 if (getSoapStyle(soapBinding.getStyle()) == null) { 208 jf.setSOAPStyle(javax.jws.soap.SOAPBinding.Style.DOCUMENT); 209 } else { 210 jf.setSOAPStyle(getSoapStyle(soapBinding.getStyle())); 211 } 212 } else { 213 jf.setSOAPStyle(javax.jws.soap.SOAPBinding.Style.DOCUMENT); 215 } 216 217 Object [] methods = jf.getMethods().toArray(); 218 for (int i = 0; i < methods.length; i++) { 219 JavaMethod jm = (JavaMethod)methods[i]; 220 if (jm.getOperationName() != null && jm.getOperationName().equals(bop.getName())) { 221 if (isSoapBinding()) { 222 doCustomizeOperation(jf, jm, bop); 223 Map prop = getSoapOperationProp(bop); 224 String soapAction = prop.get(soapOPAction) == null ? "" : (String )prop.get(soapOPAction); 225 String soapStyle = prop.get(soapOPStyle) == null ? "" : (String )prop.get(soapOPStyle); 226 jm.setSoapAction(soapAction); 227 if (getSoapStyle(soapStyle) == null && this.bindingObj == null) { 228 org.objectweb.celtix.common.i18n.Message msg = 229 new org.objectweb.celtix.common.i18n.Message("BINDING_STYLE_NOT_DEFINED", 230 LOG); 231 throw new ToolException(msg); 232 } 233 if (getSoapStyle(soapStyle) == null) { 234 jm.setSoapStyle(jf.getSOAPStyle()); 235 } else { 236 jm.setSoapStyle(getSoapStyle(soapStyle)); 237 } 238 } else { 239 jm.setSoapStyle(jf.getSOAPStyle()); 241 } 242 OperationProcessor processor = new OperationProcessor(env); 243 244 int headerType = isNonWrappable(bop); 245 246 if (jm.isWrapperStyle() && headerType > this.noHEADER) { 247 249 jm.setWrapperStyle(false); 250 processor.processMethod(jm, bop.getOperation()); 251 jm.getAnnotationMap().remove("ResponseWrapper"); 252 jm.getAnnotationMap().remove("RequestWrapper"); 253 254 } else { 255 processor.processMethod(jm, bop.getOperation()); 256 257 } 258 259 if (headerType == this.resultHeader) { 260 JavaAnnotation resultAnno = jm.getAnnotationMap().get("WebResult"); 261 if (resultAnno != null) { 262 resultAnno.addArgument("header", "true", ""); 263 } 264 } 265 processParameter(jm, bop); 266 } 267 } 268 } 269 270 private void setParameterAsHeader(JavaParameter parameter) { 271 parameter.setHeader(true); 272 parameter.getAnnotation().addArgument("header", "true", ""); 273 } 274 275 private void processParameter(JavaMethod jm, BindingOperation operation) throws ToolException { 276 277 Iterator inbindings = operation.getBindingInput().getExtensibilityElements().iterator(); 279 String use = null; 280 while (inbindings.hasNext()) { 281 Object obj = inbindings.next(); 282 if (obj instanceof SOAPBody) { 283 SOAPBody soapBody = (SOAPBody)obj; 284 use = soapBody.getUse(); 285 } 286 if (obj instanceof SOAPHeader) { 287 SOAPHeader soapHeader = (SOAPHeader)obj; 288 boolean found = false; 289 for (JavaParameter parameter : jm.getParameters()) { 290 if (soapHeader.getPart().equals(parameter.getPartName())) { 291 setParameterAsHeader(parameter); 292 found = true; 293 } 294 } 295 if (Boolean.valueOf((String )env.get(ToolConstants.CFG_EXTRA_SOAPHEADER)).booleanValue() 296 && !found) { 297 ParameterProcessor processor = new ParameterProcessor(this.env); 301 Part exPart = this.definition.getMessage(soapHeader.getMessage()).getPart( 302 soapHeader 303 .getPart()); 304 JavaType.Style jpStyle = JavaType.Style.IN; 305 if (isInOutParam(soapHeader.getPart(), operation.getBindingOutput())) { 306 jpStyle = JavaType.Style.INOUT; 307 } 308 JavaParameter jp = processor.addParameterFromBinding(jm, exPart, jpStyle); 309 if (soapHeader.getPart() != null && soapHeader.getPart().length() > 0) { 310 jp.getAnnotation().addArgument("partName", soapHeader.getPart()); 311 } 312 setParameterAsHeader(jp); 313 } 314 } 315 if (obj instanceof MIMEMultipartRelated && jm.getBindingExt().isEnableMime()) { 316 LOG.warning("The MIME content in wsdl file will be ignored, " 318 + "current version does not support MIME content"); 319 } 323 } 324 325 if (operation.getBindingOutput() != null) { 327 Iterator outbindings = operation.getBindingOutput().getExtensibilityElements().iterator(); 328 while (outbindings.hasNext()) { 329 Object obj = outbindings.next(); 330 if (obj instanceof SOAPHeader) { 331 SOAPHeader soapHeader = (SOAPHeader)obj; 332 boolean found = false; 333 for (JavaParameter parameter : jm.getParameters()) { 334 if (soapHeader.getPart().equals(parameter.getPartName())) { 335 setParameterAsHeader(parameter); 336 found = true; 337 } 338 } 339 if (jm.getReturn().getName().equals(soapHeader.getPart())) { 340 found = true; 341 } 342 if (Boolean.valueOf((String )env.get(ToolConstants.CFG_EXTRA_SOAPHEADER)).booleanValue() 343 && !found) { 344 ParameterProcessor processor = new ParameterProcessor(this.env); 348 Part exPart = this.definition.getMessage(soapHeader.getMessage()) 349 .getPart(soapHeader.getPart()); 350 JavaParameter jp = processor.addParameterFromBinding(jm, exPart, JavaType.Style.OUT); 351 setParameterAsHeader(jp); 352 } 353 } 354 if (obj instanceof MIMEMultipartRelated && jm.getBindingExt().isEnableMime()) { 355 LOG.warning("The MIME content in wsdl file will be ignored, " 357 + "current version does not support MIME content"); 358 363 } 364 } 365 } 366 367 jm.setSoapUse(getSoapUse(use)); 368 if (javax.jws.soap.SOAPBinding.Style.RPC == jm.getSoapStyle() 369 && javax.jws.soap.SOAPBinding.Use.ENCODED == jm.getSoapUse()) { 370 System.err.println("** Unsupported RPC-Encoded Style Use **"); 371 } 372 if (javax.jws.soap.SOAPBinding.Style.RPC == jm.getSoapStyle() 373 && javax.jws.soap.SOAPBinding.Use.LITERAL == jm.getSoapUse()) { 374 return; 375 } 376 if (javax.jws.soap.SOAPBinding.Style.DOCUMENT == jm.getSoapStyle() 377 && javax.jws.soap.SOAPBinding.Use.LITERAL == jm.getSoapUse()) { 378 return; 379 } 380 } 381 382 private Map getSoapOperationProp(BindingOperation bop) { 383 Map <String , Object > soapOPProp = new HashMap <String , Object >(); 384 385 if (bop.getExtensibilityElements() != null) { 386 Iterator ite = bop.getExtensibilityElements().iterator(); 387 while (ite.hasNext()) { 388 Object obj = ite.next(); 389 if (obj instanceof SOAPOperation) { 390 SOAPOperation soapOP = (SOAPOperation)obj; 391 soapOPProp.put(this.soapOPAction, soapOP.getSoapActionURI()); 392 393 soapOPProp.put(this.soapOPStyle, soapOP.getStyle()); 394 } 395 } 396 } 397 return soapOPProp; 398 } 399 400 private String getPortAddress(Port port) { 401 Iterator it = port.getExtensibilityElements().iterator(); 402 String address = null; 403 while (it.hasNext()) { 404 Object obj = it.next(); 405 if (obj instanceof SOAPAddress) { 406 address = ((SOAPAddress)obj).getLocationURI(); 407 } 408 if (obj instanceof JMSAddress) { 409 address = ((JMSAddress)obj).getAddress(); 410 } 411 412 if (obj instanceof HTTPAddress) { 413 address = ((HTTPAddress)obj).getLocationURI(); 414 } 415 416 } 417 return address; 418 } 419 420 private BindingType getBindingType(Binding binding) { 421 Iterator it = binding.getExtensibilityElements().iterator(); 422 while (it.hasNext()) { 423 Object obj = it.next(); 424 if (obj instanceof SOAPBinding) { 425 bindingObj = (SOAPBinding)obj; 426 return BindingType.SOAPBinding; 427 } 428 if (obj instanceof HTTPBinding) { 429 bindingObj = (HTTPBinding)obj; 430 return BindingType.HTTPBinding; 431 } 432 return BindingType.XMLBinding; 434 435 } 436 return null; 437 } 438 439 private int isNonWrappable(BindingOperation bop) { 440 String operationName = bop.getName(); 441 Message bodyMessage = null; 442 QName headerMessage = null; 443 SOAPHeader header = null; 444 boolean containParts = false; 445 boolean isSameMessage = false; 446 boolean isNonWrappable = false; 447 boolean allPartsHeader = false; 448 int result = this.noHEADER; 449 450 if (bop.getBindingInput() != null) { 452 Iterator ite = bop.getBindingInput().getExtensibilityElements().iterator(); 453 while (ite.hasNext()) { 454 Object obj = ite.next(); 455 if (obj instanceof SOAPBody) { 456 bodyMessage = getMessage(operationName, true); 457 } 458 if (obj instanceof SOAPHeader) { 459 header = (SOAPHeader)obj; 460 headerMessage = header.getMessage(); 461 if (header.getPart().length() > 0) { 462 containParts = true; 463 } 464 } 465 } 466 467 if (headerMessage != null && bodyMessage != null 468 && headerMessage.getNamespaceURI().equalsIgnoreCase(bodyMessage.getQName().getNamespaceURI()) 469 && headerMessage.getLocalPart().equalsIgnoreCase(bodyMessage.getQName().getLocalPart())) { 470 isSameMessage = true; 471 } 472 473 isNonWrappable = isSameMessage && containParts; 474 if (isNonWrappable) { 476 result = this.inHEADER; 477 } 478 } 479 isSameMessage = false; 480 containParts = false; 481 482 if (bop.getBindingOutput() != null) { 484 Iterator ite1 = bop.getBindingOutput().getExtensibilityElements().iterator(); 485 while (ite1.hasNext()) { 486 Object obj = ite1.next(); 487 if (obj instanceof SOAPBody) { 488 bodyMessage = getMessage(operationName, false); 489 } 490 if (obj instanceof SOAPHeader) { 491 header = (SOAPHeader)obj; 492 headerMessage = header.getMessage(); 493 if (header.getPart().length() > 0) { 494 containParts = true; 495 } 496 } 497 } 498 if (headerMessage != null && bodyMessage != null 499 && headerMessage.getNamespaceURI().equalsIgnoreCase(bodyMessage.getQName().getNamespaceURI()) 500 && headerMessage.getLocalPart().equalsIgnoreCase(bodyMessage.getQName().getLocalPart())) { 501 isSameMessage = true; 502 if (bodyMessage.getParts().size() == 1) { 503 allPartsHeader = true; 504 } 505 506 } 507 isNonWrappable = isSameMessage && containParts; 508 if (isNonWrappable && allPartsHeader) { 509 result = this.resultHeader; 510 } 511 if (isNonWrappable && !allPartsHeader) { 512 result = this.outHEADER; 513 } 514 } 515 516 return result; 517 518 } 519 520 private Message getMessage(String operationName, boolean isIn) { 521 Iterator ite = definition.getPortTypes().values().iterator(); 522 Message msg = null; 523 while (ite.hasNext()) { 524 PortType portType = (PortType)ite.next(); 525 Iterator ite1 = portType.getOperations().iterator(); 526 while (ite1.hasNext()) { 527 Operation op = (Operation)ite1.next(); 528 if (operationName.equals(op.getName())) { 529 if (isIn) { 530 msg = op.getInput().getMessage(); 531 } else { 532 msg = op.getOutput().getMessage(); 533 } 534 break; 535 } 536 } 537 } 538 539 return msg; 540 } 541 542 private void doCustomizeBinding(JavaModel jmodel, JavaInterface ji, Binding binding) { 543 JAXWSBinding bindingExt = null; 544 List extElements = binding.getExtensibilityElements(); 545 if (extElements.size() > 0) { 546 Iterator iterator = extElements.iterator(); 547 while (iterator.hasNext()) { 548 Object obj = iterator.next(); 549 if (obj instanceof JAXWSBinding) { 550 bindingExt = (JAXWSBinding)obj; 551 ji.setBindingExt(bindingExt); 552 return; 553 } 554 } 555 } 556 String portTypeName = binding.getPortType().getQName().getLocalPart(); 557 bindingExt = CustomizationParser.getInstance().getPortTypeExtension(portTypeName); 558 if (bindingExt != null) { 559 if (!bindingExt.isSetMimeEnable() && jmodel.getJAXWSBinding().isSetMimeEnable() 560 && jmodel.getJAXWSBinding().isEnableMime()) { 561 bindingExt.setSetMimeEnable(true); 562 bindingExt.setEnableMime(true); 563 } 564 } else if (jmodel.getJAXWSBinding() != null) { 565 bindingExt = new JAXWSBinding(); 566 if (jmodel.getJAXWSBinding().isSetMimeEnable() && jmodel.getJAXWSBinding().isEnableMime()) { 567 bindingExt.setSetMimeEnable(true); 568 bindingExt.setEnableMime(true); 569 } 570 } else { 571 bindingExt = new JAXWSBinding(); 573 } 574 ji.setBindingExt(bindingExt); 575 } 576 577 private void doCustomizeOperation(JavaInterface ji, JavaMethod jm, BindingOperation bo) { 578 JAXWSBinding bindingExt = null; 579 List extElements = bo.getExtensibilityElements(); 580 if (extElements.size() > 0) { 581 Iterator iterator = extElements.iterator(); 582 while (iterator.hasNext()) { 583 Object obj = iterator.next(); 584 if (obj instanceof JAXWSBinding) { 585 bindingExt = (JAXWSBinding)obj; 586 jm.setBindingExt(bindingExt); 587 return; 588 } 589 } 590 } 591 String portTypeName = ji.getWebServiceName(); 592 String operationName = bo.getName(); 593 bindingExt = CustomizationParser.getInstance().getPortTypeOperationExtension(portTypeName, 594 operationName); 595 if (bindingExt != null) { 596 if (!bindingExt.isSetMimeEnable() && ji.getBindingExt() != null 597 && ji.getBindingExt().isSetMimeEnable() && ji.getBindingExt().isEnableMime()) { 598 bindingExt.setSetMimeEnable(true); 599 bindingExt.setEnableMime(true); 600 } 601 } else if (ji.getBindingExt() != null) { 602 bindingExt = new JAXWSBinding(); 603 if (ji.getBindingExt().isSetMimeEnable() && ji.getBindingExt().isEnableMime()) { 604 bindingExt.setSetMimeEnable(true); 605 bindingExt.setEnableMime(true); 606 } 607 } else { 608 bindingExt = new JAXWSBinding(); 610 } 611 jm.setBindingExt(bindingExt); 612 } 613 614 public enum BindingType { 615 HTTPBinding, SOAPBinding, XMLBinding 616 } 617 618 private boolean isSoapBinding() { 619 return bindingType != null && "SOAPBinding".equals(bindingType.name()); 620 621 } 622 623 private boolean isInOutParam(String inPartName, BindingOutput bop) { 624 Iterator it = bop.getExtensibilityElements().iterator(); 625 while (it.hasNext()) { 626 Object obj = it.next(); 627 if (obj instanceof SOAPHeader) { 628 String outPartName = ((SOAPHeader)obj).getPart(); 629 if (inPartName.equals(outPartName)) { 630 return true; 631 } 632 } 633 } 634 return false; 635 } 636 } 637 | Popular Tags |