1 /*
   2  * Copyright (c) 2005, 2015, 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     protected static final String NS = "http://www.w3.org/2005/08/addressing";
  76 
  77     // default constructor forbidden ...
  78     // should be private, keeping original modifier to keep backwards compatibility
  79     protected W3CEndpointReference() {
  80     }
  81 
  82     /**
  83      * Creates an EPR from infoset representation
  84      *
  85      * @param source A source object containing valid XmlInfoset
  86      * instance consistent with the W3C WS-Addressing Core
  87      * recommendation.
  88      *
  89      * @throws WebServiceException
  90      *   If the source does NOT contain a valid W3C WS-Addressing
  91      *   EndpointReference.
  92      * @throws NullPointerException
  93      *   If the {@code null} {@code source} value is given
  94      */
  95     public W3CEndpointReference(Source source) {
  96         try {
  97             W3CEndpointReference epr = w3cjc.createUnmarshaller().unmarshal(source,W3CEndpointReference.class).getValue();
  98             this.address = epr.address;
  99             this.metadata = epr.metadata;
 100             this.referenceParameters = epr.referenceParameters;
 101             this.elements = epr.elements;
 102             this.attributes = epr.attributes;
 103         } catch (JAXBException e) {
 104             throw new WebServiceException("Error unmarshalling W3CEndpointReference " ,e);
 105         } catch (ClassCastException e) {
 106             throw new WebServiceException("Source did not contain W3CEndpointReference", e);
 107         }
 108     }
 109 
 110     /**
 111      * {@inheritDoc}
 112      */
 113     public void writeTo(Result result){
 114         try {
 115             Marshaller marshaller = w3cjc.createMarshaller();
 116             marshaller.marshal(this, result);
 117         } catch (JAXBException e) {
 118             throw new WebServiceException("Error marshalling W3CEndpointReference. ", e);
 119         }
 120     }
 121 
 122     private static JAXBContext getW3CJaxbContext() {
 123         try {
 124             return JAXBContext.newInstance(W3CEndpointReference.class);
 125         } catch (JAXBException e) {
 126             throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
 127         }
 128     }
 129 
 130     // private but necessary properties for databinding
 131     @XmlElement(name="Address",namespace=NS)
 132     private Address address;
 133     @XmlElement(name="ReferenceParameters",namespace=NS)
 134     private Elements referenceParameters;
 135     @XmlElement(name="Metadata",namespace=NS)
 136     private Elements metadata;
 137     // attributes and elements are not private for performance reasons
 138     // (JAXB can bypass reflection)
 139     @XmlAnyAttribute
 140     Map<QName,String> attributes;
 141     @XmlAnyElement
 142     List<Element> elements;
 143 
 144 
 145     @XmlType(name="address", namespace=W3CEndpointReference.NS)
 146     private static class Address {
 147         protected Address() {}
 148         @XmlValue
 149         String uri;
 150         @XmlAnyAttribute
 151         Map<QName,String> attributes;
 152     }
 153 
 154 
 155     @XmlType(name="elements", namespace=W3CEndpointReference.NS)
 156     private static class Elements {
 157         protected Elements() {}
 158         @XmlAnyElement
 159         List<Element> elements;
 160         @XmlAnyAttribute
 161         Map<QName,String> attributes;
 162     }
 163 
 164 }