001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.addressing;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.stream.buffer.XMLStreamBufferSource;
042: import com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator;
043: import com.sun.xml.ws.api.addressing.AddressingVersion;
044: import com.sun.xml.ws.developer.MemberSubmissionEndpointReference;
045: import com.sun.xml.ws.util.DOMUtil;
046: import com.sun.xml.ws.util.xml.XmlUtil;
047: import com.sun.xml.ws.wsdl.parser.WSDLConstants;
048: import com.sun.xml.ws.addressing.v200408.MemberSubmissionAddressingConstants;
049: import org.w3c.dom.*;
050:
051: import javax.xml.namespace.QName;
052: import javax.xml.stream.XMLStreamException;
053: import javax.xml.transform.dom.DOMResult;
054: import javax.xml.ws.EndpointReference;
055: import javax.xml.ws.WebServiceException;
056: import javax.xml.ws.wsaddressing.W3CEndpointReference;
057: import java.util.ArrayList;
058: import java.util.HashMap;
059: import java.util.Map;
060:
061: /**
062: * @author Rama Pulavarthi
063: */
064:
065: public class EndpointReferenceUtil {
066: /**
067: * Gives the EPR based on the clazz. It may need to perform tranformation from
068: * W3C EPR to MS EPR or vise-versa.
069: */
070: public static <T extends EndpointReference> T transform(
071: Class<T> clazz, @NotNull
072: EndpointReference epr) {
073: assert epr != null;
074: if (clazz.isAssignableFrom(W3CEndpointReference.class)) {
075: if (epr instanceof W3CEndpointReference) {
076: return (T) epr;
077: } else if (epr instanceof MemberSubmissionEndpointReference) {
078: return (T) toW3CEpr((MemberSubmissionEndpointReference) epr);
079: }
080: } else if (clazz
081: .isAssignableFrom(MemberSubmissionEndpointReference.class)) {
082: if (epr instanceof W3CEndpointReference) {
083: return (T) toMSEpr((W3CEndpointReference) epr);
084: } else if (epr instanceof MemberSubmissionEndpointReference) {
085: return (T) epr;
086: }
087: }
088:
089: //This must be an EPR that we dont know
090: throw new WebServiceException("Unknwon EndpointReference: "
091: + epr.getClass());
092: }
093:
094: //TODO: bit of redundency on writes of w3c epr, should modularize it
095: private static W3CEndpointReference toW3CEpr(
096: MemberSubmissionEndpointReference msEpr) {
097: StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
098: w3cMetadataWritten = false;
099: try {
100: writer.writeStartDocument();
101: writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
102: "EndpointReference", AddressingVersion.W3C.nsUri);
103: writer.writeNamespace(AddressingVersion.W3C.getPrefix(),
104: AddressingVersion.W3C.nsUri);
105: //write wsa:Address
106: writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
107: W3CAddressingConstants.WSA_ADDRESS_NAME,
108: AddressingVersion.W3C.nsUri);
109: writer.writeCharacters(msEpr.addr.uri);
110: writer.writeEndElement();
111: //TODO: write extension attributes on wsa:Address
112: if ((msEpr.referenceProperties != null && msEpr.referenceProperties.elements
113: .size() > 0)
114: || (msEpr.referenceParameters != null && msEpr.referenceParameters.elements
115: .size() > 0)) {
116:
117: writer.writeStartElement(AddressingVersion.W3C
118: .getPrefix(), "ReferenceParameters",
119: AddressingVersion.W3C.nsUri);
120:
121: //write ReferenceProperties
122: if (msEpr.referenceProperties != null) {
123: for (Element e : msEpr.referenceProperties.elements) {
124: DOMUtil.serializeNode(e, writer);
125: }
126: }
127: //write referenceParameters
128: if (msEpr.referenceParameters != null) {
129: for (Element e : msEpr.referenceParameters.elements) {
130: DOMUtil.serializeNode(e, writer);
131: }
132: }
133: writer.writeEndElement();
134: }
135: // Supress writing ServiceName and EndpointName in W3CEPR,
136: // Until the ns for those metadata elements is resolved.
137: /*
138: //Write Interface info
139: if (msEpr.portTypeName != null) {
140: writeW3CMetadata(writer);
141: writer.writeStartElement(AddressingVersion.W3C.getWsdlPrefix(),
142: W3CAddressingConstants.WSAW_INTERFACENAME_NAME,
143: AddressingVersion.W3C.wsdlNsUri);
144: writer.writeNamespace(AddressingVersion.W3C.getWsdlPrefix(),
145: AddressingVersion.W3C.wsdlNsUri);
146: String portTypePrefix = fixNull(msEpr.portTypeName.name.getPrefix());
147: writer.writeNamespace(portTypePrefix, msEpr.portTypeName.name.getNamespaceURI());
148: if (portTypePrefix.equals(""))
149: writer.writeCharacters(msEpr.portTypeName.name.getLocalPart());
150: else
151: writer.writeCharacters(portTypePrefix + ":" + msEpr.portTypeName.name.getLocalPart());
152: writer.writeEndElement();
153: }
154: if (msEpr.serviceName != null) {
155: writeW3CMetadata(writer);
156: //Write service and Port info
157: writer.writeStartElement(AddressingVersion.W3C.getWsdlPrefix(),
158: W3CAddressingConstants.WSAW_SERVICENAME_NAME,
159: AddressingVersion.W3C.wsdlNsUri);
160: writer.writeNamespace(AddressingVersion.W3C.getWsdlPrefix(),
161: AddressingVersion.W3C.wsdlNsUri);
162:
163: String servicePrefix = fixNull(msEpr.serviceName.name.getPrefix());
164: if (msEpr.serviceName.portName != null)
165: writer.writeAttribute(W3CAddressingConstants.WSAW_ENDPOINTNAME_NAME,
166: msEpr.serviceName.portName);
167:
168: writer.writeNamespace(servicePrefix, msEpr.serviceName.name.getNamespaceURI());
169: if (servicePrefix.length() > 0)
170: writer.writeCharacters(servicePrefix + ":" + msEpr.serviceName.name.getLocalPart());
171: else
172: writer.writeCharacters(msEpr.serviceName.name.getLocalPart());
173: writer.writeEndElement();
174: }
175: */
176: //TODO: revisit this
177: Element wsdlElement = null;
178: //Check for wsdl in extension elements
179: if ((msEpr.elements != null) && (msEpr.elements.size() > 0)) {
180: for (Element e : msEpr.elements) {
181: if (e
182: .getNamespaceURI()
183: .equals(
184: MemberSubmissionAddressingConstants.MEX_METADATA
185: .getNamespaceURI())
186: && e
187: .getLocalName()
188: .equals(
189: MemberSubmissionAddressingConstants.MEX_METADATA
190: .getLocalPart())) {
191: NodeList nl = e.getElementsByTagNameNS(
192: WSDLConstants.NS_WSDL,
193: WSDLConstants.QNAME_DEFINITIONS
194: .getLocalPart());
195: if (nl != null)
196: wsdlElement = (Element) nl.item(0);
197: }
198: }
199: }
200: //write WSDL
201: if (wsdlElement != null) {
202: DOMUtil.serializeNode(wsdlElement, writer);
203: }
204:
205: if (w3cMetadataWritten)
206: writer.writeEndElement();
207: //TODO revisit this
208: //write extension elements
209: if ((msEpr.elements != null) && (msEpr.elements.size() > 0)) {
210: for (Element e : msEpr.elements) {
211: if (e.getNamespaceURI().equals(
212: WSDLConstants.NS_WSDL)
213: && e.getLocalName().equals(
214: WSDLConstants.QNAME_DEFINITIONS
215: .getLocalPart())) {
216: // Don't write it as this is written already in Metadata
217: }
218: DOMUtil.serializeNode(e, writer);
219: }
220: }
221:
222: //TODO:write extension attributes
223:
224: //</EndpointReference>
225: writer.writeEndElement();
226: writer.writeEndDocument();
227: writer.flush();
228: } catch (XMLStreamException e) {
229: throw new WebServiceException(e);
230: }
231: return new W3CEndpointReference(new XMLStreamBufferSource(
232: writer.getXMLStreamBuffer()));
233: }
234:
235: private static boolean w3cMetadataWritten = false;
236:
237: private static void writeW3CMetadata(
238: StreamWriterBufferCreator writer) throws XMLStreamException {
239: if (!w3cMetadataWritten) {
240: writer.writeStartElement(AddressingVersion.W3C.getPrefix(),
241: W3CAddressingConstants.WSA_METADATA_NAME,
242: AddressingVersion.W3C.nsUri);
243: w3cMetadataWritten = true;
244: }
245: }
246:
247: private static MemberSubmissionEndpointReference toMSEpr(
248: W3CEndpointReference w3cEpr) {
249: DOMResult result = new DOMResult();
250: w3cEpr.writeTo(result);
251: Node eprNode = result.getNode();
252: Element e = DOMUtil.getFirstElementChild(eprNode);
253: if (e == null)
254: return null;
255:
256: MemberSubmissionEndpointReference msEpr = new MemberSubmissionEndpointReference();
257:
258: NodeList nodes = e.getChildNodes();
259: for (int i = 0; i < nodes.getLength(); i++) {
260: if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
261: Element child = (Element) nodes.item(i);
262: if (child.getNamespaceURI().equals(
263: AddressingVersion.W3C.nsUri)
264: && child
265: .getLocalName()
266: .equals(
267: W3CAddressingConstants.WSA_ADDRESS_NAME)) {
268: if (msEpr.addr == null)
269: msEpr.addr = new MemberSubmissionEndpointReference.Address();
270: msEpr.addr.uri = XmlUtil.getTextForNode(child);
271:
272: //now add the attribute extensions
273: msEpr.addr.attributes = getAttributes(child);
274: } else if (child.getNamespaceURI().equals(
275: AddressingVersion.W3C.nsUri)
276: && child.getLocalName().equals(
277: "ReferenceParameters")) {
278: NodeList refParams = child.getChildNodes();
279: for (int j = 0; j < refParams.getLength(); j++) {
280: if (refParams.item(j).getNodeType() == Node.ELEMENT_NODE) {
281: if (msEpr.referenceParameters == null) {
282: msEpr.referenceParameters = new MemberSubmissionEndpointReference.Elements();
283: msEpr.referenceParameters.elements = new ArrayList<Element>();
284: }
285: msEpr.referenceParameters.elements
286: .add((Element) refParams.item(i));
287: }
288: }
289: } else if (child.getNamespaceURI().equals(
290: AddressingVersion.W3C.nsUri)
291: && child
292: .getLocalName()
293: .equals(
294: W3CAddressingConstants.WSA_METADATA_NAME)) {
295: NodeList metadata = child.getChildNodes();
296: for (int j = 0; j < metadata.getLength(); j++) {
297: Node node = metadata.item(j);
298: if (node.getNodeType() != Node.ELEMENT_NODE)
299: continue;
300:
301: Element elm = (Element) node;
302: if (elm.getNamespaceURI().equals(
303: AddressingVersion.W3C.wsdlNsUri)
304: && elm
305: .getLocalName()
306: .equals(
307: W3CAddressingConstants.WSAW_SERVICENAME_NAME)) {
308: msEpr.serviceName = new MemberSubmissionEndpointReference.ServiceNameType();
309: msEpr.serviceName.portName = elm
310: .getAttribute(W3CAddressingConstants.WSAW_ENDPOINTNAME_NAME);
311:
312: String service = elm.getTextContent();
313: String prefix = XmlUtil.getPrefix(service);
314: String name = XmlUtil.getLocalPart(service);
315:
316: //if there is no service name then its not a valid EPR but lets continue as its optional anyway
317: if (name == null)
318: continue;
319:
320: if (prefix != null) {
321: String ns = elm
322: .lookupNamespaceURI(prefix);
323: if (ns != null)
324: msEpr.serviceName.name = new QName(
325: ns, name, prefix);
326: } else {
327: msEpr.serviceName.name = new QName(
328: null, name);
329: }
330: msEpr.serviceName.attributes = getAttributes(elm);
331: } else if (elm.getNamespaceURI().equals(
332: AddressingVersion.W3C.wsdlNsUri)
333: && elm
334: .getLocalName()
335: .equals(
336: W3CAddressingConstants.WSAW_INTERFACENAME_NAME)) {
337: msEpr.portTypeName = new MemberSubmissionEndpointReference.AttributedQName();
338:
339: String portType = elm.getTextContent();
340: String prefix = XmlUtil.getPrefix(portType);
341: String name = XmlUtil
342: .getLocalPart(portType);
343:
344: //if there is no portType name then its not a valid EPR but lets continue as its optional anyway
345: if (name == null)
346: continue;
347:
348: if (prefix != null) {
349: String ns = elm
350: .lookupNamespaceURI(prefix);
351: if (ns != null)
352: msEpr.portTypeName.name = new QName(
353: ns, name, prefix);
354: } else {
355: msEpr.portTypeName.name = new QName(
356: null, name);
357: }
358: msEpr.portTypeName.attributes = getAttributes(elm);
359: } else if (elm.getNamespaceURI().equals(
360: WSDLConstants.NS_WSDL)
361: && elm.getLocalName().equals(
362: WSDLConstants.QNAME_DEFINITIONS
363: .getLocalPart())) {
364: Document doc = DOMUtil.createDom();
365: Element mexEl = doc
366: .createElementNS(
367: MemberSubmissionAddressingConstants.MEX_METADATA
368: .getNamespaceURI(),
369: MemberSubmissionAddressingConstants.MEX_METADATA
370: .getPrefix()
371: + ":"
372: + MemberSubmissionAddressingConstants.MEX_METADATA
373: .getLocalPart());
374: Element metadataEl = doc
375: .createElementNS(
376: MemberSubmissionAddressingConstants.MEX_METADATA_SECTION
377: .getNamespaceURI(),
378: MemberSubmissionAddressingConstants.MEX_METADATA_SECTION
379: .getPrefix()
380: + ":"
381: + MemberSubmissionAddressingConstants.MEX_METADATA_SECTION
382: .getLocalPart());
383: metadataEl
384: .setAttribute(
385: MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_ATTRIBUTE,
386: MemberSubmissionAddressingConstants.MEX_METADATA_DIALECT_VALUE);
387: metadataEl.appendChild(elm);
388: mexEl.appendChild(metadataEl);
389:
390: } else {
391: //TODO : Revisit this
392: //its extensions in META-DATA and should be copied to extensions in MS EPR
393: if (msEpr.elements == null) {
394: msEpr.elements = new ArrayList<Element>();
395: }
396: msEpr.elements.add(elm);
397: }
398: }
399: } else {
400: //its extensions
401: if (msEpr.elements == null) {
402: msEpr.elements = new ArrayList<Element>();
403: }
404: msEpr.elements.add((Element) child);
405:
406: }
407: } else if (nodes.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
408: Node n = nodes.item(i);
409: if (msEpr.attributes == null) {
410: msEpr.attributes = new HashMap<QName, String>();
411: String prefix = fixNull(n.getPrefix());
412: String ns = fixNull(n.getNamespaceURI());
413: String localName = n.getLocalName();
414: msEpr.attributes.put(new QName(ns, localName,
415: prefix), n.getNodeValue());
416: }
417: }
418: }
419:
420: return msEpr;
421: }
422:
423: private static Map<QName, String> getAttributes(Node node) {
424: Map<QName, String> attribs = null;
425:
426: NamedNodeMap nm = node.getAttributes();
427: for (int i = 0; i < nm.getLength(); i++) {
428: if (attribs == null)
429: attribs = new HashMap<QName, String>();
430: Node n = nm.item(i);
431: String prefix = fixNull(n.getPrefix());
432: String ns = fixNull(n.getNamespaceURI());
433: String localName = n.getLocalName();
434: if (prefix.equals("xmlns") || prefix.length() == 0
435: && localName.equals("xmlns"))
436: continue;
437:
438: //exclude some attributes
439: if (!localName
440: .equals(W3CAddressingConstants.WSAW_ENDPOINTNAME_NAME))
441: attribs.put(new QName(ns, localName, prefix), n
442: .getNodeValue());
443: }
444: return attribs;
445: }
446:
447: private static @NotNull
448: String fixNull(@Nullable
449: String s) {
450: if (s == null)
451: return "";
452: else
453: return s;
454: }
455:
456: }
|