View Javadoc

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