com.sun.xml.ws.api.addressing.WSEndpointReference Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.xml.ws.api.addressing;
import com.sun.istack.NotNull;
import com.sun.istack.Nullable;
import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
import com.sun.xml.stream.buffer.XMLStreamBuffer;
import com.sun.xml.stream.buffer.XMLStreamBufferResult;
import com.sun.xml.stream.buffer.XMLStreamBufferSource;
import com.sun.xml.stream.buffer.sax.SAXBufferProcessor;
import com.sun.xml.stream.buffer.stax.StreamReaderBufferProcessor;
import com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator;
import com.sun.xml.ws.addressing.EndpointReferenceUtil;
import com.sun.xml.ws.addressing.W3CAddressingConstants;
import com.sun.xml.ws.addressing.model.InvalidAddressingHeaderException;
import com.sun.xml.ws.addressing.v200408.MemberSubmissionAddressingConstants;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.HeaderList;
import com.sun.xml.ws.api.message.Message;
import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
import com.sun.xml.ws.resources.AddressingMessages;
import com.sun.xml.ws.resources.ClientMessages;
import com.sun.xml.ws.spi.ProviderImpl;
import com.sun.xml.ws.streaming.XMLStreamReaderUtil;
import com.sun.xml.ws.util.DOMUtil;
import com.sun.xml.ws.util.xml.XMLStreamWriterFilter;
import com.sun.xml.ws.util.xml.XmlUtil;
import com.sun.xml.ws.wsdl.parser.WSDLConstants;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
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.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.ArrayList;
import java.util.List;
/**
* Internal representation of the EPR.
*
*
* Instances of this class are immutable and thread-safe.
*
* @author Kohsuke Kawaguchi
* @see AddressingVersion#anonymousEpr
*/
public final class WSEndpointReference {
private final XMLStreamBuffer infoset;
/**
* Version of the addressing spec.
*/
private final AddressingVersion version;
/**
* Marked Reference parameters inside this EPR.
*
* Parsed when the object is created. can be empty but never null.
* @see #parse()
*/
private @NotNull Header[] referenceParameters;
private @NotNull String address;
/**
* Creates from the spec version of {@link EndpointReference}.
*
*
* This method performs the data conversion, so it's slow.
* Do not use this method in a performance critical path.
*/
public WSEndpointReference(EndpointReference epr, AddressingVersion version) {
try {
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
epr.writeTo(new XMLStreamBufferResult(xsb));
this.infoset = xsb;
this.version = version;
parse();
} catch (XMLStreamException e) {
throw new WebServiceException(ClientMessages.FAILED_TO_PARSE_EPR(epr),e);
}
}
/**
* Creates from the spec version of {@link EndpointReference}.
*
*
* This method performs the data conversion, so it's slow.
* Do not use this method in a performance critical path.
*/
public WSEndpointReference(EndpointReference epr) {
this(epr,AddressingVersion.fromSpecClass(epr.getClass()));
}
/**
* Creates a {@link WSEndpointReference} that wraps a given infoset.
*/
public WSEndpointReference(XMLStreamBuffer infoset, AddressingVersion version) {
try {
this.infoset = infoset;
this.version = version;
parse();
} catch (XMLStreamException e) {
// this can never happen because XMLStreamBuffer never has underlying I/O error.
throw new AssertionError(e);
}
}
/**
* Creates a {@link WSEndpointReference} by parsing an infoset.
*/
public WSEndpointReference(InputStream infoset, AddressingVersion version) throws XMLStreamException {
this(XMLStreamReaderFactory.create(null,infoset,false),version);
}
/**
* Creates a {@link WSEndpointReference} from the given infoset.
* The {@link XMLStreamReader} must point to either a document or an element.
*/
public WSEndpointReference(XMLStreamReader in, AddressingVersion version) throws XMLStreamException {
this(XMLStreamBuffer.createNewBufferFromXMLStreamReader(in), version);
}
/**
* @see #WSEndpointReference(String, AddressingVersion)
*/
public WSEndpointReference(URL address, AddressingVersion version) {
this(address.toExternalForm(), version);
}
/**
* @see #WSEndpointReference(String, AddressingVersion)
*/
public WSEndpointReference(URI address, AddressingVersion version) {
this(address.toString(), version);
}
/**
* Creates a {@link WSEndpointReference} that only has an address.
*/
public WSEndpointReference(String address, AddressingVersion version) {
this.infoset = createBufferFromAddress(address,version);
this.version = version;
this.address = address;
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(),
W3CAddressingConstants.WSA_ADDRESS_NAME, version.nsUri);
w.writeCharacters(address);
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
w.close();
return xsb;
} catch (XMLStreamException e) {
// can never happen because we are writing to XSB
throw new AssertionError(e);
}
}
/**
* Creates an EPR from individual components.
*
*
* This version takes various information about metadata, and creates an EPR that has
* the necessary embedded WSDL.
*/
public WSEndpointReference(@NotNull AddressingVersion version,
@NotNull String address,
@Nullable QName service,
@Nullable QName port,
@Nullable QName portType,
@Nullable List metadata,
@Nullable String wsdlAddress,
@Nullable List referenceParameters) {
this(
createBufferFromData(version, address, referenceParameters, service, port, portType, metadata, wsdlAddress),
version );
}
private static XMLStreamBuffer createBufferFromData(AddressingVersion version, String address, List referenceParameters, QName service, QName port, QName portType, List metadata, String wsdlAddress) {
StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
try {
writer.writeStartDocument();
writer.writeStartElement(version.getPrefix(),"EndpointReference", version.nsUri);
writer.writeNamespace(version.getPrefix(),version.nsUri);
writer.writeStartElement(version.getPrefix(),"Address", version.nsUri);
writer.writeCharacters(address);
writer.writeEndElement();
if(referenceParameters != null) {
writer.writeStartElement(version.getPrefix(),"ReferenceParameters", version.nsUri);
for (Element e : referenceParameters)
DOMUtil.serializeNode(e, writer);
writer.writeEndElement();
}
switch(version) {
case W3C:
writeW3CMetaData(writer, service, port, portType, metadata, wsdlAddress);
break;
case MEMBER:
writeMSMetaData(writer, service, port, portType, metadata);
if (wsdlAddress != null) {
//Inline the wsdl as extensibility element
//Write mex:Metadata wrapper
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;
}
writer.writeEndElement();
writer.writeEndDocument();
writer.flush();
return writer.getXMLStreamBuffer();
} catch (XMLStreamException e) {
throw new WebServiceException(e);
}
}
private static void writeW3CMetaData(StreamWriterBufferCreator writer,
QName service,
QName port,
QName portType, List metadata,
String wsdlAddress) throws XMLStreamException {
writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
W3CAddressingConstants.WSA_METADATA_NAME, AddressingVersion.W3C.nsUri);
writer.writeNamespace(AddressingVersion.W3C.getWsdlPrefix(),
AddressingVersion.W3C.wsdlNsUri);
//Write Interface info
if (portType != null) {
writer.writeStartElement(AddressingVersion.W3C.getWsdlPrefix(),
W3CAddressingConstants.WSAW_INTERFACENAME_NAME,
AddressingVersion.W3C.wsdlNsUri);
String portTypePrefix = portType.getPrefix();
if (portTypePrefix == null || portTypePrefix.equals("")) {
//TODO check prefix again
portTypePrefix = "wsns";
}
writer.writeNamespace(portTypePrefix, portType.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + portType.getLocalPart());
writer.writeEndElement();
}
if (service != null) {
//Write service and Port info
if (!(service.getNamespaceURI().equals("") || service.getLocalPart().equals(""))) {
writer.writeStartElement(AddressingVersion.W3C.getWsdlPrefix(),
W3CAddressingConstants.WSAW_SERVICENAME_NAME,
AddressingVersion.W3C.wsdlNsUri);
String servicePrefix = service.getPrefix();
if (servicePrefix == null || servicePrefix.equals("")) {
//TODO check prefix again
servicePrefix = "wsns";
}
writer.writeNamespace(servicePrefix, service.getNamespaceURI());
if (port != null) {
writer.writeAttribute(W3CAddressingConstants.WSAW_ENDPOINTNAME_NAME, port.getLocalPart());
}
writer.writeCharacters(servicePrefix + ":" + service.getLocalPart());
writer.writeEndElement();
}
}
//Inline the wsdl
if (wsdlAddress != null) {
writeWsdl(writer, service, wsdlAddress);
}
//Add the extra metadata Elements
if (metadata != null)
for (Element e : metadata) {
DOMUtil.serializeNode(e, writer);
}
writer.writeEndElement();
}
private static void writeMSMetaData(StreamWriterBufferCreator writer,
QName service,
QName port,
QName portType, List metadata) throws XMLStreamException {
// TODO: write ReferenceProperties
//TODO: write ReferenceParameters
if (portType != null) {
//Write Interface info
writer.writeStartElement(AddressingVersion.MEMBER.getPrefix(),
MemberSubmissionAddressingConstants.WSA_PORTTYPE_NAME,
AddressingVersion.MEMBER.nsUri);
String portTypePrefix = portType.getPrefix();
if (portTypePrefix == null || portTypePrefix.equals("")) {
//TODO check prefix again
portTypePrefix = "wsns";
}
writer.writeNamespace(portTypePrefix, portType.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + portType.getLocalPart());
writer.writeEndElement();
}
//Write service and Port info
if (service != null) {
if (!(service.getNamespaceURI().equals("") || service.getLocalPart().equals(""))) {
writer.writeStartElement(AddressingVersion.MEMBER.getPrefix(),
MemberSubmissionAddressingConstants.WSA_SERVICENAME_NAME,
AddressingVersion.MEMBER.nsUri);
String servicePrefix = service.getPrefix();
if (servicePrefix == null || servicePrefix.equals("")) {
//TODO check prefix again
servicePrefix = "wsns";
}
writer.writeNamespace(servicePrefix, service.getNamespaceURI());
if (port != null) {
writer.writeAttribute(MemberSubmissionAddressingConstants.WSA_PORTNAME_NAME,
port.getLocalPart());
}
writer.writeCharacters(servicePrefix + ":" + service.getLocalPart());
writer.writeEndElement();
}
}
}
private static void writeWsdl(StreamWriterBufferCreator writer, QName service, String wsdlAddress) throws XMLStreamException {
// Inline-wsdl
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();
}
/**
* Converts from {@link EndpointReference}.
*
* This handles null {@link EndpointReference} correctly.
* Call {@link #WSEndpointReference(EndpointReference)} directly
* if you know it's not null.
*/
public static @Nullable
WSEndpointReference create(@Nullable EndpointReference epr) {
if (epr != null)
return new WSEndpointReference(epr);
else
return null;
}
/**
* @see #createWithAddress(String)
*/
public @NotNull WSEndpointReference createWithAddress(@NotNull URI newAddress) {
return createWithAddress(newAddress.toString());
}
/**
* @see #createWithAddress(String)
*/
public @NotNull WSEndpointReference createWithAddress(@NotNull URL newAddress) {
return createWithAddress(newAddress.toString());
}
/**
* Creates a new {@link WSEndpointReference} by replacing the address of this EPR
* to the new one.
*
*
* The following example shows how you can use this to force an HTTPS EPR,
* when the endpoint can serve both HTTP and HTTPS requests.
*
* if(epr.getAddress().startsWith("http:"))
* epr = epr.createWithAddress("https:"+epr.getAddress().substring(5));
*
*
* @param newAddress
* This is a complete URL to be written inside <Adress> element of the EPR,
* such as "http://foo.bar/abc/def"
*/
public @NotNull WSEndpointReference createWithAddress(@NotNull final String newAddress) {
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
XMLFilterImpl filter = new XMLFilterImpl() {
private boolean inAddress = false;
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);
}
public void characters(char ch[], int start, int length) throws SAXException {
if(!inAddress)
super.characters(ch, start, length);
}
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); // impossible since we are writing from XSB to XSB.
}
return new WSEndpointReference(xsb,version);
}
/**
* Convert the EPR to the spec version. The actual type of
* {@link EndpointReference} to be returned depends on which version
* of the addressing spec this EPR conforms to.
*
* @throws WebServiceException
* if the conversion fails, which can happen if the EPR contains
* invalid infoset (wrong namespace URI, etc.)
*/
public @NotNull EndpointReference toSpec() {
return ProviderImpl.INSTANCE.readEndpointReference(asSource("EndpointReference"));
}
/**
* Converts the EPR to the specified spec version.
*
* If the {@link #getVersion() the addressing version in use} and
* the given class is different, then this may involve version conversion.
*/
public @NotNull T toSpec(Class clazz) {
return EndpointReferenceUtil.transform(clazz,toSpec());
}
/**
* Creates a proxy that can be used to talk to this EPR.
*
*
* All the normal WS-Addressing processing happens automatically,
* such as setting the endpoint address to {@link #getAddress() the address},
* and sending the reference parameters associated with this EPR as
* headers, etc.
*/
public @NotNull T getPort(@NotNull Service jaxwsService,
@NotNull Class serviceEndpointInterface,
WebServiceFeature... features) {
// TODO: implement it in a better way
return jaxwsService.getPort(toSpec(),serviceEndpointInterface,features);
}
/**
* Creates a {@link Dispatch} that can be used to talk to this EPR.
*
*
* All the normal WS-Addressing processing happens automatically,
* such as setting the endpoint address to {@link #getAddress() the address},
* and sending the reference parameters associated with this EPR as
* headers, etc.
*/
public @NotNull Dispatch createDispatch(
@NotNull Service jaxwsService,
@NotNull Class type,
@NotNull Service.Mode mode,
WebServiceFeature... features) {
// TODO: implement it in a better way
return jaxwsService.createDispatch(toSpec(),type,mode,features);
}
/**
* Creates a {@link Dispatch} that can be used to talk to this EPR.
*
*
* All the normal WS-Addressing processing happens automatically,
* such as setting the endpoint address to {@link #getAddress() the address},
* and sending the reference parameters associated with this EPR as
* headers, etc.
*/
public @NotNull Dispatch