org.apache.cxf.wsdl.EndpointReferenceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-minimal Show documentation
Show all versions of cxf-bundle-minimal Show documentation
Apache CXF Minimal Bundle Jar
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.wsdl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.WSDLException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
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 org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.InputSource;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.WSDLConstants;
import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.jaxb.JAXBContextCache;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.common.xmlschema.LSInputImpl;
import org.apache.cxf.endpoint.EndpointResolverRegistry;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.endpoint.ServerRegistry;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.helpers.LoadingByteArrayOutputStream;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.resource.ExtendedURIResolver;
import org.apache.cxf.resource.ResourceManager;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.service.model.ServiceInfo;
import org.apache.cxf.staxutils.StaxUtils;
import org.apache.cxf.staxutils.W3CDOMStreamWriter;
import org.apache.cxf.transport.Destination;
import org.apache.cxf.transport.MultiplexDestination;
import org.apache.cxf.ws.addressing.AttributedURIType;
import org.apache.cxf.ws.addressing.EndpointReferenceType;
import org.apache.cxf.ws.addressing.JAXWSAConstants;
import org.apache.cxf.ws.addressing.MetadataType;
import org.apache.cxf.ws.addressing.wsdl.AttributedQNameType;
import org.apache.cxf.ws.addressing.wsdl.ServiceNameType;
import org.apache.ws.commons.schema.XmlSchema;
/**
* Provides utility methods for obtaining endpoint references, wsdl definitions, etc.
*/
public final class EndpointReferenceUtils {
/**
* We want to load the schemas, including references to external schemas, into a SchemaFactory
* to validate. There seem to be bugs in resolving inter-schema references in Xerces, so even when we are
* handing the factory all the schemas, interrelated with <import> elements, we need
* to also hand over extra copies (!) as character images when requested.
*
* To do this, we use the DOM representation kept in the SchemaInfo. This has the bonus
* of benefiting from the use of the catalog resolver in there, which is missing from
* the code in here.
*/
private static final class SchemaLSResourceResolver implements LSResourceResolver {
private final Map schemas;
private final Set done = new HashSet();
private final ExtendedURIResolver resolver = new ExtendedURIResolver();
private final Bus bus;
private SchemaLSResourceResolver(Map schemas, Bus b) {
this.schemas = schemas;
this.bus = b;
}
public LSInput resolveResource(String type, String namespaceURI, String publicId,
String systemId, String baseURI) {
String newId = systemId;
if (baseURI != null && systemId != null) { //add additional systemId null check
try {
URI uri = new URI(baseURI);
uri = uri.resolve(systemId);
newId = uri.toString();
if (newId.equals(systemId)) {
URL url = new URL(baseURI);
url = new URL(url, systemId);
newId = url.toExternalForm();
}
} catch (IllegalArgumentException e) {
//ignore - systemId not a valid URI
} catch (URISyntaxException e) {
//ignore - baseURI not a valid URI
} catch (MalformedURLException e) {
//ignore - baseURI or systemId not a URL either
}
}
if (done.contains(newId + ":" + namespaceURI)) {
return null;
}
LSInputImpl impl = null;
if (schemas.containsKey(newId + ":" + namespaceURI)) {
byte[] ds = schemas.remove(newId + ":" + namespaceURI);
impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setByteStream(new ByteArrayInputStream(ds));
done.add(newId + ":" + namespaceURI);
}
if (impl == null && schemas.containsKey(newId + ":null")) {
byte[] ds = schemas.get(newId + ":null");
impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setByteStream(new ByteArrayInputStream(ds));
done.add(newId + ":" + namespaceURI);
}
if (impl == null && bus != null && systemId != null) {
ResourceManager rm = bus.getExtension(ResourceManager.class);
URL url = rm == null ? null : rm.resolveResource(systemId, URL.class);
if (url != null) {
newId = url.toString();
if (done.contains(newId + ":" + namespaceURI)) {
return null;
}
if (schemas.containsKey(newId + ":" + namespaceURI)) {
byte[] ds = schemas.remove(newId + ":" + namespaceURI);
impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setByteStream(new ByteArrayInputStream(ds));
done.add(newId + ":" + namespaceURI);
}
}
}
if (impl != null) {
return impl;
}
for (Map.Entry ent : schemas.entrySet()) {
if (ent.getKey().endsWith(systemId + ":" + namespaceURI)) {
schemas.remove(ent.getKey());
impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setCharacterStream(
new InputStreamReader(
new ByteArrayInputStream(ent.getValue())));
done.add(newId + ":" + namespaceURI);
return impl;
}
}
// handle case where given systemId is null (so that
// direct key lookup fails) by scanning through map
// searching for a namespace match
if (namespaceURI != null) {
for (Map.Entry ent : schemas.entrySet()) {
if (ent.getKey().endsWith(namespaceURI)) {
schemas.remove(ent.getKey());
impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setCharacterStream(
new InputStreamReader(
new ByteArrayInputStream(ent.getValue())));
done.add(newId + ":" + namespaceURI);
return impl;
}
}
}
//REVIST - we need to get catalogs in here somehow :-(
if (systemId == null) {
systemId = publicId;
}
if (systemId != null) {
InputSource source = resolver.resolve(systemId, baseURI);
if (source != null) {
impl = new LSInputImpl();
impl.setByteStream(source.getByteStream());
impl.setSystemId(source.getSystemId());
impl.setPublicId(source.getPublicId());
}
}
LOG.warning("Could not resolve Schema for " + systemId);
return impl;
}
}
public static final String ANONYMOUS_ADDRESS = WSAEndpointReferenceUtils.ANONYMOUS_ADDRESS;
private static final Logger LOG = LogUtils.getL7dLogger(EndpointReferenceUtils.class);
private static final String NS_WSAW_2005 = "http://www.w3.org/2005/02/addressing/wsdl";
private static final String WSDL_INSTANCE_NAMESPACE2 =
"http://www.w3.org/2006/01/wsdl-instance";
private static final String WSDL_INSTANCE_NAMESPACE =
"http://www.w3.org/ns/wsdl-instance";
private static final QName WSA_WSDL_NAMESPACE_NS =
new QName("xmlns:" + JAXWSAConstants.WSAW_PREFIX);
private static final String XML_SCHEMA_NAMESPACE =
"http://www.w3.org/2001/XMLSchema";
private static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
private static final QName XML_SCHEMA_NAMESPACE_NS =
new QName("xmlns:" + XML_SCHEMA_NAMESPACE_PREFIX);
private static final String XML_SCHEMA_INSTANCE_NAMESPACE =
"http://www.w3.org/2001/XMLSchema-instance";
private static final QName WSDL_LOCATION2 =
new QName(WSDL_INSTANCE_NAMESPACE2, "wsdlLocation");
private static final QName WSDL_LOCATION =
new QName(WSDL_INSTANCE_NAMESPACE, "wsdlLocation");
private static final QName XSI_TYPE =
new QName(XML_SCHEMA_INSTANCE_NAMESPACE, "type", "xsi");
private static final org.apache.cxf.ws.addressing.wsdl.ObjectFactory WSA_WSDL_OBJECT_FACTORY =
new org.apache.cxf.ws.addressing.wsdl.ObjectFactory();
private static final Set> ADDRESSING_CLASSES = new HashSet>();
private static final AtomicReference> ADDRESSING_CONTEXT
= new AtomicReference>(new SoftReference(null));
static {
ADDRESSING_CLASSES.add(WSA_WSDL_OBJECT_FACTORY.getClass());
ADDRESSING_CLASSES.add(WSAEndpointReferenceUtils.WSA_OBJECT_FACTORY.getClass());
}
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) {
if (null != serviceName) {
JAXBElement jaxbElement = getServiceNameType(serviceName, portName);
MetadataType mt = WSAEndpointReferenceUtils.getSetMetadata(ref);
mt.getAny().add(jaxbElement);
}
}
public static JAXBElement getServiceNameType(QName serviceName, String portName) {
ServiceNameType serviceNameType = WSA_WSDL_OBJECT_FACTORY.createServiceNameType();
serviceNameType.setValue(serviceName);
serviceNameType.setEndpointName(portName);
serviceNameType.getOtherAttributes().put(WSA_WSDL_NAMESPACE_NS, JAXWSAConstants.NS_WSAW);
serviceNameType.getOtherAttributes().put(XSI_TYPE,
JAXWSAConstants.WSAW_PREFIX + ":"
+ serviceNameType.getClass().getSimpleName());
return WSA_WSDL_OBJECT_FACTORY.createServiceName(serviceNameType);
}
/**
* Gets the service name of the provided endpoint reference.
* @param ref the endpoint reference.
* @return the service name.
*/
public static QName getServiceName(EndpointReferenceType ref, Bus bus) {
MetadataType metadata = ref.getMetadata();
if (metadata == null) {
return null;
}
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Node node = (Element)obj;
if ((node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAW)
|| node.getNamespaceURI().equals(NS_WSAW_2005)
|| node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAM))
&& node.getLocalName().equals("ServiceName")) {
String content = node.getTextContent();
String namespaceURI = node.getFirstChild().getNamespaceURI();
String service = content;
if (content.contains(":")) {
namespaceURI = getNameSpaceUri(node, content, namespaceURI);
if (StringUtils.isEmpty(namespaceURI)) {
namespaceURI = findNamespaceHack(ref, bus);
}
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(JAXWSAConstants.NS_WSAW)
|| node.getNamespaceURI().equals(NS_WSAW_2005)
|| node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAM))
&& node.getNodeName().contains("ServiceName")) {
Node item = node.getAttributes().getNamedItem("EndpointName");
return item != null ? item.getTextContent() : null;
}
} 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 QName getPortQName(EndpointReferenceType ref, Bus bus) {
QName serviceName = getServiceName(ref, bus);
return new QName(serviceName.getNamespaceURI(), getPortName(ref));
}
public static void setPortName(EndpointReferenceType ref, String portName) {
MetadataType metadata = ref.getMetadata();
if (metadata != null) {
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Element node = (Element)obj;
if (node.getNodeName().contains("ServiceName")
&& (node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAW)
|| node.getNamespaceURI().equals(NS_WSAW_2005)
|| node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAM))) {
node.setAttribute(JAXWSAConstants.WSAM_ENDPOINT_NAME, portName);
}
} else if (obj instanceof JAXBElement) {
Object val = ((JAXBElement>)obj).getValue();
if (val instanceof ServiceNameType) {
((ServiceNameType)val).setEndpointName(portName);
}
} else if (obj instanceof ServiceNameType) {
((ServiceNameType)obj).setEndpointName(portName);
}
}
}
}
public static void setInterfaceName(EndpointReferenceType ref, QName portTypeName) {
if (null != portTypeName) {
AttributedQNameType interfaceNameType =
WSA_WSDL_OBJECT_FACTORY.createAttributedQNameType();
interfaceNameType.setValue(portTypeName);
interfaceNameType.getOtherAttributes().put(XML_SCHEMA_NAMESPACE_NS,
XML_SCHEMA_NAMESPACE);
interfaceNameType.getOtherAttributes().put(XSI_TYPE,
XML_SCHEMA_NAMESPACE_PREFIX + ":"
+ portTypeName.getClass().getSimpleName());
JAXBElement jaxbElement =
WSA_WSDL_OBJECT_FACTORY.createInterfaceName(interfaceNameType);
MetadataType mt = WSAEndpointReferenceUtils.getSetMetadata(ref);
mt.getAny().add(jaxbElement);
}
}
public static QName getInterfaceName(EndpointReferenceType ref, Bus bus) {
MetadataType metadata = ref.getMetadata();
if (metadata == null) {
return null;
}
for (Object obj : metadata.getAny()) {
if (obj instanceof Element) {
Node node = (Element)obj;
if ((node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAW)
|| node.getNamespaceURI().equals(JAXWSAConstants.NS_WSAM))
&& node.getNodeName().contains("InterfaceName")) {
String content = node.getTextContent();
String namespaceURI = node.getFirstChild().getNamespaceURI();
//String service = content;
if (content.contains(":")) {
namespaceURI = getNameSpaceUri(node, content, namespaceURI);
if (StringUtils.isEmpty(namespaceURI)) {
namespaceURI = findNamespaceHack(ref, bus);
}
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 String findNamespaceHack(EndpointReferenceType ref, Bus bus) {
//probably a broken version of Xalan, we'll have to
//try a hack to figure out the namespace as xalan
//dropped the namespace declaration so there isn't
//a way to map the namespace prefix to the real namespace.
//This is fixed in xalan 2.7.1, but older versions may
//be used
if (bus == null) {
return "";
}
String wsdlLocation = getWSDLLocation(ref);
if (StringUtils.isEmpty(wsdlLocation)) {
return "";
}
if (bus != null) {
WSDLManager manager = bus.getExtension(WSDLManager.class);
if (manager != null) {
try {
Definition def = manager.getDefinition(wsdlLocation);
return def.getTargetNamespace();
} catch (WSDLException e) {
//ignore
}
}
}
return "";
}
public static void setWSDLLocation(EndpointReferenceType ref, String... wsdlLocation) {
MetadataType metadata = WSAEndpointReferenceUtils.getSetMetadata(ref);
//wsdlLocation attribute is a list of anyURI.
StringBuilder strBuf = new StringBuilder();
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 (wsdlLocation == null) {
wsdlLocation = metadata.getOtherAttributes().get(WSDL_LOCATION2);
}
}
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 metadata the list of metadata source.
*/
public static void setMetadata(EndpointReferenceType ref, List