1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.http.transformers; |
12 | |
|
13 | |
import org.mule.config.MuleManifest; |
14 | |
import org.mule.config.MuleProperties; |
15 | |
import org.mule.providers.NullPayload; |
16 | |
import org.mule.providers.http.HttpConnector; |
17 | |
import org.mule.providers.http.HttpConstants; |
18 | |
import org.mule.providers.http.HttpResponse; |
19 | |
import org.mule.transformers.AbstractEventAwareTransformer; |
20 | |
import org.mule.transformers.simple.SerializableToByteArray; |
21 | |
import org.mule.umo.UMOEventContext; |
22 | |
import org.mule.umo.UMOMessage; |
23 | |
import org.mule.umo.transformer.TransformerException; |
24 | |
import org.mule.util.StringUtils; |
25 | |
|
26 | |
import java.io.ByteArrayInputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.InputStream; |
29 | |
import java.text.SimpleDateFormat; |
30 | |
import java.util.Collection; |
31 | |
import java.util.Date; |
32 | |
import java.util.Iterator; |
33 | |
import java.util.Locale; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
import org.apache.commons.httpclient.Header; |
37 | |
import org.apache.commons.httpclient.HttpVersion; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class UMOMessageToHttpResponse extends AbstractEventAwareTransformer |
45 | |
{ |
46 | |
public static final String CUSTOM_HEADER_PREFIX = ""; |
47 | |
|
48 | |
|
49 | |
private SimpleDateFormat format; |
50 | |
private String server; |
51 | |
private SerializableToByteArray serializableToByteArray; |
52 | |
|
53 | |
public UMOMessageToHttpResponse() |
54 | 124 | { |
55 | 126 | registerSourceType(Object.class); |
56 | 124 | setReturnClass(Object.class); |
57 | |
|
58 | 124 | format = new SimpleDateFormat(HttpConstants.DATE_FORMAT, Locale.US); |
59 | |
|
60 | |
|
61 | |
|
62 | 124 | String productName = MuleManifest.getProductName(); |
63 | 124 | if (productName == null) |
64 | |
{ |
65 | 0 | server = "Mule/SNAPSHOT"; |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 124 | server = productName + "/" + MuleManifest.getProductVersion(); |
70 | |
} |
71 | |
|
72 | 124 | serializableToByteArray = new SerializableToByteArray(); |
73 | 124 | } |
74 | |
|
75 | |
public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException |
76 | |
{ |
77 | |
|
78 | 160 | if (context.getMessage().getExceptionPayload() != null) |
79 | |
{ |
80 | |
|
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 160 | if (src instanceof NullPayload) |
87 | |
{ |
88 | 6 | src = StringUtils.EMPTY; |
89 | |
} |
90 | |
|
91 | |
try |
92 | |
{ |
93 | |
HttpResponse response; |
94 | 160 | if (src instanceof HttpResponse) |
95 | |
{ |
96 | 8 | response = (HttpResponse)src; |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 152 | response = createResponse(src, encoding, context); |
101 | |
} |
102 | |
|
103 | |
|
104 | 160 | if (!response.containsHeader(HttpConstants.HEADER_CONTENT_TYPE)) |
105 | |
{ |
106 | 4 | response.addHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, |
107 | |
HttpConstants.DEFAULT_CONTENT_TYPE)); |
108 | |
} |
109 | |
|
110 | |
|
111 | 160 | if (!response.containsHeader(HttpConstants.HEADER_CONTENT_LENGTH) |
112 | |
&& !response.containsHeader(HttpConstants.HEADER_TRANSFER_ENCODING)) |
113 | |
{ |
114 | 18 | InputStream content = response.getBody(); |
115 | 18 | if (content != null) |
116 | |
{ |
117 | 14 | long len = response.getContentLength(); |
118 | 14 | if (len < 0) |
119 | |
{ |
120 | 14 | if (response.getHttpVersion().lessEquals(HttpVersion.HTTP_1_0)) |
121 | |
{ |
122 | 0 | throw new IOException("Chunked encoding not supported for HTTP version " |
123 | |
+ response.getHttpVersion()); |
124 | |
} |
125 | 14 | Header header = new Header(HttpConstants.HEADER_TRANSFER_ENCODING, "chunked"); |
126 | 14 | response.addHeader(header); |
127 | 14 | } |
128 | |
else |
129 | |
{ |
130 | 0 | Header header = new Header(HttpConstants.HEADER_CONTENT_LENGTH, Long.toString(len)); |
131 | 0 | response.setHeader(header); |
132 | |
} |
133 | 14 | } |
134 | |
else |
135 | |
{ |
136 | 4 | Header header = new Header(HttpConstants.HEADER_CONTENT_LENGTH, "0"); |
137 | 4 | response.addHeader(header); |
138 | |
} |
139 | |
} |
140 | |
|
141 | 160 | UMOMessage msg = context.getMessage(); |
142 | |
|
143 | 160 | if (!response.containsHeader(HttpConstants.HEADER_CONNECTION)) |
144 | |
{ |
145 | |
|
146 | 132 | String connHeader = msg.getStringProperty(HttpConstants.HEADER_CONNECTION, null); |
147 | 132 | if (connHeader != null) |
148 | |
{ |
149 | 0 | if (connHeader.equalsIgnoreCase("keep-alive")) |
150 | |
{ |
151 | 0 | Header header = new Header(HttpConstants.HEADER_CONNECTION, "keep-alive"); |
152 | 0 | response.addHeader(header); |
153 | 0 | response.setKeepAlive(true); |
154 | |
} |
155 | 0 | if (connHeader.equalsIgnoreCase("close")) |
156 | |
{ |
157 | 0 | Header header = new Header(HttpConstants.HEADER_CONNECTION, "close"); |
158 | 0 | response.addHeader(header); |
159 | 0 | response.setKeepAlive(false); |
160 | 0 | } |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | |
|
165 | 132 | if (response.getHttpVersion().greaterEquals(HttpVersion.HTTP_1_1)) |
166 | |
{ |
167 | 132 | response.setKeepAlive(true); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | response.setKeepAlive(false); |
172 | |
} |
173 | |
} |
174 | |
} |
175 | 160 | if ("HEAD".equalsIgnoreCase(msg.getStringProperty(HttpConnector.HTTP_METHOD_PROPERTY, null))) |
176 | |
{ |
177 | |
|
178 | 0 | response.setBody(null); |
179 | |
} |
180 | 160 | return response; |
181 | |
} |
182 | 0 | catch (IOException e) |
183 | |
{ |
184 | 0 | throw new TransformerException(this, e); |
185 | |
} |
186 | |
|
187 | |
} |
188 | |
|
189 | |
protected HttpResponse createResponse(Object src, String encoding, UMOEventContext context) |
190 | |
throws IOException, TransformerException |
191 | |
{ |
192 | 152 | HttpResponse response = new HttpResponse(); |
193 | 152 | UMOMessage msg = context.getMessage(); |
194 | |
|
195 | 152 | int status = msg.getIntProperty(HttpConnector.HTTP_STATUS_PROPERTY, HttpConstants.SC_OK); |
196 | 152 | String version = msg.getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, HttpConstants.HTTP11); |
197 | |
|
198 | |
String date; |
199 | 152 | synchronized (format) |
200 | |
{ |
201 | 152 | date = format.format(new Date()); |
202 | 152 | } |
203 | |
|
204 | 152 | String contentType = msg.getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, |
205 | |
HttpConstants.DEFAULT_CONTENT_TYPE); |
206 | |
|
207 | 152 | response.setStatusLine(HttpVersion.parse(version), status); |
208 | 152 | response.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType)); |
209 | 152 | response.setHeader(new Header(HttpConstants.HEADER_DATE, date)); |
210 | 152 | response.setHeader(new Header(HttpConstants.HEADER_SERVER, server)); |
211 | 152 | if (msg.getProperty(HttpConstants.HEADER_EXPIRES) == null) |
212 | |
{ |
213 | 136 | response.setHeader(new Header(HttpConstants.HEADER_EXPIRES, date)); |
214 | |
} |
215 | 152 | response.setFallbackCharset(encoding); |
216 | |
|
217 | 152 | Collection headerNames = HttpConstants.RESPONSE_HEADER_NAMES.values(); |
218 | |
String headerName, value; |
219 | 152 | for (Iterator iterator = headerNames.iterator(); iterator.hasNext();) |
220 | |
{ |
221 | 4104 | headerName = (String)iterator.next(); |
222 | 4104 | value = msg.getStringProperty(headerName, null); |
223 | 4104 | if (value != null) |
224 | |
{ |
225 | 78 | response.setHeader(new Header(headerName, value)); |
226 | |
} |
227 | |
} |
228 | |
|
229 | |
|
230 | 152 | Map customHeaders = (Map)msg.getProperty(HttpConnector.HTTP_CUSTOM_HEADERS_MAP_PROPERTY); |
231 | 152 | if (customHeaders != null) |
232 | |
{ |
233 | |
Map.Entry entry; |
234 | 0 | for (Iterator iterator = customHeaders.entrySet().iterator(); iterator.hasNext();) |
235 | |
{ |
236 | 0 | entry = (Map.Entry)iterator.next(); |
237 | 0 | if (entry.getValue() != null) |
238 | |
{ |
239 | 0 | response.setHeader(new Header(entry.getKey().toString(), entry.getValue().toString())); |
240 | |
} |
241 | |
} |
242 | |
} |
243 | |
|
244 | |
|
245 | 152 | String user = msg.getStringProperty(MuleProperties.MULE_USER_PROPERTY, null); |
246 | 152 | if (user != null) |
247 | |
{ |
248 | 0 | response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_USER_PROPERTY, user)); |
249 | |
} |
250 | 152 | if (msg.getCorrelationId() != null) |
251 | |
{ |
252 | 16 | response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_CORRELATION_ID_PROPERTY, |
253 | |
msg.getCorrelationId())); |
254 | 16 | response.setHeader(new Header(CUSTOM_HEADER_PREFIX |
255 | |
+ MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, |
256 | |
String.valueOf(msg.getCorrelationGroupSize()))); |
257 | 16 | response.setHeader(new Header(CUSTOM_HEADER_PREFIX |
258 | |
+ MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, |
259 | |
String.valueOf(msg.getCorrelationSequence()))); |
260 | |
} |
261 | 152 | if (msg.getReplyTo() != null) |
262 | |
{ |
263 | 0 | response.setHeader(new Header(CUSTOM_HEADER_PREFIX + MuleProperties.MULE_REPLY_TO_PROPERTY, |
264 | |
msg.getReplyTo().toString())); |
265 | |
} |
266 | 152 | if (src instanceof InputStream) |
267 | |
{ |
268 | 0 | response.setBody((InputStream)src); |
269 | |
} |
270 | 152 | else if (src instanceof String) |
271 | |
{ |
272 | 136 | response.setBodyString(src.toString()); |
273 | |
} |
274 | |
else |
275 | |
{ |
276 | 16 | response.setBody(new ByteArrayInputStream((byte[])serializableToByteArray.doTransform(src, |
277 | |
encoding))); |
278 | |
} |
279 | 152 | return response; |
280 | |
} |
281 | |
|
282 | |
public boolean isAcceptNull() |
283 | |
{ |
284 | 4 | return true; |
285 | |
} |
286 | |
} |