Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2011 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.server.endpoint.interceptor;
 
 
 
Abstract base class for EndpointInterceptor implementations that validate part of the message using a schema. The exact message part is determined by the getValidationRequestSource and getValidationResponseSource template methods.

By default, only the request message is validated, but this behaviour can be changed using the validateRequest and validateResponse properties.

 
 public abstract class AbstractValidatingInterceptor extends TransformerObjectSupport
         implements EndpointInterceptorInitializingBean {
 
 
     private Resource[] schemas;
 
     private boolean validateRequest = true;
 
     private boolean validateResponse = false;
 
     private XmlValidator validator;
 
 
     public String getSchemaLanguage() {
         return ;
     }

    
Sets the schema language. Default is the W3C XML Schema: http://www.w3.org/2001/XMLSchema".

 
     public void setSchemaLanguage(String schemaLanguage) {
         this. = schemaLanguage;
     }

    
Returns the schema resources to use for validation.
 
     public Resource[] getSchemas() {
         return ;
     }

    
Sets the schema resource to use for validation. Setting this property, setXsdSchemaCollection(org.springframework.xml.xsd.XsdSchemaCollection), schema, or setSchemas(org.springframework.core.io.Resource[]) is required.
 
     public void setSchema(Resource schema) {
         setSchemas(new Resource[]{schema});
     }

    
Sets the schema resources to use for validation. Setting this property, setXsdSchemaCollection(org.springframework.xml.xsd.XsdSchemaCollection), schema, or setSchemas(org.springframework.core.io.Resource[]) is required.
    public void setSchemas(Resource[] schemas) {
        Assert.notEmpty(schemas"schemas must not be empty or null");
        for (Resource schema : schemas) {
            Assert.notNull(schema"schema must not be null");
            Assert.isTrue(schema.exists(), "schema \"" + schema + "\" does not exit");
        }
        this. = schemas;
    }

    
Sets the org.springframework.xml.xsd.XsdSchema to use for validation. Setting this property, setXsdSchemaCollection(org.springframework.xml.xsd.XsdSchemaCollection), schema, or setSchemas(org.springframework.core.io.Resource[]) is required.

Parameters:
schema the xsd schema to use
Throws:
java.io.IOException in case of I/O errors
    public void setXsdSchema(XsdSchema schemathrows IOException {
        this. = schema.createValidator();
    }

    
Sets the org.springframework.xml.xsd.XsdSchemaCollection to use for validation. Setting this property, setXsdSchema(org.springframework.xml.xsd.XsdSchema), schema, or schemas is required.

Parameters:
schemaCollection the xsd schema collection to use
Throws:
java.io.IOException in case of I/O errors
    public void setXsdSchemaCollection(XsdSchemaCollection schemaCollectionthrows IOException {
        this. = schemaCollection.createValidator();
    }

    
Sets the error handler to use for validation. If not set, a default error handler will be used.

Parameters:
errorHandler the error handler.
    public void setErrorHandler(ValidationErrorHandler errorHandler) {
        this. = errorHandler;
    }

    
Indicates whether the request should be validated against the schema. Default is true.
    public void setValidateRequest(boolean validateRequest) {
        this. = validateRequest;
    }

    
Indicates whether the response should be validated against the schema. Default is false.
    public void setValidateResponse(boolean validateResponse) {
        this. = validateResponse;
    }
    public void afterPropertiesSet() throws Exception {
        if ( == null && !ObjectUtils.isEmpty()) {
            Assert.hasLength("schemaLanguage is required");
            for (Resource schema : ) {
                Assert.isTrue(schema.exists(), "schema [" + schema + "] does not exist");
            }
            if (.isInfoEnabled()) {
                .info("Validating using " + StringUtils.arrayToCommaDelimitedString());
            }
             = XmlValidatorFactory.createValidator();
        }
        Assert.notNull("Setting 'schema', 'schemas', 'xsdSchema', or 'xsdSchemaCollection' is required");
    }

    
Validates the request message in the given message context. Validation only occurs if validateRequest is set to true, which is the default.

Returns true if the request is valid, or false if it isn't. Additionally, when the request message is a org.springframework.ws.soap.SoapMessage, a org.springframework.ws.soap.SoapFault is added as response.

Parameters:
messageContext the message context
Returns:
true if the message is valid; false otherwise
See also:
setValidateRequest(boolean)
    public boolean handleRequest(MessageContext messageContextObject endpoint)
            throws IOExceptionSAXExceptionTransformerException {
        if () {
            Source requestSource = getValidationRequestSource(messageContext.getRequest());
            if (requestSource != null) {
                SAXParseException[] errors = .validate(requestSource);
                if (!ObjectUtils.isEmpty(errors)) {
                    return handleRequestValidationErrors(messageContexterrors);
                }
                else if (.isDebugEnabled()) {
                    .debug("Request message validated");
                }
            }
        }
        return true;
    }

    
Template method that is called when the request message contains validation errors. Default implementation logs all errors, and returns false, i.e. do not process the request.

Parameters:
messageContext the message context
errors the validation errors
Returns:
true to continue processing the request, false (the default) otherwise
    protected boolean handleRequestValidationErrors(MessageContext messageContextSAXParseException[] errors)
            throws TransformerException {
        for (SAXParseException error : errors) {
            .warn("XML validation error on request: " + error.getMessage());
        }
        return false;
    }

    
Validates the response message in the given message context. Validation only occurs if validateResponse is set to true, which is not the default.

Returns true if the request is valid, or false if it isn't.

Parameters:
messageContext the message context.
Returns:
true if the response is valid; false otherwise
See also:
setValidateResponse(boolean)
    public boolean handleResponse(MessageContext messageContextObject endpointthrows IOExceptionSAXException {
        if () {
            Source responseSource = getValidationResponseSource(messageContext.getResponse());
            if (responseSource != null) {
                SAXParseException[] errors = .validate(responseSource);
                if (!ObjectUtils.isEmpty(errors)) {
                    return handleResponseValidationErrors(messageContexterrors);
                }
                else if (.isDebugEnabled()) {
                    .debug("Response message validated");
                }
            }
        }
        return true;
    }

    
Template method that is called when the response message contains validation errors. Default implementation logs all errors, and returns false, i.e. do not cot continue to process the response interceptor chain.

Parameters:
messageContext the message context
errors the validation errors
Returns:
true to continue the response interceptor chain, false (the default) otherwise
    protected boolean handleResponseValidationErrors(MessageContext messageContextSAXParseException[] errors) {
        for (SAXParseException error : errors) {
            .error("XML validation error on response: " + error.getMessage());
        }
        return false;
    }

    
Does nothing by default. Faults are not validated.
    public boolean handleFault(MessageContext messageContextObject endpointthrows Exception {
        return true;
    }

    
Does nothing by default
    public void afterCompletion(MessageContext messageContextObject endpointException ex) {
    }

    
Abstract template method that returns the part of the request message that is to be validated.

Parameters:
request the request message
Returns:
the part of the message that is to validated, or null not to validate anything
    protected abstract Source getValidationRequestSource(WebServiceMessage request);

    
Abstract template method that returns the part of the response message that is to be validated.

Parameters:
response the response message
Returns:
the part of the message that is to validated, or null not to validate anything
    protected abstract Source getValidationResponseSource(WebServiceMessage response);
New to GrepCode? Check out our FAQ X