1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.ibeans.spi; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.transport.http.HttpConstants; |
14 | |
|
15 | |
import java.lang.reflect.Method; |
16 | |
import java.util.HashMap; |
17 | |
import java.util.List; |
18 | |
import java.util.Map; |
19 | |
import java.util.Set; |
20 | |
|
21 | |
import javax.activation.DataSource; |
22 | |
import javax.activation.MimeTypeParseException; |
23 | |
|
24 | |
import org.ibeans.api.DataType; |
25 | |
import org.ibeans.api.InvocationContext; |
26 | |
import org.ibeans.api.Response; |
27 | |
import org.ibeans.api.channel.MimeType; |
28 | |
import org.ibeans.impl.support.datatype.DataTypeFactory; |
29 | |
import org.ibeans.impl.test.MockIBean; |
30 | |
import org.ibeans.impl.test.MockMessageCallback; |
31 | |
import org.ibeans.spi.IBeansPlugin; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class MuleMockCallAnnotationHandler extends MuleCallAnnotationHandler implements MockIBean |
38 | |
{ |
39 | |
private Object mock; |
40 | |
private InvocationContext ctx; |
41 | |
private MockMessageCallback callback; |
42 | |
private IBeansPlugin plugin; |
43 | |
|
44 | |
public MuleMockCallAnnotationHandler(MuleContext muleContext, Object mock, IBeansPlugin plugin) |
45 | |
{ |
46 | 0 | super(muleContext); |
47 | 0 | this.mock = mock; |
48 | 0 | this.plugin = plugin; |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public Response invoke(InvocationContext ctx) throws Exception |
53 | |
{ |
54 | 0 | this.ctx = ctx; |
55 | 0 | Method method = ctx.getMethod(); |
56 | |
|
57 | |
|
58 | |
Object result; |
59 | 0 | if (method.getName().startsWith("ibean")) |
60 | |
{ |
61 | 0 | result = method.invoke(this, ctx.getArgs()); |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | result = ctx.getMethod().invoke(mock, ctx.getArgs()); |
66 | |
} |
67 | |
|
68 | 0 | Map<String, Object> props = new HashMap<String, Object>(); |
69 | 0 | props.put(HttpConstants.HEADER_CONTENT_TYPE, (ctx.getInvocationReturnType()==null ? |
70 | |
ctx.getReturnType().getMimeType() : ctx.getInvocationReturnType().getMimeType())); |
71 | 0 | Response response = (result instanceof Response ? (Response)result : plugin.createResponse(result, props, null)); |
72 | |
|
73 | |
|
74 | 0 | if (callback != null) |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | 0 | callback.onMessage(response); |
79 | |
} |
80 | |
finally |
81 | |
{ |
82 | |
|
83 | 0 | callback = null; |
84 | 0 | this.ctx = null; |
85 | 0 | } |
86 | |
|
87 | |
} |
88 | 0 | return response; |
89 | |
} |
90 | |
|
91 | |
public String getScheme(Method method) |
92 | |
{ |
93 | |
|
94 | 0 | return "http"; |
95 | |
} |
96 | |
|
97 | |
public void ibeanSetMimeType(MimeType mime) throws MimeTypeParseException |
98 | |
{ |
99 | 0 | if (ctx.getIBeanConfig().getReturnType() != null) |
100 | |
{ |
101 | 0 | ctx.setInvocationReturnType(DataTypeFactory.create(ctx.getIBeanConfig().getReturnType().getType(), mime)); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
public DataType ibeanReturnType() |
106 | |
{ |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
{ |
113 | 0 | DataType type = ctx.getIBeanConfig().getReturnType(); |
114 | 0 | if(type==null) |
115 | |
{ |
116 | 0 | type = ctx.getIBeanDefaultConfig().getReturnType(); |
117 | |
} |
118 | 0 | return type; |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
public Object ibeanUriParam(String name) |
123 | |
{ |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
{ |
130 | 0 | return ctx.getIBeanConfig().getUriParams().get(name); |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
public Object ibeanHeaderParam(String name) |
135 | |
{ |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
{ |
142 | 0 | return ctx.getIBeanConfig().getHeaderParams().get(name); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
public Object ibeanPropertyParam(String name) |
147 | |
{ |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
{ |
154 | 0 | return ctx.getIBeanConfig().getPropertyParams().get(name); |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
public Object ibeanPayloadParam(String name) |
159 | |
{ |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
{ |
166 | 0 | return ctx.getIBeanConfig().getPayloadParams().get(name); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
public List<Object> ibeanPayloads() |
171 | |
{ |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
{ |
178 | 0 | return ctx.getIBeanConfig().getPayloads(); |
179 | |
} |
180 | |
} |
181 | |
|
182 | |
public Set<DataSource> ibeanAttachments() |
183 | |
{ |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
{ |
190 | 0 | return ctx.getIBeanConfig().getAttachments(); |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
|
195 | |
public void ibeanSetMessageCallback(MockMessageCallback callback) |
196 | |
{ |
197 | 0 | this.callback = callback; |
198 | 0 | } |
199 | |
|
200 | |
} |