Re: Schema validation Axis 1.3

2006-11-10 Thread Davanum Srinivas
 XMLValidationException validationException
  */
 public boolean validateDocument(String xmlInput, URL url) throws
XMLValidationException {
 return validateDocument(new
ByteArrayInputStream(xmlInput.getBytes()), url);
 }

 /**
  * Validate an XML InputStream against a list of xsd's.
  * @param xmlInput xmlInput
  * @param url url
  * @return isValid
  * @throws XMLValidationException validationException
  */
 public boolean validateDocument(InputStream xmlInput, URL url) throws
XMLValidationException {

 boolean isValid = false;

 try {

 if (logger.isDebugEnabled()) {
 logger.debug("XSD URL = " + url);
 }

 if (url != null) {

this.getParser().setProperty(JAXP_SCHEMA_SOURCE,
url.toString());
 }

 Validator validator = new Validator();

 if (logger.isDebugEnabled()) {
 logger.debug("Attempting to validate schema");
 }

 this.getParser().parse(xmlInput, validator);

 isValid = !validator.isValidationError();

 if (logger.isDebugEnabled()) {
 if (isValid) {
 logger.debug("Validation passed");
 } else {
 logger.debug("Validation failed");
 }
 }

 if (!isValid) {
 logger.error("Schema validation failed ",
validator.getSaxParseException());
 throw new
XMLValidationException(validator.getSaxParseException().getMessage());
 }

 } catch (XMLValidationException ve) {
   throw ve;

 } catch (Throwable t) {

 logger.error("Error validating schema", t);
 throw new XMLValidationException(t.getMessage());
 }

 return isValid;
 }

 /**
  * Error handler used to log SAX Parser Errors.
  * @author I4Commerce
  *
  */
 private class Validator extends DefaultHandler {

 /**
  * validationError
  */
 private boolean validationError = false;

 /**
  * saxParseException
  */
 private SAXParseException saxParseException = null;

 /** (non-Javadoc)
  * @see
org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException)
  */
 public void error(SAXParseException exception) throws SAXException
{
 validationError = true;
 saxParseException = exception;
 }

 /** (non-Javadoc)
  * @see
org.xml.sax.helpers.DefaultHandler#fatalError(org.xml.sax.SAXParseException)
  */
 public void fatalError(SAXParseException exception) throws
SAXException {
 validationError = true;
 saxParseException = exception;
 }

 /** (non-Javadoc)
  * @see
org.xml.sax.helpers.DefaultHandler#warning(org.xml.sax.SAXParseException)
  */
 public void warning(SAXParseException exception) throws
SAXException {
 saxParseException = exception;
 }

 /**
  * Get validationError
  * @return the validationError
  */
 public boolean isValidationError() {
 return validationError;
 }

 /**
  * Get saxParseException
  * @return the saxParseException
  */
 public SAXParseException getSaxParseException() {
 return saxParseException;
 }

     /**
      * Set saxParseException
  * @param saxParseException the saxParseException to set
  */
 public void setSaxParseException(SAXParseException
saxParseException) {
 this.saxParseException = saxParseException;
 }
 }
 }


 -Original Message-
 From: Rodrigo Ruiz [mailto:[EMAIL PROTECTED]
 Sent: Fri 11/10/2006 9:06 AM
 To: axis-user@ws.apache.org
 Subject: Re: Schema validation Axis 1.3

 Hi John, how complex is your filter code? It would be a great entry in
 the Axis wiki ;-)

 Regards,
 Rodrigo Ruiz

 John Pfeifer wrote:
 >
 > Good luck with this one.  I have posted several times about schema
 > validation in axis2 and it looks like you have to do it yourself.  I
 > wrote a servlet filter that sits in front of the axis servlet and
 > validates the request against a given xsd.
 >
 --
---
 GRIDSYSTEMS S.A.   Rodrigo Ruiz
 Parc Bit - Son EspanyolR & D
 07120 Palma de Mallorcarruiz at gridsystems dot com
 Baleares - Espa�a  Tel: +34 971 435 085
 http://www.gridsystems.com/Fax: +34 971 435 082
---

-
 To unsubscribe, e-mail:
[EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]






--
Davanum Srinivas : http://www.wso2.net (Oxygen for Web Service Developers)


RE: Schema validation Axis 1.3

2006-11-10 Thread John Pfeifer
Title: RE: Schema validation Axis 1.3






Here is the filter... The trick with this is that you have to wrap the HttpServletRequest so that you can send the request on to filters down stream once you have read the input stream.  You can do this using HttpServletRequestWrapper.  I will be happy to post this somewhere, but I am not sure where to post it.

public class XMLValidationFilter extends GenericFilterBean {

    /**
 * The object that is used for logging.
 */
    private static Log log = LogFactory.getLog(XMLValidationFilter.class);

    /**
 * schemaMap
 */
    private Map schemaMap = null;
 
    /** (non-Javadoc)
 * @see javax.servlet.Filter#destroy()
 */
    public void destroy() {
    super.destroy();
    }
   
    /**
 * Initialize schemaMap from Spring Context
 */
    private void initSchemaMap() {
    if (schemaMap == null) {
    schemaMap = (Map) WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean("schemaToURIMap");
    }
    }
   
    /** (non-Javadoc)
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    final String methodName = "doFilter()";
    if (log.isTraceEnabled()) {
    log.trace(methodName + ": Entry");
    }
   
    HttpServletRequest httpRequest = ((HttpServletRequest) request);

    if (log.isDebugEnabled()) {
    log.debug("XMLValidationFilter: doFilter");
    log.debug("Schema Map Entries "+ schemaMap);
    }

    // set the schemaMap from the spring context, the
    // context is loaded after the filter is initialized but
    // before the first request can be handled
    initSchemaMap();
   
    if (schemaMap.get(httpRequest.getRequestURI()) == null) {
    if (log.isDebugEnabled()) {
    log.debug("No schema found for RequestURI " + httpRequest.getRequestURI());
    }
   
    // continue with rest of the filter chain as no further processing is required
    chain.doFilter(request, response);
    return;
    } else {
    if (log.isDebugEnabled()) {
    log.debug("Schema found for RequestURI " + httpRequest.getRequestURI());
    }
    }
   
    XMLValidationRequestWrapper requestWrapper =
    new XMLValidationRequestWrapper((HttpServletRequest) request);
    String queryString = httpRequest.getQueryString();
    if (log.isDebugEnabled()) {
    log.debug("queryString = " + queryString);
    }
    boolean isValid = true;
    if (!StringUtils.equalsIgnoreCase(queryString, "wsdl"))
    {
    isValid = validateIncomingXML(httpRequest, requestWrapper);
    }
    else
    {
    if (log.isInfoEnabled()) {
    log.info("Skipping XML Schema validation.");
    }
    }
   
    if (isValid) {
    if (log.isDebugEnabled()) {
    log.debug("Continue processing filter chain");
    }
   
    chain.doFilter(requestWrapper, response);
    } else {
    log.error("Schema validation failed");   
    generateSOAPFault(response.getOutputStream());
    }
   
    if (log.isTraceEnabled()) {
    log.trace(methodName + ": Exit");
    }
    }

    /**
 * validateIncomingXML
 * @param request
 * @param httpRequest
 * @param buffer
 * @param requestWrapper
 * @return
 */
    private boolean validateIncomingXML(HttpServletRequest httpRequest,
    XMLValidationRequestWrapper requestWrapper) {
    boolean isValid = false;
    StringBuffer buffer = new StringBuffer();
    InputStream xmlIn;
    String soapRequest;
    String xmlBody;
    try {
   
    xmlIn = httpRequest.getInputStream();
   
    int x = 0;
    while ((xmlIn != null) && ((x = xmlIn.read()) >= 0)) {
 buffer.append((char) x);
    }
   
    soapRequest = buffer.toString();
   
    if (log.isDebugEnabled()) {
    log.debug("Original SOAP Request = " + soapRequest);
    }
   
    xmlBody = SOAPUtils.getSOAPBody(buffer.toString());
   
    if (log.isDebugEnabled()) {
    log.debug("XML to be validated = " + xmlBody);
    }
   
    XMLSchemaValidator validator = new XMLSchemaValidator();
    URL url = "" schemaMap.get(httpRequest.getRequestURI()));

Re: Schema validation Axis 1.3

2006-11-10 Thread Rodrigo Ruiz
Hi John, how complex is your filter code? It would be a great entry in
the Axis wiki ;-)

Regards,
Rodrigo Ruiz

John Pfeifer wrote:
> 
> Good luck with this one.  I have posted several times about schema
> validation in axis2 and it looks like you have to do it yourself.  I
> wrote a servlet filter that sits in front of the axis servlet and
> validates the request against a given xsd. 
> 
-- 
---
GRIDSYSTEMS S.A.   Rodrigo Ruiz
Parc Bit - Son EspanyolR & D
07120 Palma de Mallorcarruiz at gridsystems dot com
Baleares - España  Tel: +34 971 435 085
http://www.gridsystems.com/Fax: +34 971 435 082
---

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Schema validation Axis 1.3

2006-11-10 Thread John Pfeifer
Title: RE: Schema validation Axis 1.3







Good luck with this one.  I have posted several times about schema validation in axis2 and it looks like you have to do it yourself.  I wrote a servlet filter that sits in front of the axis servlet and validates the request against a given xsd. 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Fri 11/10/2006 6:47 AM
To: axis-user@ws.apache.org
Subject: Schema validation Axis 1.3

Hi



I am using Axis 1.3, I wanted to know if there is a way to validate request
and response SOAP messages against schemas. I've googled for quite a bit,
but couldn't come out with a satisfactory solution.



So far I've investigated the Castor option, I think it is a kludge at best.
The other option that I am looking at is to use a custom handler to perform
the validation.



I just wanted to know if users have found this a problem and is there a
solution.



Thanks in advance,

Rishi




This message is intended for the addressee or its representative only.
Any form of unauthorized use, publication, reproduction, copying or
disclosure of the content of this e-mail is not permitted. If you are
not the intended recipient of this e-mail message and its contents,
please notify the sender immediately and delete this message and
all its attachments subsequently.