1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.ibeans.spi; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.MuleRuntimeException; |
14 | |
import org.mule.api.config.MuleProperties; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.module.ibeans.spi.support.DataTypeConverter; |
17 | |
import org.mule.transport.NullPayload; |
18 | |
import org.mule.transport.http.HttpConnector; |
19 | |
|
20 | |
import java.io.InputStream; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import javax.activation.DataHandler; |
24 | |
import javax.activation.MimeTypeParseException; |
25 | |
|
26 | |
import org.ibeans.api.DataType; |
27 | |
import org.ibeans.api.Response; |
28 | |
import org.ibeans.api.channel.MimeTypes; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class MuleResponseMessage implements Response |
34 | |
{ |
35 | |
private MuleMessage message; |
36 | |
private DataType dataType; |
37 | |
private String status; |
38 | |
|
39 | |
public MuleResponseMessage(MuleMessage message) throws MimeTypeParseException |
40 | 0 | { |
41 | 0 | this.message = message; |
42 | |
|
43 | 0 | if(message.getDataType()==null) |
44 | |
{ |
45 | |
|
46 | 0 | String mime = message.findPropertyInAnyScope(MuleProperties.CONTENT_TYPE_PROPERTY,null); |
47 | 0 | if (mime == null) |
48 | |
{ |
49 | |
|
50 | 0 | mime = message.findPropertyInAnyScope("ContentType", null); |
51 | |
} |
52 | 0 | if(mime==null) mime = MimeTypes.ANY.getBaseType(); |
53 | |
|
54 | 0 | dataType = org.ibeans.impl.support.datatype.DataTypeFactory.create(message.getPayload().getClass(), mime); |
55 | 0 | } |
56 | |
else |
57 | |
{ |
58 | 0 | dataType = DataTypeConverter.convertMuleToIBeans(message.getDataType()); |
59 | |
} |
60 | 0 | status = message.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY); |
61 | 0 | } |
62 | |
|
63 | |
public String getStatusCode() |
64 | |
{ |
65 | |
|
66 | 0 | return status; |
67 | |
} |
68 | |
|
69 | |
public void setStatusCode(String code) |
70 | |
{ |
71 | 0 | this.status = code; |
72 | 0 | } |
73 | |
|
74 | |
public String getMimeType() |
75 | |
{ |
76 | 0 | return dataType.getMimeType(); |
77 | |
} |
78 | |
|
79 | |
public DataType getDataType() |
80 | |
{ |
81 | 0 | return dataType; |
82 | |
} |
83 | |
|
84 | |
public InputStream getPayloadAsStream() |
85 | |
{ |
86 | 0 | if(message.getPayload() instanceof NullPayload) |
87 | |
{ |
88 | 0 | return null; |
89 | |
} |
90 | |
try |
91 | |
{ |
92 | 0 | return message.getPayload(InputStream.class); |
93 | |
} |
94 | 0 | catch (TransformerException e) |
95 | |
{ |
96 | 0 | throw new MuleRuntimeException(e); |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
public Object getPayload() |
101 | |
{ |
102 | 0 | if(message.getPayload() instanceof NullPayload) |
103 | |
{ |
104 | 0 | return null; |
105 | |
} |
106 | 0 | return message.getPayload(); |
107 | |
} |
108 | |
|
109 | |
public Object getHeader(String name) |
110 | |
{ |
111 | 0 | return message.getInboundProperty(name); |
112 | |
} |
113 | |
|
114 | |
public Set<String> getHeaderNames() |
115 | |
{ |
116 | 0 | return message.getInboundPropertyNames(); |
117 | |
} |
118 | |
|
119 | |
public DataHandler getAttachment(String name) |
120 | |
{ |
121 | 0 | return message.getInboundAttachment(name); |
122 | |
} |
123 | |
|
124 | |
public Set<String> getAttachmentNames() |
125 | |
{ |
126 | 0 | return message.getInboundAttachmentNames(); |
127 | |
} |
128 | |
|
129 | |
public MuleMessage getMessage() |
130 | |
{ |
131 | 0 | return message; |
132 | |
} |
133 | |
|
134 | |
public Throwable getException() |
135 | |
{ |
136 | 0 | if(message.getExceptionPayload()!=null) |
137 | |
{ |
138 | 0 | return message.getExceptionPayload().getRootException(); |
139 | |
} |
140 | 0 | return null; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
} |
145 | |
|