001: /*
002: * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005:
006: package javax.xml.ws.wsaddressing;
007:
008: import org.w3c.dom.Element;
009:
010: import javax.xml.bind.JAXBContext;
011: import javax.xml.bind.JAXBException;
012: import javax.xml.bind.Marshaller;
013: import javax.xml.bind.annotation.XmlAnyAttribute;
014: import javax.xml.bind.annotation.XmlAnyElement;
015: import javax.xml.bind.annotation.XmlElement;
016: import javax.xml.bind.annotation.XmlRootElement;
017: import javax.xml.bind.annotation.XmlType;
018: import javax.xml.bind.annotation.XmlValue;
019: import javax.xml.namespace.QName;
020: import javax.xml.transform.Result;
021: import javax.xml.transform.Source;
022: import javax.xml.ws.EndpointReference;
023: import javax.xml.ws.WebServiceException;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: * This class represents a W3C Addressing EndpointReferece which is
029: * a remote reference to a web service endpoint that supports the
030: * W3C WS-Addressing 1.0 - Core Recommendation.
031: * <p>
032: * Developers should use this class in their SEIs if they want to
033: * pass/return endpoint references that represent the W3C WS-Addressing
034: * recommendation.
035: * <p>
036: * JAXB will use the JAXB annotations and bind this class to XML infoset
037: * that is consistent with that defined by WS-Addressing. See
038: * <a href="http://www.w3.org/TR/2006/REC-ws-addr-core-20060509/">
039: * WS-Addressing</a>
040: * for more information on WS-Addressing EndpointReferences.
041: *
042: * @since JAX-WS 2.1
043: */
044:
045: // XmlRootElement allows this class to be marshalled on its own
046: @XmlRootElement(name="EndpointReference",namespace=W3CEndpointReference.NS)
047: @XmlType(name="EndpointReferenceType",namespace=W3CEndpointReference.NS)
048: public final class W3CEndpointReference extends EndpointReference {
049:
050: private final static JAXBContext w3cjc = getW3CJaxbContext();
051:
052: protected W3CEndpointReference() {
053: }
054:
055: /**
056: * Creates an EPR from infoset representation
057: *
058: * @param source A source object containing valid XmlInfoset
059: * instance consistent with the W3C WS-Addressing Core
060: * recommendation.
061: *
062: * @throws WebServiceException
063: * If the source does NOT contain a valid W3C WS-Addressing
064: * EndpointReference.
065: * @throws NullPointerException
066: * If the <code>null</code> <code>source</code> value is given
067: */
068: public W3CEndpointReference(Source source) {
069: try {
070: W3CEndpointReference epr = w3cjc.createUnmarshaller()
071: .unmarshal(source, W3CEndpointReference.class)
072: .getValue();
073: this .address = epr.address;
074: this .metadata = epr.metadata;
075: this .referenceParameters = epr.referenceParameters;
076: } catch (JAXBException e) {
077: throw new WebServiceException(
078: "Error unmarshalling W3CEndpointReference ", e);
079: } catch (ClassCastException e) {
080: throw new WebServiceException(
081: "Source did not contain W3CEndpointReference", e);
082: }
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: public void writeTo(Result result) {
089: try {
090: Marshaller marshaller = w3cjc.createMarshaller();
091: marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
092: marshaller.marshal(this , result);
093: } catch (JAXBException e) {
094: throw new WebServiceException(
095: "Error marshalling W3CEndpointReference. ", e);
096: }
097: }
098:
099: private static JAXBContext getW3CJaxbContext() {
100: try {
101: return JAXBContext.newInstance(W3CEndpointReference.class);
102: } catch (JAXBException e) {
103: throw new WebServiceException(
104: "Error creating JAXBContext for W3CEndpointReference. ",
105: e);
106: }
107: }
108:
109: // private but necessary properties for databinding
110: @XmlElement(name="Address",namespace=NS)
111: private Address address;
112: @XmlElement(name="ReferenceParameters",namespace=NS)
113: private Elements referenceParameters;
114: @XmlElement(name="Metadata",namespace=NS)
115: private Elements metadata;
116: @XmlAnyAttribute
117: Map<QName, String> attributes;
118: @XmlAnyElement
119: List<Element> elements;
120:
121: private static class Address {
122: protected Address() {
123: }
124:
125: @XmlValue
126: String uri;
127: @XmlAnyAttribute
128: Map<QName, String> attributes;
129: }
130:
131: private static class Elements {
132: protected Elements() {
133: }
134:
135: @XmlAnyElement
136: List<Element> elements;
137: @XmlAnyAttribute
138: Map<QName, String> attributes;
139: }
140:
141: protected static final String NS = "http://www.w3.org/2005/08/addressing";
142: }
|