KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > ws > wsaddressing > W3CEndpointReference


1 /*
2  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.ws.wsaddressing;
7
8
9 import org.w3c.dom.Element JavaDoc;
10
11 import javax.xml.bind.JAXBContext;
12 import javax.xml.bind.JAXBException;
13 import javax.xml.bind.Marshaller;
14 import javax.xml.bind.annotation.XmlAnyAttribute;
15 import javax.xml.bind.annotation.XmlAnyElement;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
18 import javax.xml.bind.annotation.XmlType;
19 import javax.xml.bind.annotation.XmlValue;
20 import javax.xml.namespace.QName JavaDoc;
21 import javax.xml.transform.Result JavaDoc;
22 import javax.xml.transform.Source JavaDoc;
23 import javax.xml.ws.EndpointReference;
24 import javax.xml.ws.WebServiceException;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28
29 /**
30  * This class represents a W3C Addressing EndpointReferece which is
31  * a remote reference to a web service endpoint that supports the
32  * W3C WS-Addressing 1.0 - Core Recommendation.
33  * <p>
34  * Developers should use this class in their SEIs if they want to
35  * pass/return endpoint references that represent the W3C WS-Addressing
36  * recommendation.
37  * <p>
38  * JAXB will use the JAXB annotations and bind this class to XML infoset
39  * that is consistent with that defined by WS-Addressing. See
40  * <a HREF="http://www.w3.org/TR/2006/REC-ws-addr-core-20060509/">
41  * WS-Addressing</a>
42  * for more information on WS-Addressing EndpointReferences.
43  *
44  * @since JAX-WS 2.1
45  */

46
47 // XmlRootElement allows this class to be marshalled on its own
48
@XmlRootElement(name="EndpointReference",namespace=W3CEndpointReference.NS)
49 @XmlType(name="EndpointReferenceType",namespace=W3CEndpointReference.NS)
50 public final class W3CEndpointReference extends EndpointReference {
51     
52     private final static JAXBContext w3cjc = getW3CJaxbContext();
53
54     protected W3CEndpointReference() {
55     }
56     
57     /**
58      * Creates an EPR from infoset representation
59      *
60      * @param source A source object containing valid XmlInfoset
61      * instance consistent with the W3C WS-Addressing Core
62      * recommendation.
63      *
64      * @throws WebServiceException
65      * If the source does NOT contain a valid W3C WS-Addressing
66      * EndpointReference.
67      * @throws NullPointerException
68      * If the <code>null</code> <code>source</code> value is given
69      */

70     public W3CEndpointReference(Source JavaDoc source) {
71         try {
72             W3CEndpointReference epr = w3cjc.createUnmarshaller().unmarshal(source,W3CEndpointReference.class).getValue();
73             this.address = epr.address;
74             this.metadata = epr.metadata;
75             this.referenceParameters = epr.referenceParameters;
76         } catch (JAXBException e) {
77             throw new WebServiceException("Error unmarshalling W3CEndpointReference " ,e);
78         } catch (ClassCastException JavaDoc e) {
79             throw new WebServiceException("Source did not contain W3CEndpointReference", e);
80         }
81     }
82     
83     /**
84      * {@inheritDoc}
85      */

86     public void writeTo(Result JavaDoc result){
87         try {
88             Marshaller marshaller = w3cjc.createMarshaller();
89             marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
90             marshaller.marshal(this, result);
91         } catch (JAXBException e) {
92             throw new WebServiceException("Error marshalling W3CEndpointReference. ", e);
93         }
94     }
95     
96     private static JAXBContext getW3CJaxbContext() {
97         try {
98             return JAXBContext.newInstance(W3CEndpointReference.class);
99         } catch (JAXBException e) {
100             throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
101         }
102     }
103     
104     // private but necessary properties for databinding
105
@XmlElement(name="Address",namespace=NS)
106     private Address address;
107     @XmlElement(name="ReferenceParameters",namespace=NS)
108     private Elements referenceParameters;
109     @XmlElement(name="Metadata",namespace=NS)
110     private Elements metadata;
111     @XmlAnyAttribute
112     Map JavaDoc<QName JavaDoc,String JavaDoc> attributes;
113     @XmlAnyElement
114     List JavaDoc<Element JavaDoc> elements;
115     
116     
117     private static class Address {
118         protected Address() {}
119         @XmlValue
120         String JavaDoc uri;
121         @XmlAnyAttribute
122         Map JavaDoc<QName JavaDoc,String JavaDoc> attributes;
123     }
124     
125     
126     private static class Elements {
127         protected Elements() {}
128         @XmlAnyElement
129         List JavaDoc<Element JavaDoc> elements;
130         @XmlAnyAttribute
131         Map JavaDoc<QName JavaDoc,String JavaDoc> attributes;
132     }
133     
134     protected static final String JavaDoc NS = "http://www.w3.org/2005/08/addressing";
135 }
136
Popular Tags