1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jersey; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.transport.OutputHandler; |
12 | |
import org.mule.module.jersey.JerseyResourcesComponent; |
13 | |
import org.mule.transport.http.HttpConnector; |
14 | |
|
15 | |
import com.sun.jersey.spi.container.ContainerResponse; |
16 | |
import com.sun.jersey.spi.container.ContainerResponseWriter; |
17 | |
|
18 | |
import java.io.ByteArrayOutputStream; |
19 | |
import java.io.IOException; |
20 | |
import java.io.OutputStream; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | 0 | public class MuleResponseWriter implements ContainerResponseWriter |
26 | |
{ |
27 | |
|
28 | |
private ByteArrayOutputStream out; |
29 | |
private OutputHandler output; |
30 | |
private final MuleMessage message; |
31 | |
|
32 | |
public MuleResponseWriter(MuleMessage request) |
33 | |
{ |
34 | 0 | super(); |
35 | 0 | this.message = request; |
36 | 0 | this.out = new ByteArrayOutputStream(); |
37 | 0 | this.output = new OutputHandler() |
38 | 0 | { |
39 | |
|
40 | |
public void write(MuleEvent arg0, OutputStream realOut) throws IOException |
41 | |
{ |
42 | 0 | realOut.write(out.toByteArray()); |
43 | 0 | realOut.flush(); |
44 | 0 | } |
45 | |
|
46 | |
}; |
47 | 0 | } |
48 | |
|
49 | |
public OutputStream writeStatusAndHeaders(long x, ContainerResponse response) throws IOException |
50 | |
{ |
51 | 0 | Map<String, String> customHeaders = new HashMap<String, String>(); |
52 | 0 | for (Map.Entry<String, List<Object>> e : response.getHttpHeaders().entrySet()) |
53 | |
{ |
54 | |
|
55 | 0 | message.setOutboundProperty(e.getKey(), getHeaderValue(e.getValue())); |
56 | |
} |
57 | |
|
58 | 0 | message.setInvocationProperty(JerseyResourcesComponent.JERSEY_RESPONSE, response); |
59 | 0 | message.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, response.getStatus()); |
60 | |
|
61 | 0 | return out; |
62 | |
} |
63 | |
|
64 | |
private String getHeaderValue(List<Object> values) |
65 | |
{ |
66 | 0 | StringBuilder sb = new StringBuilder(); |
67 | 0 | boolean first = true; |
68 | 0 | for (Object o : values) |
69 | |
{ |
70 | 0 | if (!first) |
71 | |
{ |
72 | 0 | sb.append(", "); |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | first = false; |
77 | |
} |
78 | |
|
79 | 0 | sb.append(ContainerResponse.getHeaderValue(o)); |
80 | |
} |
81 | 0 | return sb.toString(); |
82 | |
} |
83 | |
|
84 | |
public void finish() throws IOException |
85 | |
{ |
86 | 0 | } |
87 | |
|
88 | |
public OutputHandler getResponse() |
89 | |
{ |
90 | 0 | return output; |
91 | |
} |
92 | |
} |