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 super(muleContext);
47 this.mock = mock;
48 this.plugin = plugin;
49 }
50
51 @Override
52 public Response invoke(InvocationContext ctx) throws Exception
53 {
54 this.ctx = ctx;
55 Method method = ctx.getMethod();
56
57
58 Object result;
59 if (method.getName().startsWith("ibean"))
60 {
61 result = method.invoke(this, ctx.getArgs());
62 }
63 else
64 {
65 result = ctx.getMethod().invoke(mock, ctx.getArgs());
66 }
67
68 Map<String, Object> props = new HashMap<String, Object>();
69 props.put(HttpConstants.HEADER_CONTENT_TYPE, (ctx.getInvocationReturnType()==null ?
70 ctx.getReturnType().getMimeType() : ctx.getInvocationReturnType().getMimeType()));
71 Response response = (result instanceof Response ? (Response)result : plugin.createResponse(result, props, null));
72
73
74 if (callback != null)
75 {
76 try
77 {
78 callback.onMessage(response);
79 }
80 finally
81 {
82
83 callback = null;
84 this.ctx = null;
85 }
86
87 }
88 return response;
89 }
90
91 public String getScheme(Method method)
92 {
93
94 return "http";
95 }
96
97 public void ibeanSetMimeType(MimeType mime) throws MimeTypeParseException
98 {
99 if (ctx.getIBeanConfig().getReturnType() != null)
100 {
101 ctx.setInvocationReturnType(DataTypeFactory.create(ctx.getIBeanConfig().getReturnType().getType(), mime));
102 }
103 }
104
105 public DataType ibeanReturnType()
106 {
107
108
109
110
111
112 {
113 DataType type = ctx.getIBeanConfig().getReturnType();
114 if(type==null)
115 {
116 type = ctx.getIBeanDefaultConfig().getReturnType();
117 }
118 return type;
119 }
120 }
121
122 public Object ibeanUriParam(String name)
123 {
124
125
126
127
128
129 {
130 return ctx.getIBeanConfig().getUriParams().get(name);
131 }
132 }
133
134 public Object ibeanHeaderParam(String name)
135 {
136
137
138
139
140
141 {
142 return ctx.getIBeanConfig().getHeaderParams().get(name);
143 }
144 }
145
146 public Object ibeanPropertyParam(String name)
147 {
148
149
150
151
152
153 {
154 return ctx.getIBeanConfig().getPropertyParams().get(name);
155 }
156 }
157
158 public Object ibeanPayloadParam(String name)
159 {
160
161
162
163
164
165 {
166 return ctx.getIBeanConfig().getPayloadParams().get(name);
167 }
168 }
169
170 public List<Object> ibeanPayloads()
171 {
172
173
174
175
176
177 {
178 return ctx.getIBeanConfig().getPayloads();
179 }
180 }
181
182 public Set<DataSource> ibeanAttachments()
183 {
184
185
186
187
188
189 {
190 return ctx.getIBeanConfig().getAttachments();
191 }
192 }
193
194
195 public void ibeanSetMessageCallback(MockMessageCallback callback)
196 {
197 this.callback = callback;
198 }
199
200 }