1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.cxf.support;
12
13 import org.mule.module.xml.transformer.DelayedResult;
14
15 import java.util.List;
16
17 import javanet.staxutils.ContentHandlerToXMLStreamWriter;
18
19 import javax.xml.stream.XMLStreamException;
20 import javax.xml.stream.XMLStreamReader;
21 import javax.xml.stream.XMLStreamWriter;
22 import javax.xml.transform.sax.SAXResult;
23
24 import org.apache.cxf.databinding.stax.XMLStreamWriterCallback;
25 import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
26 import org.apache.cxf.interceptor.Fault;
27 import org.apache.cxf.message.Message;
28 import org.apache.cxf.message.MessageContentsList;
29 import org.apache.cxf.phase.Phase;
30 import org.apache.cxf.staxutils.StaxUtils;
31 import org.xml.sax.SAXException;
32
33 public class OutputPayloadInterceptor extends AbstractOutDatabindingInterceptor
34 {
35
36 public OutputPayloadInterceptor()
37 {
38 super(Phase.PRE_LOGICAL);
39 }
40
41 @SuppressWarnings("unchecked")
42 public void handleMessage(Message message) throws Fault
43 {
44 MessageContentsList objs = MessageContentsList.getContentsList(message);
45 if (objs == null || objs.size() == 0)
46 {
47 return;
48 }
49
50 List<Object> originalParts = (List<Object>) objs.clone();
51
52 objs.clear();
53
54 for (final Object o : originalParts)
55 {
56 if (o instanceof DelayedResult)
57 {
58 objs.add(getDelayedResultCallback((DelayedResult)o));
59 }
60 else if (o instanceof XMLStreamReader)
61 {
62 objs.add(new XMLStreamWriterCallback()
63 {
64 public void write(XMLStreamWriter writer) throws Fault, XMLStreamException
65 {
66 StaxUtils.copy((XMLStreamReader)o, writer);
67 }
68 });
69 }
70 else
71 {
72 objs.add(o);
73 }
74 }
75 }
76
77 protected Object getDelayedResultCallback(final DelayedResult r)
78 {
79 return new XMLStreamWriterCallback()
80 {
81 public void write(XMLStreamWriter writer) throws Fault, XMLStreamException
82 {
83 ContentHandlerToXMLStreamWriter handler = new ContentHandlerToXMLStreamWriter(writer) {
84
85 @Override
86 public void endDocument() throws SAXException
87 {
88 }
89
90 @Override
91 public void processingInstruction(String target, String data) throws SAXException
92 {
93 }
94
95 @Override
96 public void startDocument() throws SAXException
97 {
98 }
99
100 @Override
101 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
102 {
103 }
104
105 };
106
107 try
108 {
109 r.write(new SAXResult(handler));
110 }
111 catch (Exception e)
112 {
113 throw new Fault(e);
114 }
115 }
116 };
117 }
118
119 }