Coverage Report - org.mule.module.cxf.builder.AbstractInboundMessageProcessorBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractInboundMessageProcessorBuilder
0%
0/146
0%
0/36
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 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.lifecycle.Callable;
 14  
 import org.mule.api.lifecycle.Disposable;
 15  
 import org.mule.api.lifecycle.Initialisable;
 16  
 import org.mule.api.processor.MessageProcessorBuilder;
 17  
 import org.mule.api.service.ServiceAware;
 18  
 import org.mule.module.cxf.CxfConfiguration;
 19  
 import org.mule.module.cxf.CxfConstants;
 20  
 import org.mule.module.cxf.CxfInboundMessageProcessor;
 21  
 import org.mule.module.cxf.MuleInvoker;
 22  
 import org.mule.module.cxf.support.CxfUtils;
 23  
 import org.mule.module.cxf.support.MuleHeadersInInterceptor;
 24  
 import org.mule.module.cxf.support.MuleHeadersOutInterceptor;
 25  
 import org.mule.module.cxf.support.MuleServiceConfiguration;
 26  
 import org.mule.util.ClassUtils;
 27  
 
 28  
 import java.util.Collection;
 29  
 import java.util.HashMap;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 import java.util.concurrent.CopyOnWriteArrayList;
 33  
 
 34  
 import org.apache.cxf.Bus;
 35  
 import org.apache.cxf.configuration.Configurer;
 36  
 import org.apache.cxf.endpoint.Server;
 37  
 import org.apache.cxf.feature.AbstractFeature;
 38  
 import org.apache.cxf.frontend.ServerFactoryBean;
 39  
 import org.apache.cxf.interceptor.AttachmentOutInterceptor;
 40  
 import org.apache.cxf.interceptor.Interceptor;
 41  
 import org.apache.cxf.interceptor.OneWayProcessorInterceptor;
 42  
 import org.apache.cxf.message.Message;
 43  
 import org.apache.cxf.service.factory.AbstractServiceConfiguration;
 44  
 import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
 45  
 import org.apache.cxf.service.invoker.Invoker;
 46  
 
 47  
 /**
 48  
  * An abstract builder for CXF services. It handles all common operations such
 49  
  * as interceptor configuration, mule header enabling, etc. Subclasses can extend
 50  
  * this and control how the Server is created and how the {@link CxfInboundMessageProcessor}
 51  
  * is configured.
 52  
  */
 53  0
 public abstract class AbstractInboundMessageProcessorBuilder implements MuleContextAware, MessageProcessorBuilder
 54  
 {
 55  
     private CxfConfiguration configuration;
 56  
     private Server server;
 57  0
     private boolean enableMuleSoapHeaders = true;
 58  
     private String wsdlLocation;
 59  
     private String bindingId;
 60  
     private String mtomEnabled;
 61  
     private String soapVersion;
 62  
     private String service;
 63  
     private String namespace;
 64  
     private List<AbstractFeature> features;
 65  0
     private List<Interceptor<? extends Message>> inInterceptors = new CopyOnWriteArrayList<Interceptor<? extends Message>>();
 66  0
     private List<Interceptor<? extends Message>> inFaultInterceptors = new CopyOnWriteArrayList<Interceptor<? extends Message>>();
 67  0
     private List<Interceptor<? extends Message>> outInterceptors = new CopyOnWriteArrayList<Interceptor<? extends Message>>();
 68  0
     private List<Interceptor<? extends Message>> outFaultInterceptors = new CopyOnWriteArrayList<Interceptor<? extends Message>>();
 69  
     protected MuleContext muleContext;
 70  
     private String port;
 71  0
     private Map<String,Object> properties = new HashMap<String, Object>();
 72  
     private boolean validationEnabled;
 73  
     private List<String> schemaLocations;
 74  0
     private String onException = CxfConstants.CREATE_SOAP_FAULT;
 75  
 
 76  
     public CxfInboundMessageProcessor build() throws MuleException
 77  
     {
 78  0
         if (muleContext == null)
 79  
         {
 80  0
             throw new IllegalStateException("MuleContext must be supplied.");
 81  
         }
 82  
 
 83  0
         if (configuration == null)
 84  
         {
 85  0
             configuration = CxfConfiguration.getConfiguration(muleContext);
 86  
         }
 87  
 
 88  0
         if (configuration == null)
 89  
         {
 90  0
             throw new IllegalStateException("A CxfConfiguration object must be supplied.");
 91  
         }
 92  
 
 93  
         ServerFactoryBean sfb;
 94  
         try
 95  
         {
 96  0
             sfb = createServerFactory();
 97  
         }
 98  0
         catch (Exception e)
 99  
         {
 100  0
             throw new DefaultMuleException(e);
 101  0
         }
 102  
 
 103  
         // The binding - i.e. SOAP, XML, HTTP Binding, etc
 104  0
         if (bindingId != null)
 105  
         {
 106  0
             sfb.setBindingId(bindingId);
 107  
         }
 108  
 
 109  0
         if (features != null)
 110  
         {
 111  0
             sfb.getFeatures().addAll(features);
 112  
         }
 113  
 
 114  0
         if (mtomEnabled != null)
 115  
         {
 116  0
             properties.put("mtom-enabled", mtomEnabled);
 117  0
             properties.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, true);
 118  
         }
 119  
 
 120  0
         if (inInterceptors != null)
 121  
         {
 122  0
             sfb.getInInterceptors().addAll((Collection<? extends Interceptor<? extends Message>>) inInterceptors);
 123  
         }
 124  
 
 125  
 
 126  0
         if (inFaultInterceptors != null)
 127  
         {
 128  0
             sfb.getInFaultInterceptors().addAll((Collection<? extends Interceptor<? extends Message>>) inFaultInterceptors);
 129  
         }
 130  
 
 131  0
         if (outInterceptors != null)
 132  
         {
 133  0
             sfb.getOutInterceptors().addAll((Collection<? extends Interceptor<? extends Message>>) outInterceptors);
 134  
         }
 135  
 
 136  0
         if (outFaultInterceptors != null)
 137  
         {
 138  0
             sfb.getOutFaultInterceptors().addAll((Collection<? extends Interceptor<? extends Message>>) outFaultInterceptors);
 139  
         }
 140  
 
 141  0
         if (enableMuleSoapHeaders && !configuration.isEnableMuleSoapHeaders())
 142  
         {
 143  0
             sfb.getInInterceptors().add(new MuleHeadersInInterceptor());
 144  0
             sfb.getInFaultInterceptors().add(new MuleHeadersInInterceptor());
 145  0
             sfb.getOutInterceptors().add(new MuleHeadersOutInterceptor());
 146  0
             sfb.getOutFaultInterceptors().add(new MuleHeadersOutInterceptor());
 147  
         }
 148  
 
 149  0
         String address = getAddress();
 150  0
         address = CxfUtils.mapUnsupportedSchemas(address);
 151  0
         sfb.setAddress(address); // dummy URL for CXF
 152  
 
 153  0
         if (wsdlLocation != null)
 154  
         {
 155  0
             sfb.setWsdlURL(wsdlLocation);
 156  
         }
 157  
 
 158  0
         sfb.setSchemaLocations(schemaLocations);
 159  
 
 160  0
         ReflectionServiceFactoryBean svcFac = sfb.getServiceFactory();
 161  0
         initServiceFactory(svcFac);
 162  
 
 163  0
         CxfInboundMessageProcessor processor = new CxfInboundMessageProcessor();
 164  0
         processor.setMuleContext(muleContext);
 165  0
         processor.setOnFaultInvokeStrategy(CxfConstants.INVOKE_EXCEPTION_STRATEGY.equals(onException));
 166  0
         configureMessageProcessor(sfb, processor);
 167  0
         sfb.setStart(false);
 168  
 
 169  0
         Bus bus = configuration.getCxfBus();
 170  0
         sfb.setBus(bus);
 171  0
         svcFac.setBus(bus);
 172  
 
 173  0
         Configurer configurer = bus.getExtension(Configurer.class);
 174  0
         if (null != configurer)
 175  
         {
 176  0
             configurer.configureBean(svcFac.getEndpointName().toString(), sfb);
 177  
         }
 178  
 
 179  0
         if (validationEnabled)
 180  
         {
 181  0
             properties.put("schema-validation-enabled", "true");
 182  
         }
 183  
 
 184  
         // If there's a soapVersion defined then the corresponding bindingId will be set
 185  0
         if(soapVersion != null)
 186  
         {
 187  0
             sfb.setBindingId(CxfUtils.getBindingIdForSoapVersion(soapVersion));
 188  
         }
 189  
         
 190  0
         sfb.setProperties(properties);
 191  0
         sfb.setInvoker(createInvoker(processor));
 192  
         
 193  0
         server = sfb.create();
 194  
         
 195  0
         CxfUtils.removeInterceptor(server.getEndpoint().getService().getInInterceptors(), OneWayProcessorInterceptor.class.getName());
 196  0
         configureServer(server);
 197  
 
 198  0
         processor.setBus(sfb.getBus());
 199  0
         processor.setServer(server);
 200  0
         processor.setProxy(isProxy());
 201  0
         return processor;
 202  
     }
 203  
     
 204  
     protected Invoker createInvoker(CxfInboundMessageProcessor processor)
 205  
     {
 206  0
         return new MuleInvoker(processor, getServiceClass());
 207  
     }
 208  
 
 209  
     protected void configureServer(Server server2)
 210  
     {
 211  0
     }
 212  
 
 213  
     protected abstract Class<?> getServiceClass();
 214  
 
 215  
     protected void configureMessageProcessor(ServerFactoryBean sfb, CxfInboundMessageProcessor processor)
 216  
     {
 217  0
     }
 218  
 
 219  
     protected abstract ServerFactoryBean createServerFactory() throws Exception;
 220  
 
 221  
     protected String getAddress()
 222  
     {
 223  0
         return "http://internalMuleCxfRegistry/" + hashCode();
 224  
     }
 225  
 
 226  
     /**
 227  
      * This method configures the {@link ReflectionServiceFactoryBean}.
 228  
      */
 229  
     private void initServiceFactory(ReflectionServiceFactoryBean svcFac)
 230  
     {
 231  0
         addIgnoredMethods(svcFac, Callable.class.getName());
 232  0
         addIgnoredMethods(svcFac, Initialisable.class.getName());
 233  0
         addIgnoredMethods(svcFac, Disposable.class.getName());
 234  0
         addIgnoredMethods(svcFac, ServiceAware.class.getName());
 235  
 
 236  0
         svcFac.getServiceConfigurations().add(0, new MuleServiceConfiguration(this));
 237  
 
 238  0
         svcFac.setServiceClass(getServiceClass());
 239  0
         for (AbstractServiceConfiguration c : svcFac.getServiceConfigurations())
 240  
         {
 241  0
             c.setServiceFactory(svcFac);
 242  
         }
 243  0
     }
 244  
 
 245  
     public void addIgnoredMethods(ReflectionServiceFactoryBean svcFac, String className)
 246  
     {
 247  
         try
 248  
         {
 249  0
             Class<?> c = ClassUtils.loadClass(className, getClass());
 250  0
             for (int i = 0; i < c.getMethods().length; i++)
 251  
             {
 252  0
                 svcFac.getIgnoredMethods().add(c.getMethods()[i]);
 253  
             }
 254  
         }
 255  0
         catch (ClassNotFoundException e)
 256  
         {
 257  
             // can be ignored.
 258  0
         }
 259  0
     }
 260  
 
 261  
 
 262  
     public Server getServer()
 263  
     {
 264  0
         return server;
 265  
     }
 266  
 
 267  
     public abstract boolean isProxy();
 268  
 
 269  
     public CxfConfiguration getConfiguration()
 270  
     {
 271  0
         return configuration;
 272  
     }
 273  
 
 274  
     public void setConfiguration(CxfConfiguration configuration)
 275  
     {
 276  0
         this.configuration = configuration;
 277  0
     }
 278  
 
 279  
     public boolean isEnableMuleSoapHeaders()
 280  
     {
 281  0
         return enableMuleSoapHeaders;
 282  
     }
 283  
 
 284  
     public void setEnableMuleSoapHeaders(boolean enableMuleSoapHeaders)
 285  
     {
 286  0
         this.enableMuleSoapHeaders = enableMuleSoapHeaders;
 287  0
     }
 288  
 
 289  
     public String getWsdlLocation()
 290  
     {
 291  0
         return wsdlLocation;
 292  
     }
 293  
 
 294  
     public void setWsdlLocation(String wsdlUrl)
 295  
     {
 296  0
         this.wsdlLocation = wsdlUrl;
 297  0
     }
 298  
 
 299  
     public String getBindingId()
 300  
     {
 301  0
         return bindingId;
 302  
     }
 303  
 
 304  
     public void setBindingId(String bindingId)
 305  
     {
 306  0
         this.bindingId = bindingId;
 307  0
     }
 308  
 
 309  
     public void setSoapVersion(String soapVersion)
 310  
     {
 311  0
         this.soapVersion = soapVersion;
 312  0
     }
 313  
 
 314  
     public String getSoapVersion()
 315  
     {
 316  0
         return soapVersion;
 317  
     }
 318  
 
 319  
     public String getMtomEnabled()
 320  
     {
 321  0
         return mtomEnabled;
 322  
     }
 323  
 
 324  
     public void setMtomEnabled(String mtomEnabled)
 325  
     {
 326  0
         this.mtomEnabled = mtomEnabled;
 327  0
     }
 328  
 
 329  
     public String getService()
 330  
     {
 331  0
         return service;
 332  
     }
 333  
 
 334  
     public void setService(String name)
 335  
     {
 336  0
         this.service = name;
 337  0
     }
 338  
 
 339  
     public String getNamespace()
 340  
     {
 341  0
         return namespace;
 342  
     }
 343  
 
 344  
     public void setNamespace(String namespace)
 345  
     {
 346  0
         this.namespace = namespace;
 347  0
     }
 348  
 
 349  
     public List<AbstractFeature> getFeatures()
 350  
     {
 351  0
         return features;
 352  
     }
 353  
 
 354  
     public void setFeatures(List<AbstractFeature> features)
 355  
     {
 356  0
         this.features = features;
 357  0
     }
 358  
 
 359  
     public List<Interceptor<? extends Message>> getInInterceptors()
 360  
     {
 361  0
         return inInterceptors;
 362  
     }
 363  
 
 364  
     public void setInInterceptors(List<Interceptor<? extends Message>> inInterceptors)
 365  
     {
 366  0
         this.inInterceptors = inInterceptors;
 367  0
     }
 368  
 
 369  
     public List<Interceptor<? extends Message>> getInFaultInterceptors()
 370  
     {
 371  0
         return inFaultInterceptors;
 372  
     }
 373  
 
 374  
     public void setInFaultInterceptors(List<Interceptor<? extends Message>> inFaultInterceptors)
 375  
     {
 376  0
         this.inFaultInterceptors = inFaultInterceptors;
 377  0
     }
 378  
 
 379  
     public List<Interceptor<? extends Message>> getOutInterceptors()
 380  
     {
 381  0
         return outInterceptors;
 382  
     }
 383  
 
 384  
     public void setOutInterceptors(List<Interceptor<? extends Message>> outInterceptors)
 385  
     {
 386  0
         this.outInterceptors = outInterceptors;
 387  0
     }
 388  
 
 389  
     public List<Interceptor<? extends Message>> getOutFaultInterceptors()
 390  
     {
 391  0
         return outFaultInterceptors;
 392  
     }
 393  
 
 394  
     public void setOutFaultInterceptors(List<Interceptor<? extends Message>> outFaultInterceptors)
 395  
     {
 396  0
         this.outFaultInterceptors = outFaultInterceptors;
 397  0
     }
 398  
     
 399  
     public void setMuleContext(MuleContext muleContext)
 400  
     {
 401  0
         this.muleContext = muleContext;
 402  0
     }
 403  
 
 404  
     public String getPort()
 405  
     {
 406  0
         return port;
 407  
     }
 408  
 
 409  
     public void setPort(String endpoint)
 410  
     {
 411  0
         this.port = endpoint;
 412  0
     }
 413  
 
 414  
     public Map<String, Object> getProperties()
 415  
     {
 416  0
         return properties;
 417  
     }
 418  
 
 419  
     public void setProperties(Map<String, Object> properties)
 420  
     {
 421  0
         this.properties = properties;
 422  0
     }
 423  
 
 424  
     public boolean isValidationEnabled()
 425  
     {
 426  0
         return validationEnabled;
 427  
     }
 428  
 
 429  
     public void setValidationEnabled(boolean validationEnabled)
 430  
     {
 431  0
         this.validationEnabled = validationEnabled;
 432  0
     }
 433  
 
 434  
     public List<String> getSchemaLocations()
 435  
     {
 436  0
         return schemaLocations;
 437  
     }
 438  
 
 439  
     public void setSchemaLocations(List<String> schemaLocations)
 440  
     {
 441  0
         this.schemaLocations = schemaLocations;
 442  0
     }
 443  
 
 444  
     public String getOnException()
 445  
     {
 446  0
         return this.onException;
 447  
     }
 448  
 
 449  
     public void setOnException(String onException)
 450  
     {
 451  0
         this.onException = onException;
 452  0
     }
 453  
     
 454  
 }