View Javadoc

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