package com.sun.xml.internal.ws.api.addressing;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
import com.sun.xml.internal.stream.buffer.XMLStreamBufferSource;
import com.sun.xml.internal.stream.buffer.sax.SAXBufferProcessor;
import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferProcessor;
import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferCreator;
import com.sun.xml.internal.ws.addressing.EndpointReferenceUtil;
import com.sun.xml.internal.ws.addressing.W3CAddressingMetadataConstants;
import com.sun.xml.internal.ws.addressing.WSEPRExtension;
import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException;
import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants;
import com.sun.xml.internal.ws.api.message.Header;
import com.sun.xml.internal.ws.api.message.HeaderList;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.MessageHeaders;
import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLExtension;
import com.sun.xml.internal.ws.resources.AddressingMessages;
import com.sun.xml.internal.ws.resources.ClientMessages;
import com.sun.xml.internal.ws.spi.ProviderImpl;
import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
import com.sun.xml.internal.ws.util.DOMUtil;
import com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import com.sun.xml.internal.org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter;
import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
import org.w3c.dom.Element;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;
import javax.xml.bind.JAXBContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.EndpointReference;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;
import java.net.URL;
import java.util.*;
public final class WSEndpointReference implements WSDLExtension {
private final XMLStreamBuffer infoset;
private final AddressingVersion version;
private @NotNull Header[] referenceParameters;
private @NotNull String address;
private @NotNull QName rootElement;
public WSEndpointReference(EndpointReference epr, AddressingVersion version) {
try {
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
epr.writeTo(new XMLStreamBufferResult(xsb));
this.infoset = xsb;
this.version = version;
this.rootElement = new QName("EndpointReference", version.nsUri);
parse();
} catch (XMLStreamException e) {
throw new WebServiceException(ClientMessages.FAILED_TO_PARSE_EPR(epr),e);
}
}
public WSEndpointReference(EndpointReference epr) {
this(epr,AddressingVersion.fromSpecClass(epr.getClass()));
}
public WSEndpointReference(XMLStreamBuffer infoset, AddressingVersion version) {
try {
this.infoset = infoset;
this.version = version;
this.rootElement = new QName("EndpointReference", version.nsUri);
parse();
} catch (XMLStreamException e) {
throw new AssertionError(e);
}
}
public WSEndpointReference(InputStream infoset, AddressingVersion version) throws XMLStreamException {
this(XMLStreamReaderFactory.create(null,infoset,false),version);
}
public WSEndpointReference(XMLStreamReader in, AddressingVersion version) throws XMLStreamException {
this(XMLStreamBuffer.createNewBufferFromXMLStreamReader(in), version);
}
public WSEndpointReference(URL address, AddressingVersion version) {
this(address.toExternalForm(), version);
}
public WSEndpointReference(URI address, AddressingVersion version) {
this(address.toString(), version);
}
public WSEndpointReference(String address, AddressingVersion version) {
this.infoset = createBufferFromAddress(address,version);
this.version = version;
this.address = address;
this.rootElement = new QName("EndpointReference", version.nsUri);
this.referenceParameters = EMPTY_ARRAY;
}
private static XMLStreamBuffer createBufferFromAddress(String address, AddressingVersion version) {
try {
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
StreamWriterBufferCreator w = new StreamWriterBufferCreator(xsb);
w.writeStartDocument();
w.writeStartElement(version.getPrefix(),
"EndpointReference", version.nsUri);
w.writeNamespace(version.getPrefix(), version.nsUri);
w.writeStartElement(version.getPrefix(),version.eprType.address, version.nsUri);
w.writeCharacters(address);
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
w.close();
return xsb;
} catch (XMLStreamException e) {
throw new AssertionError(e);
}
}
public WSEndpointReference(@NotNull AddressingVersion version,
@NotNull String address,
@Nullable QName service,
@Nullable QName port,
@Nullable QName portType,
@Nullable List<Element> metadata,
@Nullable String wsdlAddress,
@Nullable List<Element> referenceParameters) {
this(version, address, service, port, portType, metadata, wsdlAddress, null, referenceParameters, null, null);
}
public WSEndpointReference(@NotNull AddressingVersion version,
@NotNull String address,
@Nullable QName service,
@Nullable QName port,
@Nullable QName portType,
@Nullable List<Element> metadata,
@Nullable String wsdlAddress,
@Nullable List<Element> referenceParameters,
@Nullable Collection<EPRExtension> extns,@Nullable Map<QName, String> attributes) {
this(createBufferFromData(version, address, referenceParameters, service, port, portType, metadata, wsdlAddress, null, extns, attributes),
version );
}
public WSEndpointReference(@NotNull AddressingVersion version,
@NotNull String address,
@Nullable QName service,
@Nullable QName port,
@Nullable QName portType,
@Nullable List<Element> metadata,
@Nullable String wsdlAddress,
@Nullable String wsdlTargetNamepsace,
@Nullable List<Element> referenceParameters,
@Nullable List<Element> elements, @Nullable Map<QName, String> attributes) {
this(
createBufferFromData(version, address, referenceParameters, service, port, portType, metadata, wsdlAddress,wsdlTargetNamepsace, elements, attributes),
version );
}
private static XMLStreamBuffer createBufferFromData(AddressingVersion version, String address, List<Element> referenceParameters, QName service, QName port, QName portType,
List<Element> metadata, String wsdlAddress, String wsdlTargetNamespace, @Nullable List<Element> elements, @Nullable Map<QName, String> attributes) {
StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
try {
writer.writeStartDocument();
writer.writeStartElement(version.getPrefix(),"EndpointReference", version.nsUri);
writer.writeNamespace(version.getPrefix(),version.nsUri);
writePartialEPRInfoset(writer, version, address, referenceParameters, service, port, portType,
metadata,wsdlAddress, wsdlTargetNamespace, attributes);
if (elements != null) {
for (Element e : elements) {
DOMUtil.serializeNode(e, writer);
}
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
return writer.getXMLStreamBuffer();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
}
private static XMLStreamBuffer createBufferFromData(AddressingVersion version, String address, List<Element> referenceParameters, QName service, QName port, QName portType,
List<Element> metadata, String wsdlAddress, String wsdlTargetNamespace, @Nullable Collection<EPRExtension> extns, @Nullable Map<QName, String> attributes) {
StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
try {
writer.writeStartDocument();
writer.writeStartElement(version.getPrefix(),"EndpointReference", version.nsUri);
writer.writeNamespace(version.getPrefix(),version.nsUri);
writePartialEPRInfoset(writer, version, address, referenceParameters, service, port, portType,
metadata,wsdlAddress, wsdlTargetNamespace, attributes);
if (extns != null) {
for (EPRExtension e : extns) {
XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
XMLStreamReader r = e.readAsXMLStreamReader();
c.bridge(r, writer);
XMLStreamReaderFactory.recycle(r);
}
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
return writer.getXMLStreamBuffer();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
}
private static void writePartialEPRInfoset(StreamWriterBufferCreator writer, AddressingVersion version, String address, List<Element> referenceParameters, QName service, QName port, QName portType,
List<Element> metadata, String wsdlAddress, String wsdlTargetNamespace, @Nullable Map<QName, String> attributes) throws XMLStreamException {
if (attributes != null) {
for (Map.Entry<QName, String> entry : attributes.entrySet()) {
QName qname = entry.getKey();
writer.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(), entry.getValue());
}
}
writer.writeStartElement(version.getPrefix(), version.eprType.address, version.nsUri);
writer.writeCharacters(address);
writer.writeEndElement();
if(referenceParameters != null && referenceParameters.size() > 0) {
writer.writeStartElement(version.getPrefix(), version.eprType.referenceParameters, version.nsUri);
for (Element e : referenceParameters) {
DOMUtil.serializeNode(e, writer);
}
writer.writeEndElement();
}
switch (version) {
case W3C:
writeW3CMetaData(writer, service, port, portType, metadata, wsdlAddress, wsdlTargetNamespace);
break;
case MEMBER:
writeMSMetaData(writer, service, port, portType, metadata);
if (wsdlAddress != null) {
writer.writeStartElement(MemberSubmissionAddressingConstants.MEX_METADATA.getPrefix(),
MemberSubmissionAddressingConstants.MEX_METADATA.getLocalPart(),
MemberSubmissionAddressingConstants.MEX_METADATA.getNamespaceURI());
writer.writeStartElement(MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getPrefix(),
MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getLocalPart(),
MemberSubmissionAddressingConstants.MEX_METADATA_SECTION.getNamespaceURI());
writer.writeAttribute(MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_ATTRIBUTE,
MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_VALUE);
writeWsdl(writer, service, wsdlAddress);
writer.writeEndElement();
writer.writeEndElement();
}
break;
}
}
private static boolean isEmty(QName qname) {
return qname == null || qname.toString().trim().length()== 0;
}
private static void writeW3CMetaData(StreamWriterBufferCreator writer,
QName service,
QName port,
QName portType, List<Element> metadata,
String wsdlAddress, String wsdlTargetNamespace) throws XMLStreamException {
if (isEmty(service) && isEmty(port) && isEmty(portType) && metadata == null) {
return;
}
writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
AddressingVersion.W3C.eprType.wsdlMetadata.getLocalPart(), AddressingVersion.W3C.nsUri);
writer.writeNamespace(AddressingVersion.W3C.getWsdlPrefix(),
AddressingVersion.W3C.wsdlNsUri);
if(wsdlAddress != null) {
writeWsdliLocation(writer, service, wsdlAddress, wsdlTargetNamespace);
}
if (portType != null) {
writer.writeStartElement(W3CAddressingMetadataConstants.WSAM_PREFIX_NAME,
AddressingVersion.W3C.eprType.portTypeName,
W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME);
writer.writeNamespace(W3CAddressingMetadataConstants.WSAM_PREFIX_NAME,
W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME);
String portTypePrefix = portType.getPrefix();
if (portTypePrefix == null || portTypePrefix.equals("")) {
portTypePrefix = "wsns";
}
writer.writeNamespace(portTypePrefix, portType.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + portType.getLocalPart());
writer.writeEndElement();
}
if (service != null) {
if (!(service.getNamespaceURI().equals("") || service.getLocalPart().equals(""))) {
writer.writeStartElement(W3CAddressingMetadataConstants.WSAM_PREFIX_NAME,
AddressingVersion.W3C.eprType.serviceName,
W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME);
writer.writeNamespace(W3CAddressingMetadataConstants.WSAM_PREFIX_NAME,
W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME);
String servicePrefix = service.getPrefix();
if (servicePrefix == null || servicePrefix.equals("")) {
servicePrefix = "wsns";
}
writer.writeNamespace(servicePrefix, service.getNamespaceURI());
if (port != null) {
writer.writeAttribute(AddressingVersion.W3C.eprType.portName, port.getLocalPart());
}
writer.writeCharacters(servicePrefix + ":" + service.getLocalPart());
writer.writeEndElement();
}
}
if (metadata != null) {
for (Element e : metadata) {
DOMUtil.serializeNode(e, writer);
}
}
writer.writeEndElement();
}
private static void writeWsdliLocation(StreamWriterBufferCreator writer, QName service,String wsdlAddress,String wsdlTargetNamespace) throws XMLStreamException {
String wsdliLocation = "";
if(wsdlTargetNamespace != null) {
wsdliLocation = wsdlTargetNamespace + " ";
} else if (service != null) {
wsdliLocation = service.getNamespaceURI() + " ";
} else {
throw new WebServiceException("WSDL target Namespace cannot be resolved");
}
wsdliLocation += wsdlAddress;
writer.writeNamespace(W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_PREFIX,
W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_NAMESPACE);
writer.writeAttribute(W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_PREFIX,
W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_NAMESPACE,
W3CAddressingMetadataConstants.WSAM_WSDLI_ATTRIBUTE_LOCALNAME,
wsdliLocation);
}
private static void writeMSMetaData(StreamWriterBufferCreator writer,
QName service,
QName port,
QName portType, List<Element> metadata) throws XMLStreamException {
if (portType != null) {
writer.writeStartElement(AddressingVersion.MEMBER.getPrefix(),
AddressingVersion.MEMBER.eprType.portTypeName,
AddressingVersion.MEMBER.nsUri);
String portTypePrefix = portType.getPrefix();
if (portTypePrefix == null || portTypePrefix.equals("")) {
portTypePrefix = "wsns";
}
writer.writeNamespace(portTypePrefix, portType.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + portType.getLocalPart());
writer.writeEndElement();
}
if (service != null) {
if (!(service.getNamespaceURI().equals("") || service.getLocalPart().equals(""))) {
writer.writeStartElement(AddressingVersion.MEMBER.getPrefix(),
AddressingVersion.MEMBER.eprType.serviceName,
AddressingVersion.MEMBER.nsUri);
String servicePrefix = service.getPrefix();
if (servicePrefix == null || servicePrefix.equals("")) {
servicePrefix = "wsns";
}
writer.writeNamespace(servicePrefix, service.getNamespaceURI());
if (port != null) {
writer.writeAttribute(AddressingVersion.MEMBER.eprType.portName,
port.getLocalPart());
}
writer.writeCharacters(servicePrefix + ":" + service.getLocalPart());
writer.writeEndElement();
}
}
}
private static void writeWsdl(StreamWriterBufferCreator writer, QName service, String wsdlAddress) throws XMLStreamException {
writer.writeStartElement(WSDLConstants.PREFIX_NS_WSDL,
WSDLConstants.QNAME_DEFINITIONS.getLocalPart(),
WSDLConstants.NS_WSDL);
writer.writeNamespace(WSDLConstants.PREFIX_NS_WSDL, WSDLConstants.NS_WSDL);
writer.writeStartElement(WSDLConstants.PREFIX_NS_WSDL,
WSDLConstants.QNAME_IMPORT.getLocalPart(),
WSDLConstants.NS_WSDL);
writer.writeAttribute("namespace", service.getNamespaceURI());
writer.writeAttribute("location", wsdlAddress);
writer.writeEndElement();
writer.writeEndElement();
}
public static @Nullable
WSEndpointReference create(@Nullable EndpointReference epr) {
if (epr != null) {
return new WSEndpointReference(epr);
} else {
return null;
}
}
public @NotNull WSEndpointReference createWithAddress(@NotNull URI newAddress) {
return createWithAddress(newAddress.toString());
}
public @NotNull WSEndpointReference createWithAddress(@NotNull URL newAddress) {
return createWithAddress(newAddress.toString());
}
public @NotNull WSEndpointReference createWithAddress(@NotNull final String newAddress) {
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
XMLFilterImpl filter = new XMLFilterImpl() {
private boolean inAddress = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("Address") && uri.equals(version.nsUri)) {
inAddress = true;
}
super.startElement(uri,localName,qName,atts);
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (!inAddress) {
super.characters(ch, start, length);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (inAddress) {
super.characters(newAddress.toCharArray(),0,newAddress.length());
}
inAddress = false;
super.endElement(uri, localName, qName);
}
};
filter.setContentHandler(xsb.createFromSAXBufferCreator());
try {
infoset.writeTo(filter,false);
} catch (SAXException e) {
throw new AssertionError(e);
}
return new WSEndpointReference(xsb,version);
}
public @NotNull EndpointReference toSpec() {
return ProviderImpl.INSTANCE.readEndpointReference(asSource("EndpointReference"));
}
public @NotNull <T extends EndpointReference> T toSpec(Class<T> clazz) {
return EndpointReferenceUtil.transform(clazz,toSpec());
}
public @NotNull <T> T getPort(@NotNull Service jaxwsService,
@NotNull Class<T> serviceEndpointInterface,
WebServiceFeature... features) {
return jaxwsService.getPort(toSpec(),serviceEndpointInterface,features);
}
public @NotNull <T> Dispatch<T> createDispatch(
@NotNull Service jaxwsService,
@NotNull Class<T> type,
@NotNull Service.Mode mode,
WebServiceFeature... features) {
return jaxwsService.createDispatch(toSpec(),type,mode,features);
}
public @NotNull Dispatch<Object> createDispatch(
@NotNull Service jaxwsService,
@NotNull JAXBContext context,
@NotNull Service.Mode mode,
WebServiceFeature... features) {
return jaxwsService.createDispatch(toSpec(),context,mode,features);
}
public @NotNull AddressingVersion getVersion() {
return version;
}
public @NotNull String getAddress() {
return address;
}
public boolean isAnonymous() {
return address.equals(version.anonymousUri);
}
public boolean isNone() {
return address.equals(version.noneUri);
}
private void parse() throws XMLStreamException {
StreamReaderBufferProcessor xsr = infoset.readAsXMLStreamReader();
if (xsr.getEventType()==XMLStreamReader.START_DOCUMENT) {
xsr.nextTag();
}
assert xsr.getEventType()==XMLStreamReader.START_ELEMENT;
String rootLocalName = xsr.getLocalName();
if(!xsr.getNamespaceURI().equals(version.nsUri)) {
throw new WebServiceException(AddressingMessages.WRONG_ADDRESSING_VERSION(
version.nsUri, xsr.getNamespaceURI()));
}
this.rootElement = new QName(xsr.getNamespaceURI(), rootLocalName);
List<Header> marks=null;
while(xsr.nextTag()==XMLStreamReader.START_ELEMENT) {
String localName = xsr.getLocalName();
if(version.isReferenceParameter(localName)) {
XMLStreamBuffer mark;
while((mark = xsr.nextTagAndMark())!=null) {
if (marks==null) {
marks = new ArrayList<Header>();
}
marks.add(version.createReferenceParameterHeader(
mark, xsr.getNamespaceURI(), xsr.getLocalName()));
XMLStreamReaderUtil.skipElement(xsr);
}
} else
if(localName.equals("Address")) {
if (address!=null) {
throw new InvalidAddressingHeaderException(new QName(version.nsUri,rootLocalName),AddressingVersion.fault_duplicateAddressInEpr);
}
address = xsr.getElementText().trim();
} else {
XMLStreamReaderUtil.skipElement(xsr);
}
}
if (marks==null) {
this.referenceParameters = EMPTY_ARRAY;
} else {
this.referenceParameters = marks.toArray(new Header[marks.size()]);
}
if (address==null) {
throw new InvalidAddressingHeaderException(new QName(version.nsUri,rootLocalName),version.fault_missingAddressInEpr);
}
}
public XMLStreamReader read(final @NotNull String localName) throws XMLStreamException {
return new StreamReaderBufferProcessor(infoset) {
@Override
protected void processElement(String prefix, String uri, String _localName, boolean inScope) {
if (_depth == 0) {
_localName = localName;
}
super.processElement(prefix, uri, _localName, isInscope(infoset,_depth));
}
};
}
private boolean isInscope(XMLStreamBuffer buffer, int depth) {
return buffer.getInscopeNamespaces().size() > 0 && depth ==0;
}
public Source asSource(@NotNull String localName) {
return new SAXSource(new SAXBufferProcessorImpl(localName),new InputSource());
}
public void writeTo(@NotNull String localName, ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
SAXBufferProcessorImpl p = new SAXBufferProcessorImpl(localName);
p.setContentHandler(contentHandler);
p.setErrorHandler(errorHandler);
p.process(infoset,fragment);
}
public void writeTo(final @NotNull String localName, @NotNull XMLStreamWriter w) throws XMLStreamException {
infoset.writeToXMLStreamWriter(new XMLStreamWriterFilter(w) {
private boolean root=true;
@Override
public void writeStartDocument() throws XMLStreamException {
}
@Override
public void writeStartDocument(String encoding, String version) throws XMLStreamException {
}
@Override
public void writeStartDocument(String version) throws XMLStreamException {
}
@Override
public void writeEndDocument() throws XMLStreamException {
}
private String override(String ln) {
if(root) {
root = false;
return localName;
}
return ln;
}
@Override
public void writeStartElement(String localName) throws XMLStreamException {
super.writeStartElement(override(localName));
}
@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
super.writeStartElement(namespaceURI, override(localName));
}
@Override
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
super.writeStartElement(prefix, override(localName), namespaceURI);
}
},true);
}
public Header (QName rootTagName) {
return new EPRHeader(rootTagName,this);
}
@SuppressWarnings("ManualArrayToCollectionCopy")
public void (HeaderList outbound) {
for (Header header : referenceParameters) {
outbound.add(header);
}
}
public void (MessageHeaders outbound) {
for (Header header : referenceParameters) {
outbound.add(header);
}
}
public void (HeaderList headers) {
if (headers != null) {
Header[] hs = new Header[referenceParameters.length + headers.size()];
System.arraycopy(referenceParameters, 0, hs, 0, referenceParameters.length);
int i = referenceParameters.length;
for (Header h : headers) {
hs[i++] = h;
}
referenceParameters = hs;
}
}
@Override
public String toString() {
try {
StringWriter sw = new StringWriter();
XmlUtil.newTransformer().transform(asSource("EndpointReference"),new StreamResult(sw));
return sw.toString();
} catch (TransformerException e) {
return e.toString();
}
}
@Override
public QName getName() {
return rootElement;
}
class SAXBufferProcessorImpl extends SAXBufferProcessor {
private final String rootLocalName;
private boolean root=true;
public SAXBufferProcessorImpl(String rootLocalName) {
super(infoset,false);
this.rootLocalName = rootLocalName;
}
@Override
protected void processElement(String uri, String localName, String qName, boolean inscope) throws SAXException {
if(root) {
root = false;
if(qName.equals(localName)) {
qName = localName = rootLocalName;
} else {
localName = rootLocalName;
int idx = qName.indexOf(':');
qName = qName.substring(0,idx+1)+rootLocalName;
}
}
super.processElement(uri, localName, qName, inscope);
}
}
private static final OutboundReferenceParameterHeader[] EMPTY_ARRAY = new OutboundReferenceParameterHeader[0];
private Map<QName, EPRExtension> rootEprExtensions;
public static abstract class EPRExtension {
public abstract XMLStreamReader readAsXMLStreamReader() throws XMLStreamException;
public abstract QName getQName();
}
public @Nullable
EPRExtension getEPRExtension(final QName extnQName) throws XMLStreamException {
if (rootEprExtensions == null) {
parseEPRExtensions();
}
return rootEprExtensions.get(extnQName);
}
public @NotNull Collection<EPRExtension> getEPRExtensions() throws XMLStreamException {
if (rootEprExtensions == null) {
parseEPRExtensions();
}
return rootEprExtensions.values();
}
private void parseEPRExtensions() throws XMLStreamException {
rootEprExtensions = new HashMap<QName, EPRExtension>();
StreamReaderBufferProcessor xsr = infoset.readAsXMLStreamReader();
if (xsr.getEventType() == XMLStreamReader.START_DOCUMENT) {
xsr.nextTag();
}
assert xsr.getEventType() == XMLStreamReader.START_ELEMENT;
if (!xsr.getNamespaceURI().equals(version.nsUri)) {
throw new WebServiceException(AddressingMessages.WRONG_ADDRESSING_VERSION(
version.nsUri, xsr.getNamespaceURI()));
}
XMLStreamBuffer mark;
String localName;
String ns;
while ((mark = xsr.nextTagAndMark()) != null) {
localName = xsr.getLocalName();
ns = xsr.getNamespaceURI();
if (version.nsUri.equals(ns)) {
XMLStreamReaderUtil.skipElement(xsr);
} else {
QName qn = new QName(ns, localName);
rootEprExtensions.put(qn, new WSEPRExtension(mark,qn));
XMLStreamReaderUtil.skipElement(xsr);
}
}
}
public @NotNull Metadata getMetaData() {
return new Metadata();
}
public class Metadata {
private @Nullable QName serviceName;
private @Nullable QName portName;
private @Nullable QName portTypeName;
private @Nullable Source wsdlSource;
private @Nullable String wsdliLocation;
public @Nullable QName getServiceName(){
return serviceName;
}
public @Nullable QName getPortName(){
return portName;
}
public @Nullable QName getPortTypeName(){
return portTypeName;
}
public @Nullable Source getWsdlSource(){
return wsdlSource;
}
public @Nullable String getWsdliLocation(){
return wsdliLocation;
}
private Metadata() {
try {
parseMetaData();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
}
private void parseMetaData() throws XMLStreamException {
StreamReaderBufferProcessor xsr = infoset.readAsXMLStreamReader();
if (xsr.getEventType() == XMLStreamReader.START_DOCUMENT) {
xsr.nextTag();
}
assert xsr.getEventType() == XMLStreamReader.START_ELEMENT;
String rootElement = xsr.getLocalName();
if (!xsr.getNamespaceURI().equals(version.nsUri)) {
throw new WebServiceException(AddressingMessages.WRONG_ADDRESSING_VERSION(
version.nsUri, xsr.getNamespaceURI()));
}
String localName;
String ns;
if (version == AddressingVersion.W3C) {
do {
if (xsr.getLocalName().equals(version.eprType.wsdlMetadata.getLocalPart())) {
String wsdlLoc = xsr.getAttributeValue("http://www.w3.org/ns/wsdl-instance","wsdlLocation");
if (wsdlLoc != null) {
wsdliLocation = wsdlLoc.trim();
}
XMLStreamBuffer mark;
while ((mark = xsr.nextTagAndMark()) != null) {
localName = xsr.getLocalName();
ns = xsr.getNamespaceURI();
if (localName.equals(version.eprType.serviceName)) {
String portStr = xsr.getAttributeValue(null, version.eprType.portName);
if (serviceName != null) {
throw new RuntimeException("More than one "+ version.eprType.serviceName +" element in EPR Metadata");
}
serviceName = getElementTextAsQName(xsr);
if (serviceName != null && portStr != null) {
portName = new QName(serviceName.getNamespaceURI(), portStr);
}
} else if (localName.equals(version.eprType.portTypeName)) {
if (portTypeName != null) {
throw new RuntimeException("More than one "+ version.eprType.portTypeName +" element in EPR Metadata");
}
portTypeName = getElementTextAsQName(xsr);
} else if (ns.equals(WSDLConstants.NS_WSDL)
&& localName.equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart())) {
wsdlSource = new XMLStreamBufferSource(mark);
} else {
XMLStreamReaderUtil.skipElement(xsr);
}
}
} else {
if (!xsr.getLocalName().equals(rootElement)) {
XMLStreamReaderUtil.skipElement(xsr);
}
}
} while (XMLStreamReaderUtil.nextElementContent(xsr) == XMLStreamReader.START_ELEMENT);
if(wsdliLocation != null) {
String wsdlLocation = wsdliLocation.trim();
wsdlLocation = wsdlLocation.substring(wsdliLocation.lastIndexOf(" "));
wsdlSource = new StreamSource(wsdlLocation);
}
} else if (version == AddressingVersion.MEMBER) {
do {
localName = xsr.getLocalName();
ns = xsr.getNamespaceURI();
if (localName.equals(version.eprType.wsdlMetadata.getLocalPart()) &&
ns.equals(version.eprType.wsdlMetadata.getNamespaceURI())) {
while (xsr.nextTag() == XMLStreamReader.START_ELEMENT) {
XMLStreamBuffer mark;
while ((mark = xsr.nextTagAndMark()) != null) {
localName = xsr.getLocalName();
ns = xsr.getNamespaceURI();
if (ns.equals(WSDLConstants.NS_WSDL)
&& localName.equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart())) {
wsdlSource = new XMLStreamBufferSource(mark);
} else {
XMLStreamReaderUtil.skipElement(xsr);
}
}
}
} else if (localName.equals(version.eprType.serviceName)) {
String portStr = xsr.getAttributeValue(null, version.eprType.portName);
serviceName = getElementTextAsQName(xsr);
if (serviceName != null && portStr != null) {
portName = new QName(serviceName.getNamespaceURI(), portStr);
}
} else if (localName.equals(version.eprType.portTypeName)) {
portTypeName = getElementTextAsQName(xsr);
} else {
if (!xsr.getLocalName().equals(rootElement)) {
XMLStreamReaderUtil.skipElement(xsr);
}
}
} while (XMLStreamReaderUtil.nextElementContent(xsr) == XMLStreamReader.START_ELEMENT);
}
}
private QName getElementTextAsQName(StreamReaderBufferProcessor xsr) throws XMLStreamException {
String text = xsr.getElementText().trim();
String prefix = XmlUtil.getPrefix(text);
String name = XmlUtil.getLocalPart(text);
if (name != null) {
if (prefix != null) {
String ns = xsr.getNamespaceURI(prefix);
if (ns != null) {
return new QName(ns, name, prefix);
}
} else {
return new QName(null, name);
}
}
return null;
}
}
}