package com.sun.xml.internal.ws.addressing;
import com.sun.istack.internal.Nullable;
import com.sun.istack.internal.NotNull;
import com.sun.xml.internal.stream.buffer.XMLStreamBufferSource;
import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferCreator;
import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference;
import com.sun.xml.internal.ws.util.DOMUtil;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants;
import org.w3c.dom.*;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.EndpointReference;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class EndpointReferenceUtil {
public static <T extends EndpointReference> T transform(Class<T> clazz, @NotNull EndpointReference epr) {
assert epr != null;
if (clazz.isAssignableFrom(W3CEndpointReference.class)) {
if (epr instanceof W3CEndpointReference) {
return (T) epr;
} else if (epr instanceof MemberSubmissionEndpointReference) {
return (T) toW3CEpr((MemberSubmissionEndpointReference) epr);
}
} else if (clazz.isAssignableFrom(MemberSubmissionEndpointReference.class)) {
if (epr instanceof W3CEndpointReference) {
return (T) toMSEpr((W3CEndpointReference) epr);
} else if (epr instanceof MemberSubmissionEndpointReference) {
return (T) epr;
}
}
throw new WebServiceException("Unknwon EndpointReference: " + epr.getClass());
}
private static W3CEndpointReference toW3CEpr(MemberSubmissionEndpointReference msEpr) {
StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
w3cMetadataWritten = false;
try {
writer.writeStartDocument();
writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
"EndpointReference", AddressingVersion.W3C.nsUri);
writer.writeNamespace(AddressingVersion.W3C.getPrefix(),
AddressingVersion.W3C.nsUri);
writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
AddressingVersion.W3C.eprType.address
, AddressingVersion.W3C.nsUri);
writer.writeCharacters(msEpr.addr.uri);
writer.writeEndElement();
if ((msEpr.referenceProperties != null && msEpr.referenceProperties.elements.size() > 0) ||
(msEpr.referenceParameters != null && msEpr.referenceParameters.elements.size() > 0)) {
writer.writeStartElement(AddressingVersion.W3C.getPrefix(), "ReferenceParameters", AddressingVersion.W3C.nsUri);
if (msEpr.referenceProperties != null) {
for (Element e : msEpr.referenceProperties.elements) {
DOMUtil.serializeNode(e, writer);
}
}
if (msEpr.referenceParameters != null) {
for (Element e : msEpr.referenceParameters.elements) {
DOMUtil.serializeNode(e, writer);
}
}
writer.writeEndElement();
}
Element wsdlElement = null;
if ((msEpr.elements != null) && (msEpr.elements.size() > 0)) {
for (Element e : msEpr.elements) {
if(e.getNamespaceURI().equals(MemberSubmissionAddressingConstants.MEX_METADATA.getNamespaceURI()) &&
e.getLocalName().equals(MemberSubmissionAddressingConstants.MEX_METADATA.getLocalPart())) {
NodeList nl = e.getElementsByTagNameNS(WSDLConstants.NS_WSDL,
WSDLConstants.QNAME_DEFINITIONS.getLocalPart());
if(nl != null) {
wsdlElement = (Element) nl.item(0);
}
}
}
}
if (wsdlElement != null) {
DOMUtil.serializeNode(wsdlElement, writer);
}
if (w3cMetadataWritten) {
writer.writeEndElement();
}
if ((msEpr.elements != null) && (msEpr.elements.size() > 0)) {
for (Element e : msEpr.elements) {
if (e.getNamespaceURI().equals(WSDLConstants.NS_WSDL) &&
e.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart())) {
}
DOMUtil.serializeNode(e, writer);
}
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
return new W3CEndpointReference(new XMLStreamBufferSource(writer.getXMLStreamBuffer()));
}
private static boolean w3cMetadataWritten = false;
private static MemberSubmissionEndpointReference toMSEpr(W3CEndpointReference w3cEpr) {
DOMResult result = new DOMResult();
w3cEpr.writeTo(result);
Node eprNode = result.getNode();
Element e = DOMUtil.getFirstElementChild(eprNode);
if (e == null) {
return null;
}
MemberSubmissionEndpointReference msEpr = new MemberSubmissionEndpointReference();
NodeList nodes = e.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element child = (Element) nodes.item(i);
if (child.getNamespaceURI().equals(AddressingVersion.W3C.nsUri) &&
child.getLocalName().equals(AddressingVersion.W3C.eprType.address)) {
if (msEpr.addr == null) {
msEpr.addr = new MemberSubmissionEndpointReference.Address();
}
msEpr.addr.uri = XmlUtil.getTextForNode(child);
} else if (child.getNamespaceURI().equals(AddressingVersion.W3C.nsUri) &&
child.getLocalName().equals("ReferenceParameters")) {
NodeList refParams = child.getChildNodes();
for (int j = 0; j < refParams.getLength(); j++) {
if (refParams.item(j).getNodeType() == Node.ELEMENT_NODE) {
if (msEpr.referenceParameters == null) {
msEpr.referenceParameters = new MemberSubmissionEndpointReference.Elements();
msEpr.referenceParameters.elements = new ArrayList<Element>();
}
msEpr.referenceParameters.elements.add((Element) refParams.item(j));
}
}
} else if (child.getNamespaceURI().equals(AddressingVersion.W3C.nsUri) &&
child.getLocalName().equals(AddressingVersion.W3C.eprType.wsdlMetadata.getLocalPart())) {
NodeList metadata = child.getChildNodes();
String wsdlLocation = child.getAttributeNS(W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_NAMESPACE,
W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_LOCALNAME);
Element wsdlDefinitions = null;
for (int j = 0; j < metadata.getLength(); j++) {
Node node = metadata.item(j);
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element elm = (Element) node;
if ((elm.getNamespaceURI().equals(AddressingVersion.W3C.wsdlNsUri) ||
elm.getNamespaceURI().equals(W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME)) &&
elm.getLocalName().equals(AddressingVersion.W3C.eprType.serviceName)) {
msEpr.serviceName = new MemberSubmissionEndpointReference.ServiceNameType();
msEpr.serviceName.portName = elm.getAttribute(AddressingVersion.W3C.eprType.portName);
String service = elm.getTextContent();
String prefix = XmlUtil.getPrefix(service);
String name = XmlUtil.getLocalPart(service);
if (name == null) {
continue;
}
if (prefix != null) {
String ns = elm.lookupNamespaceURI(prefix);
if (ns != null) {
msEpr.serviceName.name = new QName(ns, name, prefix);
}
} else {
msEpr.serviceName.name = new QName(null, name);
}
msEpr.serviceName.attributes = getAttributes(elm);
} else if ((elm.getNamespaceURI().equals(AddressingVersion.W3C.wsdlNsUri) ||
elm.getNamespaceURI().equals(W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME)) &&
elm.getLocalName().equals(AddressingVersion.W3C.eprType.portTypeName)) {
msEpr.portTypeName = new MemberSubmissionEndpointReference.AttributedQName();
String portType = elm.getTextContent();
String prefix = XmlUtil.getPrefix(portType);
String name = XmlUtil.getLocalPart(portType);
if (name == null) {
continue;
}
if (prefix != null) {
String ns = elm.lookupNamespaceURI(prefix);
if (ns != null) {
msEpr.portTypeName.name = new QName(ns, name, prefix);
}
} else {
msEpr.portTypeName.name = new QName(null, name);
}
msEpr.portTypeName.attributes = getAttributes(elm);
} else if(elm.getNamespaceURI().equals(WSDLConstants.NS_WSDL) &&
elm.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart())) {
wsdlDefinitions = elm;
} else {
if (msEpr.elements == null) {
msEpr.elements = new ArrayList<Element>();
}
msEpr.elements.add(elm);
}
}
Document doc = DOMUtil.createDom();
Element mexEl = doc.createElementNS(MemberSubmissionAddressingConstants.MEX_METADATA.getNamespaceURI(),
MemberSubmissionAddressingConstants.MEX_METADATA.getPrefix() + ":"
+ MemberSubmissionAddressingConstants.MEX_METADATA.getLocalPart());
Element metadataEl = doc.createElementNS(MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getNamespaceURI(),
MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getPrefix() + ":"
+ MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getLocalPart());
metadataEl.setAttribute(MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_ATTRIBUTE,
MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_VALUE);
if (wsdlDefinitions == null && wsdlLocation != null && !wsdlLocation.equals("")) {
wsdlLocation = wsdlLocation.trim();
String wsdlTns = wsdlLocation.substring(0, wsdlLocation.indexOf(' '));
wsdlLocation = wsdlLocation.substring(wsdlLocation.indexOf(' ') + 1);
Element wsdlEl = doc.createElementNS(WSDLConstants.NS_WSDL,
WSDLConstants.PREFIX_NS_WSDL + ":"
+ WSDLConstants.QNAME_DEFINITIONS.getLocalPart());
Element wsdlImportEl = doc.createElementNS(WSDLConstants.NS_WSDL,
WSDLConstants.PREFIX_NS_WSDL + ":"
+ WSDLConstants.QNAME_IMPORT.getLocalPart());
wsdlImportEl.setAttribute("namespace", wsdlTns);
wsdlImportEl.setAttribute("location", wsdlLocation);
wsdlEl.appendChild(wsdlImportEl);
metadataEl.appendChild(wsdlEl);
} else if(wsdlDefinitions != null){
metadataEl.appendChild(wsdlDefinitions);
}
mexEl.appendChild(metadataEl);
if (msEpr.elements == null) {
msEpr.elements = new ArrayList<Element>();
}
msEpr.elements.add(mexEl);
} else {
if (msEpr.elements == null) {
msEpr.elements = new ArrayList<Element>();
}
msEpr.elements.add((Element) child);
}
} else if (nodes.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
Node n = nodes.item(i);
if (msEpr.attributes == null) {
msEpr.attributes = new HashMap<QName, String>();
String prefix = fixNull(n.getPrefix());
String ns = fixNull(n.getNamespaceURI());
String localName = n.getLocalName();
msEpr.attributes.put(new QName(ns, localName, prefix), n.getNodeValue());
}
}
}
return msEpr;
}
private static Map<QName, String> getAttributes(Node node) {
Map<QName, String> attribs = null;
NamedNodeMap nm = node.getAttributes();
for (int i = 0; i < nm.getLength(); i++) {
if (attribs == null) {
attribs = new HashMap<QName, String>();
}
Node n = nm.item(i);
String prefix = fixNull(n.getPrefix());
String ns = fixNull(n.getNamespaceURI());
String localName = n.getLocalName();
if (prefix.equals("xmlns") || prefix.length() == 0 && localName.equals("xmlns")) {
continue;
}
if (!localName.equals(AddressingVersion.W3C.eprType.portName)) {
attribs.put(new QName(ns, localName, prefix), n.getNodeValue());
}
}
return attribs;
}
private static
@NotNull
String fixNull(@Nullable String s) {
if (s == null) {
return "";
} else {
return s;
}
}
}