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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/message/stream/OutboundStreamHeader.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,171 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.message.stream;
    1.30 +
    1.31 +import com.sun.istack.internal.FinalArrayList;
    1.32 +import com.sun.istack.internal.NotNull;
    1.33 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
    1.34 +import com.sun.xml.internal.stream.buffer.XMLStreamBufferException;
    1.35 +import com.sun.xml.internal.ws.api.message.Header;
    1.36 +import com.sun.xml.internal.ws.message.AbstractHeaderImpl;
    1.37 +import org.xml.sax.ContentHandler;
    1.38 +import org.xml.sax.ErrorHandler;
    1.39 +import org.xml.sax.SAXException;
    1.40 +
    1.41 +import javax.xml.soap.SOAPException;
    1.42 +import javax.xml.soap.SOAPMessage;
    1.43 +import javax.xml.soap.SOAPHeader;
    1.44 +import javax.xml.stream.XMLStreamException;
    1.45 +import javax.xml.stream.XMLStreamReader;
    1.46 +import javax.xml.stream.XMLStreamWriter;
    1.47 +import javax.xml.ws.WebServiceException;
    1.48 +
    1.49 +/**
    1.50 + * Used to represent outbound header created from {@link XMLStreamBuffer}.
    1.51 + *
    1.52 + * <p>
    1.53 + * This is optimized for outbound use, so it implements some of the methods lazily,
    1.54 + * in a slow way.
    1.55 + *
    1.56 + * @author Kohsuke Kawaguchi
    1.57 + */
    1.58 +public final class OutboundStreamHeader extends AbstractHeaderImpl {
    1.59 +    private final XMLStreamBuffer infoset;
    1.60 +    private final String nsUri,localName;
    1.61 +
    1.62 +    /**
    1.63 +     * The attributes on the header element.
    1.64 +     * Lazily parsed.
    1.65 +     * Null if not parsed yet.
    1.66 +     */
    1.67 +    private FinalArrayList<Attribute> attributes;
    1.68 +
    1.69 +    public OutboundStreamHeader(XMLStreamBuffer infoset, String nsUri, String localName) {
    1.70 +        this.infoset = infoset;
    1.71 +        this.nsUri = nsUri;
    1.72 +        this.localName = localName;
    1.73 +    }
    1.74 +
    1.75 +    public @NotNull String getNamespaceURI() {
    1.76 +        return nsUri;
    1.77 +    }
    1.78 +
    1.79 +    public @NotNull String getLocalPart() {
    1.80 +        return localName;
    1.81 +    }
    1.82 +
    1.83 +    public String getAttribute(String nsUri, String localName) {
    1.84 +        if(attributes==null)
    1.85 +            parseAttributes();
    1.86 +        for(int i=attributes.size()-1; i>=0; i-- ) {
    1.87 +            Attribute a = attributes.get(i);
    1.88 +            if(a.localName.equals(localName) && a.nsUri.equals(nsUri))
    1.89 +                return a.value;
    1.90 +        }
    1.91 +        return null;
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * We don't really expect this to be used, but just to satisfy
    1.96 +     * the {@link Header} contract.
    1.97 +     *
    1.98 +     * So this is rather slow.
    1.99 +     */
   1.100 +    private void parseAttributes() {
   1.101 +        try {
   1.102 +            XMLStreamReader reader = readHeader();
   1.103 +
   1.104 +            attributes = new FinalArrayList<Attribute>();
   1.105 +
   1.106 +            for (int i = 0; i < reader.getAttributeCount(); i++) {
   1.107 +                final String localName = reader.getAttributeLocalName(i);
   1.108 +                final String namespaceURI = reader.getAttributeNamespace(i);
   1.109 +                final String value = reader.getAttributeValue(i);
   1.110 +
   1.111 +                attributes.add(new Attribute(namespaceURI,localName,value));
   1.112 +            }
   1.113 +        } catch (XMLStreamException e) {
   1.114 +            throw new WebServiceException("Unable to read the attributes for {"+nsUri+"}"+localName+" header",e);
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    public XMLStreamReader readHeader() throws XMLStreamException {
   1.119 +        return infoset.readAsXMLStreamReader();
   1.120 +    }
   1.121 +
   1.122 +    public void writeTo(XMLStreamWriter w) throws XMLStreamException {
   1.123 +        infoset.writeToXMLStreamWriter(w,true);
   1.124 +    }
   1.125 +
   1.126 +    public void writeTo(SOAPMessage saaj) throws SOAPException {
   1.127 +        try {
   1.128 +            SOAPHeader header = saaj.getSOAPHeader();
   1.129 +            if (header == null)
   1.130 +                header = saaj.getSOAPPart().getEnvelope().addHeader();
   1.131 +            infoset.writeTo(header);
   1.132 +        } catch (XMLStreamBufferException e) {
   1.133 +            throw new SOAPException(e);
   1.134 +        }
   1.135 +    }
   1.136 +
   1.137 +    public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
   1.138 +        infoset.writeTo(contentHandler,errorHandler);
   1.139 +    }
   1.140 +
   1.141 +
   1.142 +    /**
   1.143 +     * Keep the information about an attribute on the header element.
   1.144 +     */
   1.145 +    static final class Attribute {
   1.146 +        /**
   1.147 +         * Can be empty but never null.
   1.148 +         */
   1.149 +        final String nsUri;
   1.150 +        final String localName;
   1.151 +        final String value;
   1.152 +
   1.153 +        public Attribute(String nsUri, String localName, String value) {
   1.154 +            this.nsUri = fixNull(nsUri);
   1.155 +            this.localName = localName;
   1.156 +            this.value = value;
   1.157 +        }
   1.158 +
   1.159 +        /**
   1.160 +         * Convert null to "".
   1.161 +         */
   1.162 +        private static String fixNull(String s) {
   1.163 +            if(s==null) return "";
   1.164 +            else        return s;
   1.165 +        }
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * We the performance paranoid people in the JAX-WS RI thinks
   1.170 +     * saving three bytes is worth while...
   1.171 +     */
   1.172 +    private static final String TRUE_VALUE = "1";
   1.173 +    private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter";
   1.174 +}

mercurial