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