src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/JAXBDispatchMessage.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/JAXBDispatchMessage.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,222 @@
     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.xml.internal.ws.api.SOAPVersion;
    1.32 +import com.sun.xml.internal.ws.api.message.Message;
    1.33 +import com.sun.xml.internal.ws.api.message.MessageHeaders;
    1.34 +import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
    1.35 +import com.sun.xml.internal.ws.message.AbstractMessageImpl;
    1.36 +import com.sun.xml.internal.ws.message.PayloadElementSniffer;
    1.37 +import com.sun.xml.internal.ws.spi.db.BindingContext;
    1.38 +import com.sun.xml.internal.ws.spi.db.XMLBridge;
    1.39 +import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
    1.40 +import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
    1.41 +import org.xml.sax.ContentHandler;
    1.42 +import org.xml.sax.ErrorHandler;
    1.43 +import org.xml.sax.SAXException;
    1.44 +
    1.45 +import javax.xml.bind.JAXBContext;
    1.46 +import javax.xml.bind.JAXBException;
    1.47 +import javax.xml.bind.Marshaller;
    1.48 +import javax.xml.bind.attachment.AttachmentMarshaller;
    1.49 +import javax.xml.namespace.QName;
    1.50 +import javax.xml.stream.XMLStreamException;
    1.51 +import javax.xml.stream.XMLStreamReader;
    1.52 +import javax.xml.stream.XMLStreamWriter;
    1.53 +import javax.xml.transform.Source;
    1.54 +import javax.xml.ws.WebServiceException;
    1.55 +import java.io.OutputStream;
    1.56 +
    1.57 +/**
    1.58 + * {@link Message} backed by a JAXB bean; this implementation is used when client uses
    1.59 + * Dispatch mechanism in JAXB/MESSAGE mode; difference from {@link JAXBMessage} is
    1.60 + * that {@code jaxbObject} holds whole SOAP message including SOAP envelope;
    1.61 + * it's the client who is responsible for preparing message content.
    1.62 + *
    1.63 + * @author Miroslav Kos (miroslav.kos at oracle.com)
    1.64 + */
    1.65 +public class JAXBDispatchMessage extends AbstractMessageImpl {
    1.66 +
    1.67 +    private final Object jaxbObject;
    1.68 +
    1.69 +    private final XMLBridge bridge;
    1.70 +
    1.71 +    /**
    1.72 +     * For the use case of a user-supplied JAXB context that is not
    1.73 +     * a known JAXB type, as when creating a Disaptch object with a
    1.74 +     * JAXB object parameter, we will marshal and unmarshal directly with
    1.75 +     * the context object, as there is no Bond available.  In this case,
    1.76 +     * swaRef is not supported.
    1.77 +     */
    1.78 +    private final JAXBContext rawContext;
    1.79 +
    1.80 +    /**
    1.81 +     * Lazily sniffed payload element name
    1.82 +     */
    1.83 +    private QName payloadQName;
    1.84 +
    1.85 +    /**
    1.86 +     * Copy constructor.
    1.87 +     */
    1.88 +    private JAXBDispatchMessage(JAXBDispatchMessage that) {
    1.89 +        super(that);
    1.90 +        jaxbObject = that.jaxbObject;
    1.91 +        rawContext = that.rawContext;
    1.92 +        bridge = that.bridge;
    1.93 +    }
    1.94 +
    1.95 +    public JAXBDispatchMessage(JAXBContext rawContext, Object jaxbObject, SOAPVersion soapVersion) {
    1.96 +        super(soapVersion);
    1.97 +        this.bridge = null;
    1.98 +        this.rawContext = rawContext;
    1.99 +        this.jaxbObject = jaxbObject;
   1.100 +    }
   1.101 +
   1.102 +    public JAXBDispatchMessage(BindingContext context, Object jaxbObject, SOAPVersion soapVersion) {
   1.103 +        super(soapVersion);
   1.104 +        this.bridge = context.createFragmentBridge();
   1.105 +        this.rawContext = null;
   1.106 +        this.jaxbObject = jaxbObject;
   1.107 +    }
   1.108 +
   1.109 +    @Override
   1.110 +    protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
   1.111 +        throw new UnsupportedOperationException();
   1.112 +    }
   1.113 +
   1.114 +    @Override
   1.115 +    public boolean hasHeaders() {
   1.116 +        return false;
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public MessageHeaders getHeaders() {
   1.121 +        return null;
   1.122 +    }
   1.123 +
   1.124 +    @Override
   1.125 +    public String getPayloadLocalPart() {
   1.126 +        if (payloadQName == null) {
   1.127 +            readPayloadElement();
   1.128 +        }
   1.129 +        return payloadQName.getLocalPart();
   1.130 +    }
   1.131 +
   1.132 +    @Override
   1.133 +    public String getPayloadNamespaceURI() {
   1.134 +        if (payloadQName == null) {
   1.135 +            readPayloadElement();
   1.136 +        }
   1.137 +        return payloadQName.getNamespaceURI();
   1.138 +    }
   1.139 +
   1.140 +    private void readPayloadElement() {
   1.141 +        PayloadElementSniffer sniffer = new PayloadElementSniffer();
   1.142 +        try {
   1.143 +            if (rawContext != null) {
   1.144 +                Marshaller m = rawContext.createMarshaller();
   1.145 +                m.setProperty("jaxb.fragment", Boolean.FALSE);
   1.146 +                m.marshal(jaxbObject, sniffer);
   1.147 +            } else {
   1.148 +                bridge.marshal(jaxbObject, sniffer, null);
   1.149 +            }
   1.150 +
   1.151 +        } catch (JAXBException e) {
   1.152 +            // if it's due to us aborting the processing after the first element,
   1.153 +            // we can safely ignore this exception.
   1.154 +            //
   1.155 +            // if it's due to error in the object, the same error will be reported
   1.156 +            // when the readHeader() method is used, so we don't have to report
   1.157 +            // an error right now.
   1.158 +            payloadQName = sniffer.getPayloadQName();
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    @Override
   1.163 +    public boolean hasPayload() {
   1.164 +        return true;
   1.165 +    }
   1.166 +
   1.167 +    @Override
   1.168 +    public Source readPayloadAsSource() {
   1.169 +        throw new UnsupportedOperationException();
   1.170 +    }
   1.171 +
   1.172 +    @Override
   1.173 +    public XMLStreamReader readPayload() throws XMLStreamException {
   1.174 +        throw new UnsupportedOperationException();
   1.175 +    }
   1.176 +
   1.177 +    @Override
   1.178 +    public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
   1.179 +        throw new UnsupportedOperationException();
   1.180 +    }
   1.181 +
   1.182 +    @Override
   1.183 +    public Message copy() {
   1.184 +        return new JAXBDispatchMessage(this);
   1.185 +    }
   1.186 +
   1.187 +    @Override
   1.188 +    @SuppressWarnings("unchecked")
   1.189 +    public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
   1.190 +        try {
   1.191 +            // MtomCodec sets its own AttachmentMarshaller
   1.192 +            AttachmentMarshaller am = (sw instanceof MtomStreamWriter)
   1.193 +                    ? ((MtomStreamWriter) sw).getAttachmentMarshaller()
   1.194 +                    : new AttachmentMarshallerImpl(attachmentSet);
   1.195 +
   1.196 +            // Get the encoding of the writer
   1.197 +            String encoding = XMLStreamWriterUtil.getEncoding(sw);
   1.198 +
   1.199 +            // Get output stream and use JAXB UTF-8 writer
   1.200 +            OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
   1.201 +            if (rawContext != null) {
   1.202 +                Marshaller m = rawContext.createMarshaller();
   1.203 +                m.setProperty("jaxb.fragment", Boolean.FALSE);
   1.204 +                m.setAttachmentMarshaller(am);
   1.205 +                if (os != null) {
   1.206 +                    m.marshal(jaxbObject, os);
   1.207 +                } else {
   1.208 +                    m.marshal(jaxbObject, sw);
   1.209 +                }
   1.210 +
   1.211 +            } else {
   1.212 +
   1.213 +                if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
   1.214 +                    bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
   1.215 +                } else {
   1.216 +                    bridge.marshal(jaxbObject, sw, am);
   1.217 +                }
   1.218 +            }
   1.219 +            //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
   1.220 +        } catch (JAXBException e) {
   1.221 +            // bug 6449684, spec 4.3.4
   1.222 +            throw new WebServiceException(e);
   1.223 +        }
   1.224 +    }
   1.225 +}

mercurial