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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,238 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, 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 org.w3c.dom.Node; 1.41 +import org.xml.sax.ContentHandler; 1.42 +import org.xml.sax.ErrorHandler; 1.43 +import org.xml.sax.SAXException; 1.44 + 1.45 +import javax.xml.soap.SOAPException; 1.46 +import javax.xml.soap.SOAPHeader; 1.47 +import javax.xml.soap.SOAPMessage; 1.48 +import javax.xml.stream.XMLStreamException; 1.49 +import javax.xml.stream.XMLStreamReader; 1.50 +import javax.xml.stream.XMLStreamWriter; 1.51 +import javax.xml.transform.Transformer; 1.52 +import javax.xml.transform.TransformerFactory; 1.53 +import javax.xml.transform.dom.DOMResult; 1.54 +import java.util.List; 1.55 +import java.util.Set; 1.56 + 1.57 +/** 1.58 + * {@link Header} whose physical data representation is an XMLStreamBuffer. 1.59 + * 1.60 + * @author Paul.Sandoz@Sun.Com 1.61 + */ 1.62 +public abstract class StreamHeader extends AbstractHeaderImpl { 1.63 + protected final XMLStreamBuffer _mark; 1.64 + 1.65 + protected boolean _isMustUnderstand; 1.66 + 1.67 + /** 1.68 + * Role or actor value. 1.69 + */ 1.70 + protected @NotNull String _role; 1.71 + 1.72 + protected boolean _isRelay; 1.73 + 1.74 + protected String _localName; 1.75 + 1.76 + protected String _namespaceURI; 1.77 + 1.78 + /** 1.79 + * Keep the information about an attribute on the header element. 1.80 + * 1.81 + * TODO: this whole attribute handling could be done better, I think. 1.82 + */ 1.83 + protected static final class Attribute { 1.84 + /** 1.85 + * Can be empty but never null. 1.86 + */ 1.87 + final String nsUri; 1.88 + final String localName; 1.89 + final String value; 1.90 + 1.91 + public Attribute(String nsUri, String localName, String value) { 1.92 + this.nsUri = fixNull(nsUri); 1.93 + this.localName = localName; 1.94 + this.value = value; 1.95 + } 1.96 + } 1.97 + 1.98 + /** 1.99 + * The attributes on the header element. 1.100 + * We expect there to be only a small number of them, 1.101 + * so the use of {@link List} would be justified. 1.102 + * 1.103 + * Null if no attribute is present. 1.104 + */ 1.105 + private final FinalArrayList<Attribute> attributes; 1.106 + 1.107 + /** 1.108 + * Creates a {@link StreamHeader}. 1.109 + * 1.110 + * @param reader 1.111 + * The parser pointing at the start of the mark. 1.112 + * Technically this information is redundant, 1.113 + * but it achieves a better performance. 1.114 + * @param mark 1.115 + * The start of the buffered header content. 1.116 + */ 1.117 + protected StreamHeader(XMLStreamReader reader, XMLStreamBuffer mark) { 1.118 + assert reader!=null && mark!=null; 1.119 + _mark = mark; 1.120 + _localName = reader.getLocalName(); 1.121 + _namespaceURI = reader.getNamespaceURI(); 1.122 + attributes = processHeaderAttributes(reader); 1.123 + } 1.124 + 1.125 + /** 1.126 + * Creates a {@link StreamHeader}. 1.127 + * 1.128 + * @param reader 1.129 + * The parser that points to the start tag of the header. 1.130 + * By the end of this method, the parser will point at 1.131 + * the end tag of this element. 1.132 + */ 1.133 + protected StreamHeader(XMLStreamReader reader) throws XMLStreamException { 1.134 + _localName = reader.getLocalName(); 1.135 + _namespaceURI = reader.getNamespaceURI(); 1.136 + attributes = processHeaderAttributes(reader); 1.137 + // cache the body 1.138 + _mark = XMLStreamBuffer.createNewBufferFromXMLStreamReader(reader); 1.139 + } 1.140 + 1.141 + public final boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles) { 1.142 + // check mustUnderstand 1.143 + if(!_isMustUnderstand) return true; 1.144 + 1.145 + if (roles == null) 1.146 + return true; 1.147 + 1.148 + // now role 1.149 + return !roles.contains(_role); 1.150 + } 1.151 + 1.152 + public @NotNull String getRole(@NotNull SOAPVersion soapVersion) { 1.153 + assert _role!=null; 1.154 + return _role; 1.155 + } 1.156 + 1.157 + public boolean isRelay() { 1.158 + return _isRelay; 1.159 + } 1.160 + 1.161 + public @NotNull String getNamespaceURI() { 1.162 + return _namespaceURI; 1.163 + } 1.164 + 1.165 + public @NotNull String getLocalPart() { 1.166 + return _localName; 1.167 + } 1.168 + 1.169 + public String getAttribute(String nsUri, String localName) { 1.170 + if(attributes!=null) { 1.171 + for(int i=attributes.size()-1; i>=0; i-- ) { 1.172 + Attribute a = attributes.get(i); 1.173 + if(a.localName.equals(localName) && a.nsUri.equals(nsUri)) 1.174 + return a.value; 1.175 + } 1.176 + } 1.177 + return null; 1.178 + } 1.179 + 1.180 + /** 1.181 + * Reads the header as a {@link XMLStreamReader} 1.182 + */ 1.183 + public XMLStreamReader readHeader() throws XMLStreamException { 1.184 + return _mark.readAsXMLStreamReader(); 1.185 + } 1.186 + 1.187 + public void writeTo(XMLStreamWriter w) throws XMLStreamException { 1.188 + if(_mark.getInscopeNamespaces().size() > 0) 1.189 + _mark.writeToXMLStreamWriter(w,true); 1.190 + else 1.191 + _mark.writeToXMLStreamWriter(w); 1.192 + } 1.193 + 1.194 + public void writeTo(SOAPMessage saaj) throws SOAPException { 1.195 + try { 1.196 + // TODO what about in-scope namespaces 1.197 + // Not very efficient consider implementing a stream buffer 1.198 + // processor that produces a DOM node from the buffer. 1.199 + TransformerFactory tf = TransformerFactory.newInstance(); 1.200 + Transformer t = tf.newTransformer(); 1.201 + XMLStreamBufferSource source = new XMLStreamBufferSource(_mark); 1.202 + DOMResult result = new DOMResult(); 1.203 + t.transform(source, result); 1.204 + Node d = result.getNode(); 1.205 + if(d.getNodeType() == Node.DOCUMENT_NODE) 1.206 + d = d.getFirstChild(); 1.207 + SOAPHeader header = saaj.getSOAPHeader(); 1.208 + if(header == null) 1.209 + header = saaj.getSOAPPart().getEnvelope().addHeader(); 1.210 + Node node = header.getOwnerDocument().importNode(d, true); 1.211 + header.appendChild(node); 1.212 + } catch (Exception e) { 1.213 + throw new SOAPException(e); 1.214 + } 1.215 + } 1.216 + 1.217 + public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { 1.218 + _mark.writeTo(contentHandler); 1.219 + } 1.220 + 1.221 + /** 1.222 + * Creates an EPR without copying infoset. 1.223 + * 1.224 + * This is the most common implementation on which {@link Header#readAsEPR(AddressingVersion)} 1.225 + * is invoked on. 1.226 + */ 1.227 + @Override @NotNull 1.228 + public WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException { 1.229 + return new WSEndpointReference(_mark,expected); 1.230 + } 1.231 + 1.232 + protected abstract FinalArrayList<Attribute> processHeaderAttributes(XMLStreamReader reader); 1.233 + 1.234 + /** 1.235 + * Convert null to "". 1.236 + */ 1.237 + private static String fixNull(String s) { 1.238 + if(s==null) return ""; 1.239 + else return s; 1.240 + } 1.241 +}