14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.ws.message.jaxb;
27
28 import com.sun.istack.internal.FragmentContentHandler;
29 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
31 import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
32 import com.sun.xml.internal.ws.api.SOAPVersion;
33 import com.sun.xml.internal.ws.api.message.AttachmentSet;
34 import com.sun.xml.internal.ws.api.message.HeaderList;
35 import com.sun.xml.internal.ws.api.message.Message;
36 import com.sun.xml.internal.ws.api.message.MessageHeaders;
37 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
38 import com.sun.xml.internal.ws.message.AbstractMessageImpl;
39 import com.sun.xml.internal.ws.message.AttachmentSetImpl;
40 import com.sun.xml.internal.ws.message.RootElementSniffer;
41 import com.sun.xml.internal.ws.message.stream.StreamMessage;
42 import com.sun.xml.internal.ws.spi.db.BindingContext;
43 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
44 import com.sun.xml.internal.ws.spi.db.XMLBridge;
45 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
46 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
47 import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
48 import org.xml.sax.ContentHandler;
49 import org.xml.sax.ErrorHandler;
50 import org.xml.sax.SAXException;
51
52 import javax.xml.bind.JAXBContext;
53 import javax.xml.bind.JAXBElement;
54 import javax.xml.bind.JAXBException;
55 import javax.xml.bind.Marshaller;
56 import javax.xml.bind.Unmarshaller;
57 import javax.xml.bind.attachment.AttachmentMarshaller;
58 import javax.xml.bind.annotation.XmlRootElement;
59 import javax.xml.bind.util.JAXBResult;
60 import javax.xml.namespace.QName;
61 import javax.xml.stream.XMLStreamException;
62 import javax.xml.stream.XMLStreamReader;
63 import javax.xml.stream.XMLStreamWriter;
64 import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;
65 import javax.xml.transform.Source;
66 import javax.xml.ws.WebServiceException;
67 import java.io.OutputStream;
68
69 /**
70 * {@link Message} backed by a JAXB bean.
71 *
72 * @author Kohsuke Kawaguchi
73 */
74 public final class JAXBMessage extends AbstractMessageImpl {
75 private MessageHeaders headers;
76
77 /**
78 * The JAXB object that represents the payload.
79 */
80 private final Object jaxbObject;
81
82 private final XMLBridge bridge;
83
84 /**
85 * For the use case of a user-supplied JAXB context that is not
86 * a known JAXB type, as when creating a Disaptch object with a
87 * JAXB object parameter, we will marshal and unmarshal directly with
88 * the context object, as there is no Bond available. In this case,
89 * swaRef is not supported.
90 */
91 private final JAXBContext rawContext;
92
93 /**
94 * Lazily sniffed payload element name
302 // since the bridge only produces fragments, we need to fire start/end document.
303 try {
304 out.getHandler().startDocument();
305 if (rawContext != null) {
306 Marshaller m = rawContext.createMarshaller();
307 m.setProperty("jaxb.fragment", Boolean.TRUE);
308 m.marshal(jaxbObject,out);
309 } else
310 bridge.marshal(jaxbObject,out);
311 out.getHandler().endDocument();
312 } catch (SAXException e) {
313 throw new JAXBException(e);
314 }
315 return (T)out.getResult();
316 }
317
318 @Override
319 public XMLStreamReader readPayload() throws XMLStreamException {
320 try {
321 if(infoset==null) {
322 XMLStreamBufferResult sbr = new XMLStreamBufferResult();
323 if (rawContext != null) {
324 Marshaller m = rawContext.createMarshaller();
325 m.setProperty("jaxb.fragment", Boolean.TRUE);
326 m.marshal(jaxbObject, sbr);
327 } else
328 bridge.marshal(jaxbObject, sbr);
329 infoset = sbr.getXMLStreamBuffer();
330 }
331 XMLStreamReader reader = infoset.readAsXMLStreamReader();
332 if(reader.getEventType()== START_DOCUMENT)
333 XMLStreamReaderUtil.nextElementContent(reader);
334 return reader;
335 } catch (JAXBException e) {
336 // bug 6449684, spec 4.3.4
337 throw new WebServiceException(e);
338 }
339 }
340
341 /**
342 * Writes the payload as SAX events.
343 */
344 @Override
345 protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
346 try {
347 if(fragment)
348 contentHandler = new FragmentContentHandler(contentHandler);
349 AttachmentMarshallerImpl am = new AttachmentMarshallerImpl(attachmentSet);
363 }
364 }
365
366 @Override
367 public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
368 try {
369 // MtomCodec sets its own AttachmentMarshaller
370 AttachmentMarshaller am = (sw instanceof MtomStreamWriter)
371 ? ((MtomStreamWriter)sw).getAttachmentMarshaller()
372 : new AttachmentMarshallerImpl(attachmentSet);
373
374 // Get the encoding of the writer
375 String encoding = XMLStreamWriterUtil.getEncoding(sw);
376
377 // Get output stream and use JAXB UTF-8 writer
378 OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
379 if (rawContext != null) {
380 Marshaller m = rawContext.createMarshaller();
381 m.setProperty("jaxb.fragment", Boolean.TRUE);
382 m.setAttachmentMarshaller(am);
383 if (os != null)
384 m.marshal(jaxbObject, os);
385 else
386 m.marshal(jaxbObject, sw);
387 } else {
388 if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
389 bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
390 } else {
391 bridge.marshal(jaxbObject, sw, am);
392 }
393 }
394 //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
395 //am.cleanup();
396 } catch (JAXBException e) {
397 // bug 6449684, spec 4.3.4
398 throw new WebServiceException(e);
399 }
400 }
401
402 @Override
403 public Message copy() {
404 return new JAXBMessage(this);
405 }
406
407 }
|
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.ws.message.jaxb;
27
28 import com.sun.istack.internal.FragmentContentHandler;
29 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
31 import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
32 import com.sun.xml.internal.ws.api.SOAPVersion;
33 import com.sun.xml.internal.ws.api.message.AttachmentSet;
34 import com.sun.xml.internal.ws.api.message.Header;
35 import com.sun.xml.internal.ws.api.message.HeaderList;
36 import com.sun.xml.internal.ws.api.message.Message;
37 import com.sun.xml.internal.ws.api.message.MessageHeaders;
38 import com.sun.xml.internal.ws.api.message.StreamingSOAP;
39 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
40 import com.sun.xml.internal.ws.message.AbstractMessageImpl;
41 import com.sun.xml.internal.ws.message.AttachmentSetImpl;
42 import com.sun.xml.internal.ws.message.RootElementSniffer;
43 import com.sun.xml.internal.ws.message.stream.StreamMessage;
44 import com.sun.xml.internal.ws.spi.db.BindingContext;
45 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
46 import com.sun.xml.internal.ws.spi.db.XMLBridge;
47 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
48 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
49 import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
50 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite;
51 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.ElemInfo;
52
53 import org.xml.sax.ContentHandler;
54 import org.xml.sax.ErrorHandler;
55 import org.xml.sax.SAXException;
56
57 import javax.xml.bind.JAXBContext;
58 import javax.xml.bind.JAXBElement;
59 import javax.xml.bind.JAXBException;
60 import javax.xml.bind.Marshaller;
61 import javax.xml.bind.Unmarshaller;
62 import javax.xml.bind.attachment.AttachmentMarshaller;
63 import javax.xml.bind.annotation.XmlRootElement;
64 import javax.xml.bind.util.JAXBResult;
65 import javax.xml.namespace.QName;
66 import javax.xml.stream.XMLStreamException;
67 import javax.xml.stream.XMLStreamReader;
68 import javax.xml.stream.XMLStreamWriter;
69 import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;
70 import javax.xml.transform.Source;
71 import javax.xml.ws.WebServiceException;
72 import java.io.OutputStream;
73 import java.util.List;
74
75 /**
76 * {@link Message} backed by a JAXB bean.
77 *
78 * @author Kohsuke Kawaguchi
79 */
80 public final class JAXBMessage extends AbstractMessageImpl implements StreamingSOAP {
81 private MessageHeaders headers;
82
83 /**
84 * The JAXB object that represents the payload.
85 */
86 private final Object jaxbObject;
87
88 private final XMLBridge bridge;
89
90 /**
91 * For the use case of a user-supplied JAXB context that is not
92 * a known JAXB type, as when creating a Disaptch object with a
93 * JAXB object parameter, we will marshal and unmarshal directly with
94 * the context object, as there is no Bond available. In this case,
95 * swaRef is not supported.
96 */
97 private final JAXBContext rawContext;
98
99 /**
100 * Lazily sniffed payload element name
308 // since the bridge only produces fragments, we need to fire start/end document.
309 try {
310 out.getHandler().startDocument();
311 if (rawContext != null) {
312 Marshaller m = rawContext.createMarshaller();
313 m.setProperty("jaxb.fragment", Boolean.TRUE);
314 m.marshal(jaxbObject,out);
315 } else
316 bridge.marshal(jaxbObject,out);
317 out.getHandler().endDocument();
318 } catch (SAXException e) {
319 throw new JAXBException(e);
320 }
321 return (T)out.getResult();
322 }
323
324 @Override
325 public XMLStreamReader readPayload() throws XMLStreamException {
326 try {
327 if(infoset==null) {
328 if (rawContext != null) {
329 XMLStreamBufferResult sbr = new XMLStreamBufferResult();
330 Marshaller m = rawContext.createMarshaller();
331 m.setProperty("jaxb.fragment", Boolean.TRUE);
332 m.marshal(jaxbObject, sbr);
333 infoset = sbr.getXMLStreamBuffer();
334 } else {
335 MutableXMLStreamBuffer buffer = new MutableXMLStreamBuffer();
336 writePayloadTo(buffer.createFromXMLStreamWriter());
337 infoset = buffer;
338 }
339 }
340 XMLStreamReader reader = infoset.readAsXMLStreamReader();
341 if(reader.getEventType()== START_DOCUMENT)
342 XMLStreamReaderUtil.nextElementContent(reader);
343 return reader;
344 } catch (JAXBException e) {
345 // bug 6449684, spec 4.3.4
346 throw new WebServiceException(e);
347 }
348 }
349
350 /**
351 * Writes the payload as SAX events.
352 */
353 @Override
354 protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
355 try {
356 if(fragment)
357 contentHandler = new FragmentContentHandler(contentHandler);
358 AttachmentMarshallerImpl am = new AttachmentMarshallerImpl(attachmentSet);
372 }
373 }
374
375 @Override
376 public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException {
377 try {
378 // MtomCodec sets its own AttachmentMarshaller
379 AttachmentMarshaller am = (sw instanceof MtomStreamWriter)
380 ? ((MtomStreamWriter)sw).getAttachmentMarshaller()
381 : new AttachmentMarshallerImpl(attachmentSet);
382
383 // Get the encoding of the writer
384 String encoding = XMLStreamWriterUtil.getEncoding(sw);
385
386 // Get output stream and use JAXB UTF-8 writer
387 OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null;
388 if (rawContext != null) {
389 Marshaller m = rawContext.createMarshaller();
390 m.setProperty("jaxb.fragment", Boolean.TRUE);
391 m.setAttachmentMarshaller(am);
392 if (os != null) {
393 m.marshal(jaxbObject, os);
394 } else {
395 m.marshal(jaxbObject, sw);
396 }
397 } else {
398 if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
399 bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
400 } else {
401 bridge.marshal(jaxbObject, sw, am);
402 }
403 }
404 //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
405 //am.cleanup();
406 } catch (JAXBException e) {
407 // bug 6449684, spec 4.3.4
408 throw new WebServiceException(e);
409 }
410 }
411
412 @Override
413 public Message copy() {
414 return new JAXBMessage(this);
415 }
416
417 public XMLStreamReader readEnvelope() {
418 int base = soapVersion.ordinal()*3;
419 this.envelopeTag = DEFAULT_TAGS.get(base);
420 this.bodyTag = DEFAULT_TAGS.get(base+2);
421 List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>();
422 ElemInfo envElem = new ElemInfo(envelopeTag, null);
423 ElemInfo bdyElem = new ElemInfo(bodyTag, envElem);
424 for (Header h : getHeaders().asList()) {
425 try {
426 hReaders.add(h.readHeader());
427 } catch (XMLStreamException e) {
428 throw new RuntimeException(e);
429 }
430 }
431 XMLStreamReader soapHeader = null;
432 if(hReaders.size()>0) {
433 headerTag = DEFAULT_TAGS.get(base+1);
434 ElemInfo hdrElem = new ElemInfo(headerTag, envElem);
435 soapHeader = new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()]));
436 }
437 try {
438 XMLStreamReader payload= readPayload();
439 XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, new XMLStreamReader[]{payload});
440 XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody};
441 return new XMLReaderComposite(envElem, soapContent);
442 } catch (XMLStreamException e) {
443 throw new RuntimeException(e);
444 }
445 }
446 }
|