1 package org.objectweb.celtix.wsdl; 2 3 import java.net.URL ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.WeakHashMap ; 8 import java.util.logging.Level ; 9 import java.util.logging.Logger ; 10 11 import javax.jws.WebService; 12 import javax.wsdl.Definition; 13 import javax.wsdl.Import; 14 import javax.wsdl.Port; 15 import javax.wsdl.Service; 16 import javax.wsdl.Types; 17 import javax.wsdl.WSDLException; 18 import javax.xml.XMLConstants ; 19 import javax.xml.bind.JAXBElement; 20 import javax.xml.namespace.QName ; 21 import javax.xml.transform.Source ; 22 import javax.xml.transform.Transformer ; 23 import javax.xml.transform.TransformerConfigurationException ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.TransformerFactory ; 26 import javax.xml.transform.dom.DOMResult ; 27 import javax.xml.transform.dom.DOMSource ; 28 import javax.xml.transform.stream.StreamSource ; 29 import javax.xml.validation.Schema ; 30 import javax.xml.validation.SchemaFactory ; 31 import javax.xml.ws.WebServiceException; 32 import javax.xml.ws.WebServiceProvider; 33 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.xml.sax.SAXException ; 38 39 import org.objectweb.celtix.common.logging.LogUtils; 40 import org.objectweb.celtix.jaxb.JAXBUtils; 41 import org.objectweb.celtix.ws.addressing.AttributedURIType; 42 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 43 import org.objectweb.celtix.ws.addressing.MetadataType; 44 import org.objectweb.celtix.ws.addressing.ObjectFactory; 45 import org.objectweb.celtix.ws.addressing.wsdl.AttributedQNameType; 46 import org.objectweb.celtix.ws.addressing.wsdl.ServiceNameType; 47 48 51 public final class EndpointReferenceUtils { 52 53 static WeakHashMap <Definition, Schema > schemaMap = new WeakHashMap <Definition, Schema >(); 54 55 private static final Logger LOG = LogUtils.getL7dLogger(EndpointReferenceUtils.class); 56 57 private static final QName WSDL_LOCATION = new QName ("http://www.w3.org/2006/01/wsdl-instance", 58 "wsdlLocation"); 59 private static final Transformer XML_TRANSFORMER; 60 static { 61 Transformer transformer = null; 62 try { 63 TransformerFactory tf = TransformerFactory.newInstance(); 64 transformer = tf.newTransformer(); 65 } catch (TransformerConfigurationException tce) { 66 throw new WebServiceException("Could not create transformer", tce); 67 } 68 XML_TRANSFORMER = transformer; 69 } 70 71 private EndpointReferenceUtils() { 72 } 74 75 81 public static void setServiceAndPortName(EndpointReferenceType ref, 82 QName serviceName, 83 String portName) 84 throws WebServiceException { 85 if (null != serviceName) { 86 ServiceNameType serviceNameType = new ServiceNameType(); 87 serviceNameType.setValue(serviceName); 88 serviceNameType.setEndpointName(portName); 89 org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory = 90 new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory(); 91 JAXBElement<ServiceNameType> jaxbElement = objectFactory.createServiceName(serviceNameType); 92 93 MetadataType mt = ref.getMetadata(); 94 if (null == mt) { 95 mt = new MetadataType(); 96 ref.setMetadata(mt); 97 } 98 99 mt.getAny().add(jaxbElement); 100 } 101 } 102 103 108 public static QName getServiceName(EndpointReferenceType ref) { 109 MetadataType metadata = ref.getMetadata(); 110 if (metadata != null) { 111 for (Object obj : metadata.getAny()) { 112 if (obj instanceof Element) { 113 Node node = (Element)obj; 114 if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl") 115 && node.getLocalName().equals("ServiceName")) { 116 String content = node.getTextContent(); 117 String namespaceURI = node.getFirstChild().getNamespaceURI(); 118 String service = content; 119 if (content.contains(":")) { 120 namespaceURI = getNameSpaceUri(node, content, namespaceURI); 121 service = getService(content); 122 } else { 123 Node nodeAttr = node.getAttributes().getNamedItem("xmlns"); 124 namespaceURI = nodeAttr.getNodeValue(); 125 } 126 127 return new QName (namespaceURI, service); 128 } 129 } else if (obj instanceof JAXBElement) { 130 Object val = ((JAXBElement)obj).getValue(); 131 if (val instanceof ServiceNameType) { 132 return ((ServiceNameType)val).getValue(); 133 } 134 } else if (obj instanceof ServiceNameType) { 135 return ((ServiceNameType)obj).getValue(); 136 } 137 } 138 } 139 return null; 140 } 141 142 147 public static String getPortName(EndpointReferenceType ref) { 148 MetadataType metadata = ref.getMetadata(); 149 if (metadata != null) { 150 for (Object obj : metadata.getAny()) { 151 if (obj instanceof Element) { 152 Node node = (Element)obj; 153 if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl") 154 && node.getNodeName().contains("ServiceName")) { 155 return node.getAttributes().getNamedItem("EndpointName").getTextContent(); 156 } 157 } else if (obj instanceof JAXBElement) { 158 Object val = ((JAXBElement)obj).getValue(); 159 if (val instanceof ServiceNameType) { 160 return ((ServiceNameType)val).getEndpointName(); 161 } 162 } else if (obj instanceof ServiceNameType) { 163 return ((ServiceNameType)obj).getEndpointName(); 164 } 165 } 166 } 167 return null; 168 } 169 170 public static void setInterfaceName(EndpointReferenceType ref, QName portTypeName) { 171 if (null != portTypeName) { 172 AttributedQNameType interfaceNameType = new AttributedQNameType(); 173 174 interfaceNameType.setValue(portTypeName); 175 176 org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory = 177 new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory(); 178 JAXBElement<AttributedQNameType> jaxbElement = 179 objectFactory.createInterfaceName(interfaceNameType); 180 181 MetadataType mt = ref.getMetadata(); 182 if (null == mt) { 183 mt = new MetadataType(); 184 ref.setMetadata(mt); 185 } 186 mt.getAny().add(jaxbElement); 187 } 188 } 189 190 public static QName getInterfaceName(EndpointReferenceType ref) { 191 MetadataType metadata = ref.getMetadata(); 192 if (metadata != null) { 193 for (Object obj : metadata.getAny()) { 194 if (obj instanceof Element) { 195 Node node = (Element)obj; 196 System.out.println(node.getNamespaceURI() + ":" + node.getNodeName()); 197 if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl") 198 && node.getNodeName().contains("InterfaceName")) { 199 200 String content = node.getTextContent(); 201 String namespaceURI = node.getFirstChild().getNamespaceURI(); 202 if (content.contains(":")) { 204 namespaceURI = getNameSpaceUri(node, content, namespaceURI); 205 content = getService(content); 206 } else { 207 Node nodeAttr = node.getAttributes().getNamedItem("xmlns"); 208 namespaceURI = nodeAttr.getNodeValue(); 209 } 210 211 return new QName (namespaceURI, content); 212 } 213 } else if (obj instanceof JAXBElement) { 214 Object val = ((JAXBElement)obj).getValue(); 215 if (val instanceof AttributedQNameType) { 216 return ((AttributedQNameType)val).getValue(); 217 } 218 } else if (obj instanceof AttributedQNameType) { 219 return ((AttributedQNameType)obj).getValue(); 220 } 221 } 222 } 223 224 return null; 225 } 226 227 private static void setWSDLLocation(EndpointReferenceType ref, String ... wsdlLocation) { 228 229 MetadataType metadata = ref.getMetadata(); 230 if (null == metadata) { 231 metadata = new MetadataType(); 232 ref.setMetadata(metadata); 233 } 234 235 StringBuffer strBuf = new StringBuffer (); 237 for (String str : wsdlLocation) { 238 strBuf.append(str); 239 strBuf.append(" "); 240 } 241 242 metadata.getOtherAttributes().put(WSDL_LOCATION, strBuf.toString().trim()); 243 } 244 245 public static String getWSDLLocation(EndpointReferenceType ref) { 246 String wsdlLocation = null; 247 MetadataType metadata = ref.getMetadata(); 248 249 if (metadata != null) { 250 wsdlLocation = metadata.getOtherAttributes().get(WSDL_LOCATION); 251 } 252 253 if (null == wsdlLocation) { 254 return null; 255 } 256 257 return wsdlLocation; 261 } 262 263 268 public static void setMetadata(EndpointReferenceType ref, List <Source > metadata) { 269 if (null != ref) { 270 MetadataType mt = ref.getMetadata(); 271 if (null == mt) { 272 mt = new MetadataType(); 273 ref.setMetadata(mt); 274 } 275 List <Object > anyList = mt.getAny(); 276 try { 277 for (Source source : metadata) { 278 Node node = null; 279 boolean doTransform = true; 280 if (source instanceof StreamSource ) { 281 StreamSource ss = (StreamSource )source; 282 if (null == ss.getInputStream() 283 && null == ss.getReader()) { 284 setWSDLLocation(ref, ss.getSystemId()); 285 doTransform = false; 286 } 287 } else if (source instanceof DOMSource ) { 288 node = ((DOMSource )node).getNode(); 289 doTransform = false; 290 } 291 292 if (doTransform) { 293 DOMResult domResult = new DOMResult (); 294 domResult.setSystemId(source.getSystemId()); 295 296 XML_TRANSFORMER.transform(source, domResult); 297 298 node = domResult.getNode(); 299 } 300 301 if (null != node) { 302 if (node instanceof Document ) { 303 ((Document )node).setDocumentURI(source.getSystemId()); 304 node = node.getFirstChild(); 305 } 306 307 while (node.getNodeType() != Node.ELEMENT_NODE) { 308 node = node.getNextSibling(); 309 } 310 311 anyList.add(node); 312 } 313 } 314 } catch (TransformerException te) { 315 throw new WebServiceException("Populating metadata in EPR failed", te); 316 } 317 } 318 } 319 320 327 public static Definition getWSDLDefinition(WSDLManager manager, EndpointReferenceType ref) 328 throws WSDLException { 329 330 if (null == manager) { 331 return null; 332 } 333 334 MetadataType metadata = ref.getMetadata(); 335 String location = getWSDLLocation(ref); 336 337 if (null != location) { 338 return manager.getDefinition(location); 340 } 341 342 for (Object obj : metadata.getAny()) { 343 if (obj instanceof Element) { 344 Element el = (Element)obj; 345 if ("http://schemas.xmlsoap.org/wsdl/".equals(el.getNamespaceURI()) 346 && "definitions".equals(el.getLocalName())) { 347 return manager.getDefinition(el); 348 } 349 } 350 } 351 352 QName portTypeName = getInterfaceName(ref); 353 if (null != portTypeName) { 354 355 StringBuffer seiName = new StringBuffer (); 356 seiName.append(JAXBUtils.namespaceURIToPackage(portTypeName.getNamespaceURI())); 357 seiName.append("."); 358 seiName.append(JAXBUtils.nameToIdentifier(portTypeName.getLocalPart(), 359 JAXBUtils.IdentifierType.INTERFACE)); 360 361 Class <?> sei = null; 362 try { 363 sei = Class.forName(seiName.toString(), true, 364 manager.getClass().getClassLoader()); 365 } catch (ClassNotFoundException ex) { 366 LOG.log(Level.INFO, "SEI_LOAD_FAILURE_MSG", ex); 367 return null; 368 } 369 Definition def = manager.getDefinition(sei); 370 if (def == null && sei.getInterfaces().length > 0) { 371 sei = sei.getInterfaces()[0]; 372 def = manager.getDefinition(sei); 373 } 374 return def; 375 } 376 return null; 377 } 378 379 private static List <javax.wsdl.extensions.schema.Schema> getSchemas(Definition definition) { 380 Types types = definition.getTypes(); 381 List <javax.wsdl.extensions.schema.Schema> schemaList = 382 new ArrayList <javax.wsdl.extensions.schema.Schema>(); 383 if (types != null) { 384 for (Object o : types.getExtensibilityElements()) { 385 if (o instanceof javax.wsdl.extensions.schema.Schema) { 386 javax.wsdl.extensions.schema.Schema s = 387 (javax.wsdl.extensions.schema.Schema)o; 388 schemaList.add(s); 389 } 390 } 391 } 392 393 Map wsdlImports = definition.getImports(); 394 for (Object o : wsdlImports.values()) { 395 if (o instanceof List ) { 396 for (Object p : (List )o) { 397 if (p instanceof Import) { 398 schemaList.addAll(getSchemas(((Import)p).getDefinition())); 399 } 400 } 401 } 402 } 403 return schemaList; 404 } 405 406 public static Schema getSchema(WSDLManager manager, EndpointReferenceType ref) { 407 Definition definition; 408 try { 409 definition = getWSDLDefinition(manager, ref); 410 } catch (javax.wsdl.WSDLException wsdlEx) { 411 return null; 412 } 413 if (definition == null) { 414 return null; 415 } 416 synchronized (schemaMap) { 417 if (schemaMap.containsKey(definition)) { 418 return schemaMap.get(definition); 419 } 420 } 421 Schema schema = schemaMap.get(definition); 422 if (schema == null) { 423 List <javax.wsdl.extensions.schema.Schema> schemas = getSchemas(definition); 424 SchemaFactory factory = SchemaFactory.newInstance( 425 XMLConstants.W3C_XML_SCHEMA_NS_URI); 426 List <Source > schemaSources = new ArrayList <Source >(); 427 for (javax.wsdl.extensions.schema.Schema s : schemas) { 428 Source source = new DOMSource (s.getElement()); 429 if (source != null) { 430 schemaSources.add(source); 431 } 432 } 433 try { 434 schema = factory.newSchema(schemaSources.toArray( 435 new Source [schemaSources.size()])); 436 if (schema != null) { 437 synchronized (schemaMap) { 438 schemaMap.put(definition, schema); 439 } 440 LOG.log(Level.FINE, "Obtained schema from wsdl definition"); 441 } 442 } catch (SAXException ex) { 443 LOG.log(Level.WARNING, "SAXException for newSchema()", ex); 445 } 446 } 447 return schema; 448 } 449 450 457 public static Port getPort(WSDLManager manager, EndpointReferenceType ref) throws WSDLException { 458 459 Definition def = getWSDLDefinition(manager, ref); 460 if (def == null) { 461 throw new WSDLException(WSDLException.OTHER_ERROR, "unable to find definition for reference"); 462 } 463 464 MetadataType metadata = ref.getMetadata(); 465 for (Object obj : metadata.getAny()) { 466 467 if (obj instanceof JAXBElement) { 468 Object jaxbVal = ((JAXBElement)obj).getValue(); 469 470 if (jaxbVal instanceof ServiceNameType) { 471 Port port = null; 472 ServiceNameType snt = (ServiceNameType)jaxbVal; 473 LOG.log(Level.FINEST, "found service name ", snt.getEndpointName()); 474 Service service = def.getService(snt.getValue()); 475 if (service == null) { 476 service = (Service)def.getServices().values().iterator().next(); 477 if (service == null) { 478 return null; 479 } 480 } 481 String endpoint = snt.getEndpointName(); 482 if ("".equals(endpoint) && service.getPorts().size() == 1) { 483 port = (Port)service.getPorts().values().iterator().next(); 484 } else { 485 port = service.getPort(endpoint); 486 } 487 if (port == null) { 490 port = (Port)service.getPorts().values().iterator().next(); 491 } 492 return port; 493 } 494 } 495 } 496 497 if (def.getServices().size() == 1) { 498 Service service = (Service)def.getServices().values().iterator().next(); 499 if (service.getPorts().size() == 1) { 500 return (Port)service.getPorts().values().iterator().next(); 501 } 502 } 503 504 QName serviceName = getServiceName(ref); 505 if (null != serviceName) { 506 Service service = def.getService(serviceName); 507 if (service == null) { 508 throw new WSDLException(WSDLException.OTHER_ERROR, "Cannot find service for " + serviceName); 509 } 510 if (service.getPorts().size() == 1) { 511 return (Port)service.getPorts().values().iterator().next(); 512 } 513 String str = getPortName(ref); 514 LOG.log(Level.FINE, "getting port " + str + " from service " + service.getQName()); 515 Port port = service.getPort(str); 516 if (port == null) { 517 throw new WSDLException(WSDLException.OTHER_ERROR, "unable to find port " + str); 518 } 519 return port; 520 } 521 return null; 523 } 524 525 530 public static String getAddress(EndpointReferenceType ref) { 531 AttributedURIType a = ref.getAddress(); 532 if (null != a) { 533 return a.getValue(); 534 } 535 return null; 537 } 538 539 544 public static void setAddress(EndpointReferenceType ref, String address) { 545 AttributedURIType a = new ObjectFactory().createAttributedURIType(); 546 a.setValue(address); 547 ref.setAddress(a); 548 } 549 556 public static EndpointReferenceType getEndpointReference(URL wsdlUrl, 557 QName serviceName, 558 String portName) { 559 EndpointReferenceType reference = new EndpointReferenceType(); 560 reference.setMetadata(new MetadataType()); 561 setServiceAndPortName(reference, serviceName, portName); 562 setWSDLLocation(reference, wsdlUrl.toString()); 564 565 return reference; 566 } 567 568 573 public static EndpointReferenceType getEndpointReference(String address) { 574 575 EndpointReferenceType reference = new EndpointReferenceType(); 576 setAddress(reference, address); 577 return reference; 578 } 579 580 587 public static WebService getWebServiceAnnotation(Class <?> cls) { 588 if (cls == null) { 589 return null; 590 } 591 WebService ws = cls.getAnnotation(WebService.class); 592 if (null != ws) { 593 return ws; 594 } 595 for (Class <?> inf : cls.getInterfaces()) { 596 ws = getWebServiceAnnotation(inf); 597 if (null != ws) { 598 return ws; 599 } 600 } 601 602 return getWebServiceAnnotation(cls.getSuperclass()); 603 } 604 605 612 public static EndpointReferenceType getEndpointReference(WSDLManager manager, 613 Object implementor) { 614 return getEndpointReference(manager, implementor.getClass()); 615 } 616 617 624 public static EndpointReferenceType getEndpointReference(WSDLManager manager, 625 Class <?> implementorClass) { 626 627 WebService ws = getWebServiceAnnotation(implementorClass); 628 629 WebServiceProvider wsp = null; 630 if (null == ws) { 631 wsp = implementorClass.getAnnotation(WebServiceProvider.class); 632 if (null == wsp) { 633 return null; 634 } 635 } 636 637 EndpointReferenceType reference = new EndpointReferenceType(); 638 reference.setMetadata(new MetadataType()); 639 String serviceName = (null != ws) ? ws.serviceName() : wsp.serviceName(); 640 String targetNamespace = (null != ws) ? ws.targetNamespace() : wsp.targetNamespace(); 641 String portName = (null != ws) ? ws.portName() : wsp.portName(); 642 String url = (null != ws) ? ws.wsdlLocation() : wsp.wsdlLocation(); 643 String className = (null != ws) ? ws.endpointInterface() : null; 644 645 QName portTypeName = null; 646 if (null != className && !"".equals(className)) { 647 Class <?> seiClazz = null; 648 try { 649 seiClazz = Class.forName(className); 650 } catch (ClassNotFoundException cnfe) { 651 LOG.log(Level.SEVERE, "SEI_LOAD_FAILURE_MSG", cnfe); 652 throw new WebServiceException("endpointInterface element in WebService annotation invalid", 653 cnfe); 654 } 655 656 if (!seiClazz.isInterface()) { 657 throw new WebServiceException("endpointInterface element does not refer to a java interface"); 658 } 659 660 WebService seiws = seiClazz.getAnnotation(WebService.class); 661 if (null == seiws) { 662 throw new WebServiceException("SEI should have a WebService Annotation"); 663 } 664 665 if ("".equals(url)) { 666 url = seiws.wsdlLocation(); 667 } 668 669 portTypeName = new QName (ws.targetNamespace(), seiws.name()); 671 672 } else { 676 677 if (null != ws) { 678 className = ws.name(); 679 } 680 if (null == className || "".equals(className)) { 681 className = implementorClass.getSimpleName(); 682 } 683 portTypeName = new QName (targetNamespace, className); 684 } 685 686 setInterfaceName(reference, portTypeName); 687 if (!"".equals(serviceName)) { 689 setServiceAndPortName(reference, new QName (targetNamespace, serviceName), 690 portName); 691 } 692 693 if (null != url && url.length() > 0) { 694 URL wsdlUrl = implementorClass.getResource(url); 696 if (wsdlUrl != null) { 697 url = wsdlUrl.toExternalForm(); 698 } 699 } 700 if (!"".equals(url)) { 702 setWSDLLocation(reference, url); 703 } 704 705 if (LOG.isLoggable(Level.FINE)) { 706 LOG.fine("created endpoint reference with"); 707 LOG.fine(" service name: " + getServiceName(reference)); 708 LOG.fine(" wsdl location: " + getWSDLLocation(reference)); 709 LOG.fine(" sei class: " + getInterfaceName(reference)); 710 } 711 return reference; 712 } 713 714 private static String getNameSpaceUri(Node node, String content, String namespaceURI) { 715 if (namespaceURI == null) { 716 namespaceURI = node.lookupNamespaceURI(content.substring(0, 717 content.indexOf(":"))); 718 } 719 return namespaceURI; 720 } 721 722 private static String getService(String content) { 723 return content.substring(content.indexOf(":") + 1, content.length()); 724 } 725 } 726
| Popular Tags
|