1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf.support; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.config.MuleProperties; |
12 | |
import org.mule.module.cxf.CxfConstants; |
13 | |
|
14 | |
import java.util.Set; |
15 | |
|
16 | |
import javax.xml.namespace.QName; |
17 | |
|
18 | |
import org.apache.cxf.binding.soap.SoapMessage; |
19 | |
import org.apache.cxf.headers.Header; |
20 | |
import org.apache.cxf.interceptor.Fault; |
21 | |
import org.apache.cxf.message.Message; |
22 | |
import org.apache.cxf.phase.Phase; |
23 | |
import org.w3c.dom.Element; |
24 | |
import org.w3c.dom.Node; |
25 | |
import org.w3c.dom.NodeList; |
26 | |
import org.w3c.dom.Text; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class MuleHeadersInInterceptor extends AbstractMuleHeaderInterceptor |
32 | |
{ |
33 | |
|
34 | |
public MuleHeadersInInterceptor() |
35 | |
{ |
36 | 0 | super(Phase.PRE_PROTOCOL); |
37 | 0 | } |
38 | |
|
39 | |
public void handleMessage(Message m) throws Fault |
40 | |
{ |
41 | 0 | if (!(m instanceof SoapMessage)) |
42 | |
{ |
43 | 0 | return; |
44 | |
} |
45 | |
|
46 | 0 | SoapMessage message = (SoapMessage) m; |
47 | 0 | if (!message.hasHeaders()) |
48 | |
{ |
49 | 0 | return; |
50 | |
} |
51 | 0 | Header mule_header = message.getHeader(MULE_HEADER_Q); |
52 | 0 | if (mule_header == null) |
53 | |
{ |
54 | 0 | return; |
55 | |
} |
56 | 0 | Object obj = mule_header.getObject(); |
57 | 0 | if (!(obj instanceof Element)) |
58 | |
{ |
59 | |
|
60 | 0 | return; |
61 | |
} |
62 | |
|
63 | 0 | Element header_element = (Element) obj; |
64 | 0 | NodeList mule_headers = header_element.getChildNodes(); |
65 | 0 | int idx = 0; |
66 | |
Node child; |
67 | 0 | while ((child = mule_headers.item(idx++)) != null) |
68 | |
{ |
69 | 0 | if (child.getNodeType() != Node.ELEMENT_NODE) |
70 | |
{ |
71 | 0 | continue; |
72 | |
} |
73 | 0 | Element child_el = (Element) child; |
74 | 0 | if (child_el.getNamespaceURI() == null || !child_el.getNamespaceURI().equals(MULE_NS_URI)) |
75 | |
{ |
76 | 0 | continue; |
77 | |
} |
78 | |
|
79 | 0 | if (SUPPORTED_HEADERS.contains(child_el.getLocalName())) |
80 | |
{ |
81 | 0 | message.put(child_el.getLocalName(), collectTextFrom(child_el)); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
|
86 | 0 | MuleMessage reqMsg = ((MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT)).getMessage(); |
87 | |
|
88 | |
|
89 | 0 | String replyTo = (String) message.get(MuleProperties.MULE_REPLY_TO_PROPERTY); |
90 | 0 | if (replyTo != null) |
91 | |
{ |
92 | 0 | reqMsg.setReplyTo(replyTo); |
93 | |
} |
94 | |
|
95 | 0 | String corId = (String) message.get(MuleProperties.MULE_CORRELATION_ID_PROPERTY); |
96 | 0 | if (corId != null) |
97 | |
{ |
98 | 0 | reqMsg.setCorrelationId(corId); |
99 | |
} |
100 | |
|
101 | 0 | String corGroupSize = (String) message.get(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY); |
102 | 0 | if (corGroupSize != null) |
103 | |
{ |
104 | 0 | reqMsg.setCorrelationGroupSize(Integer.valueOf(corGroupSize)); |
105 | |
} |
106 | |
|
107 | 0 | String corSeq = (String) message.get(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY); |
108 | 0 | if (corSeq != null) |
109 | |
{ |
110 | 0 | reqMsg.setCorrelationSequence(Integer.valueOf(corSeq)); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
public Set<QName> getUnderstoodHeaders() |
115 | |
{ |
116 | 0 | return UNDERSTOOD_HEADERS; |
117 | |
} |
118 | |
|
119 | |
private String collectTextFrom(Element e) |
120 | |
{ |
121 | 0 | NodeList children = e.getChildNodes(); |
122 | 0 | StringBuilder sb = new StringBuilder(); |
123 | 0 | int idx = 0; |
124 | |
Node n; |
125 | 0 | while ((n = children.item(idx++)) != null) |
126 | |
{ |
127 | 0 | if (n.getNodeType() == Node.TEXT_NODE) |
128 | |
{ |
129 | 0 | sb.append(((Text) n).getTextContent()); |
130 | |
} |
131 | |
} |
132 | 0 | return sb.toString(); |
133 | |
} |
134 | |
|
135 | |
} |