Fri, 14 Feb 2014 11:13:45 +0100
8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
1 /*
2 * Copyright (c) 1997, 2012, 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 */
26 package com.sun.xml.internal.ws.message;
28 import com.sun.xml.internal.bind.api.Bridge;
29 import com.sun.xml.internal.bind.unmarshaller.DOMScanner;
30 import com.sun.xml.internal.ws.streaming.DOMStreamReader;
31 import com.sun.xml.internal.ws.util.DOMUtil;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.Node;
34 import org.xml.sax.ContentHandler;
35 import org.xml.sax.ErrorHandler;
36 import org.xml.sax.SAXException;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.soap.SOAPException;
41 import javax.xml.soap.SOAPHeader;
42 import javax.xml.soap.SOAPMessage;
43 import javax.xml.stream.XMLStreamException;
44 import javax.xml.stream.XMLStreamReader;
45 import javax.xml.stream.XMLStreamWriter;
47 /**
48 * {@link com.sun.xml.internal.ws.api.message.Header} implementation for a DOM.
49 *
50 * @author Kohsuke Kawaguchi
51 */
52 public class DOMHeader<N extends Element> extends AbstractHeaderImpl {
53 protected final N node;
55 private final String nsUri;
56 private final String localName;
58 public DOMHeader(N node) {
59 assert node!=null;
60 this.node = node;
62 this.nsUri = fixNull(node.getNamespaceURI());
63 this.localName = node.getLocalName();
64 }
67 public String getNamespaceURI() {
68 return nsUri;
69 }
71 public String getLocalPart() {
72 return localName;
73 }
75 public XMLStreamReader readHeader() throws XMLStreamException {
76 DOMStreamReader r = new DOMStreamReader(node);
77 r.nextTag(); // move ahead to the start tag
78 return r;
79 }
81 public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
82 return (T) unmarshaller.unmarshal(node);
83 }
84 /** @deprecated */
85 public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
86 return bridge.unmarshal(node);
87 }
89 public void writeTo(XMLStreamWriter w) throws XMLStreamException {
90 DOMUtil.serializeNode(node, w);
91 }
93 private static String fixNull(String s) {
94 if(s!=null) return s;
95 else return "";
96 }
98 public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
99 DOMScanner ds = new DOMScanner();
100 ds.setContentHandler(contentHandler);
101 ds.scan(node);
102 }
104 public String getAttribute(String nsUri, String localName) {
105 if(nsUri.length()==0) nsUri=null; // DOM wants null, not "".
106 return node.getAttributeNS(nsUri,localName);
107 }
109 public void writeTo(SOAPMessage saaj) throws SOAPException {
110 SOAPHeader header = saaj.getSOAPHeader();
111 if(header == null)
112 header = saaj.getSOAPPart().getEnvelope().addHeader();
113 Node clone = header.getOwnerDocument().importNode(node,true);
114 header.appendChild(clone);
115 }
117 @Override
118 public String getStringContent() {
119 return node.getTextContent();
120 }
122 public N getWrappedNode() {
123 return node;
124 }
127 @Override
128 public int hashCode() {
129 return getWrappedNode().hashCode();
130 }
133 @Override
134 public boolean equals(Object obj) {
135 if (obj instanceof DOMHeader) {
136 return getWrappedNode().equals(((DOMHeader) obj).getWrappedNode());
137 } else {
138 return false;
139 }
140 }
143 }