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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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;
ohair@286 37 import org.w3c.dom.Node;
ohair@286 38 import org.xml.sax.ContentHandler;
ohair@286 39 import org.xml.sax.ErrorHandler;
ohair@286 40 import org.xml.sax.SAXException;
ohair@286 41
ohair@286 42 import javax.xml.soap.SOAPException;
ohair@286 43 import javax.xml.soap.SOAPHeader;
ohair@286 44 import javax.xml.soap.SOAPMessage;
ohair@286 45 import javax.xml.stream.XMLStreamException;
ohair@286 46 import javax.xml.stream.XMLStreamReader;
ohair@286 47 import javax.xml.stream.XMLStreamWriter;
ohair@286 48 import javax.xml.transform.Transformer;
ohair@286 49 import javax.xml.transform.TransformerFactory;
ohair@286 50 import javax.xml.transform.dom.DOMResult;
ohair@286 51 import java.util.List;
ohair@286 52 import java.util.Set;
ohair@286 53
ohair@286 54 /**
ohair@286 55 * {@link Header} whose physical data representation is an XMLStreamBuffer.
ohair@286 56 *
ohair@286 57 * @author Paul.Sandoz@Sun.Com
ohair@286 58 */
ohair@286 59 public abstract class StreamHeader extends AbstractHeaderImpl {
ohair@286 60 protected final XMLStreamBuffer _mark;
ohair@286 61
ohair@286 62 protected boolean _isMustUnderstand;
ohair@286 63
ohair@286 64 /**
ohair@286 65 * Role or actor value.
ohair@286 66 */
ohair@286 67 protected @NotNull String _role;
ohair@286 68
ohair@286 69 protected boolean _isRelay;
ohair@286 70
ohair@286 71 protected String _localName;
ohair@286 72
ohair@286 73 protected String _namespaceURI;
ohair@286 74
ohair@286 75 /**
ohair@286 76 * Keep the information about an attribute on the header element.
ohair@286 77 *
ohair@286 78 * TODO: this whole attribute handling could be done better, I think.
ohair@286 79 */
ohair@286 80 protected static final class Attribute {
ohair@286 81 /**
ohair@286 82 * Can be empty but never null.
ohair@286 83 */
ohair@286 84 final String nsUri;
ohair@286 85 final String localName;
ohair@286 86 final String value;
ohair@286 87
ohair@286 88 public Attribute(String nsUri, String localName, String value) {
ohair@286 89 this.nsUri = fixNull(nsUri);
ohair@286 90 this.localName = localName;
ohair@286 91 this.value = value;
ohair@286 92 }
ohair@286 93 }
ohair@286 94
ohair@286 95 /**
ohair@286 96 * The attributes on the header element.
ohair@286 97 * We expect there to be only a small number of them,
ohair@286 98 * so the use of {@link List} would be justified.
ohair@286 99 *
ohair@286 100 * Null if no attribute is present.
ohair@286 101 */
ohair@286 102 private final FinalArrayList<Attribute> attributes;
ohair@286 103
ohair@286 104 /**
ohair@286 105 * Creates a {@link StreamHeader}.
ohair@286 106 *
ohair@286 107 * @param reader
ohair@286 108 * The parser pointing at the start of the mark.
ohair@286 109 * Technically this information is redundant,
ohair@286 110 * but it achieves a better performance.
ohair@286 111 * @param mark
ohair@286 112 * The start of the buffered header content.
ohair@286 113 */
ohair@286 114 protected StreamHeader(XMLStreamReader reader, XMLStreamBuffer mark) {
ohair@286 115 assert reader!=null && mark!=null;
ohair@286 116 _mark = mark;
ohair@286 117 _localName = reader.getLocalName();
ohair@286 118 _namespaceURI = reader.getNamespaceURI();
ohair@286 119 attributes = processHeaderAttributes(reader);
ohair@286 120 }
ohair@286 121
ohair@286 122 /**
ohair@286 123 * Creates a {@link StreamHeader}.
ohair@286 124 *
ohair@286 125 * @param reader
ohair@286 126 * The parser that points to the start tag of the header.
ohair@286 127 * By the end of this method, the parser will point at
ohair@286 128 * the end tag of this element.
ohair@286 129 */
ohair@286 130 protected StreamHeader(XMLStreamReader reader) throws XMLStreamException {
ohair@286 131 _localName = reader.getLocalName();
ohair@286 132 _namespaceURI = reader.getNamespaceURI();
ohair@286 133 attributes = processHeaderAttributes(reader);
ohair@286 134 // cache the body
ohair@286 135 _mark = XMLStreamBuffer.createNewBufferFromXMLStreamReader(reader);
ohair@286 136 }
ohair@286 137
ohair@286 138 public final boolean isIgnorable(@NotNull SOAPVersion soapVersion, @NotNull Set<String> roles) {
ohair@286 139 // check mustUnderstand
ohair@286 140 if(!_isMustUnderstand) return true;
ohair@286 141
ohair@286 142 if (roles == null)
ohair@286 143 return true;
ohair@286 144
ohair@286 145 // now role
ohair@286 146 return !roles.contains(_role);
ohair@286 147 }
ohair@286 148
ohair@286 149 public @NotNull String getRole(@NotNull SOAPVersion soapVersion) {
ohair@286 150 assert _role!=null;
ohair@286 151 return _role;
ohair@286 152 }
ohair@286 153
ohair@286 154 public boolean isRelay() {
ohair@286 155 return _isRelay;
ohair@286 156 }
ohair@286 157
ohair@286 158 public @NotNull String getNamespaceURI() {
ohair@286 159 return _namespaceURI;
ohair@286 160 }
ohair@286 161
ohair@286 162 public @NotNull String getLocalPart() {
ohair@286 163 return _localName;
ohair@286 164 }
ohair@286 165
ohair@286 166 public String getAttribute(String nsUri, String localName) {
ohair@286 167 if(attributes!=null) {
ohair@286 168 for(int i=attributes.size()-1; i>=0; i-- ) {
ohair@286 169 Attribute a = attributes.get(i);
ohair@286 170 if(a.localName.equals(localName) && a.nsUri.equals(nsUri))
ohair@286 171 return a.value;
ohair@286 172 }
ohair@286 173 }
ohair@286 174 return null;
ohair@286 175 }
ohair@286 176
ohair@286 177 /**
ohair@286 178 * Reads the header as a {@link XMLStreamReader}
ohair@286 179 */
ohair@286 180 public XMLStreamReader readHeader() throws XMLStreamException {
ohair@286 181 return _mark.readAsXMLStreamReader();
ohair@286 182 }
ohair@286 183
ohair@286 184 public void writeTo(XMLStreamWriter w) throws XMLStreamException {
ohair@286 185 if(_mark.getInscopeNamespaces().size() > 0)
ohair@286 186 _mark.writeToXMLStreamWriter(w,true);
ohair@286 187 else
ohair@286 188 _mark.writeToXMLStreamWriter(w);
ohair@286 189 }
ohair@286 190
ohair@286 191 public void writeTo(SOAPMessage saaj) throws SOAPException {
ohair@286 192 try {
ohair@286 193 // TODO what about in-scope namespaces
ohair@286 194 // Not very efficient consider implementing a stream buffer
ohair@286 195 // processor that produces a DOM node from the buffer.
ohair@286 196 TransformerFactory tf = TransformerFactory.newInstance();
ohair@286 197 Transformer t = tf.newTransformer();
ohair@286 198 XMLStreamBufferSource source = new XMLStreamBufferSource(_mark);
ohair@286 199 DOMResult result = new DOMResult();
ohair@286 200 t.transform(source, result);
ohair@286 201 Node d = result.getNode();
ohair@286 202 if(d.getNodeType() == Node.DOCUMENT_NODE)
ohair@286 203 d = d.getFirstChild();
ohair@286 204 SOAPHeader header = saaj.getSOAPHeader();
ohair@286 205 if(header == null)
ohair@286 206 header = saaj.getSOAPPart().getEnvelope().addHeader();
ohair@286 207 Node node = header.getOwnerDocument().importNode(d, true);
ohair@286 208 header.appendChild(node);
ohair@286 209 } catch (Exception e) {
ohair@286 210 throw new SOAPException(e);
ohair@286 211 }
ohair@286 212 }
ohair@286 213
ohair@286 214 public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
ohair@286 215 _mark.writeTo(contentHandler);
ohair@286 216 }
ohair@286 217
ohair@286 218 /**
ohair@286 219 * Creates an EPR without copying infoset.
ohair@286 220 *
ohair@286 221 * This is the most common implementation on which {@link Header#readAsEPR(AddressingVersion)}
ohair@286 222 * is invoked on.
ohair@286 223 */
ohair@286 224 @Override @NotNull
ohair@286 225 public WSEndpointReference readAsEPR(AddressingVersion expected) throws XMLStreamException {
ohair@286 226 return new WSEndpointReference(_mark,expected);
ohair@286 227 }
ohair@286 228
ohair@286 229 protected abstract FinalArrayList<Attribute> processHeaderAttributes(XMLStreamReader reader);
ohair@286 230
ohair@286 231 /**
ohair@286 232 * Convert null to "".
ohair@286 233 */
ohair@286 234 private static String fixNull(String s) {
ohair@286 235 if(s==null) return "";
ohair@286 236 else return s;
ohair@286 237 }
ohair@286 238 }

mercurial