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 | 46 | super(Phase.PRE_LOGICAL); |
39 | 46 | } |
40 | |
|
41 | |
@SuppressWarnings("unchecked") |
42 | |
public void handleMessage(Message message) throws Fault |
43 | |
{ |
44 | 20 | MessageContentsList objs = MessageContentsList.getContentsList(message); |
45 | 20 | if (objs == null || objs.size() == 0) |
46 | |
{ |
47 | 0 | return; |
48 | |
} |
49 | |
|
50 | 20 | List<Object> originalParts = (List<Object>) objs.clone(); |
51 | |
|
52 | 20 | objs.clear(); |
53 | |
|
54 | 20 | for (final Object o : originalParts) |
55 | |
{ |
56 | 20 | if (o instanceof DelayedResult) |
57 | |
{ |
58 | 4 | objs.add(getDelayedResultCallback((DelayedResult)o)); |
59 | |
} |
60 | 16 | else if (o instanceof XMLStreamReader) |
61 | |
{ |
62 | 16 | objs.add(new XMLStreamWriterCallback() |
63 | |
{ |
64 | 16 | public void write(XMLStreamWriter writer) throws Fault, XMLStreamException |
65 | |
{ |
66 | 16 | StaxUtils.copy((XMLStreamReader)o, writer); |
67 | 16 | } |
68 | |
}); |
69 | |
} |
70 | |
else |
71 | |
{ |
72 | 0 | objs.add(o); |
73 | |
} |
74 | |
} |
75 | 20 | } |
76 | |
|
77 | |
protected Object getDelayedResultCallback(final DelayedResult r) |
78 | |
{ |
79 | 4 | return new XMLStreamWriterCallback() |
80 | |
{ |
81 | 4 | public void write(XMLStreamWriter writer) throws Fault, XMLStreamException |
82 | |
{ |
83 | 4 | ContentHandlerToXMLStreamWriter handler = new ContentHandlerToXMLStreamWriter(writer) { |
84 | |
|
85 | |
@Override |
86 | |
public void endDocument() throws SAXException |
87 | |
{ |
88 | 4 | } |
89 | |
|
90 | |
@Override |
91 | |
public void processingInstruction(String target, String data) throws SAXException |
92 | |
{ |
93 | 0 | } |
94 | |
|
95 | |
@Override |
96 | |
public void startDocument() throws SAXException |
97 | |
{ |
98 | 4 | } |
99 | |
|
100 | |
@Override |
101 | 4 | public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException |
102 | |
{ |
103 | 0 | } |
104 | |
|
105 | |
}; |
106 | |
|
107 | |
try |
108 | |
{ |
109 | 4 | r.write(new SAXResult(handler)); |
110 | |
} |
111 | 0 | catch (Exception e) |
112 | |
{ |
113 | 0 | throw new Fault(e); |
114 | 4 | } |
115 | 4 | } |
116 | |
}; |
117 | |
} |
118 | |
|
119 | |
} |