Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.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.StreamResult;
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.common.WSDLConstants;
import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.endpoint.EndpointResolverRegistry;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.endpoint.ServerRegistry;
import org.apache.cxf.helpers.LoadingByteArrayOutputStream;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.resource.ExtendedURIResolver;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.service.model.ServiceInfo;
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 ExtendedURIResolver resolver = new ExtendedURIResolver();
private SchemaLSResourceResolver(Map schemas) {
this.schemas = schemas;
}
private Reader getSchemaAsStream(DOMSource source) {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
try {
TransformerFactory.newInstance().newTransformer().transform(source, result);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new StringReader(writer.toString());
}
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();
} catch (URISyntaxException e) {
//ignore
}
}
if (schemas.containsKey(newId + ":" + namespaceURI)) {
DOMSource ds = schemas.remove(newId + ":" + namespaceURI);
LSInputImpl impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setCharacterStream(getSchemaAsStream(ds));
return impl;
}
for (Map.Entry ent : schemas.entrySet()) {
if (ent.getKey().endsWith(namespaceURI)) {
schemas.remove(ent.getKey());
LSInputImpl impl = new LSInputImpl();
impl.setSystemId(newId);
impl.setBaseURI(newId);
impl.setCharacterStream(getSchemaAsStream(ent.getValue()));
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) {
LSInputImpl impl = new LSInputImpl();
impl.setByteStream(source.getByteStream());
impl.setSystemId(source.getSystemId());
impl.setPublicId(source.getPublicId());
return impl;
}
}
LOG.warning("Could not resolve Schema for " + systemId);
return null;
}
}
public static final String ANONYMOUS_ADDRESS = "http://www.w3.org/2005/08/addressing/anonymous";
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_NAMESPACE =
"http://www.w3.org/2006/01/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_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 org.apache.cxf.ws.addressing.ObjectFactory WSA_OBJECT_FACTORY =
new org.apache.cxf.ws.addressing.ObjectFactory();
private EndpointReferenceUtils() {
// Utility class - never constructed
}
private static Transformer getTransformer() throws EndpointUtilsException {
//To Support IBM JDK
//If use the default transformFactory ,org.apache.xalan.processor.TransformerFactoryImpl \
//when transform stuff will lost attributes
if (System.getProperty("java.vendor").indexOf("IBM") > -1) {
System.setProperty("javax.xml.transform.TransformerFactory",
"org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
}
try {
return TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException tce) {
throw new EndpointUtilsException(new Message("COULD_NOT_CREATE_TRANSFORMER", LOG),
tce);
}
}
/**
* 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 = ref.getMetadata();
if (null == mt) {
mt = new MetadataType();
ref.setMetadata(mt);
}
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.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.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.setAttribute(JAXWSAConstants.WSAW_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 = ref.getMetadata();
if (null == mt) {
mt = WSA_OBJECT_FACTORY.createMetadataType();
ref.setMetadata(mt);
}
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.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 "";
}
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 = ref.getMetadata();
if (null == metadata) {
metadata = WSA_OBJECT_FACTORY.createMetadataType();
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 metadata the list of metadata source.
*/
public static void setMetadata(EndpointReferenceType ref, List metadata)
throws EndpointUtilsException {
if (null != ref) {
MetadataType mt = ref.getMetadata();
if (null == mt) {
mt = new MetadataType();
ref.setMetadata(mt);
}
List