Moved PayloadValidatingInterceptor to o.sfw.soap.server.endpoint.interceptor, to solve tangle: it contains SOAP-specific features.

This commit is contained in:
Arjen Poutsma
2007-01-22 17:16:29 +00:00
parent 21a9ef49ae
commit c250ef8d20
14 changed files with 251 additions and 172 deletions

View File

@@ -17,13 +17,9 @@
package org.springframework.ws.server.endpoint.interceptor;
import java.io.IOException;
import java.util.Locale;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@@ -32,12 +28,6 @@ import org.springframework.util.StringUtils;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapFaultDetail;
import org.springframework.ws.soap.SoapFaultDetailElement;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.transform.TransformerObjectSupport;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
@@ -50,8 +40,7 @@ import org.xml.sax.SAXParseException;
* <code>getValidationResponseSource</code> template methods.
* <p/>
* By default, only the request message is validated, but this behaviour can be changed using the
* <code>validateRequest</code> and <code>validateResponse</code> properties. Responses that contains SOAP faults are
* not validated.
* <code>validateRequest</code> and <code>validateResponse</code> properties.
*
* @author Arjen Poutsma
* @see #getValidationRequestSource(org.springframework.ws.WebServiceMessage)
@@ -60,31 +49,6 @@ import org.xml.sax.SAXParseException;
public abstract class AbstractValidatingInterceptor extends TransformerObjectSupport
implements EndpointInterceptor, InitializingBean {
/**
* Default SOAP Fault Detail name used when a validation errors occur on the request.
*
* @see #setDetailElementName(javax.xml.namespace.QName)
*/
public static final QName DEFAULT_DETAIL_ELEMENT_NAME =
QNameUtils.createQName("http://springframework.org/spring-ws", "ValidationError", "spring-ws");
/**
* Default SOAP Fault string used when a validation errors occur on the request.
*
* @see #setFaultStringOrReason(String)
*/
public static final String DEFAULT_FAULTSTRING_OR_REASON = "Validation error";
protected final Log logger = LogFactory.getLog(getClass());
private boolean addValidationErrorDetail = true;
private QName detailElementName = DEFAULT_DETAIL_ELEMENT_NAME;
private String faultStringOrReason = DEFAULT_FAULTSTRING_OR_REASON;
private Locale faultStringOrReasonLocale = Locale.ENGLISH;
private String schemaLanguage = XmlValidatorFactory.SCHEMA_W3C_XML;
private Resource[] schemas;
@@ -95,73 +59,6 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
private XmlValidator validator;
public boolean getAddValidationErrorDetail() {
return addValidationErrorDetail;
}
/**
* Indicates whether a SOAP Fault detail element should be created when a validation error occurs. This detail
* element will contain the exact validation errors. It is only added when the underlying message is a
* <code>SoapMessage</code>. Defaults to <code>true</code>.
*
* @see org.springframework.ws.soap.SoapFault#addFaultDetail()
*/
public void setAddValidationErrorDetail(boolean addValidationErrorDetail) {
this.addValidationErrorDetail = addValidationErrorDetail;
}
/**
* Returns the fault detail element name when validation errors occur on the request.
*/
public QName getDetailElementName() {
return detailElementName;
}
/**
* Sets the fault detail element name when validation errors occur on the request. Defaults to
* <code>DEFAULT_DETAIL_ELEMENT_NAME</code>.
*
* @see #DEFAULT_DETAIL_ELEMENT_NAME
*/
public void setDetailElementName(QName detailElementName) {
this.detailElementName = detailElementName;
}
/**
* Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
*/
public String getFaultStringOrReason() {
return faultStringOrReason;
}
/**
* Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
* It is only added when the underlying message is a <code>SoapMessage</code>. Defaults to
* <code>DEFAULT_FAULTSTRING_OR_REASON</code>.
*
* @see #DEFAULT_FAULTSTRING_OR_REASON
*/
public void setFaultStringOrReason(String faultStringOrReason) {
this.faultStringOrReason = faultStringOrReason;
}
/**
* Returns the SOAP fault reason locale used when validation errors occur on the request.
*/
public Locale getFaultStringOrReasonLocale() {
return faultStringOrReasonLocale;
}
/**
* Sets the SOAP fault reason locale used when validation errors occur on the request. It is only added when the
* underlying message is a <code>SoapMessage</code>. Defaults to English.
*
* @see java.util.Locale#ENGLISH
*/
public void setFaultStringOrReasonLocale(Locale faultStringOrReasonLocale) {
this.faultStringOrReasonLocale = faultStringOrReasonLocale;
}
public String getSchemaLanguage() {
return schemaLanguage;
}
@@ -236,13 +133,7 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
if (requestSource != null) {
SAXParseException[] errors = validator.validate(requestSource);
if (!ObjectUtils.isEmpty(errors)) {
for (int i = 0; i < errors.length; i++) {
logger.warn("XML validation error on request: " + errors[i].getMessage());
}
if (messageContext.getResponse() instanceof SoapMessage) {
createRequestValidationFault((SoapMessage) messageContext.getResponse(), errors);
}
return false;
return handleRequestValidationErrors(messageContext, errors);
}
else if (logger.isDebugEnabled()) {
logger.debug("Request message validated");
@@ -252,6 +143,22 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
return true;
}
/**
* Template method that is called when the request message contains validation errors. Default implementation logs
* all errors, and returns <code>false</code>, i.e. do not process the request.
*
* @param messageContext the message context
* @param errors the validation errors
* @return <code>true</code> to continue processing the request, <code>false</code> (the default) otherwise
*/
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors)
throws TransformerException {
for (int i = 0; i < errors.length; i++) {
logger.warn("XML validation error on request: " + errors[i].getMessage());
}
return false;
}
/**
* Validates the response message in the given message context. Validation only occurs if
* <code>validateResponse</code> is set to <code>true</code>, which is <strong>not</strong> the default.
@@ -268,10 +175,7 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
if (responseSource != null) {
SAXParseException[] errors = validator.validate(responseSource);
if (!ObjectUtils.isEmpty(errors)) {
for (int i = 0; i < errors.length; i++) {
logger.error("XML validation error on response: " + errors[i].getMessage());
}
return false;
return handleResponseValidationErrors(messageContext, errors);
}
else if (logger.isDebugEnabled()) {
logger.debug("Response message validated");
@@ -281,6 +185,21 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
return true;
}
/**
* Template method that is called when the response message contains validation errors. Default implementation logs
* all errors, and returns <code>false</code>, i.e. do not send the response.
*
* @param messageContext the message context
* @param errors the validation errors @return <code>true</code> to continue sending the response,
* <code>false</code> (the default) otherwise
*/
protected boolean handleResponseValidationErrors(MessageContext messageContext, SAXParseException[] errors) {
for (int i = 0; i < errors.length; i++) {
logger.error("XML validation error on response: " + errors[i].getMessage());
}
return false;
}
public void afterPropertiesSet() throws Exception {
Assert.notEmpty(schemas, "setting either the schema or schemas property is required");
Assert.hasLength(schemaLanguage, "schemaLanguage is required");
@@ -293,22 +212,6 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
validator = XmlValidatorFactory.createValidator(schemas, schemaLanguage);
}
/**
* Creates a response soap message containing a <code>SoapFault</code> that descibes the validation errors.
*/
protected void createRequestValidationFault(SoapMessage response, SAXParseException[] errors)
throws TransformerException {
SoapBody body = response.getSoapBody();
SoapFault fault = body.addClientOrSenderFault(getFaultStringOrReason(), getFaultStringOrReasonLocale());
if (getAddValidationErrorDetail()) {
SoapFaultDetail detail = fault.addFaultDetail();
for (int i = 0; i < errors.length; i++) {
SoapFaultDetailElement detailElement = detail.addFaultDetailElement(getDetailElementName());
detailElement.addText(errors[i].getMessage());
}
}
}
/**
* Abstract template method that returns the part of the request message that is to be validated.
*

View File

@@ -0,0 +1,165 @@
/*
* Copyright 2007 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.soap.server.endpoint.interceptor;
import java.util.Locale;
import javax.xml.namespace.QName;
import javax.xml.transform.TransformerException;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.interceptor.AbstractValidatingInterceptor;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapFaultDetail;
import org.springframework.ws.soap.SoapFaultDetailElement;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.xml.namespace.QNameUtils;
import org.xml.sax.SAXParseException;
/**
* Subclass of <code>AbstractValidatingInterceptor</code> that creates a SOAP Fault whenever the request message cannot
* be validated. The contents of the SOAP Fault can be specified by setting the <code>addValidationErrorDetail</code>,
* <code>faultStringOrReason</code>, or <code>detailElementName</code> properties. Further customizing can be
* accomplished by overriding <code>handleRequestValidationErrors</code>.
*
* @author Arjen Poutsma
* @see #setAddValidationErrorDetail(boolean)
* @see #setFaultStringOrReason(String)
* @see #DEFAULT_FAULTSTRING_OR_REASON
* @see #setDetailElementName(javax.xml.namespace.QName)
* @see #DEFAULT_DETAIL_ELEMENT_NAME
* @see #handleResponseValidationErrors(org.springframework.ws.context.MessageContext,org.xml.sax.SAXParseException[])
*/
public abstract class AbstractFaultCreatingValidatingInterceptor extends AbstractValidatingInterceptor {
/**
* Default SOAP Fault Detail name used when a validation errors occur on the request.
*
* @see #setDetailElementName(javax.xml.namespace.QName)
*/
public static final QName DEFAULT_DETAIL_ELEMENT_NAME =
QNameUtils.createQName("http://springframework.org/spring-ws", "ValidationError", "spring-ws");
/**
* Default SOAP Fault string used when a validation errors occur on the request.
*
* @see #setFaultStringOrReason(String)
*/
public static final String DEFAULT_FAULTSTRING_OR_REASON = "Validation error";
private boolean addValidationErrorDetail = true;
private QName detailElementName = DEFAULT_DETAIL_ELEMENT_NAME;
private String faultStringOrReason = DEFAULT_FAULTSTRING_OR_REASON;
private Locale faultStringOrReasonLocale = Locale.ENGLISH;
/**
* Returns whether a SOAP Fault detail element should be created when a validation error occurs. This detail element
* will contain the exact validation errors. It is only added when the underlying message is a
* <code>SoapMessage</code>. Defaults to <code>true</code>.
*
* @see org.springframework.ws.soap.SoapFault#addFaultDetail()
*/
public boolean getAddValidationErrorDetail() {
return addValidationErrorDetail;
}
/**
* Indicates whether a SOAP Fault detail element should be created when a validation error occurs. This detail
* element will contain the exact validation errors. It is only added when the underlying message is a
* <code>SoapMessage</code>. Defaults to <code>true</code>.
*
* @see org.springframework.ws.soap.SoapFault#addFaultDetail()
*/
public void setAddValidationErrorDetail(boolean addValidationErrorDetail) {
this.addValidationErrorDetail = addValidationErrorDetail;
}
/**
* Returns the fault detail element name when validation errors occur on the request.
*/
public QName getDetailElementName() {
return detailElementName;
}
/**
* Sets the fault detail element name when validation errors occur on the request. Defaults to
* <code>DEFAULT_DETAIL_ELEMENT_NAME</code>.
*
* @see #DEFAULT_DETAIL_ELEMENT_NAME
*/
public void setDetailElementName(QName detailElementName) {
this.detailElementName = detailElementName;
}
/**
* Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
*/
public String getFaultStringOrReason() {
return faultStringOrReason;
}
/**
* Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
* It is only added when the underlying message is a <code>SoapMessage</code>. Defaults to
* <code>DEFAULT_FAULTSTRING_OR_REASON</code>.
*
* @see #DEFAULT_FAULTSTRING_OR_REASON
*/
public void setFaultStringOrReason(String faultStringOrReason) {
this.faultStringOrReason = faultStringOrReason;
}
/**
* Returns the SOAP fault reason locale used when validation errors occur on the request.
*/
public Locale getFaultStringOrReasonLocale() {
return faultStringOrReasonLocale;
}
/**
* Sets the SOAP fault reason locale used when validation errors occur on the request. It is only added when the
* underlying message is a <code>SoapMessage</code>. Defaults to English.
*
* @see java.util.Locale#ENGLISH
*/
public void setFaultStringOrReasonLocale(Locale faultStringOrReasonLocale) {
this.faultStringOrReasonLocale = faultStringOrReasonLocale;
}
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors)
throws TransformerException {
for (int i = 0; i < errors.length; i++) {
logger.warn("XML validation error on request: " + errors[i].getMessage());
}
if (messageContext.getResponse() instanceof SoapMessage) {
SoapMessage response = (SoapMessage) messageContext.getResponse();
SoapBody body = response.getSoapBody();
SoapFault fault = body.addClientOrSenderFault(getFaultStringOrReason(), getFaultStringOrReasonLocale());
if (getAddValidationErrorDetail()) {
SoapFaultDetail detail = fault.addFaultDetail();
for (int i = 0; i < errors.length; i++) {
SoapFaultDetailElement detailElement = detail.addFaultDetailElement(getDetailElementName());
detailElement.addText(errors[i].getMessage());
}
}
}
return false;
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.server.endpoint.interceptor;
package org.springframework.ws.soap.server.endpoint.interceptor;
import javax.xml.transform.Source;
@@ -27,16 +27,18 @@ import org.springframework.ws.WebServiceMessage;
* When the payload is invalid, this interceptor stops processing of the interceptor chain. Additionally, if the message
* is a SOAP request message, a SOAP Fault is created as reply. Invalid SOAP responses do not result in a fault.
* <p/>
* The schema to validate against is set with the <code>schema</code> property. By default, only the request message is
* validated, but this behaviour can be changed using the <code>validateRequest</code> and <code>validateResponse</code>
* properties. Responses that contains faults are not validated.
* The schema to validate against is set with the <code>schema</code> property or <code>schemas</code> property. By
* default, only the request message is validated, but this behaviour can be changed using the
* <code>validateRequest</code> and <code>validateResponse</code> properties. Responses that contains faults are not
* validated.
*
* @author Arjen Poutsma
* @see #setSchema
* @see #setSchema(org.springframework.core.io.Resource)
* @see #setSchemas(org.springframework.core.io.Resource[])
* @see #setValidateRequest(boolean)
* @see #setValidateResponse(boolean)
*/
public class PayloadValidatingInterceptor extends AbstractValidatingInterceptor {
public class PayloadValidatingInterceptor extends AbstractFaultCreatingValidatingInterceptor {
/**
* Returns the payload source of the given message.

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.server.endpoint.interceptor;
package org.springframework.ws.soap.server.endpoint.interceptor;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@@ -27,8 +27,6 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory;
import org.custommonkey.xmlunit.XMLTestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@@ -38,7 +36,7 @@ import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.SoapVersion;
import org.springframework.ws.soap.axiom.AxiomSoapMessage;
import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.springframework.ws.soap.saaj.support.SaajUtils;
@@ -59,9 +57,23 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
private Transformer transformer;
private static final String INVALID_MESSAGE = "invalidMessage.xml";
private static final String SCHEMA = "schema.xsd";
private static final String VALID_MESSAGE = "validMessage.xml";
private static final String PRODUCT_SCHEMA = "productSchema.xsd";
private static final String SIZE_SCHEMA = "sizeSchema.xsd";
private static final String VALID_SOAP_MESSAGE = "validSoapMessage.xml";
private static final String SCHEMA2 = "schema2.xsd";
protected void setUp() throws Exception {
interceptor = new PayloadValidatingInterceptor();
interceptor.setSchema(new ClassPathResource("schema.xsd", getClass()));
interceptor.setSchema(new ClassPathResource(SCHEMA, getClass()));
interceptor.setValidateRequest(true);
interceptor.setValidateResponse(true);
interceptor.afterPropertiesSet();
@@ -73,7 +85,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
public void testHandleInvalidRequestSoap11() throws Exception {
SoapMessage invalidMessage = (SoapMessage) soap11Factory.createWebServiceMessage();
InputStream inputStream = getClass().getResourceAsStream("invalidMessage.xml");
InputStream inputStream = getClass().getResourceAsStream(INVALID_MESSAGE);
transformer.transform(new StreamSource(inputStream), invalidMessage.getPayloadResult());
context = new DefaultMessageContext(invalidMessage, soap11Factory);
@@ -92,7 +104,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
public void testHandleInvalidRequestSoap12() throws Exception {
SoapMessage invalidMessage = (SoapMessage) soap12Factory.createWebServiceMessage();
InputStream inputStream = getClass().getResourceAsStream("invalidMessage.xml");
InputStream inputStream = getClass().getResourceAsStream(INVALID_MESSAGE);
transformer.transform(new StreamSource(inputStream), invalidMessage.getPayloadResult());
context = new DefaultMessageContext(invalidMessage, soap12Factory);
@@ -117,7 +129,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
interceptor.setAddValidationErrorDetail(false);
SoapMessage invalidMessage = (SoapMessage) soap11Factory.createWebServiceMessage();
InputStream inputStream = getClass().getResourceAsStream("invalidMessage.xml");
InputStream inputStream = getClass().getResourceAsStream(INVALID_MESSAGE);
transformer.transform(new StreamSource(inputStream), invalidMessage.getPayloadResult());
context = new DefaultMessageContext(invalidMessage, soap11Factory);
@@ -136,7 +148,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
public void testHandlerInvalidRequest() throws Exception {
MockWebServiceMessage request = new MockWebServiceMessage();
request.setPayload(new ClassPathResource("invalidMessage.xml", getClass()));
request.setPayload(new ClassPathResource(INVALID_MESSAGE, getClass()));
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
boolean result = interceptor.handleRequest(context, null);
assertFalse("Invalid response from interceptor", result);
@@ -144,7 +156,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
public void testHandleValidRequest() throws Exception {
MockWebServiceMessage request = new MockWebServiceMessage();
request.setPayload(new ClassPathResource("validMessage.xml", getClass()));
request.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
boolean result = interceptor.handleRequest(context, null);
assertTrue("Invalid response from interceptor", result);
@@ -155,7 +167,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
MockWebServiceMessage request = new MockWebServiceMessage();
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
response.setPayload(new ClassPathResource("invalidMessage.xml", getClass()));
response.setPayload(new ClassPathResource(INVALID_MESSAGE, getClass()));
boolean result = interceptor.handleResponse(context, null);
assertFalse("Invalid response from interceptor", result);
}
@@ -164,7 +176,7 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
MockWebServiceMessage request = new MockWebServiceMessage();
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
response.setPayload(new ClassPathResource("validMessage.xml", getClass()));
response.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
boolean result = interceptor.handleResponse(context, null);
assertTrue("Invalid response from interceptor", result);
}
@@ -177,11 +189,11 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
System.setProperty("javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI,
"org.apache.xerces.jaxp.validation.XMLSchemaFactory");
try {
interceptor.setSchema(new ClassPathResource("schema2.xsd", PayloadValidatingInterceptorTest.class));
interceptor.setSchema(new ClassPathResource(SCHEMA2, PayloadValidatingInterceptorTest.class));
interceptor.afterPropertiesSet();
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage saajMessage =
SaajUtils.loadMessage(new ClassPathResource("validSoapMessage.xml", getClass()), messageFactory);
SaajUtils.loadMessage(new ClassPathResource(VALID_SOAP_MESSAGE, getClass()), messageFactory);
context = new DefaultMessageContext(new SaajSoapMessage(saajMessage),
new SaajSoapMessageFactory(messageFactory));
@@ -208,22 +220,20 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
}
public void testHandlerInvalidRequestMultipleSchemas() throws Exception {
interceptor.setSchemas(new Resource[]{new ClassPathResource("productSchema.xsd", getClass()),
new ClassPathResource("sizeSchema.xsd", getClass())});
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
new ClassPathResource(SIZE_SCHEMA, getClass())});
interceptor.afterPropertiesSet();
MockWebServiceMessage request =
new MockWebServiceMessage(new ClassPathResource("invalidMessage.xml", getClass()));
MockWebServiceMessage request = new MockWebServiceMessage(new ClassPathResource(INVALID_MESSAGE, getClass()));
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
boolean result = interceptor.handleRequest(context, null);
assertFalse("Invalid response from interceptor", result);
}
public void testHandleValidRequestMultipleSchemas() throws Exception {
interceptor.setSchemas(new Resource[]{new ClassPathResource("productSchema.xsd", getClass()),
new ClassPathResource("sizeSchema.xsd", getClass())});
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
new ClassPathResource(SIZE_SCHEMA, getClass())});
interceptor.afterPropertiesSet();
MockWebServiceMessage request =
new MockWebServiceMessage(new ClassPathResource("validMessage.xml", getClass()));
MockWebServiceMessage request = new MockWebServiceMessage(new ClassPathResource(VALID_MESSAGE, getClass()));
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
boolean result = interceptor.handleRequest(context, null);
@@ -232,40 +242,39 @@ public class PayloadValidatingInterceptorTest extends XMLTestCase {
}
public void testHandleInvalidResponseMultipleSchemas() throws Exception {
interceptor.setSchemas(new Resource[]{new ClassPathResource("productSchema.xsd", getClass()),
new ClassPathResource("sizeSchema.xsd", getClass())});
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
new ClassPathResource(SIZE_SCHEMA, getClass())});
interceptor.afterPropertiesSet();
MockWebServiceMessage request = new MockWebServiceMessage();
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
response.setPayload(new ClassPathResource("invalidMessage.xml", getClass()));
response.setPayload(new ClassPathResource(INVALID_MESSAGE, getClass()));
boolean result = interceptor.handleResponse(context, null);
assertFalse("Invalid response from interceptor", result);
}
public void testHandleValidResponseMultipleSchemas() throws Exception {
interceptor.setSchemas(new Resource[]{new ClassPathResource("productSchema.xsd", getClass()),
new ClassPathResource("sizeSchema.xsd", getClass())});
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
new ClassPathResource(SIZE_SCHEMA, getClass())});
interceptor.afterPropertiesSet();
MockWebServiceMessage request = new MockWebServiceMessage();
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
response.setPayload(new ClassPathResource("validMessage.xml", getClass()));
response.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
boolean result = interceptor.handleResponse(context, null);
assertTrue("Invalid response from interceptor", result);
}
public void testCreateRequestValidationFaultAxiom() throws Exception {
SOAPFactory soapFactory = new SOAP11Factory();
AxiomSoapMessage message = new AxiomSoapMessage(soapFactory);
LocatorImpl locator = new LocatorImpl();
locator.setLineNumber(0);
locator.setColumnNumber(0);
SAXParseException[] exceptions = new SAXParseException[]{new SAXParseException("Message 1", locator),
new SAXParseException("Message 2", locator),};
interceptor.createRequestValidationFault(message, exceptions);
MessageContext messageContext = new DefaultMessageContext(new AxiomSoapMessageFactory());
interceptor.handleRequestValidationErrors(messageContext, exceptions);
ByteArrayOutputStream os = new ByteArrayOutputStream();
message.writeTo(os);
messageContext.getResponse().writeTo(os);
assertXMLEqual("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soapenv:Header />" + "<soapenv:Body>" + "<soapenv:Fault>" + "<faultcode>soapenv:Client</faultcode>" +
"<faultstring>Validation error</faultstring>" + "<detail>" +

View File

@@ -3,7 +3,7 @@
targetNamespace="http://www.springframework.org/spring-ws/test/validation"
xmlns:tns="http://www.springframework.org/spring-ws/test/validation" elementFormDefault="qualified">
<include schemaLocation="sizeSchema.xsd"/>
<include schemaLocation="../../../server/endpoint/interceptor/sizeSchema.xsd"/>
<element name="product" type="tns:ProductType"/>

View File

@@ -82,7 +82,7 @@
</bean>
<bean id="validatingInterceptor"
class="org.springframework.ws.server.endpoint.interceptor.PayloadValidatingInterceptor">
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<description>
This interceptor validates both incoming and outgoing message contents according to the 'airline.xsd' XML
Schema file.

View File

@@ -23,7 +23,7 @@
</bean>
<bean id="validatingInterceptor"
class="org.springframework.ws.server.endpoint.interceptor.PayloadValidatingInterceptor">
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<description>
This interceptor validates both incoming and outgoing message contents according to the 'echo.xsd' XML
Schema file.