View Javadoc

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