src/share/jaxws_classes/com/sun/xml/internal/ws/api/addressing/OutboundReferenceParameterHeader.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.api.addressing;
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.XMLStreamBufferException;
aoqi@0 32 import com.sun.xml.internal.ws.api.message.Header;
aoqi@0 33 import com.sun.xml.internal.ws.message.AbstractHeaderImpl;
aoqi@0 34 import com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter;
aoqi@0 35 import org.w3c.dom.Element;
aoqi@0 36 import org.xml.sax.Attributes;
aoqi@0 37 import org.xml.sax.ContentHandler;
aoqi@0 38 import org.xml.sax.ErrorHandler;
aoqi@0 39 import org.xml.sax.SAXException;
aoqi@0 40 import org.xml.sax.helpers.AttributesImpl;
aoqi@0 41 import org.xml.sax.helpers.XMLFilterImpl;
aoqi@0 42
aoqi@0 43 import javax.xml.namespace.QName;
aoqi@0 44 import javax.xml.soap.SOAPException;
aoqi@0 45 import javax.xml.soap.SOAPMessage;
aoqi@0 46 import javax.xml.soap.SOAPHeader;
aoqi@0 47 import javax.xml.stream.XMLStreamException;
aoqi@0 48 import javax.xml.stream.XMLStreamReader;
aoqi@0 49 import javax.xml.stream.XMLStreamWriter;
aoqi@0 50 import javax.xml.stream.util.StreamReaderDelegate;
aoqi@0 51 import javax.xml.ws.WebServiceException;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * Used to represent outbound header created from {@link WSEndpointReference}'s
aoqi@0 55 * referenec parameters.
aoqi@0 56 *
aoqi@0 57 * <p>
aoqi@0 58 * This is optimized for outbound use, so it implements some of the methods lazily,
aoqi@0 59 * in a slow way.
aoqi@0 60 *
aoqi@0 61 * <p>
aoqi@0 62 * This header adds "wsa:IsReferenceParameter" and thus only used for the W3C version.
aoqi@0 63 *
aoqi@0 64 * @author Kohsuke Kawaguchi
aoqi@0 65 */
aoqi@0 66 final class OutboundReferenceParameterHeader extends AbstractHeaderImpl {
aoqi@0 67 private final XMLStreamBuffer infoset;
aoqi@0 68 private final String nsUri,localName;
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * The attributes on the header element.
aoqi@0 72 * Lazily parsed.
aoqi@0 73 * Null if not parsed yet.
aoqi@0 74 */
aoqi@0 75 private FinalArrayList<Attribute> attributes;
aoqi@0 76
aoqi@0 77 OutboundReferenceParameterHeader(XMLStreamBuffer infoset, String nsUri, String localName) {
aoqi@0 78 this.infoset = infoset;
aoqi@0 79 this.nsUri = nsUri;
aoqi@0 80 this.localName = localName;
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 @Override
aoqi@0 84 public @NotNull String getNamespaceURI() {
aoqi@0 85 return nsUri;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 @Override
aoqi@0 89 public @NotNull String getLocalPart() {
aoqi@0 90 return localName;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 @Override
aoqi@0 94 public String getAttribute(String nsUri, String localName) {
aoqi@0 95 if(attributes==null) {
aoqi@0 96 parseAttributes();
aoqi@0 97 }
aoqi@0 98 for(int i=attributes.size()-1; i>=0; i-- ) {
aoqi@0 99 Attribute a = attributes.get(i);
aoqi@0 100 if (a.localName.equals(localName) && a.nsUri.equals(nsUri)) {
aoqi@0 101 return a.value;
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104 return null;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /**
aoqi@0 108 * We don't really expect this to be used, but just to satisfy
aoqi@0 109 * the {@link Header} contract.
aoqi@0 110 *
aoqi@0 111 * So this is rather slow.
aoqi@0 112 */
aoqi@0 113 private void parseAttributes() {
aoqi@0 114 try {
aoqi@0 115 XMLStreamReader reader = readHeader();
aoqi@0 116 reader.nextTag(); // move to the first element, which is the header element
aoqi@0 117
aoqi@0 118 attributes = new FinalArrayList<Attribute>();
aoqi@0 119 boolean refParamAttrWritten = false;
aoqi@0 120 for (int i = 0; i < reader.getAttributeCount(); i++) {
aoqi@0 121 final String attrLocalName = reader.getAttributeLocalName(i);
aoqi@0 122 final String namespaceURI = reader.getAttributeNamespace(i);
aoqi@0 123 final String value = reader.getAttributeValue(i);
aoqi@0 124 if (namespaceURI.equals(AddressingVersion.W3C.nsUri)&& attrLocalName.equals("IS_REFERENCE_PARAMETER")) {
aoqi@0 125 refParamAttrWritten = true;
aoqi@0 126 }
aoqi@0 127 attributes.add(new Attribute(namespaceURI,attrLocalName,value));
aoqi@0 128 }
aoqi@0 129 // we are adding one more attribute "wsa:IsReferenceParameter", if its not alrady there
aoqi@0 130 if (!refParamAttrWritten) {
aoqi@0 131 attributes.add(new Attribute(AddressingVersion.W3C.nsUri,IS_REFERENCE_PARAMETER,TRUE_VALUE));
aoqi@0 132 }
aoqi@0 133 } catch (XMLStreamException e) {
aoqi@0 134 throw new WebServiceException("Unable to read the attributes for {"+nsUri+"}"+localName+" header",e);
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 @Override
aoqi@0 139 public XMLStreamReader readHeader() throws XMLStreamException {
aoqi@0 140 return new StreamReaderDelegate(infoset.readAsXMLStreamReader()) {
aoqi@0 141 int state=0; /* 0:expecting root, 1:in root, 2:past root */
aoqi@0 142 @Override
aoqi@0 143 public int next() throws XMLStreamException {
aoqi@0 144 return check(super.next());
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 @Override
aoqi@0 148 public int nextTag() throws XMLStreamException {
aoqi@0 149 return check(super.nextTag());
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 private int check(int type) {
aoqi@0 153 switch (state) {
aoqi@0 154 case 0:
aoqi@0 155 if (type == START_ELEMENT) {
aoqi@0 156 state = 1;
aoqi@0 157 }
aoqi@0 158 break;
aoqi@0 159 case 1:
aoqi@0 160 state = 2;
aoqi@0 161 break;
aoqi@0 162 default:
aoqi@0 163 break;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 return type;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 @Override
aoqi@0 170 public int getAttributeCount() {
aoqi@0 171 if (state == 1) {
aoqi@0 172 return super.getAttributeCount()+1;
aoqi@0 173 } else {
aoqi@0 174 return super.getAttributeCount();
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 @Override
aoqi@0 179 public String getAttributeLocalName(int index) {
aoqi@0 180 if (state == 1 && index == super.getAttributeCount()) {
aoqi@0 181 return IS_REFERENCE_PARAMETER;
aoqi@0 182 } else {
aoqi@0 183 return super.getAttributeLocalName(index);
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 @Override
aoqi@0 188 public String getAttributeNamespace(int index) {
aoqi@0 189 if (state == 1 && index==super.getAttributeCount()) {
aoqi@0 190 return AddressingVersion.W3C.nsUri;
aoqi@0 191 }
aoqi@0 192 else {
aoqi@0 193 return super.getAttributeNamespace(index);
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 @Override
aoqi@0 198 public String getAttributePrefix(int index) {
aoqi@0 199 if(state==1 && index==super.getAttributeCount()) {
aoqi@0 200 return "wsa";
aoqi@0 201 } else {
aoqi@0 202 return super.getAttributePrefix(index);
aoqi@0 203 }
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 @Override
aoqi@0 207 public String getAttributeType(int index) {
aoqi@0 208 if(state==1 && index==super.getAttributeCount()) {
aoqi@0 209 return "CDATA";
aoqi@0 210 } else {
aoqi@0 211 return super.getAttributeType(index);
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 @Override
aoqi@0 216 public String getAttributeValue(int index) {
aoqi@0 217 if(state==1 && index==super.getAttributeCount()) {
aoqi@0 218 return TRUE_VALUE;
aoqi@0 219 } else {
aoqi@0 220 return super.getAttributeValue(index);
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 @Override
aoqi@0 225 public QName getAttributeName(int index) {
aoqi@0 226 if(state==1 && index==super.getAttributeCount()) {
aoqi@0 227 return new QName(AddressingVersion.W3C.nsUri, IS_REFERENCE_PARAMETER, "wsa");
aoqi@0 228 } else {
aoqi@0 229 return super.getAttributeName(index);
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 @Override
aoqi@0 234 public String getAttributeValue(String namespaceUri, String localName) {
aoqi@0 235 if(state==1 && localName.equals(IS_REFERENCE_PARAMETER) && namespaceUri.equals(AddressingVersion.W3C.nsUri)) {
aoqi@0 236 return TRUE_VALUE;
aoqi@0 237 } else {
aoqi@0 238 return super.getAttributeValue(namespaceUri, localName);
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241 };
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 @Override
aoqi@0 245 public void writeTo(XMLStreamWriter w) throws XMLStreamException {
aoqi@0 246 infoset.writeToXMLStreamWriter(new XMLStreamWriterFilter(w) {
aoqi@0 247 private boolean root=true;
aoqi@0 248 private boolean onRootEl = true;
aoqi@0 249 @Override
aoqi@0 250 public void writeStartElement(String localName) throws XMLStreamException {
aoqi@0 251 super.writeStartElement(localName);
aoqi@0 252 writeAddedAttribute();
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 private void writeAddedAttribute() throws XMLStreamException {
aoqi@0 256 if(!root) {
aoqi@0 257 onRootEl = false;
aoqi@0 258 return;
aoqi@0 259 }
aoqi@0 260 root=false;
aoqi@0 261 writeNamespace("wsa",AddressingVersion.W3C.nsUri);
aoqi@0 262 super.writeAttribute("wsa",AddressingVersion.W3C.nsUri,IS_REFERENCE_PARAMETER,TRUE_VALUE);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 @Override
aoqi@0 266 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
aoqi@0 267 super.writeStartElement(namespaceURI, localName);
aoqi@0 268 writeAddedAttribute();
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 @Override
aoqi@0 272 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
aoqi@0 273 //TODO: Verify with KK later
aoqi@0 274 //check if prefix is declared before writing start element.
aoqi@0 275 boolean prefixDeclared = isPrefixDeclared(prefix,namespaceURI);
aoqi@0 276 super.writeStartElement(prefix, localName, namespaceURI);
aoqi@0 277 if (!prefixDeclared && !prefix.equals("")) {
aoqi@0 278 super.writeNamespace(prefix,namespaceURI);
aoqi@0 279 }
aoqi@0 280 writeAddedAttribute();
aoqi@0 281 }
aoqi@0 282 @Override
aoqi@0 283 public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException{
aoqi@0 284 if (!isPrefixDeclared(prefix, namespaceURI)) {
aoqi@0 285 super.writeNamespace(prefix,namespaceURI);
aoqi@0 286 }
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 @Override
aoqi@0 290 public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
aoqi@0 291 //skip if its on root element and attribute is wsa;IsReferenceParameter, as we write it.
aoqi@0 292 if(onRootEl && namespaceURI.equals(AddressingVersion.W3C.nsUri)
aoqi@0 293 && localName.equals(IS_REFERENCE_PARAMETER)) {
aoqi@0 294 return;
aoqi@0 295 }
aoqi@0 296 writer.writeAttribute(prefix, namespaceURI, localName, value);
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 @Override
aoqi@0 300 public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
aoqi@0 301 writer.writeAttribute(namespaceURI, localName, value);
aoqi@0 302 }
aoqi@0 303 private boolean isPrefixDeclared(String prefix, String namespaceURI ) {
aoqi@0 304 return namespaceURI.equals(getNamespaceContext().getNamespaceURI(prefix));
aoqi@0 305 }
aoqi@0 306 },true);
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 @Override
aoqi@0 310 public void writeTo(SOAPMessage saaj) throws SOAPException {
aoqi@0 311 //TODO: SAAJ returns null instead of throwing SOAPException,
aoqi@0 312 // when there is no SOAPHeader in the message,
aoqi@0 313 // which leads to NPE.
aoqi@0 314 try {
aoqi@0 315 SOAPHeader header = saaj.getSOAPHeader();
aoqi@0 316 if (header == null) {
aoqi@0 317 header = saaj.getSOAPPart().getEnvelope().addHeader();
aoqi@0 318 }
aoqi@0 319 Element node = (Element)infoset.writeTo(header);
aoqi@0 320 node.setAttributeNS(AddressingVersion.W3C.nsUri,AddressingVersion.W3C.getPrefix()+":"+IS_REFERENCE_PARAMETER,TRUE_VALUE);
aoqi@0 321 } catch (XMLStreamBufferException e) {
aoqi@0 322 throw new SOAPException(e);
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 @Override
aoqi@0 327 public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
aoqi@0 328 class Filter extends XMLFilterImpl {
aoqi@0 329 Filter(ContentHandler ch) { setContentHandler(ch); }
aoqi@0 330 private int depth=0;
aoqi@0 331 @Override
aoqi@0 332 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
aoqi@0 333 if(depth++==0) {
aoqi@0 334 // add one more attribute
aoqi@0 335 super.startPrefixMapping("wsa",AddressingVersion.W3C.nsUri);
aoqi@0 336
aoqi@0 337 //Add the attirbute wsa:IsReferenceParameter if not present already
aoqi@0 338 if(atts.getIndex(AddressingVersion.W3C.nsUri,IS_REFERENCE_PARAMETER) == -1) {
aoqi@0 339 AttributesImpl atts2 = new AttributesImpl(atts);
aoqi@0 340 atts2.addAttribute(
aoqi@0 341 AddressingVersion.W3C.nsUri,
aoqi@0 342 IS_REFERENCE_PARAMETER,
aoqi@0 343 "wsa:IsReferenceParameter",
aoqi@0 344 "CDATA",
aoqi@0 345 TRUE_VALUE);
aoqi@0 346 atts = atts2;
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 super.startElement(uri, localName, qName, atts);
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 @Override
aoqi@0 354 public void endElement(String uri, String localName, String qName) throws SAXException {
aoqi@0 355 super.endElement(uri, localName, qName);
aoqi@0 356 if (--depth == 0) {
aoqi@0 357 super.endPrefixMapping("wsa");
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 infoset.writeTo(new Filter(contentHandler),errorHandler);
aoqi@0 363 }
aoqi@0 364
aoqi@0 365
aoqi@0 366 /**
aoqi@0 367 * Keep the information about an attribute on the header element.
aoqi@0 368 */
aoqi@0 369 static final class Attribute {
aoqi@0 370 /**
aoqi@0 371 * Can be empty but never null.
aoqi@0 372 */
aoqi@0 373 final String nsUri;
aoqi@0 374 final String localName;
aoqi@0 375 final String value;
aoqi@0 376
aoqi@0 377 public Attribute(String nsUri, String localName, String value) {
aoqi@0 378 this.nsUri = fixNull(nsUri);
aoqi@0 379 this.localName = localName;
aoqi@0 380 this.value = value;
aoqi@0 381 }
aoqi@0 382
aoqi@0 383 /**
aoqi@0 384 * Convert null to "".
aoqi@0 385 */
aoqi@0 386 private static String fixNull(String s) {
aoqi@0 387 return s == null ? "" : s;
aoqi@0 388 }
aoqi@0 389 }
aoqi@0 390
aoqi@0 391 /**
aoqi@0 392 * We the performance paranoid people in the JAX-WS RI thinks
aoqi@0 393 * saving three bytes is worth while...
aoqi@0 394 */
aoqi@0 395 private static final String TRUE_VALUE = "1";
aoqi@0 396 private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter";
aoqi@0 397 }

mercurial