src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/JAXBHeader.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/jaxb/JAXBHeader.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,212 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.jaxb;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.XMLStreamException2;
    1.33 +import com.sun.xml.internal.bind.api.Bridge;
    1.34 +import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
    1.35 +import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
    1.36 +import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
    1.37 +import com.sun.xml.internal.ws.api.message.Header;
    1.38 +import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
    1.39 +import com.sun.xml.internal.ws.message.AbstractHeaderImpl;
    1.40 +import com.sun.xml.internal.ws.message.RootElementSniffer;
    1.41 +import com.sun.xml.internal.ws.spi.db.BindingContext;
    1.42 +import com.sun.xml.internal.ws.spi.db.XMLBridge;
    1.43 +import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
    1.44 +import org.xml.sax.Attributes;
    1.45 +import org.xml.sax.ContentHandler;
    1.46 +import org.xml.sax.ErrorHandler;
    1.47 +import org.xml.sax.SAXException;
    1.48 +import org.xml.sax.SAXParseException;
    1.49 +
    1.50 +import javax.xml.bind.JAXBElement;
    1.51 +import javax.xml.bind.JAXBException;
    1.52 +import javax.xml.bind.Unmarshaller;
    1.53 +import javax.xml.bind.util.JAXBResult;
    1.54 +import javax.xml.namespace.QName;
    1.55 +import javax.xml.soap.SOAPException;
    1.56 +import javax.xml.soap.SOAPMessage;
    1.57 +import javax.xml.soap.SOAPHeader;
    1.58 +import javax.xml.stream.XMLStreamException;
    1.59 +import javax.xml.stream.XMLStreamReader;
    1.60 +import javax.xml.stream.XMLStreamWriter;
    1.61 +import java.io.OutputStream;
    1.62 +
    1.63 +/**
    1.64 + * {@link Header} whose physical data representation is a JAXB bean.
    1.65 + *
    1.66 + * @author Kohsuke Kawaguchi
    1.67 + */
    1.68 +public final class JAXBHeader extends AbstractHeaderImpl {
    1.69 +
    1.70 +    /**
    1.71 +     * The JAXB object that represents the header.
    1.72 +     */
    1.73 +    private final Object jaxbObject;
    1.74 +
    1.75 +    private final XMLBridge bridge;
    1.76 +
    1.77 +    // information about this header. lazily obtained.
    1.78 +    private String nsUri;
    1.79 +    private String localName;
    1.80 +    private Attributes atts;
    1.81 +
    1.82 +    /**
    1.83 +     * Once the header is turned into infoset,
    1.84 +     * this buffer keeps it.
    1.85 +     */
    1.86 +    private XMLStreamBuffer infoset;
    1.87 +
    1.88 +    public JAXBHeader(BindingContext context, Object jaxbObject) {
    1.89 +        this.jaxbObject = jaxbObject;
    1.90 +//        this.bridge = new MarshallerBridge(context);
    1.91 +        this.bridge = context.createFragmentBridge();
    1.92 +
    1.93 +        if (jaxbObject instanceof JAXBElement) {
    1.94 +            JAXBElement e = (JAXBElement) jaxbObject;
    1.95 +            this.nsUri = e.getName().getNamespaceURI();
    1.96 +            this.localName = e.getName().getLocalPart();
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    public JAXBHeader(XMLBridge bridge, Object jaxbObject) {
   1.101 +        this.jaxbObject = jaxbObject;
   1.102 +        this.bridge = bridge;
   1.103 +
   1.104 +        QName tagName = bridge.getTypeInfo().tagName;
   1.105 +        this.nsUri = tagName.getNamespaceURI();
   1.106 +        this.localName = tagName.getLocalPart();
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Lazily parse the first element to obtain attribute values on it.
   1.111 +     */
   1.112 +    private void parse() {
   1.113 +        RootElementSniffer sniffer = new RootElementSniffer();
   1.114 +        try {
   1.115 +            bridge.marshal(jaxbObject,sniffer,null);
   1.116 +        } catch (JAXBException e) {
   1.117 +            // if it's due to us aborting the processing after the first element,
   1.118 +            // we can safely ignore this exception.
   1.119 +            //
   1.120 +            // if it's due to error in the object, the same error will be reported
   1.121 +            // when the readHeader() method is used, so we don't have to report
   1.122 +            // an error right now.
   1.123 +            nsUri = sniffer.getNsUri();
   1.124 +            localName = sniffer.getLocalName();
   1.125 +            atts = sniffer.getAttributes();
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +
   1.130 +    public @NotNull String getNamespaceURI() {
   1.131 +        if(nsUri==null)
   1.132 +            parse();
   1.133 +        return nsUri;
   1.134 +    }
   1.135 +
   1.136 +    public @NotNull String getLocalPart() {
   1.137 +        if(localName==null)
   1.138 +            parse();
   1.139 +        return localName;
   1.140 +    }
   1.141 +
   1.142 +    public String getAttribute(String nsUri, String localName) {
   1.143 +        if(atts==null)
   1.144 +            parse();
   1.145 +        return atts.getValue(nsUri,localName);
   1.146 +    }
   1.147 +
   1.148 +    public XMLStreamReader readHeader() throws XMLStreamException {
   1.149 +        if(infoset==null) {
   1.150 +            MutableXMLStreamBuffer buffer = new MutableXMLStreamBuffer();
   1.151 +            writeTo(buffer.createFromXMLStreamWriter());
   1.152 +            infoset = buffer;
   1.153 +        }
   1.154 +        return infoset.readAsXMLStreamReader();
   1.155 +    }
   1.156 +
   1.157 +    public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
   1.158 +        try {
   1.159 +            JAXBResult r = new JAXBResult(unmarshaller);
   1.160 +            // bridge marshals a fragment, so we need to add start/endDocument by ourselves
   1.161 +            r.getHandler().startDocument();
   1.162 +            bridge.marshal(jaxbObject,r);
   1.163 +            r.getHandler().endDocument();
   1.164 +            return (T)r.getResult();
   1.165 +        } catch (SAXException e) {
   1.166 +            throw new JAXBException(e);
   1.167 +        }
   1.168 +    }
   1.169 +    /** @deprecated */
   1.170 +    public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
   1.171 +        return bridge.unmarshal(new JAXBBridgeSource(this.bridge,jaxbObject));
   1.172 +    }
   1.173 +
   1.174 +        public <T> T readAsJAXB(XMLBridge<T> bond) throws JAXBException {
   1.175 +        return bond.unmarshal(new JAXBBridgeSource(this.bridge,jaxbObject),null);
   1.176 +        }
   1.177 +
   1.178 +    public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
   1.179 +        try {
   1.180 +            // Get the encoding of the writer
   1.181 +            String encoding = XMLStreamWriterUtil.getEncoding(sw);
   1.182 +
   1.183 +            // Get output stream and use JAXB UTF-8 writer
   1.184 +            OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
   1.185 +            if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
   1.186 +                bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), null);
   1.187 +            } else {
   1.188 +                bridge.marshal(jaxbObject,sw, null);
   1.189 +            }
   1.190 +        } catch (JAXBException e) {
   1.191 +            throw new XMLStreamException2(e);
   1.192 +        }
   1.193 +    }
   1.194 +
   1.195 +    public void writeTo(SOAPMessage saaj) throws SOAPException {
   1.196 +        try {
   1.197 +            SOAPHeader header = saaj.getSOAPHeader();
   1.198 +            if (header == null)
   1.199 +                header = saaj.getSOAPPart().getEnvelope().addHeader();
   1.200 +            bridge.marshal(jaxbObject,header);
   1.201 +        } catch (JAXBException e) {
   1.202 +            throw new SOAPException(e);
   1.203 +        }
   1.204 +    }
   1.205 +
   1.206 +    public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
   1.207 +        try {
   1.208 +            bridge.marshal(jaxbObject,contentHandler,null);
   1.209 +        } catch (JAXBException e) {
   1.210 +            SAXParseException x = new SAXParseException(e.getMessage(),null,null,-1,-1,e);
   1.211 +            errorHandler.fatalError(x);
   1.212 +            throw x;
   1.213 +        }
   1.214 +    }
   1.215 +}

mercurial