View Javadoc

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