Monday, March 19, 2012

Spring-WS schema validation using PayloadValidatingInterceptor

PayloadValidatingInterceptor Configuration
when web service type is contract first development then one can use the Interceptor for validation the schema. Spring-WS supports PayloadValidatingInterceptor which is use for validate both request and response.
There are multiple ways to implement the schema validation.

  • Spring-WS schema Validation

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">

 < property name="interceptors">
< list>
< ref bean="validatingInterceptor"/ > 
< /list>
 < /property>
< /bean>

<bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
    <property name="schema" value="/jaxb/test.xsd"/>
    <property name="validateRequest" value="true"/>
    <property name="validateResponse" value="true"/>
</bean>


  • Customize schema validation


 <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
 < property name="interceptors">
 < list>
 < ref bean="validatingInterceptor"/ > 
 < /list>
 < /property>
 < /bean>

<bean id="validatingInterceptor" class="com.test.ValidationInterceptor ">
     <property name="schema" value="/jaxb/test.xsd"/>
</bean>


we can customize the the Spring configuration by extending the spring Interceptor class.

  • In customization you can use your own Validator
  • you can customize your Exception handling and SOAP Fault handling in schema Validation.

package com.test;

import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.log4j.Logger;
import org.springframework.core.io.ResourceLoader;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.client.SoapFaultClientException;
import org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor;
import org.xml.sax.SAXException;

public class ValidationInterceptor extends PayloadValidatingInterceptor{
private ResourceLoader resourceLoader;
private static final Logger log = Logger.getLogger(ValidationInterceptor.class);
protected Source getValidationRequestSource(WebServiceMessage request){
Source source =null;
try{
source =request.getPayloadSource();
validateSchema(source);
}catch(SoapFaultClientException soapException){
log.debug("SOAP Fault "+soapException.getMessage());
soapException.printStackTrace();
}catch(Exception ex){
log.debug("Exception "+ex.getMessage());
ex.printStackTrace();
}
return source;
}

private void validateSchema(Source source) throws Exception{
SchemaFactory schemaFactory = SchemaFactory.newInstance(getSchemaLanguage());
Schema schema = schemaFactory.newSchema(getSchemas()[0].getFile());
Validator validator = schema.newValidator();
DOMResult result = new DOMResult();
try {
validator.validate(source, result); 
} catch (SAXException ex) {
setFaultStringOrReason(ex.getLocalizedMessage());
}
}

public ResourceLoader getResourceLoader() {
return resourceLoader;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}

3 comments: