src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/JAXBDispatchMessage.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.message.jaxb;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 29 import com.sun.xml.internal.ws.api.message.Message;
alanb@368 30 import com.sun.xml.internal.ws.api.message.MessageHeaders;
ohair@286 31 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
ohair@286 32 import com.sun.xml.internal.ws.message.AbstractMessageImpl;
ohair@286 33 import com.sun.xml.internal.ws.message.PayloadElementSniffer;
ohair@286 34 import com.sun.xml.internal.ws.spi.db.BindingContext;
ohair@286 35 import com.sun.xml.internal.ws.spi.db.XMLBridge;
ohair@286 36 import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
ohair@286 37 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
ohair@286 38 import org.xml.sax.ContentHandler;
ohair@286 39 import org.xml.sax.ErrorHandler;
ohair@286 40 import org.xml.sax.SAXException;
ohair@286 41
ohair@286 42 import javax.xml.bind.JAXBContext;
ohair@286 43 import javax.xml.bind.JAXBException;
ohair@286 44 import javax.xml.bind.Marshaller;
ohair@286 45 import javax.xml.bind.attachment.AttachmentMarshaller;
ohair@286 46 import javax.xml.namespace.QName;
ohair@286 47 import javax.xml.stream.XMLStreamException;
ohair@286 48 import javax.xml.stream.XMLStreamReader;
ohair@286 49 import javax.xml.stream.XMLStreamWriter;
ohair@286 50 import javax.xml.transform.Source;
ohair@286 51 import javax.xml.ws.WebServiceException;
ohair@286 52 import java.io.OutputStream;
ohair@286 53
ohair@286 54 /**
ohair@286 55 * {@link Message} backed by a JAXB bean; this implementation is used when client uses
ohair@286 56 * Dispatch mechanism in JAXB/MESSAGE mode; difference from {@link JAXBMessage} is
ohair@286 57 * that {@code jaxbObject} holds whole SOAP message including SOAP envelope;
ohair@286 58 * it's the client who is responsible for preparing message content.
ohair@286 59 *
ohair@286 60 * @author Miroslav Kos (miroslav.kos at oracle.com)
ohair@286 61 */
ohair@286 62 public class JAXBDispatchMessage extends AbstractMessageImpl {
ohair@286 63
ohair@286 64 private final Object jaxbObject;
ohair@286 65
ohair@286 66 private final XMLBridge bridge;
ohair@286 67
ohair@286 68 /**
ohair@286 69 * For the use case of a user-supplied JAXB context that is not
ohair@286 70 * a known JAXB type, as when creating a Disaptch object with a
ohair@286 71 * JAXB object parameter, we will marshal and unmarshal directly with
ohair@286 72 * the context object, as there is no Bond available. In this case,
ohair@286 73 * swaRef is not supported.
ohair@286 74 */
ohair@286 75 private final JAXBContext rawContext;
ohair@286 76
ohair@286 77 /**
ohair@286 78 * Lazily sniffed payload element name
ohair@286 79 */
ohair@286 80 private QName payloadQName;
ohair@286 81
ohair@286 82 /**
ohair@286 83 * Copy constructor.
ohair@286 84 */
ohair@286 85 private JAXBDispatchMessage(JAXBDispatchMessage that) {
ohair@286 86 super(that);
ohair@286 87 jaxbObject = that.jaxbObject;
ohair@286 88 rawContext = that.rawContext;
ohair@286 89 bridge = that.bridge;
ohair@286 90 }
ohair@286 91
ohair@286 92 public JAXBDispatchMessage(JAXBContext rawContext, Object jaxbObject, SOAPVersion soapVersion) {
ohair@286 93 super(soapVersion);
ohair@286 94 this.bridge = null;
ohair@286 95 this.rawContext = rawContext;
ohair@286 96 this.jaxbObject = jaxbObject;
ohair@286 97 }
ohair@286 98
ohair@286 99 public JAXBDispatchMessage(BindingContext context, Object jaxbObject, SOAPVersion soapVersion) {
ohair@286 100 super(soapVersion);
ohair@286 101 this.bridge = context.createFragmentBridge();
ohair@286 102 this.rawContext = null;
ohair@286 103 this.jaxbObject = jaxbObject;
ohair@286 104 }
ohair@286 105
ohair@286 106 @Override
ohair@286 107 protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
ohair@286 108 throw new UnsupportedOperationException();
ohair@286 109 }
ohair@286 110
ohair@286 111 @Override
ohair@286 112 public boolean hasHeaders() {
ohair@286 113 return false;
ohair@286 114 }
ohair@286 115
ohair@286 116 @Override
alanb@368 117 public MessageHeaders getHeaders() {
ohair@286 118 return null;
ohair@286 119 }
ohair@286 120
ohair@286 121 @Override
ohair@286 122 public String getPayloadLocalPart() {
ohair@286 123 if (payloadQName == null) {
ohair@286 124 readPayloadElement();
ohair@286 125 }
ohair@286 126 return payloadQName.getLocalPart();
ohair@286 127 }
ohair@286 128
ohair@286 129 @Override
ohair@286 130 public String getPayloadNamespaceURI() {
ohair@286 131 if (payloadQName == null) {
ohair@286 132 readPayloadElement();
ohair@286 133 }
ohair@286 134 return payloadQName.getNamespaceURI();
ohair@286 135 }
ohair@286 136
ohair@286 137 private void readPayloadElement() {
ohair@286 138 PayloadElementSniffer sniffer = new PayloadElementSniffer();
ohair@286 139 try {
ohair@286 140 if (rawContext != null) {
ohair@286 141 Marshaller m = rawContext.createMarshaller();
ohair@286 142 m.setProperty("jaxb.fragment", Boolean.FALSE);
ohair@286 143 m.marshal(jaxbObject, sniffer);
ohair@286 144 } else {
ohair@286 145 bridge.marshal(jaxbObject, sniffer, null);
ohair@286 146 }
ohair@286 147
ohair@286 148 } catch (JAXBException e) {
ohair@286 149 // if it's due to us aborting the processing after the first element,
ohair@286 150 // we can safely ignore this exception.
ohair@286 151 //
ohair@286 152 // if it's due to error in the object, the same error will be reported
ohair@286 153 // when the readHeader() method is used, so we don't have to report
ohair@286 154 // an error right now.
ohair@286 155 payloadQName = sniffer.getPayloadQName();
ohair@286 156 }
ohair@286 157 }
ohair@286 158
ohair@286 159 @Override
ohair@286 160 public boolean hasPayload() {
ohair@286 161 return true;
ohair@286 162 }
ohair@286 163
ohair@286 164 @Override
ohair@286 165 public Source readPayloadAsSource() {
ohair@286 166 throw new UnsupportedOperationException();
ohair@286 167 }
ohair@286 168
ohair@286 169 @Override
ohair@286 170 public XMLStreamReader readPayload() throws XMLStreamException {
ohair@286 171 throw new UnsupportedOperationException();
ohair@286 172 }
ohair@286 173
ohair@286 174 @Override
ohair@286 175 public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
ohair@286 176 throw new UnsupportedOperationException();
ohair@286 177 }
ohair@286 178
ohair@286 179 @Override
ohair@286 180 public Message copy() {
ohair@286 181 return new JAXBDispatchMessage(this);
ohair@286 182 }
ohair@286 183
ohair@286 184 @Override
ohair@286 185 @SuppressWarnings("unchecked")
ohair@286 186 public void writeTo(XMLStreamWriter sw) throws XMLStreamException {
ohair@286 187 try {
ohair@286 188 // MtomCodec sets its own AttachmentMarshaller
ohair@286 189 AttachmentMarshaller am = (sw instanceof MtomStreamWriter)
ohair@286 190 ? ((MtomStreamWriter) sw).getAttachmentMarshaller()
ohair@286 191 : new AttachmentMarshallerImpl(attachmentSet);
ohair@286 192
ohair@286 193 // Get the encoding of the writer
ohair@286 194 String encoding = XMLStreamWriterUtil.getEncoding(sw);
ohair@286 195
ohair@286 196 // Get output stream and use JAXB UTF-8 writer
alanb@368 197 OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
ohair@286 198 if (rawContext != null) {
ohair@286 199 Marshaller m = rawContext.createMarshaller();
ohair@286 200 m.setProperty("jaxb.fragment", Boolean.FALSE);
ohair@286 201 m.setAttachmentMarshaller(am);
ohair@286 202 if (os != null) {
ohair@286 203 m.marshal(jaxbObject, os);
ohair@286 204 } else {
ohair@286 205 m.marshal(jaxbObject, sw);
ohair@286 206 }
ohair@286 207
ohair@286 208 } else {
ohair@286 209
alanb@368 210 if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
ohair@286 211 bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
ohair@286 212 } else {
ohair@286 213 bridge.marshal(jaxbObject, sw, am);
ohair@286 214 }
ohair@286 215 }
ohair@286 216 //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
ohair@286 217 } catch (JAXBException e) {
ohair@286 218 // bug 6449684, spec 4.3.4
ohair@286 219 throw new WebServiceException(e);
ohair@286 220 }
ohair@286 221 }
ohair@286 222 }

mercurial