|
1 /* |
|
2 * Copyright (c) 1997, 2013, 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 com.sun.xml.internal.ws.api.addressing; |
|
27 |
|
28 import java.io.ByteArrayInputStream; |
|
29 import java.io.ByteArrayOutputStream; |
|
30 import java.io.Writer; |
|
31 |
|
32 import com.sun.istack.internal.NotNull; |
|
33 import com.sun.istack.internal.Nullable; |
|
34 import com.sun.xml.internal.ws.message.AbstractHeaderImpl; |
|
35 import com.sun.xml.internal.ws.util.xml.XmlUtil; |
|
36 |
|
37 import org.w3c.dom.Document; |
|
38 import org.w3c.dom.Node; |
|
39 import org.xml.sax.ContentHandler; |
|
40 import org.xml.sax.ErrorHandler; |
|
41 import org.xml.sax.SAXException; |
|
42 |
|
43 import javax.xml.namespace.QName; |
|
44 import javax.xml.parsers.DocumentBuilderFactory; |
|
45 import javax.xml.soap.SOAPException; |
|
46 import javax.xml.soap.SOAPMessage; |
|
47 import javax.xml.soap.SOAPHeader; |
|
48 import javax.xml.stream.XMLOutputFactory; |
|
49 import javax.xml.stream.XMLStreamConstants; |
|
50 import javax.xml.stream.XMLStreamException; |
|
51 import javax.xml.stream.XMLStreamReader; |
|
52 import javax.xml.stream.XMLStreamWriter; |
|
53 import javax.xml.transform.Transformer; |
|
54 import javax.xml.transform.dom.DOMResult; |
|
55 |
|
56 /** |
|
57 * Used to represent outbound endpoint reference header, |
|
58 * such as <ReplyTo> and <FaultTo>. |
|
59 * Used from {@link WSEndpointReference}. |
|
60 * |
|
61 * @author Kohsuke Kawaguchi |
|
62 * @see WSEndpointReference |
|
63 */ |
|
64 final class EPRHeader extends AbstractHeaderImpl { |
|
65 |
|
66 private final String nsUri,localName; |
|
67 private final WSEndpointReference epr; |
|
68 |
|
69 EPRHeader(QName tagName, WSEndpointReference epr) { |
|
70 this.nsUri = tagName.getNamespaceURI(); |
|
71 this.localName = tagName.getLocalPart(); |
|
72 this.epr = epr; |
|
73 } |
|
74 |
|
75 public @NotNull String getNamespaceURI() { |
|
76 return nsUri; |
|
77 } |
|
78 |
|
79 public @NotNull String getLocalPart() { |
|
80 return localName; |
|
81 } |
|
82 |
|
83 @Nullable |
|
84 public String getAttribute(@NotNull String nsUri, @NotNull String localName) { |
|
85 try { |
|
86 XMLStreamReader sr = epr.read("EndpointReference"/*doesn't matter*/); |
|
87 while(sr.getEventType()!= XMLStreamConstants.START_ELEMENT) |
|
88 sr.next(); |
|
89 |
|
90 return sr.getAttributeValue(nsUri,localName); |
|
91 } catch (XMLStreamException e) { |
|
92 // since we are reading from buffer, this can't happen. |
|
93 throw new AssertionError(e); |
|
94 } |
|
95 } |
|
96 |
|
97 public XMLStreamReader readHeader() throws XMLStreamException { |
|
98 return epr.read(localName); |
|
99 } |
|
100 |
|
101 public void writeTo(XMLStreamWriter w) throws XMLStreamException { |
|
102 epr.writeTo(localName, w); |
|
103 } |
|
104 |
|
105 public void writeTo(SOAPMessage saaj) throws SOAPException { |
|
106 try { |
|
107 // TODO what about in-scope namespaces |
|
108 // Not very efficient consider implementing a stream buffer |
|
109 // processor that produces a DOM node from the buffer. |
|
110 Transformer t = XmlUtil.newTransformer(); |
|
111 SOAPHeader header = saaj.getSOAPHeader(); |
|
112 if (header == null) |
|
113 header = saaj.getSOAPPart().getEnvelope().addHeader(); |
|
114 // TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be |
|
115 // uncommented and all lines below, except the catch block, can be removed. |
|
116 // t.transform(epr.asSource(localName), new DOMResult(header)); |
|
117 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
118 XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos); |
|
119 epr.writeTo(localName, w); |
|
120 w.flush(); |
|
121 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
|
122 DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); |
|
123 fac.setNamespaceAware(true); |
|
124 Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement(); |
|
125 Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true); |
|
126 header.appendChild(eprNodeToAdd); |
|
127 } catch (Exception e) { |
|
128 throw new SOAPException(e); |
|
129 } |
|
130 } |
|
131 |
|
132 public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { |
|
133 epr.writeTo(localName,contentHandler,errorHandler,true); |
|
134 } |
|
135 } |