
org.objectweb.celtix.wsdl.EndpointReferenceUtils Maven / Gradle / Ivy
The newest version!
package org.objectweb.celtix.wsdl;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.wsdl.Definition;
import javax.wsdl.Import;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.Types;
import javax.wsdl.WSDLException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceProvider;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.objectweb.celtix.common.logging.LogUtils;
import org.objectweb.celtix.jaxb.JAXBUtils;
import org.objectweb.celtix.ws.addressing.AttributedURIType;
import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
import org.objectweb.celtix.ws.addressing.MetadataType;
import org.objectweb.celtix.ws.addressing.ObjectFactory;
import org.objectweb.celtix.ws.addressing.wsdl.AttributedQNameType;
import org.objectweb.celtix.ws.addressing.wsdl.ServiceNameType;
/**
* Provides utility methods for obtaining endpoint references, wsdl definitions, etc.
*/
public final class EndpointReferenceUtils {
static WeakHashMap schemaMap = new WeakHashMap();
private static final Logger LOG = LogUtils.getL7dLogger(EndpointReferenceUtils.class);
private static final QName WSDL_LOCATION = new QName("http://www.w3.org/2006/01/wsdl-instance",
"wsdlLocation");
private static final Transformer XML_TRANSFORMER;
static {
Transformer transformer = null;
try {
TransformerFactory tf = TransformerFactory.newInstance();
transformer = tf.newTransformer();
} catch (TransformerConfigurationException tce) {
throw new WebServiceException("Could not create transformer", tce);
}
XML_TRANSFORMER = transformer;
}
private EndpointReferenceUtils() {
// Utility class - never constructed
}
/**
* Sets the service and port name of the provided endpoint reference.
* @param ref the endpoint reference.
* @param serviceName the name of service.
* @param portName the port name.
*/
public static void setServiceAndPortName(EndpointReferenceType ref,
QName serviceName,
String portName)
throws WebServiceException {
if (null != serviceName) {
ServiceNameType serviceNameType = new ServiceNameType();
serviceNameType.setValue(serviceName);
serviceNameType.setEndpointName(portName);
org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory =
new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory();
JAXBElement jaxbElement = objectFactory.createServiceName(serviceNameType);
MetadataType mt = ref.getMetadata();
if (null == mt) {
mt = new MetadataType();
ref.setMetadata(mt);
}
mt.getAny().add(jaxbElement);
}
}
/**
* Gets the service name of the provided endpoint reference.
* @param ref the endpoint reference.
* @return the service name.
*/
public static QName getServiceName(EndpointReferenceType ref) {
MetadataType metadata = ref.getMetadata();
if (metadata != null) {
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Node node = (Element)obj;
if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
&& node.getLocalName().equals("ServiceName")) {
String content = node.getTextContent();
String namespaceURI = node.getFirstChild().getNamespaceURI();
String service = content;
if (content.contains(":")) {
namespaceURI = getNameSpaceUri(node, content, namespaceURI);
service = getService(content);
} else {
Node nodeAttr = node.getAttributes().getNamedItem("xmlns");
namespaceURI = nodeAttr.getNodeValue();
}
return new QName(namespaceURI, service);
}
} else if (obj instanceof JAXBElement) {
Object val = ((JAXBElement)obj).getValue();
if (val instanceof ServiceNameType) {
return ((ServiceNameType)val).getValue();
}
} else if (obj instanceof ServiceNameType) {
return ((ServiceNameType)obj).getValue();
}
}
}
return null;
}
/**
* Gets the port name of the provided endpoint reference.
* @param ref the endpoint reference.
* @return the port name.
*/
public static String getPortName(EndpointReferenceType ref) {
MetadataType metadata = ref.getMetadata();
if (metadata != null) {
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Node node = (Element)obj;
if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
&& node.getNodeName().contains("ServiceName")) {
return node.getAttributes().getNamedItem("EndpointName").getTextContent();
}
} else if (obj instanceof JAXBElement) {
Object val = ((JAXBElement)obj).getValue();
if (val instanceof ServiceNameType) {
return ((ServiceNameType)val).getEndpointName();
}
} else if (obj instanceof ServiceNameType) {
return ((ServiceNameType)obj).getEndpointName();
}
}
}
return null;
}
public static void setInterfaceName(EndpointReferenceType ref, QName portTypeName) {
if (null != portTypeName) {
AttributedQNameType interfaceNameType = new AttributedQNameType();
interfaceNameType.setValue(portTypeName);
org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory =
new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory();
JAXBElement jaxbElement =
objectFactory.createInterfaceName(interfaceNameType);
MetadataType mt = ref.getMetadata();
if (null == mt) {
mt = new MetadataType();
ref.setMetadata(mt);
}
mt.getAny().add(jaxbElement);
}
}
public static QName getInterfaceName(EndpointReferenceType ref) {
MetadataType metadata = ref.getMetadata();
if (metadata != null) {
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Node node = (Element)obj;
System.out.println(node.getNamespaceURI() + ":" + node.getNodeName());
if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
&& node.getNodeName().contains("InterfaceName")) {
String content = node.getTextContent();
String namespaceURI = node.getFirstChild().getNamespaceURI();
//String service = content;
if (content.contains(":")) {
namespaceURI = getNameSpaceUri(node, content, namespaceURI);
content = getService(content);
} else {
Node nodeAttr = node.getAttributes().getNamedItem("xmlns");
namespaceURI = nodeAttr.getNodeValue();
}
return new QName(namespaceURI, content);
}
} else if (obj instanceof JAXBElement) {
Object val = ((JAXBElement)obj).getValue();
if (val instanceof AttributedQNameType) {
return ((AttributedQNameType)val).getValue();
}
} else if (obj instanceof AttributedQNameType) {
return ((AttributedQNameType)obj).getValue();
}
}
}
return null;
}
private static void setWSDLLocation(EndpointReferenceType ref, String... wsdlLocation) {
MetadataType metadata = ref.getMetadata();
if (null == metadata) {
metadata = new MetadataType();
ref.setMetadata(metadata);
}
//wsdlLocation attribute is a list of anyURI.
StringBuffer strBuf = new StringBuffer();
for (String str : wsdlLocation) {
strBuf.append(str);
strBuf.append(" ");
}
metadata.getOtherAttributes().put(WSDL_LOCATION, strBuf.toString().trim());
}
public static String getWSDLLocation(EndpointReferenceType ref) {
String wsdlLocation = null;
MetadataType metadata = ref.getMetadata();
if (metadata != null) {
wsdlLocation = metadata.getOtherAttributes().get(WSDL_LOCATION);
}
if (null == wsdlLocation) {
return null;
}
//TODO The wsdlLocation inserted should be a valid URI
//before doing a split. So temporarily return the string
//return wsdlLocation.split(" ");
return wsdlLocation;
}
/**
* Sets the metadata on the provided endpoint reference.
* @param ref the endpoint reference.
* @param the list of metadata source.
*/
public static void setMetadata(EndpointReferenceType ref, List metadata) {
if (null != ref) {
MetadataType mt = ref.getMetadata();
if (null == mt) {
mt = new MetadataType();
ref.setMetadata(mt);
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy