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 org.mule.module.xml.stax.ReversibleXMLStreamReader;
10  
11  import java.util.logging.Logger;
12  
13  import javax.xml.stream.XMLStreamException;
14  import javax.xml.stream.XMLStreamReader;
15  
16  import org.apache.cxf.Bus;
17  import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
18  import org.apache.cxf.binding.soap.interceptor.StartBodyInterceptor;
19  import org.apache.cxf.common.logging.LogUtils;
20  import org.apache.cxf.interceptor.Fault;
21  import org.apache.cxf.io.StaxValidationManager;
22  import org.apache.cxf.message.Message;
23  import org.apache.cxf.phase.AbstractPhaseInterceptor;
24  import org.apache.cxf.phase.Phase;
25  import org.apache.cxf.service.model.ServiceInfo;
26  
27  public class ProxySchemaValidationInInterceptor extends AbstractPhaseInterceptor<Message> {
28      private static final Logger LOG = LogUtils.getL7dLogger(ProxySchemaValidationInInterceptor.class);
29      
30      private ServiceInfo service;
31      private Bus bus;
32      
33      public ProxySchemaValidationInInterceptor(Bus bus, ServiceInfo service) {
34          super(Phase.READ);
35          this.bus = bus;
36          this.service = service;
37          addBefore(StartBodyInterceptor.class.getName());
38          addAfter(ReadHeadersInterceptor.class.getName());
39      }
40  
41  
42      public void handleMessage(Message message) throws Fault {
43          XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
44          
45          // if we're in proxy envelope mode, find the underlying stream reader before performing validation
46          if (xmlReader instanceof ReversibleXMLStreamReader) {
47              xmlReader = ((ReversibleXMLStreamReader) xmlReader).getDelegateReader();
48          }
49          
50          try {
51              setSchemaInMessage(message, xmlReader);
52          } catch (XMLStreamException e) {
53              throw new Fault(new org.apache.cxf.common.i18n.Message("SCHEMA_ERROR", LOG), 
54                              e);
55          }
56      }
57      
58      private void setSchemaInMessage(Message message, XMLStreamReader reader) throws XMLStreamException  {
59          Object en = message.getContextualProperty(org.apache.cxf.message.Message.SCHEMA_VALIDATION_ENABLED);
60          if (Boolean.TRUE.equals(en) || "true".equals(en)) {
61              StaxValidationManager mgr = bus.getExtension(StaxValidationManager.class);
62              if (mgr != null) {
63                  mgr.setupValidation(reader, service);
64              }
65          }
66      }
67  }