View Javadoc
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.lifecycle.CreateException;
10  import org.mule.module.cxf.CxfConstants;
11  import org.mule.module.cxf.CxfOutboundMessageProcessor;
12  import org.mule.module.cxf.support.CopyAttachmentInInterceptor;
13  import org.mule.module.cxf.support.CopyAttachmentOutInterceptor;
14  import org.mule.module.cxf.support.CxfUtils;
15  import org.mule.module.cxf.support.OutputPayloadInterceptor;
16  import org.mule.module.cxf.support.ProxyService;
17  import org.mule.module.cxf.support.ResetStaxInterceptor;
18  import org.mule.module.cxf.support.ReversibleStaxInInterceptor;
19  import org.mule.module.cxf.support.StreamClosingInterceptor;
20  import org.mule.module.cxf.transport.MuleUniversalConduit;
21  
22  import org.apache.cxf.binding.Binding;
23  import org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor;
24  import org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor;
25  import org.apache.cxf.binding.soap.interceptor.Soap12FaultInInterceptor;
26  import org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor;
27  import org.apache.cxf.databinding.stax.StaxDataBinding;
28  import org.apache.cxf.databinding.stax.StaxDataBindingFeature;
29  import org.apache.cxf.endpoint.Client;
30  import org.apache.cxf.frontend.ClientProxy;
31  import org.apache.cxf.frontend.ClientProxyFactoryBean;
32  import org.apache.cxf.interceptor.WrappedOutInterceptor;
33  
34  /**
35   * Creates an outbound proxy based on a specially configure CXF Client.
36   * This allows you to send raw XML to your MessageProcessor and have it sent
37   * through CXF for SOAP processing, WS-Security, etc.
38   * <p>
39   * The input to the resulting MessageProcessor can be either a SOAP Body
40   * or a SOAP Envelope depending on how the payload attribute is configured.
41   * Valid values are "body" or "envelope". 
42   */
43  public class ProxyClientMessageProcessorBuilder extends AbstractOutboundMessageProcessorBuilder
44  {
45      private String payload;
46      
47      @Override
48      protected void configureClient(Client client)
49      {
50          MuleUniversalConduit conduit = (MuleUniversalConduit)client.getConduit();
51  
52          // add interceptors to handle Mule proxy specific stuff
53          client.getInInterceptors().add(new CopyAttachmentInInterceptor());
54          client.getInInterceptors().add(new StreamClosingInterceptor());
55          client.getOutInterceptors().add(new OutputPayloadInterceptor());
56          client.getOutInterceptors().add(new CopyAttachmentOutInterceptor());
57          
58          // Don't close the input because people need to be able to work with the live stream
59          conduit.setCloseInput(false);
60      }
61  
62      public boolean isProxyEnvelope()
63      {
64          return CxfConstants.PAYLOAD_ENVELOPE.equals(payload);
65      }
66      
67      @Override
68      protected void configureMessageProcessor(CxfOutboundMessageProcessor processor)
69      {
70          processor.setProxy(true);
71      }
72  
73      @Override
74      protected Client createClient() throws CreateException, Exception
75      {
76          ClientProxyFactoryBean cpf = new ClientProxyFactoryBean();
77          cpf.setServiceClass(ProxyService.class);
78          cpf.setDataBinding(new StaxDataBinding());
79          cpf.getFeatures().add(new StaxDataBindingFeature());
80          cpf.setAddress(getAddress());
81          cpf.setBus(getBus());
82          cpf.setProperties(properties);
83  
84          // If there's a soapVersion defined then the corresponding bindingId will be set
85          if(soapVersion != null)
86          {
87              cpf.setBindingId(CxfUtils.getBindingIdForSoapVersion(soapVersion));
88          }
89          
90          if (wsdlLocation != null) 
91          {
92              cpf.setWsdlLocation(wsdlLocation);
93          }
94          
95          Client client = ClientProxy.getClient(cpf.create());
96  
97          Binding binding = client.getEndpoint().getBinding();
98          CxfUtils.removeInterceptor(binding.getOutInterceptors(), WrappedOutInterceptor.class.getName());
99          CxfUtils.removeInterceptor(binding.getInInterceptors(), Soap11FaultInInterceptor.class.getName());
100         CxfUtils.removeInterceptor(binding.getInInterceptors(), Soap12FaultInInterceptor.class.getName());
101         CxfUtils.removeInterceptor(binding.getInInterceptors(), CheckFaultInterceptor.class.getName());
102 
103         if (isProxyEnvelope()) 
104         {
105             CxfUtils.removeInterceptor(binding.getOutInterceptors(), SoapOutInterceptor.class.getName());
106             client.getInInterceptors().add(new ReversibleStaxInInterceptor());
107             client.getInInterceptors().add(new ResetStaxInterceptor());
108         }
109         
110         return client;
111     }
112 
113     public String getPayload()
114     {
115         return payload;
116     }
117 
118     public void setPayload(String payload)
119     {
120         this.payload = payload;
121     }
122     
123 }