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.support;
8   
9   import javax.xml.stream.XMLInputFactory;
10  import javax.xml.stream.XMLOutputFactory;
11  
12  import org.apache.cxf.Bus;
13  import org.apache.cxf.endpoint.Client;
14  import org.apache.cxf.endpoint.Server;
15  import org.apache.cxf.feature.AbstractFeature;
16  import org.apache.cxf.interceptor.Fault;
17  import org.apache.cxf.message.Message;
18  import org.apache.cxf.phase.AbstractPhaseInterceptor;
19  import org.apache.cxf.phase.Phase;
20  import org.apache.cxf.service.Service;
21  
22  /**
23   * Configures the StAX XMLInputFactory and XMLOutputFactory which CXF uses.
24   * 
25   */
26  public class StaxFeature extends AbstractFeature {
27      private String xmlInputFactory;
28      private String xmlOutputFactory;
29      
30      @Override
31      public void initialize(Client client, Bus bus) {
32          Service service = client.getEndpoint().getService();
33          
34          setProperties(service);
35      }
36  
37      private void setProperties(Service service) {
38          if (xmlInputFactory != null) {
39              service.put(XMLInputFactory.class.getName(), xmlInputFactory);
40          }
41          
42          if (xmlOutputFactory != null) {
43              service.put(XMLOutputFactory.class.getName(), xmlOutputFactory);
44          }
45      }
46  
47      @Override
48      public void initialize(Server server, Bus bus) {
49          Service service = server.getEndpoint().getService();
50          
51          setProperties(service);
52      }
53  
54      @Override
55      public void initialize(Bus bus) {
56          AbstractPhaseInterceptor<Message> in = new AbstractPhaseInterceptor<Message>(Phase.RECEIVE) {
57              public void handleMessage(Message message) throws Fault {
58                  if (xmlInputFactory != null) {
59                      message.put(XMLInputFactory.class.getName(), xmlInputFactory);
60                  }
61              }
62          };
63          
64          bus.getInInterceptors().add(in);
65          bus.getInFaultInterceptors().add(in);
66          
67          AbstractPhaseInterceptor<Message> out = new AbstractPhaseInterceptor<Message>(Phase.SETUP) {
68              public void handleMessage(Message message) throws Fault {
69                  if (xmlOutputFactory != null) {
70                      message.put(XMLOutputFactory.class.getName(), xmlOutputFactory);
71                  }
72              }
73          };
74          
75          bus.getOutInterceptors().add(out);
76          bus.getOutFaultInterceptors().add(out);
77      }
78  
79      public String getXmlInputFactory() {
80          return xmlInputFactory;
81      }
82  
83      public void setXmlInputFactory(String xmlInputFactory) {
84          this.xmlInputFactory = xmlInputFactory;
85      }
86  
87      public String getXmlOutputFactory() {
88          return xmlOutputFactory;
89      }
90  
91      public void setXmlOutputFactory(String xmlOutputFactory) {
92          this.xmlOutputFactory = xmlOutputFactory;
93      }
94  
95  }