View Javadoc

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