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.module.cxf.CxfConstants;
10  import org.mule.module.cxf.support.CopyAttachmentInInterceptor;
11  import org.mule.module.cxf.support.CopyAttachmentOutInterceptor;
12  import org.mule.module.cxf.support.CxfUtils;
13  import org.mule.module.cxf.support.OutputPayloadInterceptor;
14  import org.mule.module.cxf.support.ProxySchemaValidationInInterceptor;
15  import org.mule.module.cxf.support.ProxyService;
16  import org.mule.module.cxf.support.ProxyServiceFactoryBean;
17  import org.mule.module.cxf.support.ResetStaxInterceptor;
18  import org.mule.module.cxf.support.ReversibleStaxInInterceptor;
19  import org.mule.module.cxf.support.ReversibleValidatingInterceptor;
20  
21  import org.apache.cxf.binding.soap.interceptor.RPCOutInterceptor;
22  import org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor;
23  import org.apache.cxf.databinding.stax.StaxDataBinding;
24  import org.apache.cxf.databinding.stax.StaxDataBindingFeature;
25  import org.apache.cxf.endpoint.Server;
26  import org.apache.cxf.frontend.ServerFactoryBean;
27  import org.apache.cxf.interceptor.BareOutInterceptor;
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          // RPCOutInterceptor adds an operation node to the response, so if it's present replace if by a BareOutInterceptor
71          if(CxfUtils.removeInterceptor(server.getEndpoint().getBinding().getOutInterceptors(), RPCOutInterceptor.class.getName()))
72          {
73              server.getEndpoint().getBinding().getOutInterceptors().add(new BareOutInterceptor());
74          }
75  
76          if (isValidationEnabled())
77          {
78              server.getEndpoint().getInInterceptors().add(new ProxySchemaValidationInInterceptor(getConfiguration().getCxfBus(), 
79                 server.getEndpoint().getService().getServiceInfos().get(0)));
80          }
81      }
82  
83      @Override
84      public boolean isProxy()
85      {
86          return true;
87      }
88  
89      protected void addProxyInterceptors(ServerFactoryBean sfb)
90      {
91          sfb.getOutInterceptors().add(new OutputPayloadInterceptor());
92          sfb.getInInterceptors().add(new CopyAttachmentInInterceptor());
93          sfb.getOutInterceptors().add(new CopyAttachmentOutInterceptor());
94          
95          if (isProxyEnvelope()) 
96          {
97              sfb.getInInterceptors().add(new ReversibleStaxInInterceptor());
98              sfb.getInInterceptors().add(new ResetStaxInterceptor());
99          }
100         /* Even if the payload is body, if validation is enabled, then we need to use a ReversibleXMLStreamReader to
101          * avoid the message from being consumed during schema validation.
102          */
103         else if(isValidationEnabled())
104         {
105             sfb.getInInterceptors().add(new ReversibleValidatingInterceptor());
106             sfb.getInInterceptors().add(new ResetStaxInterceptor());
107         }
108     }
109 
110     public boolean isProxyEnvelope()
111     {
112         return CxfConstants.PAYLOAD_ENVELOPE.equals(payload);
113     }
114 
115     public String getPayload()
116     {
117         return payload;
118     }
119 
120     public void setPayload(String payload)
121     {
122         this.payload = payload;
123     }
124     
125 }