1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.cxf.support;
12
13 import org.apache.cxf.message.Exchange;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleMessage;
16 import org.mule.module.cxf.CxfConstants;
17 import org.mule.transport.http.HttpConnector;
18 import org.mule.transport.http.HttpConstants;
19
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.cxf.helpers.CastUtils;
24 import org.apache.cxf.interceptor.AttachmentOutInterceptor;
25 import org.apache.cxf.interceptor.Fault;
26 import org.apache.cxf.message.Message;
27 import org.apache.cxf.phase.AbstractPhaseInterceptor;
28 import org.apache.cxf.phase.Phase;
29
30 public class MuleProtocolHeadersOutInterceptor
31 extends AbstractPhaseInterceptor<Message>
32 {
33
34 public MuleProtocolHeadersOutInterceptor()
35 {
36 super(Phase.PRE_STREAM);
37 getAfter().add(AttachmentOutInterceptor.class.getName());
38 }
39
40 public void handleMessage(Message message) throws Fault
41 {
42 MuleEvent event = (MuleEvent) message.getExchange().get(CxfConstants.MULE_EVENT);
43 if (event == null)
44 {
45 return;
46 }
47
48 MuleMessage muleMsg = event.getMessage();
49
50 if (muleMsg == null)
51 {
52 return;
53 }
54 extractAndSetContentType(message, muleMsg);
55 extractAndSet(message, muleMsg, Message.RESPONSE_CODE, HttpConnector.HTTP_STATUS_PROPERTY);
56
57 String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
58 if (method == null)
59 {
60 method = HttpConstants.METHOD_POST;
61 }
62
63 muleMsg.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
64
65 Map<String, List<String>> reqHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
66 if (reqHeaders != null)
67 {
68 for (Map.Entry<String, List<String>> e : reqHeaders.entrySet())
69 {
70 String key = e.getKey();
71 String val = format(e.getValue());
72
73 muleMsg.setOutboundProperty(key, val);
74 }
75 }
76
77 if (!Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)))
78 {
79 message.getInterceptorChain().pause();
80 }
81 }
82
83 private void extractAndSet(Message message, MuleMessage muleMsg, String cxfHeader, String muleHeader)
84 {
85 Object val = message.get(cxfHeader);
86 if (val != null)
87 {
88 muleMsg.setOutboundProperty(muleHeader, val);
89 }
90 }
91
92 private void extractAndSetContentType(Message message, MuleMessage muleMsg)
93 {
94 String ct = (String) message.get(Message.CONTENT_TYPE);
95 if (ct != null)
96 {
97 String encoding = getEncoding(message);
98 if (ct.indexOf("charset") == -1)
99 {
100 ct = ct + "; charset=" + encoding;
101 }
102 muleMsg.setOutboundProperty(HttpConstants.HEADER_CONTENT_TYPE, ct);
103 }
104 }
105
106 private String getEncoding(Message message)
107 {
108 Exchange ex = message.getExchange();
109 String encoding = (String)message.get(Message.ENCODING);
110 if (encoding == null && ex.getInMessage() != null) {
111 encoding = (String) ex.getInMessage().get(Message.ENCODING);
112 message.put(Message.ENCODING, encoding);
113 }
114
115 if (encoding == null) {
116 encoding = "UTF-8";
117 message.put(Message.ENCODING, encoding);
118 }
119 return encoding;
120 }
121
122 private String format(List<String> value)
123 {
124 StringBuilder sb = new StringBuilder();
125 boolean first = true;
126
127 for (String s : value) {
128 if (!first)
129 {
130 sb.append(", ");
131 first = false;
132 }
133 else
134 {
135 first = false;
136 }
137
138 sb.append(s);
139 }
140 return sb.toString();
141 }
142 }
143
144