Thu, 31 Aug 2017 15:18:52 +0800
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.message.stream; |
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 org.xml.sax.ContentHandler; |
aoqi@0 | 35 | import org.xml.sax.ErrorHandler; |
aoqi@0 | 36 | import org.xml.sax.SAXException; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import javax.xml.soap.SOAPException; |
aoqi@0 | 39 | import javax.xml.soap.SOAPMessage; |
aoqi@0 | 40 | import javax.xml.soap.SOAPHeader; |
aoqi@0 | 41 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 42 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 43 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 44 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 45 | |
aoqi@0 | 46 | /** |
aoqi@0 | 47 | * Used to represent outbound header created from {@link XMLStreamBuffer}. |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * <p> |
aoqi@0 | 50 | * This is optimized for outbound use, so it implements some of the methods lazily, |
aoqi@0 | 51 | * in a slow way. |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * @author Kohsuke Kawaguchi |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | public final class OutboundStreamHeader extends AbstractHeaderImpl { |
aoqi@0 | 56 | private final XMLStreamBuffer infoset; |
aoqi@0 | 57 | private final String nsUri,localName; |
aoqi@0 | 58 | |
aoqi@0 | 59 | /** |
aoqi@0 | 60 | * The attributes on the header element. |
aoqi@0 | 61 | * Lazily parsed. |
aoqi@0 | 62 | * Null if not parsed yet. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | private FinalArrayList<Attribute> attributes; |
aoqi@0 | 65 | |
aoqi@0 | 66 | public OutboundStreamHeader(XMLStreamBuffer infoset, String nsUri, String localName) { |
aoqi@0 | 67 | this.infoset = infoset; |
aoqi@0 | 68 | this.nsUri = nsUri; |
aoqi@0 | 69 | this.localName = localName; |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | public @NotNull String getNamespaceURI() { |
aoqi@0 | 73 | return nsUri; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | public @NotNull String getLocalPart() { |
aoqi@0 | 77 | return localName; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | public String getAttribute(String nsUri, String localName) { |
aoqi@0 | 81 | if(attributes==null) |
aoqi@0 | 82 | parseAttributes(); |
aoqi@0 | 83 | for(int i=attributes.size()-1; i>=0; i-- ) { |
aoqi@0 | 84 | Attribute a = attributes.get(i); |
aoqi@0 | 85 | if(a.localName.equals(localName) && a.nsUri.equals(nsUri)) |
aoqi@0 | 86 | return a.value; |
aoqi@0 | 87 | } |
aoqi@0 | 88 | return null; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * We don't really expect this to be used, but just to satisfy |
aoqi@0 | 93 | * the {@link Header} contract. |
aoqi@0 | 94 | * |
aoqi@0 | 95 | * So this is rather slow. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | private void parseAttributes() { |
aoqi@0 | 98 | try { |
aoqi@0 | 99 | XMLStreamReader reader = readHeader(); |
aoqi@0 | 100 | |
aoqi@0 | 101 | attributes = new FinalArrayList<Attribute>(); |
aoqi@0 | 102 | |
aoqi@0 | 103 | for (int i = 0; i < reader.getAttributeCount(); i++) { |
aoqi@0 | 104 | final String localName = reader.getAttributeLocalName(i); |
aoqi@0 | 105 | final String namespaceURI = reader.getAttributeNamespace(i); |
aoqi@0 | 106 | final String value = reader.getAttributeValue(i); |
aoqi@0 | 107 | |
aoqi@0 | 108 | attributes.add(new Attribute(namespaceURI,localName,value)); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | } catch (XMLStreamException e) { |
aoqi@0 | 111 | throw new WebServiceException("Unable to read the attributes for {"+nsUri+"}"+localName+" header",e); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | public XMLStreamReader readHeader() throws XMLStreamException { |
aoqi@0 | 116 | return infoset.readAsXMLStreamReader(); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | public void writeTo(XMLStreamWriter w) throws XMLStreamException { |
aoqi@0 | 120 | infoset.writeToXMLStreamWriter(w,true); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | public void writeTo(SOAPMessage saaj) throws SOAPException { |
aoqi@0 | 124 | try { |
aoqi@0 | 125 | SOAPHeader header = saaj.getSOAPHeader(); |
aoqi@0 | 126 | if (header == null) |
aoqi@0 | 127 | header = saaj.getSOAPPart().getEnvelope().addHeader(); |
aoqi@0 | 128 | infoset.writeTo(header); |
aoqi@0 | 129 | } catch (XMLStreamBufferException e) { |
aoqi@0 | 130 | throw new SOAPException(e); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { |
aoqi@0 | 135 | infoset.writeTo(contentHandler,errorHandler); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Keep the information about an attribute on the header element. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | static final class Attribute { |
aoqi@0 | 143 | /** |
aoqi@0 | 144 | * Can be empty but never null. |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | final String nsUri; |
aoqi@0 | 147 | final String localName; |
aoqi@0 | 148 | final String value; |
aoqi@0 | 149 | |
aoqi@0 | 150 | public Attribute(String nsUri, String localName, String value) { |
aoqi@0 | 151 | this.nsUri = fixNull(nsUri); |
aoqi@0 | 152 | this.localName = localName; |
aoqi@0 | 153 | this.value = value; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | /** |
aoqi@0 | 157 | * Convert null to "". |
aoqi@0 | 158 | */ |
aoqi@0 | 159 | private static String fixNull(String s) { |
aoqi@0 | 160 | if(s==null) return ""; |
aoqi@0 | 161 | else return s; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | /** |
aoqi@0 | 166 | * We the performance paranoid people in the JAX-WS RI thinks |
aoqi@0 | 167 | * saving three bytes is worth while... |
aoqi@0 | 168 | */ |
aoqi@0 | 169 | private static final String TRUE_VALUE = "1"; |
aoqi@0 | 170 | private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter"; |
aoqi@0 | 171 | } |