【问题标题】:How to add more than one schema in PayloadValidatingInterceptor Springs?如何在 PayloadValidatingInterceptor Springs 中添加多个模式?
【发布时间】:2015-07-15 09:50:08
【问题描述】:

我想在 payloadValidatingInterceptor 中添加 2 个 XSD 文件进行验证,我可以在属性模式下添加一个 XSD 文件,有没有一种方法可以添加 2 个或更多 XSD 文件进行验证。

【问题讨论】:

    标签: java spring-security spring-ws springsource


    【解决方案1】:

    假设两个 xsd 文件具有不同的命名空间,您可以执行以下操作:

    1) 扩展类 PayloadValidatingInterceptor

        public class PayloadValidatingInterceptor extends PayloadValidatingInterceptor {
    
        private ResourceLoader resourceLoader;
    
        private static final Logger LOGGER = Logger.getLogger(this.getClass());
    
        @Override
        protected final Source getValidationRequestSource(WebServiceMessage webServiceMessage_) {
            Source _source = null;
            try {
                _source = webServiceMessage_.getPayloadSource();
                validateSchema(_source);
              } catch (SoapFaultClientException _soapException) {
                  LOGGER.error("SOAP Fault " + _soapException.getMessage(), _soapException);              
              } catch (Exception _exception) {
                  LOGGER.error("Exception " + _exception.getMessage(), _exception);
              }
    
          return _source;  
        }
    
        private String getNamespace(final Source source_) {
            DOMSource _currentdomSource = (DOMSource) source_;
            Node _currentNode = _currentdomSource.getNode();
    
            if (_currentNode != null) {
                String _currentNamespaceUri = _currentNode.getNamespaceURI();
    
                // do whatever you need to identifiy your namespace
    
                } else {
                    logger.warn("the namespace is not recognized: " + _currentNamespaceUri);
                }
             } else {
                 logger.warn("The current namespace could not be determined");
             }
    
            return null;
        }
    
    
        private void validateSchema(Source source_) throws IOException, SAXException {
            final String _namespace = getNamespace(source_);
    
            if (_namespace != null) {
                SchemaFactory _schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema _schema = null;
    
                // based on the identified namespace you can choose the appropriate xsd
                // for example : _schema = _schemaFactory.newSchema(getSchemas()[0].getFile()); 
    
                Validator _validator = _schema.newValidator();
                DOMResult _result = new DOMResult();
                try {
                    _validator.validate(source_, _result); 
                } catch (SAXException _exception) {
                    setFaultStringOrReason(GENERIC_ERROR_MESSAGE + " : " + _exception.getLocalizedMessage());               
                }
            } else {
                setFaultStringOrReason(NAMESPACE_ERROR_MESSAGE);
            }
        }
    
        public final ResourceLoader getResourceLoader() {
            return resourceLoader;
        }
    
        public final void setResourceLoader(ResourceLoader resourceLoader_) {
            this.resourceLoader = resourceLoader_;
        }
    
    }
    

    2) 通过添加以下bean来修改spring配置

    <bean class="PayloadValidatingInterceptor" >
    <property name="schemas">
    <array>
    <value>/WEB-INF/1.xsd</value>
    <value>/WEB-INF/2.xsd</value>
    </array>
    </property>
    <property name="addValidationErrorDetail" value="false"/>
    <property name="validateRequest" value="true"/>
    <property name="validateResponse" value="false"/>
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2021-07-05
      • 2017-03-18
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      相关资源
      最近更新 更多