src/share/jaxws_classes/com/sun/xml/internal/ws/message/stream/StreamHeader.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial