View Javadoc

1   /*
2    * $Id: ProxyServiceMessageProcessorBuilder.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.module.cxf.CxfConstants;
14  import org.mule.module.cxf.support.CopyAttachmentInInterceptor;
15  import org.mule.module.cxf.support.CopyAttachmentOutInterceptor;
16  import org.mule.module.cxf.support.CxfUtils;
17  import org.mule.module.cxf.support.OutputPayloadInterceptor;
18  import org.mule.module.cxf.support.ProxyService;
19  import org.mule.module.cxf.support.ProxyServiceFactoryBean;
20  import org.mule.module.cxf.support.ResetStaxInterceptor;
21  import org.mule.module.cxf.support.ReversibleStaxInInterceptor;
22  
23  import org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor;
24  import org.apache.cxf.databinding.stax.StaxDataBinding;
25  import org.apache.cxf.databinding.stax.StaxDataBindingFeature;
26  import org.apache.cxf.endpoint.Server;
27  import org.apache.cxf.frontend.ServerFactoryBean;
28  
29  /**
30   * Creates an inbound proxy based on a specially configure CXF Server.
31   * This allows you to send raw XML to your MessageProcessor and have it sent
32   * through CXF for SOAP processing, WS-Security, etc.
33   * <p>
34   * The input to the resulting MessageProcessor can be either a SOAP Body
35   * or a SOAP Envelope depending on how the payload attribute is configured.
36   * Valid values are "body" or "envelope". 
37   */
38  public class ProxyServiceMessageProcessorBuilder extends AbstractInboundMessageProcessorBuilder
39  {
40      private String payload;
41      
42      @Override
43      protected ServerFactoryBean createServerFactory() throws Exception
44      {
45          ServerFactoryBean sfb = new ServerFactoryBean();
46          sfb.setDataBinding(new StaxDataBinding());
47          sfb.getFeatures().add(new StaxDataBindingFeature());
48          sfb.setServiceFactory(new ProxyServiceFactoryBean());
49          sfb.setServiceClass(ProxyService.class);
50          
51          addProxyInterceptors(sfb);
52          
53          return sfb;
54      }
55  
56      @Override
57      protected Class<?> getServiceClass()
58      {
59          return ProxyService.class;
60      }
61  
62      @Override
63      protected void configureServer(Server server)
64      {
65          if (isProxyEnvelope()) 
66          {
67              CxfUtils.removeInterceptor(server.getEndpoint().getBinding().getOutInterceptors(), SoapOutInterceptor.class.getName());
68          }
69          
70      }
71  
72      @Override
73      public boolean isProxy()
74      {
75          return true;
76      }
77  
78      protected void addProxyInterceptors(ServerFactoryBean sfb)
79      {
80          sfb.getOutInterceptors().add(new OutputPayloadInterceptor());
81          sfb.getInInterceptors().add(new CopyAttachmentInInterceptor());
82          sfb.getOutInterceptors().add(new CopyAttachmentOutInterceptor());
83          
84          if (isProxyEnvelope()) 
85          {
86              sfb.getInInterceptors().add(new ReversibleStaxInInterceptor());
87              sfb.getInInterceptors().add(new ResetStaxInterceptor());
88          }
89      }
90  
91      public boolean isProxyEnvelope()
92      {
93          return CxfConstants.PAYLOAD_ENVELOPE.equals(payload);
94      }
95  
96      public String getPayload()
97      {
98          return payload;
99      }
100 
101     public void setPayload(String payload)
102     {
103         this.payload = payload;
104     }
105     
106 }