aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.message.stream; aoqi@0: aoqi@0: import com.sun.istack.internal.FinalArrayList; aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; aoqi@0: import com.sun.xml.internal.stream.buffer.XMLStreamBufferException; aoqi@0: import com.sun.xml.internal.ws.api.message.Header; aoqi@0: import com.sun.xml.internal.ws.message.AbstractHeaderImpl; aoqi@0: import org.xml.sax.ContentHandler; aoqi@0: import org.xml.sax.ErrorHandler; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: import javax.xml.soap.SOAPException; aoqi@0: import javax.xml.soap.SOAPMessage; aoqi@0: import javax.xml.soap.SOAPHeader; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamReader; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: aoqi@0: /** aoqi@0: * Used to represent outbound header created from {@link XMLStreamBuffer}. aoqi@0: * aoqi@0: *

aoqi@0: * This is optimized for outbound use, so it implements some of the methods lazily, aoqi@0: * in a slow way. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class OutboundStreamHeader extends AbstractHeaderImpl { aoqi@0: private final XMLStreamBuffer infoset; aoqi@0: private final String nsUri,localName; aoqi@0: aoqi@0: /** aoqi@0: * The attributes on the header element. aoqi@0: * Lazily parsed. aoqi@0: * Null if not parsed yet. aoqi@0: */ aoqi@0: private FinalArrayList attributes; aoqi@0: aoqi@0: public OutboundStreamHeader(XMLStreamBuffer infoset, String nsUri, String localName) { aoqi@0: this.infoset = infoset; aoqi@0: this.nsUri = nsUri; aoqi@0: this.localName = localName; aoqi@0: } aoqi@0: aoqi@0: public @NotNull String getNamespaceURI() { aoqi@0: return nsUri; aoqi@0: } aoqi@0: aoqi@0: public @NotNull String getLocalPart() { aoqi@0: return localName; aoqi@0: } aoqi@0: aoqi@0: public String getAttribute(String nsUri, String localName) { aoqi@0: if(attributes==null) aoqi@0: parseAttributes(); aoqi@0: for(int i=attributes.size()-1; i>=0; i-- ) { aoqi@0: Attribute a = attributes.get(i); aoqi@0: if(a.localName.equals(localName) && a.nsUri.equals(nsUri)) aoqi@0: return a.value; aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * We don't really expect this to be used, but just to satisfy aoqi@0: * the {@link Header} contract. aoqi@0: * aoqi@0: * So this is rather slow. aoqi@0: */ aoqi@0: private void parseAttributes() { aoqi@0: try { aoqi@0: XMLStreamReader reader = readHeader(); aoqi@0: aoqi@0: attributes = new FinalArrayList(); aoqi@0: aoqi@0: for (int i = 0; i < reader.getAttributeCount(); i++) { aoqi@0: final String localName = reader.getAttributeLocalName(i); aoqi@0: final String namespaceURI = reader.getAttributeNamespace(i); aoqi@0: final String value = reader.getAttributeValue(i); aoqi@0: aoqi@0: attributes.add(new Attribute(namespaceURI,localName,value)); aoqi@0: } aoqi@0: } catch (XMLStreamException e) { aoqi@0: throw new WebServiceException("Unable to read the attributes for {"+nsUri+"}"+localName+" header",e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public XMLStreamReader readHeader() throws XMLStreamException { aoqi@0: return infoset.readAsXMLStreamReader(); aoqi@0: } aoqi@0: aoqi@0: public void writeTo(XMLStreamWriter w) throws XMLStreamException { aoqi@0: infoset.writeToXMLStreamWriter(w,true); aoqi@0: } aoqi@0: aoqi@0: public void writeTo(SOAPMessage saaj) throws SOAPException { aoqi@0: try { aoqi@0: SOAPHeader header = saaj.getSOAPHeader(); aoqi@0: if (header == null) aoqi@0: header = saaj.getSOAPPart().getEnvelope().addHeader(); aoqi@0: infoset.writeTo(header); aoqi@0: } catch (XMLStreamBufferException e) { aoqi@0: throw new SOAPException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException { aoqi@0: infoset.writeTo(contentHandler,errorHandler); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Keep the information about an attribute on the header element. aoqi@0: */ aoqi@0: static final class Attribute { aoqi@0: /** aoqi@0: * Can be empty but never null. aoqi@0: */ aoqi@0: final String nsUri; aoqi@0: final String localName; aoqi@0: final String value; aoqi@0: aoqi@0: public Attribute(String nsUri, String localName, String value) { aoqi@0: this.nsUri = fixNull(nsUri); aoqi@0: this.localName = localName; aoqi@0: this.value = value; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convert null to "". aoqi@0: */ aoqi@0: private static String fixNull(String s) { aoqi@0: if(s==null) return ""; aoqi@0: else return s; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * We the performance paranoid people in the JAX-WS RI thinks aoqi@0: * saving three bytes is worth while... aoqi@0: */ aoqi@0: private static final String TRUE_VALUE = "1"; aoqi@0: private static final String IS_REFERENCE_PARAMETER = "IsReferenceParameter"; aoqi@0: }