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  */
25 
26 package com.sun.xml.internal.ws.message;
27 
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;
37 
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;
46 
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;
54 
55     private final String nsUri;
56     private final String localName;
57 
DOMHeader(N node)58     public DOMHeader(N node) {
59         assert node!=null;
60         this.node = node;
61 
62         this.nsUri = fixNull(node.getNamespaceURI());
63         this.localName = node.getLocalName();
64     }
65 
66 
getNamespaceURI()67     public String getNamespaceURI() {
68         return nsUri;
69     }
70 
getLocalPart()71     public String getLocalPart() {
72         return localName;
73     }
74 
readHeader()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     }
80 
readAsJAXB(Unmarshaller unmarshaller)81     public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
82         return (T) unmarshaller.unmarshal(node);
83     }
84     /** @deprecated */
readAsJAXB(Bridge<T> bridge)85     public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
86         return bridge.unmarshal(node);
87     }
88 
writeTo(XMLStreamWriter w)89     public void writeTo(XMLStreamWriter w) throws XMLStreamException {
90         DOMUtil.serializeNode(node, w);
91     }
92 
fixNull(String s)93     private static String fixNull(String s) {
94         if(s!=null)     return s;
95         else            return "";
96     }
97 
writeTo(ContentHandler contentHandler, ErrorHandler errorHandler)98     public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
99         DOMScanner ds = new DOMScanner();
100         ds.setContentHandler(contentHandler);
101         ds.scan(node);
102     }
103 
getAttribute(String nsUri, String localName)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     }
108 
writeTo(SOAPMessage saaj)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     }
116 
117     @Override
getStringContent()118     public String getStringContent() {
119         return node.getTextContent();
120     }
121 
getWrappedNode()122     public N getWrappedNode() {
123         return node;
124     }
125 
126 
127     @Override
hashCode()128     public int hashCode() {
129         return getWrappedNode().hashCode();
130     }
131 
132 
133     @Override
equals(Object obj)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     }
141 
142 
143 }
144