1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/message/stream/StreamHeader.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,239 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.message.stream; 1.30 + 1.31 +import com.sun.istack.internal.FinalArrayList; 1.32 +import com.sun.istack.internal.NotNull; 1.33 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; 1.34 +import com.sun.xml.internal.stream.buffer.XMLStreamBufferSource; 1.35 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.36 +import com.sun.xml.internal.ws.api.addressing.AddressingVersion; 1.37 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 1.38 +import com.sun.xml.internal.ws.api.message.Header; 1.39 +import com.sun.xml.internal.ws.message.AbstractHeaderImpl; 1.40 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.41 +import org.w3c.dom.Node; 1.42 +import org.xml.sax.ContentHandler; 1.43 +import org.xml.sax.ErrorHandler; 1.44 +import org.xml.sax.SAXException; 1.45 + 1.46 +import javax.xml.soap.SOAPException; 1.47 +import javax.xml.soap.SOAPHeader; 1.48 +import javax.xml.soap.SOAPMessage; 1.49 +import javax.xml.stream.XMLStreamException; 1.50 +import javax.xml.stream.XMLStreamReader; 1.51 +import javax.xml.stream.XMLStreamWriter; 1.52 +import javax.xml.transform.Transformer; 1.53 +import javax.xml.transform.TransformerFactory; 1.54 +import javax.xml.transform.dom.DOMResult; 1.55 +import java.util.List; 1.56 +import java.util.Set; 1.57 + 1.58 +/** 1.59 + * {@link Header} whose physical data representation is an XMLStreamBuffer. 1.60 + * 1.61 + * @author Paul.Sandoz@Sun.Com 1.62 + */ 1.63 +public abstract class StreamHeader extends AbstractHeaderImpl { 1.64 + protected final XMLStreamBuffer _mark; 1.65 + 1.66 + protected boolean _isMustUnderstand; 1.67 + 1.68 + /** 1.69 + * Role or actor value. 1.70 + */ 1.71 + protected @NotNull String _role; 1.72 + 1.73 + protected boolean _isRelay; 1.74 + 1.75 + protected String _localName; 1.76 + 1.77 + protected String _namespaceURI; 1.78 + 1.79 + /** 1.80 + * Keep the information about an attribute on the header element. 1.81 + * 1.82 + * TODO: this whole attribute handling could be done better, I think. 1.83 + */ 1.84 + protected static final class Attribute { 1.85 + /** 1.86 + * Can be empty but never null. 1.87 + */ 1.88 + final String nsUri; 1.89 + final String localName; 1.90 + final String value; 1.91 + 1.92 + public Attribute(String nsUri, String localName, String value) { 1.93 + this.nsUri = fixNull(nsUri); 1.94 + this.localName = localName; 1.95 + this.value = value; 1.96 + } 1.97 + } 1.98 + 1.99 + /** 1.100 + * The attributes on the header element. 1.101 + * We expect there to be only a small number of them, 1.102 + * so the use of {@link List} would be justified. 1.103 + * 1.104 + * Null if no attribute is present. 1.105 + */ 1.106 + private final FinalArrayList<Attribute> attributes; 1.107 + 1.108 + /** 1.109 + * Creates a {@link StreamHeader}. 1.110 + * 1.111 + * @param reader 1.112 + * The parser pointing at the start of the mark. 1.113 + * Technically this information is redundant, 1.114 + * but it achieves a better performance. 1.115 + * @param mark 1.116 + * The start of the buffered header content. 1.117 + */ 1.118 + protected StreamHeader(XMLStreamReader reader, XMLStreamBuffer mark) { 1.119 + assert reader!=null && mark!=null; 1.120 + _mark = mark; 1.121 + _localName = reader.getLocalName(); 1.122 + _namespaceURI = reader.getNamespaceURI(); 1.123 + attributes = processHeaderAttributes(reader); 1.124 + } 1.125 + 1.126 + /** 1.127 + * Creates a {@link StreamHeader}. 1.128 + * 1.129 + * @param reader 1.130 + * The parser that points to the start tag of the header. 1.131 + * By the end of this method, the parser will point at 1.132 + * the end tag of this element. 1.133 + */ 1.134 + protected StreamHeader(XMLStreamReader reader) throws XMLStreamException { 1.135 + _localName = reader.getLocalName(); 1.136 + _namespaceURI = reader.getNamespaceURI(); 1.137 + attributes = processHeaderAttributes(reader); 1.138 + // cache the body 1.139 + _mark = XMLStreamBuffer.createNewBufferFromXMLStreamReader(reader); 1.140 + } 1.141 + 1.142 + public final boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles) { 1.143 + // check mustUnderstand 1.144 + if(!_isMustUnderstand) return true; 1.145 + 1.146 + if (roles == null) 1.147 + return true; 1.148 + 1.149 + // now role 1.150 + return !roles.contains(_role); 1.151 + } 1.152 + 1.153 + public @NotNull String getRole(@NotNull SOAPVersion soapVersion) { 1.154 + assert _role!=null; 1.155 + return _role; 1.156 + } 1.157 + 1.158 + public boolean isRelay() { 1.159 + return _isRelay; 1.160 + } 1.161 + 1.162 + public @NotNull String getNamespaceURI() { 1.163 + return _namespaceURI; 1.164 + } 1.165 + 1.166 + public @NotNull String getLocalPart() { 1.167 + return _localName; 1.168 + } 1.169 + 1.170 + public String getAttribute(String nsUri, String localName) { 1.171 + if(attributes!=null) { 1.172 + for(int i=attributes.size()-1; i>=0; i-- ) { 1.173 + Attribute a = attributes.get(i); 1.174 + if(a.localName.equals(localName) && a.nsUri.equals(nsUri)) 1.175 + return a.value; 1.176 + } 1.177 + } 1.178 + return null; 1.179 + } 1.180 + 1.181 + /** 1.182 + * Reads the header as a {@link XMLStreamReader} 1.183 + */ 1.184 + public XMLStreamReader readHeader() throws XMLStreamException { 1.185 + return _mark.readAsXMLStreamReader(); 1.186 + } 1.187 + 1.188 + public void writeTo(XMLStreamWriter w) throws XMLStreamException { 1.189 + if(_mark.getInscopeNamespaces().size() > 0) 1.190 + _mark.writeToXMLStreamWriter(w,true); 1.191 + else 1.192 + _mark.writeToXMLStreamWriter(w); 1.193 + } 1.194 + 1.195 + public void writeTo(SOAPMessage saaj) throws SOAPException { 1.196 + try { 1.197 + // TODO what about in-scope namespaces 1.198 + // Not very efficient consider implementing a stream buffer 1.199 + // processor that produces a DOM node from the buffer. 1.200 + TransformerFactory tf = XmlUtil.newTransformerFactory(); 1.201 + Transformer t = tf.newTransformer(); 1.202 + XMLStreamBufferSource source = new XMLStreamBufferSource(_mark); 1.203 + DOMResult result = new DOMResult(); 1.204 + t.transform(source, result); 1.205 + Node d = result.getNode(); 1.206 + if(d.getNodeType() == Node.DOCUMENT_NODE) 1.207 + d = d.getFirstChild(); 1.208 + SOAPHeader header = saaj.getSOAPHeader(); 1.209 + if(header == null) 1.210 + header = saaj.getSOAPPart().getEnvelope().addHeader(); 1.211 + Node node = header.getOwnerDocument().importNode(d, true); 1.212 + header.appendChild(node); 1.213 + } catch (Exception e) { 1.214 + throw new SOAPException(e); 1.215 + } 1.216 + } 1.217 + 1.218 + public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { 1.219 + _mark.writeTo(contentHandler); 1.220 + } 1.221 + 1.222 + /** 1.223 + * Creates an EPR without copying infoset. 1.224 + * 1.225 + * This is the most common implementation on which {@link Header#readAsEPR(AddressingVersion)} 1.226 + * is invoked on. 1.227 + */ 1.228 + @Override @NotNull 1.229 + public WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException { 1.230 + return new WSEndpointReference(_mark,expected); 1.231 + } 1.232 + 1.233 + protected abstract FinalArrayList<Attribute> processHeaderAttributes(XMLStreamReader reader); 1.234 + 1.235 + /** 1.236 + * Convert null to "". 1.237 + */ 1.238 + private static String fixNull(String s) { 1.239 + if(s==null) return ""; 1.240 + else return s; 1.241 + } 1.242 +}