Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.message.stream; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.FinalArrayList; |
aoqi@0 | 29 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; |
aoqi@0 | 31 | import com.sun.xml.internal.stream.buffer.XMLStreamBufferSource; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.addressing.AddressingVersion; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.message.Header; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.message.AbstractHeaderImpl; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 38 | import org.w3c.dom.Node; |
aoqi@0 | 39 | import org.xml.sax.ContentHandler; |
aoqi@0 | 40 | import org.xml.sax.ErrorHandler; |
aoqi@0 | 41 | import org.xml.sax.SAXException; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import javax.xml.soap.SOAPException; |
aoqi@0 | 44 | import javax.xml.soap.SOAPHeader; |
aoqi@0 | 45 | import javax.xml.soap.SOAPMessage; |
aoqi@0 | 46 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 47 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 48 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 49 | import javax.xml.transform.Transformer; |
aoqi@0 | 50 | import javax.xml.transform.TransformerFactory; |
aoqi@0 | 51 | import javax.xml.transform.dom.DOMResult; |
aoqi@0 | 52 | import java.util.List; |
aoqi@0 | 53 | import java.util.Set; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * {@link Header} whose physical data representation is an XMLStreamBuffer. |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * @author Paul.Sandoz@Sun.Com |
aoqi@0 | 59 | */ |
aoqi@0 | 60 | public abstract class StreamHeader extends AbstractHeaderImpl { |
aoqi@0 | 61 | protected final XMLStreamBuffer _mark; |
aoqi@0 | 62 | |
aoqi@0 | 63 | protected boolean _isMustUnderstand; |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * Role or actor value. |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | protected @NotNull String _role; |
aoqi@0 | 69 | |
aoqi@0 | 70 | protected boolean _isRelay; |
aoqi@0 | 71 | |
aoqi@0 | 72 | protected String _localName; |
aoqi@0 | 73 | |
aoqi@0 | 74 | protected String _namespaceURI; |
aoqi@0 | 75 | |
aoqi@0 | 76 | /** |
aoqi@0 | 77 | * Keep the information about an attribute on the header element. |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * TODO: this whole attribute handling could be done better, I think. |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | protected static final class Attribute { |
aoqi@0 | 82 | /** |
aoqi@0 | 83 | * Can be empty but never null. |
aoqi@0 | 84 | */ |
aoqi@0 | 85 | final String nsUri; |
aoqi@0 | 86 | final String localName; |
aoqi@0 | 87 | final String value; |
aoqi@0 | 88 | |
aoqi@0 | 89 | public Attribute(String nsUri, String localName, String value) { |
aoqi@0 | 90 | this.nsUri = fixNull(nsUri); |
aoqi@0 | 91 | this.localName = localName; |
aoqi@0 | 92 | this.value = value; |
aoqi@0 | 93 | } |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | /** |
aoqi@0 | 97 | * The attributes on the header element. |
aoqi@0 | 98 | * We expect there to be only a small number of them, |
aoqi@0 | 99 | * so the use of {@link List} would be justified. |
aoqi@0 | 100 | * |
aoqi@0 | 101 | * Null if no attribute is present. |
aoqi@0 | 102 | */ |
aoqi@0 | 103 | private final FinalArrayList<Attribute> attributes; |
aoqi@0 | 104 | |
aoqi@0 | 105 | /** |
aoqi@0 | 106 | * Creates a {@link StreamHeader}. |
aoqi@0 | 107 | * |
aoqi@0 | 108 | * @param reader |
aoqi@0 | 109 | * The parser pointing at the start of the mark. |
aoqi@0 | 110 | * Technically this information is redundant, |
aoqi@0 | 111 | * but it achieves a better performance. |
aoqi@0 | 112 | * @param mark |
aoqi@0 | 113 | * The start of the buffered header content. |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | protected StreamHeader(XMLStreamReader reader, XMLStreamBuffer mark) { |
aoqi@0 | 116 | assert reader!=null && mark!=null; |
aoqi@0 | 117 | _mark = mark; |
aoqi@0 | 118 | _localName = reader.getLocalName(); |
aoqi@0 | 119 | _namespaceURI = reader.getNamespaceURI(); |
aoqi@0 | 120 | attributes = processHeaderAttributes(reader); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Creates a {@link StreamHeader}. |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * @param reader |
aoqi@0 | 127 | * The parser that points to the start tag of the header. |
aoqi@0 | 128 | * By the end of this method, the parser will point at |
aoqi@0 | 129 | * the end tag of this element. |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | protected StreamHeader(XMLStreamReader reader) throws XMLStreamException { |
aoqi@0 | 132 | _localName = reader.getLocalName(); |
aoqi@0 | 133 | _namespaceURI = reader.getNamespaceURI(); |
aoqi@0 | 134 | attributes = processHeaderAttributes(reader); |
aoqi@0 | 135 | // cache the body |
aoqi@0 | 136 | _mark = XMLStreamBuffer.createNewBufferFromXMLStreamReader(reader); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | public final boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles) { |
aoqi@0 | 140 | // check mustUnderstand |
aoqi@0 | 141 | if(!_isMustUnderstand) return true; |
aoqi@0 | 142 | |
aoqi@0 | 143 | if (roles == null) |
aoqi@0 | 144 | return true; |
aoqi@0 | 145 | |
aoqi@0 | 146 | // now role |
aoqi@0 | 147 | return !roles.contains(_role); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | public @NotNull String getRole(@NotNull SOAPVersion soapVersion) { |
aoqi@0 | 151 | assert _role!=null; |
aoqi@0 | 152 | return _role; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public boolean isRelay() { |
aoqi@0 | 156 | return _isRelay; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | public @NotNull String getNamespaceURI() { |
aoqi@0 | 160 | return _namespaceURI; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | public @NotNull String getLocalPart() { |
aoqi@0 | 164 | return _localName; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | public String getAttribute(String nsUri, String localName) { |
aoqi@0 | 168 | if(attributes!=null) { |
aoqi@0 | 169 | for(int i=attributes.size()-1; i>=0; i-- ) { |
aoqi@0 | 170 | Attribute a = attributes.get(i); |
aoqi@0 | 171 | if(a.localName.equals(localName) && a.nsUri.equals(nsUri)) |
aoqi@0 | 172 | return a.value; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | } |
aoqi@0 | 175 | return null; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | /** |
aoqi@0 | 179 | * Reads the header as a {@link XMLStreamReader} |
aoqi@0 | 180 | */ |
aoqi@0 | 181 | public XMLStreamReader readHeader() throws XMLStreamException { |
aoqi@0 | 182 | return _mark.readAsXMLStreamReader(); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | public void writeTo(XMLStreamWriter w) throws XMLStreamException { |
aoqi@0 | 186 | if(_mark.getInscopeNamespaces().size() > 0) |
aoqi@0 | 187 | _mark.writeToXMLStreamWriter(w,true); |
aoqi@0 | 188 | else |
aoqi@0 | 189 | _mark.writeToXMLStreamWriter(w); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | public void writeTo(SOAPMessage saaj) throws SOAPException { |
aoqi@0 | 193 | try { |
aoqi@0 | 194 | // TODO what about in-scope namespaces |
aoqi@0 | 195 | // Not very efficient consider implementing a stream buffer |
aoqi@0 | 196 | // processor that produces a DOM node from the buffer. |
aoqi@0 | 197 | TransformerFactory tf = XmlUtil.newTransformerFactory(); |
aoqi@0 | 198 | Transformer t = tf.newTransformer(); |
aoqi@0 | 199 | XMLStreamBufferSource source = new XMLStreamBufferSource(_mark); |
aoqi@0 | 200 | DOMResult result = new DOMResult(); |
aoqi@0 | 201 | t.transform(source, result); |
aoqi@0 | 202 | Node d = result.getNode(); |
aoqi@0 | 203 | if(d.getNodeType() == Node.DOCUMENT_NODE) |
aoqi@0 | 204 | d = d.getFirstChild(); |
aoqi@0 | 205 | SOAPHeader header = saaj.getSOAPHeader(); |
aoqi@0 | 206 | if(header == null) |
aoqi@0 | 207 | header = saaj.getSOAPPart().getEnvelope().addHeader(); |
aoqi@0 | 208 | Node node = header.getOwnerDocument().importNode(d, true); |
aoqi@0 | 209 | header.appendChild(node); |
aoqi@0 | 210 | } catch (Exception e) { |
aoqi@0 | 211 | throw new SOAPException(e); |
aoqi@0 | 212 | } |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { |
aoqi@0 | 216 | _mark.writeTo(contentHandler); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | /** |
aoqi@0 | 220 | * Creates an EPR without copying infoset. |
aoqi@0 | 221 | * |
aoqi@0 | 222 | * This is the most common implementation on which {@link Header#readAsEPR(AddressingVersion)} |
aoqi@0 | 223 | * is invoked on. |
aoqi@0 | 224 | */ |
aoqi@0 | 225 | @Override @NotNull |
aoqi@0 | 226 | public WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException { |
aoqi@0 | 227 | return new WSEndpointReference(_mark,expected); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | |
aoqi@0 | 230 | protected abstract FinalArrayList<Attribute> processHeaderAttributes(XMLStreamReader reader); |
aoqi@0 | 231 | |
aoqi@0 | 232 | /** |
aoqi@0 | 233 | * Convert null to "". |
aoqi@0 | 234 | */ |
aoqi@0 | 235 | private static String fixNull(String s) { |
aoqi@0 | 236 | if(s==null) return ""; |
aoqi@0 | 237 | else return s; |
aoqi@0 | 238 | } |
aoqi@0 | 239 | } |