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