src/share/jaxws_classes/com/sun/xml/internal/ws/message/stream/OutboundStreamHeader.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

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.message.stream;
    28 import com.sun.istack.internal.FinalArrayList;
    29 import com.sun.istack.internal.NotNull;
    30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
    31 import com.sun.xml.internal.stream.buffer.XMLStreamBufferException;
    32 import com.sun.xml.internal.ws.api.message.Header;
    33 import com.sun.xml.internal.ws.message.AbstractHeaderImpl;
    34 import org.xml.sax.ContentHandler;
    35 import org.xml.sax.ErrorHandler;
    36 import org.xml.sax.SAXException;
    38 import javax.xml.soap.SOAPException;
    39 import javax.xml.soap.SOAPMessage;
    40 import javax.xml.soap.SOAPHeader;
    41 import javax.xml.stream.XMLStreamException;
    42 import javax.xml.stream.XMLStreamReader;
    43 import javax.xml.stream.XMLStreamWriter;
    44 import javax.xml.ws.WebServiceException;
    46 /**
    47  * Used to represent outbound header created from {@link XMLStreamBuffer}.
    48  *
    49  * <p>
    50  * This is optimized for outbound use, so it implements some of the methods lazily,
    51  * in a slow way.
    52  *
    53  * @author Kohsuke Kawaguchi
    54  */
    55 public final class OutboundStreamHeader extends AbstractHeaderImpl {
    56     private final XMLStreamBuffer infoset;
    57     private final String nsUri,localName;
    59     /**
    60      * The attributes on the header element.
    61      * Lazily parsed.
    62      * Null if not parsed yet.
    63      */
    64     private FinalArrayList<Attribute> attributes;
    66     public OutboundStreamHeader(XMLStreamBuffer infoset, String nsUri, String localName) {
    67         this.infoset = infoset;
    68         this.nsUri = nsUri;
    69         this.localName = localName;
    70     }
    72     public @NotNull String getNamespaceURI() {
    73         return nsUri;
    74     }
    76     public @NotNull String getLocalPart() {
    77         return localName;
    78     }
    80     public String getAttribute(String nsUri, String localName) {
    81         if(attributes==null)
    82             parseAttributes();
    83         for(int i=attributes.size()-1; i>=0; i-- ) {
    84             Attribute a = attributes.get(i);
    85             if(a.localName.equals(localName) && a.nsUri.equals(nsUri))
    86                 return a.value;
    87         }
    88         return null;
    89     }
    91     /**
    92      * We don't really expect this to be used, but just to satisfy
    93      * the {@link Header} contract.
    94      *
    95      * So this is rather slow.
    96      */
    97     private void parseAttributes() {
    98         try {
    99             XMLStreamReader reader = readHeader();
   101             attributes = new FinalArrayList<Attribute>();
   103             for (int i = 0; i < reader.getAttributeCount(); i++) {
   104                 final String localName = reader.getAttributeLocalName(i);
   105                 final String namespaceURI = reader.getAttributeNamespace(i);
   106                 final String value = reader.getAttributeValue(i);
   108                 attributes.add(new Attribute(namespaceURI,localName,value));
   109             }
   110         } catch (XMLStreamException e) {
   111             throw new WebServiceException("Unable to read the attributes for {"+nsUri+"}"+localName+" header",e);
   112         }
   113     }
   115     public XMLStreamReader readHeader() throws XMLStreamException {
   116         return infoset.readAsXMLStreamReader();
   117     }
   119     public void writeTo(XMLStreamWriter w) throws XMLStreamException {
   120         infoset.writeToXMLStreamWriter(w,true);
   121     }
   123     public void writeTo(SOAPMessage saaj) throws SOAPException {
   124         try {
   125             SOAPHeader header = saaj.getSOAPHeader();
   126             if (header == null)
   127                 header = saaj.getSOAPPart().getEnvelope().addHeader();
   128             infoset.writeTo(header);
   129         } catch (XMLStreamBufferException e) {
   130             throw new SOAPException(e);
   131         }
   132     }
   134     public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
   135         infoset.writeTo(contentHandler,errorHandler);
   136     }
   139     /**
   140      * Keep the information about an attribute on the header element.
   141      */
   142     static final class Attribute {
   143         /**
   144          * Can be empty but never null.
   145          */
   146         final String nsUri;
   147         final String localName;
   148         final String value;
   150         public Attribute(String nsUri, String localName, String value) {
   151             this.nsUri = fixNull(nsUri);
   152             this.localName = localName;
   153             this.value = value;
   154         }
   156         /**
   157          * Convert null to "".
   158          */
   159         private static String fixNull(String s) {
   160             if(s==null) return "";
   161             else        return s;
   162         }
   163     }
   165     /**
   166      * We the performance paranoid people in the JAX-WS RI thinks
   167      * saving three bytes is worth while...
   168      */
   169     private static final String TRUE_VALUE = "1";
   170     private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter";
   171 }

mercurial