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