1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf.builder; |
8 | |
|
9 | |
import org.mule.api.DefaultMuleException; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.context.MuleContextAware; |
13 | |
import org.mule.api.endpoint.EndpointBuilder; |
14 | |
import org.mule.api.lifecycle.CreateException; |
15 | |
import org.mule.api.processor.MessageProcessor; |
16 | |
import org.mule.api.processor.MessageProcessorBuilder; |
17 | |
import org.mule.construct.SimpleFlowConstruct; |
18 | |
import org.mule.module.cxf.CxfConfiguration; |
19 | |
import org.mule.module.cxf.CxfInboundMessageProcessor; |
20 | |
import org.mule.module.cxf.CxfOutboundMessageProcessor; |
21 | |
import org.mule.module.cxf.CxfPayloadToArguments; |
22 | |
import org.mule.module.cxf.support.MuleHeadersInInterceptor; |
23 | |
import org.mule.module.cxf.support.MuleHeadersOutInterceptor; |
24 | |
|
25 | |
import java.lang.reflect.Method; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collection; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import org.apache.cxf.Bus; |
32 | |
import org.apache.cxf.BusFactory; |
33 | |
import org.apache.cxf.databinding.DataBinding; |
34 | |
import org.apache.cxf.endpoint.Client; |
35 | |
import org.apache.cxf.feature.AbstractFeature; |
36 | |
import org.apache.cxf.interceptor.Interceptor; |
37 | |
import org.apache.cxf.message.Message; |
38 | |
|
39 | 0 | public abstract class AbstractOutboundMessageProcessorBuilder |
40 | |
implements MessageProcessorBuilder, MuleContextAware |
41 | |
{ |
42 | |
protected Client client; |
43 | |
protected String defaultMethodName; |
44 | |
protected Method defaultMethod; |
45 | |
|
46 | |
protected CxfConfiguration configuration; |
47 | |
protected List<Interceptor<? extends Message>> inInterceptors; |
48 | |
protected List<Interceptor<? extends Message>> inFaultInterceptors; |
49 | |
protected List<Interceptor<? extends Message>> outInterceptors; |
50 | |
protected List<Interceptor<? extends Message>> outFaultInterceptors; |
51 | |
protected DataBinding databinding; |
52 | |
protected List<AbstractFeature> features; |
53 | |
protected String wsdlLocation; |
54 | |
protected boolean mtomEnabled; |
55 | |
protected String soapVersion; |
56 | 0 | protected boolean enableMuleSoapHeaders = true; |
57 | 0 | protected CxfPayloadToArguments payloadToArguments = CxfPayloadToArguments.NULL_PAYLOAD_AS_PARAMETER; |
58 | |
protected Map<String,Object> properties; |
59 | |
protected MuleContext muleContext; |
60 | |
protected String address; |
61 | |
protected String operation; |
62 | |
protected String decoupledEndpoint; |
63 | |
|
64 | |
@SuppressWarnings("unchecked") |
65 | |
public CxfOutboundMessageProcessor build() throws MuleException |
66 | |
{ |
67 | 0 | if (muleContext == null) |
68 | |
{ |
69 | 0 | throw new IllegalStateException("MuleContext must be supplied."); |
70 | |
} |
71 | |
|
72 | 0 | if (configuration == null) |
73 | |
{ |
74 | 0 | configuration = CxfConfiguration.getConfiguration(muleContext); |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | 0 | BusFactory.setThreadDefaultBus(getBus()); |
80 | |
|
81 | |
try |
82 | |
{ |
83 | 0 | client = createClient(); |
84 | |
} |
85 | 0 | catch (Exception e) |
86 | |
{ |
87 | 0 | throw new DefaultMuleException(e); |
88 | 0 | } |
89 | |
|
90 | 0 | addInterceptors(client.getInInterceptors(), inInterceptors); |
91 | 0 | addInterceptors(client.getInFaultInterceptors(), inFaultInterceptors); |
92 | 0 | addInterceptors(client.getOutInterceptors(), outInterceptors); |
93 | 0 | addInterceptors(client.getOutFaultInterceptors(), outFaultInterceptors); |
94 | |
|
95 | 0 | client.setThreadLocalRequestContext(true); |
96 | |
|
97 | 0 | configureClient(client); |
98 | |
|
99 | 0 | if (features != null) |
100 | |
{ |
101 | 0 | for (AbstractFeature f : features) |
102 | |
{ |
103 | 0 | f.initialize(client, getBus()); |
104 | |
} |
105 | |
} |
106 | |
|
107 | 0 | if (mtomEnabled) |
108 | |
{ |
109 | 0 | client.getEndpoint().put(Message.MTOM_ENABLED, mtomEnabled); |
110 | |
} |
111 | |
|
112 | 0 | addMuleInterceptors(); |
113 | |
|
114 | 0 | CxfOutboundMessageProcessor processor = createMessageProcessor(); |
115 | 0 | processor.setOperation(operation); |
116 | 0 | configureMessageProcessor(processor); |
117 | 0 | processor.setPayloadToArguments(payloadToArguments); |
118 | |
|
119 | 0 | if (decoupledEndpoint != null) |
120 | |
{ |
121 | 0 | processor.setDecoupledEndpoint(decoupledEndpoint); |
122 | |
|
123 | 0 | CxfInboundMessageProcessor cxfInboundMP = new CxfInboundMessageProcessor(); |
124 | 0 | cxfInboundMP.setMuleContext(muleContext); |
125 | 0 | cxfInboundMP.setBus(getBus()); |
126 | |
|
127 | 0 | List<MessageProcessor> mps = new ArrayList<MessageProcessor>(); |
128 | 0 | mps.add(cxfInboundMP); |
129 | |
|
130 | 0 | EndpointBuilder ep = muleContext.getEndpointFactory().getEndpointBuilder(decoupledEndpoint); |
131 | |
|
132 | 0 | SimpleFlowConstruct flow = new SimpleFlowConstruct("decoupled-" + ep.toString(), muleContext); |
133 | 0 | flow.setMessageProcessors(mps); |
134 | 0 | flow.setMessageSource(ep.buildInboundEndpoint()); |
135 | 0 | muleContext.getRegistry().registerObject(flow.getName(), flow); |
136 | |
} |
137 | |
|
138 | 0 | return processor; |
139 | |
} |
140 | |
|
141 | |
protected CxfOutboundMessageProcessor createMessageProcessor() |
142 | |
{ |
143 | 0 | CxfOutboundMessageProcessor processor = new CxfOutboundMessageProcessor(client); |
144 | 0 | processor.setMuleContext(muleContext); |
145 | 0 | return processor; |
146 | |
} |
147 | |
|
148 | |
protected void configureMessageProcessor(CxfOutboundMessageProcessor processor) |
149 | |
{ |
150 | 0 | } |
151 | |
|
152 | |
protected void configureClient(Client client) |
153 | |
{ |
154 | 0 | } |
155 | |
|
156 | |
protected Bus getBus() |
157 | |
{ |
158 | 0 | return configuration.getCxfBus(); |
159 | |
} |
160 | |
|
161 | |
protected abstract Client createClient() throws CreateException, Exception; |
162 | |
|
163 | |
public Client getClient() |
164 | |
{ |
165 | 0 | return client; |
166 | |
} |
167 | |
|
168 | |
@SuppressWarnings("unchecked") |
169 | |
private void addInterceptors(List<Interceptor<? extends Message>> col, List<Interceptor<? extends Message>> supplied) |
170 | |
{ |
171 | 0 | if (supplied != null) |
172 | |
{ |
173 | 0 | col.addAll((Collection<? extends Interceptor<? extends Message>>) supplied); |
174 | |
} |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
protected String getAddress() |
179 | |
{ |
180 | 0 | if (address == null) |
181 | |
{ |
182 | |
|
183 | 0 | return "http://host"; |
184 | |
} |
185 | 0 | return address; |
186 | |
} |
187 | |
|
188 | |
public void setAddress(String address) |
189 | |
{ |
190 | 0 | this.address = address; |
191 | 0 | } |
192 | |
|
193 | |
protected void createClientFromLocalServer() throws Exception |
194 | |
{ |
195 | 0 | } |
196 | |
|
197 | |
protected void addMuleInterceptors() |
198 | |
{ |
199 | |
|
200 | 0 | if (enableMuleSoapHeaders && !configuration.isEnableMuleSoapHeaders()) |
201 | |
{ |
202 | 0 | client.getInInterceptors().add(new MuleHeadersInInterceptor()); |
203 | 0 | client.getInFaultInterceptors().add(new MuleHeadersInInterceptor()); |
204 | 0 | client.getOutInterceptors().add(new MuleHeadersOutInterceptor()); |
205 | 0 | client.getOutFaultInterceptors().add(new MuleHeadersOutInterceptor()); |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
public String getOperation() |
210 | |
{ |
211 | 0 | return operation; |
212 | |
} |
213 | |
|
214 | |
public void setOperation(String operation) |
215 | |
{ |
216 | 0 | this.operation = operation; |
217 | 0 | } |
218 | |
|
219 | |
public DataBinding getDatabinding() |
220 | |
{ |
221 | 0 | return databinding; |
222 | |
} |
223 | |
|
224 | |
public void setDatabinding(DataBinding databinding) |
225 | |
{ |
226 | 0 | this.databinding = databinding; |
227 | 0 | } |
228 | |
|
229 | |
public boolean isMtomEnabled() |
230 | |
{ |
231 | 0 | return mtomEnabled; |
232 | |
} |
233 | |
|
234 | |
public void setMtomEnabled(boolean mtomEnabled) |
235 | |
{ |
236 | 0 | this.mtomEnabled = mtomEnabled; |
237 | 0 | } |
238 | |
|
239 | |
public void setSoapVersion(String soapVersion) |
240 | |
{ |
241 | 0 | this.soapVersion = soapVersion; |
242 | 0 | } |
243 | |
|
244 | |
public String getSoapVersion() |
245 | |
{ |
246 | 0 | return soapVersion; |
247 | |
} |
248 | |
|
249 | |
public List<Interceptor<? extends Message>> getInInterceptors() |
250 | |
{ |
251 | 0 | return inInterceptors; |
252 | |
} |
253 | |
|
254 | |
public void setInInterceptors(List<Interceptor<? extends Message>> inInterceptors) |
255 | |
{ |
256 | 0 | this.inInterceptors = inInterceptors; |
257 | 0 | } |
258 | |
|
259 | |
public List<Interceptor<? extends Message>> getInFaultInterceptors() |
260 | |
{ |
261 | 0 | return inFaultInterceptors; |
262 | |
} |
263 | |
|
264 | |
public void setInFaultInterceptors(List<Interceptor<? extends Message>> inFaultInterceptors) |
265 | |
{ |
266 | 0 | this.inFaultInterceptors = inFaultInterceptors; |
267 | 0 | } |
268 | |
|
269 | |
public List<Interceptor<? extends Message>> getOutInterceptors() |
270 | |
{ |
271 | 0 | return outInterceptors; |
272 | |
} |
273 | |
|
274 | |
public void setOutInterceptors(List<Interceptor<? extends Message>> outInterceptors) |
275 | |
{ |
276 | 0 | this.outInterceptors = outInterceptors; |
277 | 0 | } |
278 | |
|
279 | |
public List<Interceptor<? extends Message>> getOutFaultInterceptors() |
280 | |
{ |
281 | 0 | return outFaultInterceptors; |
282 | |
} |
283 | |
|
284 | |
public void setOutFaultInterceptors(List<Interceptor<? extends Message>> outFaultInterceptors) |
285 | |
{ |
286 | 0 | this.outFaultInterceptors = outFaultInterceptors; |
287 | 0 | } |
288 | |
|
289 | |
public List<AbstractFeature> getFeatures() |
290 | |
{ |
291 | 0 | return features; |
292 | |
} |
293 | |
|
294 | |
public void setFeatures(List<AbstractFeature> features) |
295 | |
{ |
296 | 0 | this.features = features; |
297 | 0 | } |
298 | |
|
299 | |
public String getWsdlLocation() |
300 | |
{ |
301 | 0 | return wsdlLocation; |
302 | |
} |
303 | |
|
304 | |
public void setWsdlLocation(String wsdlLocation) |
305 | |
{ |
306 | 0 | this.wsdlLocation = wsdlLocation; |
307 | 0 | } |
308 | |
|
309 | |
public CxfConfiguration getConfiguration() |
310 | |
{ |
311 | 0 | return configuration; |
312 | |
} |
313 | |
|
314 | |
public void setConfiguration(CxfConfiguration configuration) |
315 | |
{ |
316 | 0 | this.configuration = configuration; |
317 | 0 | } |
318 | |
|
319 | |
public boolean isEnableMuleSoapHeaders() |
320 | |
{ |
321 | 0 | return enableMuleSoapHeaders; |
322 | |
} |
323 | |
|
324 | |
public void setEnableMuleSoapHeaders(boolean enableMuleSoapHeaders) |
325 | |
{ |
326 | 0 | this.enableMuleSoapHeaders = enableMuleSoapHeaders; |
327 | 0 | } |
328 | |
|
329 | |
public CxfPayloadToArguments getPayloadToArguments() |
330 | |
{ |
331 | 0 | return payloadToArguments; |
332 | |
} |
333 | |
|
334 | |
public void setPayloadToArguments(CxfPayloadToArguments payloadToArguments) |
335 | |
{ |
336 | 0 | this.payloadToArguments = payloadToArguments; |
337 | 0 | } |
338 | |
|
339 | |
public Map<String, Object> getProperties() |
340 | |
{ |
341 | 0 | return properties; |
342 | |
} |
343 | |
|
344 | |
public void setProperties(Map<String, Object> properties) |
345 | |
{ |
346 | 0 | this.properties = properties; |
347 | 0 | } |
348 | |
|
349 | |
public String getDecoupledEndpoint() |
350 | |
{ |
351 | 0 | return decoupledEndpoint; |
352 | |
} |
353 | |
|
354 | |
public void setDecoupledEndpoint(String decoupledEndpoint) |
355 | |
{ |
356 | 0 | this.decoupledEndpoint = decoupledEndpoint; |
357 | 0 | } |
358 | |
|
359 | |
public void setMuleContext(MuleContext context) |
360 | |
{ |
361 | 0 | muleContext = context; |
362 | 0 | } |
363 | |
|
364 | |
} |