1 /*
   2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.ws.wsaddressing;
  27 
  28 
  29 import org.w3c.dom.Element;
  30 
  31 import javax.xml.bind.JAXBContext;
  32 import javax.xml.bind.JAXBException;
  33 import javax.xml.bind.Marshaller;
  34 import javax.xml.bind.annotation.XmlAnyAttribute;
  35 import javax.xml.bind.annotation.XmlAnyElement;
  36 import javax.xml.bind.annotation.XmlElement;
  37 import javax.xml.bind.annotation.XmlRootElement;
  38 import javax.xml.bind.annotation.XmlType;
  39 import javax.xml.bind.annotation.XmlValue;
  40 import javax.xml.namespace.QName;
  41 import javax.xml.transform.Result;
  42 import javax.xml.transform.Source;
  43 import javax.xml.ws.EndpointReference;
  44 import javax.xml.ws.WebServiceException;
  45 import java.util.List;
  46 import java.util.Map;
  47 
  48 
  49 /**
  50  * This class represents a W3C Addressing EndpointReferece which is
  51  * a remote reference to a web service endpoint that supports the
  52  * W3C WS-Addressing 1.0 - Core Recommendation.
  53  * <p>
  54  * Developers should use this class in their SEIs if they want to
  55  * pass/return endpoint references that represent the W3C WS-Addressing
  56  * recommendation.
  57  * <p>
  58  * JAXB will use the JAXB annotations and bind this class to XML infoset
  59  * that is consistent with that defined by WS-Addressing.  See
  60  * <a href="http://www.w3.org/TR/2006/REC-ws-addr-core-20060509/">
  61  * WS-Addressing</a>
  62  * for more information on WS-Addressing EndpointReferences.
  63  *
  64  * @since 1.6, JAX-WS 2.1
  65  */
  66 
  67 // XmlRootElement allows this class to be marshalled on its own
  68 @XmlRootElement(name="EndpointReference",namespace=W3CEndpointReference.NS)
  69 @XmlType(name="EndpointReferenceType",namespace=W3CEndpointReference.NS)
  70 public final class W3CEndpointReference extends EndpointReference {
  71 
  72     private final JAXBContext w3cjc = getW3CJaxbContext();
  73 
  74     // should be changed to package private, keeping original modifier to keep backwards compatibility
  75 
  76     /**
  77      * Addressing namespace.
  78      */
  79     protected static final String NS = "http://www.w3.org/2005/08/addressing";
  80 
  81     // default constructor forbidden ...
  82     // should be private, keeping original modifier to keep backwards compatibility
  83 
  84     /**
  85      * Default constructor.
  86      */
  87     protected W3CEndpointReference() {
  88     }
  89 
  90     /**
  91      * Creates an EPR from infoset representation
  92      *
  93      * @param source A source object containing valid XmlInfoset
  94      * instance consistent with the W3C WS-Addressing Core
  95      * recommendation.
  96      *
  97      * @throws WebServiceException
  98      *   If the source does NOT contain a valid W3C WS-Addressing
  99      *   EndpointReference.
 100      * @throws NullPointerException
 101      *   If the {@code null} {@code source} value is given
 102      */
 103     public W3CEndpointReference(Source source) {
 104         try {
 105             W3CEndpointReference epr = w3cjc.createUnmarshaller().unmarshal(source,W3CEndpointReference.class).getValue();
 106             this.address = epr.address;
 107             this.metadata = epr.metadata;
 108             this.referenceParameters = epr.referenceParameters;
 109             this.elements = epr.elements;
 110             this.attributes = epr.attributes;
 111         } catch (JAXBException e) {
 112             throw new WebServiceException("Error unmarshalling W3CEndpointReference " ,e);
 113         } catch (ClassCastException e) {
 114             throw new WebServiceException("Source did not contain W3CEndpointReference", e);
 115         }
 116     }
 117 
 118     /**
 119      * {@inheritDoc}
 120      */
 121     @Override
 122     public void writeTo(Result result){
 123         try {
 124             Marshaller marshaller = w3cjc.createMarshaller();
 125             marshaller.marshal(this, result);
 126         } catch (JAXBException e) {
 127             throw new WebServiceException("Error marshalling W3CEndpointReference. ", e);
 128         }
 129     }
 130 
 131     private static JAXBContext getW3CJaxbContext() {
 132         try {
 133             return JAXBContext.newInstance(W3CEndpointReference.class);
 134         } catch (JAXBException e) {
 135             throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
 136         }
 137     }
 138 
 139     // private but necessary properties for databinding
 140     @XmlElement(name="Address",namespace=NS)
 141     private Address address;
 142     @XmlElement(name="ReferenceParameters",namespace=NS)
 143     private Elements referenceParameters;
 144     @XmlElement(name="Metadata",namespace=NS)
 145     private Elements metadata;
 146     // attributes and elements are not private for performance reasons
 147     // (JAXB can bypass reflection)
 148     @XmlAnyAttribute
 149     Map<QName,String> attributes;
 150     @XmlAnyElement
 151     List<Element> elements;
 152 
 153 
 154     @XmlType(name="address", namespace=W3CEndpointReference.NS)
 155     private static class Address {
 156         protected Address() {}
 157         @XmlValue
 158         String uri;
 159         @XmlAnyAttribute
 160         Map<QName,String> attributes;
 161     }
 162 
 163 
 164     @XmlType(name="elements", namespace=W3CEndpointReference.NS)
 165     private static class Elements {
 166         protected Elements() {}
 167         @XmlAnyElement
 168         List<Element> elements;
 169         @XmlAnyAttribute
 170         Map<QName,String> attributes;
 171     }
 172 
 173 }