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