1/*
2 * Copyright (c) 1997, 2014, 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
26package com.sun.xml.internal.ws.developer;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants;
30import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
31import org.w3c.dom.Element;
32
33import javax.xml.bind.JAXBContext;
34import javax.xml.bind.JAXBException;
35import javax.xml.bind.Marshaller;
36import javax.xml.bind.Unmarshaller;
37import javax.xml.bind.annotation.XmlAnyAttribute;
38import javax.xml.bind.annotation.XmlAnyElement;
39import javax.xml.bind.annotation.XmlAttribute;
40import javax.xml.bind.annotation.XmlElement;
41import javax.xml.bind.annotation.XmlRootElement;
42import javax.xml.bind.annotation.XmlType;
43import javax.xml.bind.annotation.XmlValue;
44import javax.xml.namespace.QName;
45import javax.xml.transform.Result;
46import javax.xml.transform.Source;
47import javax.xml.transform.dom.DOMSource;
48import javax.xml.ws.EndpointReference;
49import javax.xml.ws.WebServiceException;
50import java.util.List;
51import java.util.Map;
52
53/**
54 * Data model for Member Submission WS-Addressing specification. This is modeled after the
55 * member submission schema at:
56 *
57 *  http://schemas.xmlsoap.org/ws/2004/08/addressing/
58 *
59 * @author Kathy Walsh
60 * @author Vivek Pandey
61 */
62
63@XmlRootElement(name = "EndpointReference", namespace = MemberSubmissionEndpointReference.MSNS)
64@XmlType(name = "EndpointReferenceType", namespace = MemberSubmissionEndpointReference.MSNS)
65public final class MemberSubmissionEndpointReference extends EndpointReference implements MemberSubmissionAddressingConstants {
66
67    private final static ContextClassloaderLocal<JAXBContext> msjc = new ContextClassloaderLocal<JAXBContext>() {
68        @Override
69        protected JAXBContext initialValue() throws Exception {
70            return MemberSubmissionEndpointReference.getMSJaxbContext();
71        }
72    };
73
74    public MemberSubmissionEndpointReference() {
75    }
76
77    /**
78     * construct an EPR from infoset representation
79     *
80     * @param source A source object containing valid XmlInfoset
81     *               instance consistent with the Member Submission WS-Addressing
82     * @throws javax.xml.ws.WebServiceException
83     *                              if the source does not contain a valid W3C WS-Addressing
84     *                              EndpointReference.
85     * @throws WebServiceException if the <code>null</code> <code>source</code> value is given
86     */
87    public MemberSubmissionEndpointReference(@NotNull Source source) {
88
89        if (source == null) {
90            throw new WebServiceException("Source parameter can not be null on constructor");
91        }
92
93        try {
94            Unmarshaller unmarshaller = MemberSubmissionEndpointReference.msjc.get().createUnmarshaller();
95            MemberSubmissionEndpointReference epr = unmarshaller.unmarshal(source,MemberSubmissionEndpointReference.class).getValue();
96
97            this.addr = epr.addr;
98            this.referenceProperties = epr.referenceProperties;
99            this.referenceParameters = epr.referenceParameters;
100            this.portTypeName = epr.portTypeName;
101            this.serviceName = epr.serviceName;
102            this.attributes = epr.attributes;
103            this.elements = epr.elements;
104        } catch (JAXBException e) {
105            throw new WebServiceException("Error unmarshalling MemberSubmissionEndpointReference ", e);
106        } catch (ClassCastException e) {
107            throw new WebServiceException("Source did not contain MemberSubmissionEndpointReference", e);
108        }
109    }
110
111    @Override
112    public void writeTo(Result result) {
113        try {
114            Marshaller marshaller = MemberSubmissionEndpointReference.msjc.get().createMarshaller();
115            //marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
116            marshaller.marshal(this, result);
117        } catch (JAXBException e) {
118            throw new WebServiceException("Error marshalling W3CEndpointReference. ", e);
119        }
120    }
121
122    /**
123     * Constructs a Source containing the wsdl from the MemberSubmissionEndpointReference
124     *
125     * @return Source A source object containing the wsdl in the MemeberSubmissionEndpointReference, if present.
126     */
127    public Source toWSDLSource() {
128        Element wsdlElement = null;
129
130        for (Element elem : elements) {
131            if (elem.getNamespaceURI().equals(WSDLConstants.NS_WSDL) &&
132                    elem.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart())) {
133                wsdlElement = elem;
134            }
135        }
136
137        return new DOMSource(wsdlElement);
138    }
139
140
141    private static JAXBContext getMSJaxbContext() {
142        try {
143            return JAXBContext.newInstance(MemberSubmissionEndpointReference.class);
144        } catch (JAXBException e) {
145            throw new WebServiceException("Error creating JAXBContext for MemberSubmissionEndpointReference. ", e);
146        }
147    }
148
149    @XmlElement(name = "Address", namespace = MemberSubmissionEndpointReference.MSNS)
150    public Address addr;
151
152    @XmlElement(name = "ReferenceProperties", namespace = MemberSubmissionEndpointReference.MSNS)
153    public Elements referenceProperties;
154
155    @XmlElement(name = "ReferenceParameters", namespace = MemberSubmissionEndpointReference.MSNS)
156    public Elements referenceParameters;
157
158    @XmlElement(name = "PortType", namespace = MemberSubmissionEndpointReference.MSNS)
159    public AttributedQName portTypeName;
160
161    @XmlElement(name = "ServiceName", namespace = MemberSubmissionEndpointReference.MSNS)
162    public ServiceNameType serviceName;
163
164    @XmlAnyAttribute
165    public Map<QName,String> attributes;
166
167    @XmlAnyElement
168    public List<Element> elements;
169
170    @XmlType(name="address", namespace=MemberSubmissionEndpointReference.MSNS)
171    public static class Address {
172        public Address() {
173        }
174
175        @XmlValue
176        public String uri;
177        @XmlAnyAttribute
178        public Map<QName, String> attributes;
179    }
180
181    @XmlType(name="elements", namespace=MemberSubmissionEndpointReference.MSNS)
182    public static class Elements {
183        public Elements() {}
184
185        @XmlAnyElement
186        public List<Element> elements;
187    }
188
189
190    public static class AttributedQName {
191        public AttributedQName() {
192        }
193
194        @XmlValue
195        public QName name;
196        @XmlAnyAttribute
197        public Map<QName, String> attributes;
198    }
199
200    public static class ServiceNameType extends AttributedQName{
201        public ServiceNameType() {
202        }
203
204        @XmlAttribute(name="PortName")
205        public String portName;
206    }
207
208    protected static final String MSNS = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
209}
210