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