View Javadoc

1   /*
2    * $Id: ProxyClientMessageProcessorBuilder.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.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 (wsdlLocation != null) 
89          {
90              cpf.setWsdlLocation(wsdlLocation);
91          }
92          
93          Client client = ClientProxy.getClient(cpf.create());
94  
95          Binding binding = client.getEndpoint().getBinding();
96          CxfUtils.removeInterceptor(binding.getOutInterceptors(), WrappedOutInterceptor.class.getName());
97          CxfUtils.removeInterceptor(binding.getInInterceptors(), Soap11FaultInInterceptor.class.getName());
98          CxfUtils.removeInterceptor(binding.getInInterceptors(), Soap12FaultInInterceptor.class.getName());
99          CxfUtils.removeInterceptor(binding.getInInterceptors(), CheckFaultInterceptor.class.getName());
100 
101         if (isProxyEnvelope()) 
102         {
103             CxfUtils.removeInterceptor(binding.getOutInterceptors(), SoapOutInterceptor.class.getName());
104             client.getInInterceptors().add(new ReversibleStaxInInterceptor());
105             client.getInInterceptors().add(new ResetStaxInterceptor());
106         }
107         
108         return client;
109     }
110 
111     public String getPayload()
112     {
113         return payload;
114     }
115 
116     public void setPayload(String payload)
117     {
118         this.payload = payload;
119     }
120     
121 }