View Javadoc

1   /*
2    * $Id: AbstractOutboundMessageProcessorBuilder.java 19191 2010-08-25 21:05:23Z tcarlson $
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.lifecycle.CreateException;
18  import org.mule.api.processor.MessageProcessorBuilder;
19  import org.mule.module.cxf.CxfConfiguration;
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  import org.mule.module.cxf.support.MuleProtocolHeadersOutInterceptor;
25  
26  import java.lang.reflect.Method;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.cxf.Bus;
31  import org.apache.cxf.databinding.DataBinding;
32  import org.apache.cxf.endpoint.Client;
33  import org.apache.cxf.feature.AbstractFeature;
34  import org.apache.cxf.interceptor.Interceptor;
35  import org.apache.cxf.message.Message;
36  
37  public abstract class AbstractOutboundMessageProcessorBuilder 
38      implements MessageProcessorBuilder, MuleContextAware
39  {
40      protected Client client;
41      protected String defaultMethodName;
42      protected Method defaultMethod;
43  
44      protected CxfConfiguration configuration;
45      protected List<Interceptor> inInterceptors;
46      protected List<Interceptor> inFaultInterceptors;
47      protected List<Interceptor> outInterceptors;
48      protected List<Interceptor> outFaultInterceptors;
49      protected DataBinding databinding;
50      protected List<AbstractFeature> features;
51      protected String wsdlLocation;
52      protected boolean mtomEnabled;
53      protected boolean enableMuleSoapHeaders = true;
54      protected CxfPayloadToArguments payloadToArguments = CxfPayloadToArguments.NULL_PAYLOAD_AS_PARAMETER;
55      protected Map<String,Object> properties;
56      protected MuleContext muleContext;
57      protected String address;
58      protected String operation;
59      
60      @SuppressWarnings("unchecked")
61      public CxfOutboundMessageProcessor build() throws MuleException
62      {
63          if (muleContext == null) 
64          {
65              throw new IllegalStateException("MuleContext must be supplied.");
66          }
67          
68          if (configuration == null)
69          {
70              configuration = CxfConfiguration.getConfiguration(muleContext);
71          }
72          
73          try
74          {
75              client = createClient();
76          }
77          catch (Exception e)
78          {
79              throw new DefaultMuleException(e);
80          }
81          
82          addInterceptors(client.getInInterceptors(), inInterceptors);
83          addInterceptors(client.getInFaultInterceptors(), inFaultInterceptors);
84          addInterceptors(client.getOutInterceptors(), outInterceptors);
85          addInterceptors(client.getOutFaultInterceptors(), outFaultInterceptors);
86  
87          client.setThreadLocalRequestContext(true);
88          
89          configureClient(client);
90          
91          if (features != null)
92          {
93              for (AbstractFeature f : features)
94              {
95                  f.initialize(client, getBus());
96              }
97          }
98  
99          if (mtomEnabled)
100         {
101             client.getEndpoint().put(Message.MTOM_ENABLED, mtomEnabled);
102         }
103         
104         addMuleInterceptors();
105         
106         CxfOutboundMessageProcessor processor = createMessageProcessor();
107         processor.setOperation(operation);
108         configureMessageProcessor(processor);
109         processor.setPayloadToArguments(payloadToArguments);
110         return processor;
111     }
112 
113     protected CxfOutboundMessageProcessor createMessageProcessor()
114     {
115         CxfOutboundMessageProcessor processor = new CxfOutboundMessageProcessor(client);
116         return processor;
117     }
118 
119     protected void configureMessageProcessor(CxfOutboundMessageProcessor processor)
120     {
121     }
122 
123     protected void configureClient(Client client)
124     {
125     }
126 
127     protected Bus getBus()
128     {
129         return configuration.getCxfBus();
130     }
131 
132     protected abstract Client createClient() throws CreateException, Exception;
133 
134     public Client getClient()
135     {
136         return client;
137     }
138 
139     @SuppressWarnings("unchecked")
140     private void addInterceptors(List<Interceptor> col, List<Interceptor> supplied)
141     {
142         if (supplied != null) 
143         {
144             col.addAll(supplied);
145         }
146     }
147     
148     
149     protected String getAddress()
150     {
151         if (address == null) 
152         {
153             // dummy URL for client builder
154             return "http://host";
155         }
156         return address;
157     }
158 
159     public void setAddress(String address)
160     {
161         this.address = address;
162     }
163 
164     protected void createClientFromLocalServer() throws Exception
165     {
166     }
167 
168     protected void addMuleInterceptors()
169     {
170         if (enableMuleSoapHeaders)
171         {
172             client.getInInterceptors().add(new MuleHeadersInInterceptor());
173             client.getInFaultInterceptors().add(new MuleHeadersInInterceptor());
174             client.getOutInterceptors().add(new MuleHeadersOutInterceptor());
175             client.getOutFaultInterceptors().add(new MuleHeadersOutInterceptor());
176         }
177         client.getOutInterceptors().add(new MuleProtocolHeadersOutInterceptor());
178         client.getOutFaultInterceptors().add(new MuleProtocolHeadersOutInterceptor());
179     }
180     
181     public String getOperation()
182     {
183         return operation;
184     }
185 
186     public void setOperation(String operation)
187     {
188         this.operation = operation;
189     }
190 
191     public DataBinding getDatabinding()
192     {
193         return databinding;
194     }
195 
196     public void setDatabinding(DataBinding databinding)
197     {
198         this.databinding = databinding;
199     }
200 
201     public boolean isMtomEnabled()
202     {
203         return mtomEnabled;
204     }
205 
206     public void setMtomEnabled(boolean mtomEnabled)
207     {
208         this.mtomEnabled = mtomEnabled;
209     }
210     
211     public List<Interceptor> getInInterceptors()
212     {
213         return inInterceptors;
214     }
215 
216     public void setInInterceptors(List<Interceptor> inInterceptors)
217     {
218         this.inInterceptors = inInterceptors;
219     }
220 
221     public List<Interceptor> getInFaultInterceptors()
222     {
223         return inFaultInterceptors;
224     }
225 
226     public void setInFaultInterceptors(List<Interceptor> inFaultInterceptors)
227     {
228         this.inFaultInterceptors = inFaultInterceptors;
229     }
230 
231     public List<Interceptor> getOutInterceptors()
232     {
233         return outInterceptors;
234     }
235 
236     public void setOutInterceptors(List<Interceptor> outInterceptors)
237     {
238         this.outInterceptors = outInterceptors;
239     }
240 
241     public List<Interceptor> getOutFaultInterceptors()
242     {
243         return outFaultInterceptors;
244     }
245 
246     public void setOutFaultInterceptors(List<Interceptor> outFaultInterceptors)
247     {
248         this.outFaultInterceptors = outFaultInterceptors;
249     }
250 
251     public List<AbstractFeature> getFeatures()
252     {
253         return features;
254     }
255 
256     public void setFeatures(List<AbstractFeature> features)
257     {
258         this.features = features;
259     }
260     
261     public String getWsdlLocation()
262     {
263         return wsdlLocation;
264     }
265 
266     public void setWsdlLocation(String wsdlLocation)
267     {
268         this.wsdlLocation = wsdlLocation;
269     }
270     
271     public CxfConfiguration getConfiguration()
272     {
273         return configuration;
274     }
275 
276     public void setConfiguration(CxfConfiguration configuration)
277     {
278         this.configuration = configuration;
279     }
280 
281     public boolean isEnableMuleSoapHeaders()
282     {
283         return enableMuleSoapHeaders;
284     }
285 
286     public void setEnableMuleSoapHeaders(boolean enableMuleSoapHeaders)
287     {
288         this.enableMuleSoapHeaders = enableMuleSoapHeaders;
289     }
290 
291     public CxfPayloadToArguments getPayloadToArguments()
292     {
293         return payloadToArguments;
294     }
295 
296     public void setPayloadToArguments(CxfPayloadToArguments payloadToArguments)
297     {
298         this.payloadToArguments = payloadToArguments;
299     }
300     
301     public Map<String, Object> getProperties()
302     {
303         return properties;
304     }
305 
306     public void setProperties(Map<String, Object> properties)
307     {
308         this.properties = properties;
309     }
310 
311     public void setMuleContext(MuleContext context)
312     {
313         muleContext = context;
314     }
315     
316 }