SWS-391
This commit is contained in:
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.ws;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
@@ -26,7 +24,7 @@ import org.springframework.core.NestedRuntimeException;
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class WebServiceException extends NestedRuntimeException implements Serializable {
|
||||
public abstract class WebServiceException extends NestedRuntimeException {
|
||||
|
||||
/**
|
||||
* Create a new instance of the <code>WebServiceException</code> class.
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
* Copyright 2008 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.client.support.interceptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.client.WebServiceClientException;
|
||||
import org.springframework.ws.client.WebServiceIOException;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
import org.springframework.xml.xsd.XsdSchemaCollection;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link ClientInterceptor} implementations that validate part of the message using a schema.
|
||||
* The exact message part is determined by the {@link #getValidationRequestSource(WebServiceMessage)} and {@link
|
||||
* #getValidationResponseSource(WebServiceMessage)} 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.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #getValidationRequestSource(WebServiceMessage)
|
||||
* @see #getValidationResponseSource(WebServiceMessage)
|
||||
* @since 1.5.4
|
||||
*/
|
||||
public abstract class AbstractValidatingInterceptor extends TransformerObjectSupport
|
||||
implements ClientInterceptor, InitializingBean {
|
||||
|
||||
private String schemaLanguage = XmlValidatorFactory.SCHEMA_W3C_XML;
|
||||
|
||||
private Resource[] schemas;
|
||||
|
||||
private boolean validateRequest = true;
|
||||
|
||||
private boolean validateResponse = false;
|
||||
|
||||
private XmlValidator validator;
|
||||
|
||||
public String getSchemaLanguage() {
|
||||
return schemaLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema language. Default is the W3C XML Schema: <code>http://www.w3.org/2001/XMLSchema"</code>.
|
||||
*
|
||||
* @see XmlValidatorFactory#SCHEMA_W3C_XML
|
||||
* @see XmlValidatorFactory#SCHEMA_RELAX_NG
|
||||
*/
|
||||
public void setSchemaLanguage(String schemaLanguage) {
|
||||
this.schemaLanguage = schemaLanguage;
|
||||
}
|
||||
|
||||
/** Returns the schema resources to use for validation. */
|
||||
public Resource[] getSchemas() {
|
||||
return schemas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema resource to use for validation. Setting this property, {@link
|
||||
* #setXsdSchemaCollection(XsdSchemaCollection) xsdSchemaCollection}, {@link #setSchema(Resource) schema}, or {@link
|
||||
* #setSchemas(Resource[]) schemas} is required.
|
||||
*/
|
||||
public void setSchema(Resource schema) {
|
||||
setSchemas(new Resource[]{schema});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema resources to use for validation. Setting this property, {@link
|
||||
* #setXsdSchemaCollection(XsdSchemaCollection) xsdSchemaCollection}, {@link #setSchema(Resource) schema}, or {@link
|
||||
* #setSchemas(Resource[]) schemas} is required.
|
||||
*/
|
||||
public void setSchemas(Resource[] schemas) {
|
||||
Assert.notEmpty(schemas, "schemas must not be empty or null");
|
||||
for (int i = 0; i < schemas.length; i++) {
|
||||
Assert.notNull(schemas[i], "schema must not be null");
|
||||
Assert.isTrue(schemas[i].exists(), "schema \"" + schemas[i] + "\" does not exit");
|
||||
}
|
||||
this.schemas = schemas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link XsdSchema} to use for validation. Setting this property, {@link
|
||||
* #setXsdSchemaCollection(XsdSchemaCollection) xsdSchemaCollection}, {@link #setSchema(Resource) schema}, or {@link
|
||||
* #setSchemas(Resource[]) schemas} is required.
|
||||
*
|
||||
* @param schema the xsd schema to use
|
||||
* @throws java.io.IOException in case of I/O errors
|
||||
*/
|
||||
public void setXsdSchema(XsdSchema schema) throws IOException {
|
||||
this.validator = schema.createValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link XsdSchemaCollection} to use for validation. Setting this property, {@link
|
||||
* #setXsdSchema(XsdSchema) xsdSchema}, {@link #setSchema(Resource) schema}, or {@link #setSchemas(Resource[])
|
||||
* schemas} is required.
|
||||
*
|
||||
* @param schemaCollection the xsd schema collection to use
|
||||
* @throws java.io.IOException in case of I/O errors
|
||||
*/
|
||||
public void setXsdSchemaCollection(XsdSchemaCollection schemaCollection) throws IOException {
|
||||
this.validator = schemaCollection.createValidator();
|
||||
}
|
||||
|
||||
/** Indicates whether the request should be validated against the schema. Default is <code>true</code>. */
|
||||
public void setValidateRequest(boolean validateRequest) {
|
||||
this.validateRequest = validateRequest;
|
||||
}
|
||||
|
||||
/** Indicates whether the response should be validated against the schema. Default is <code>false</code>. */
|
||||
public void setValidateResponse(boolean validateResponse) {
|
||||
this.validateResponse = validateResponse;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
if (validator == null && !ObjectUtils.isEmpty(schemas)) {
|
||||
Assert.hasLength(schemaLanguage, "schemaLanguage is required");
|
||||
for (int i = 0; i < schemas.length; i++) {
|
||||
Assert.isTrue(schemas[i].exists(), "schema [" + schemas[i] + "] does not exist");
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Validating using " + StringUtils.arrayToCommaDelimitedString(schemas));
|
||||
}
|
||||
validator = XmlValidatorFactory.createValidator(schemas, schemaLanguage);
|
||||
}
|
||||
Assert.notNull(validator, "Setting 'schema', 'schemas', 'xsdSchema', or 'xsdSchemaCollection' is required");
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the request message in the given message context. Validation only occurs if {@link
|
||||
* #setValidateRequest(boolean) validateRequest} is set to <code>true</code>, which is the default.
|
||||
* <p/>
|
||||
* Returns <code>true</code> if the request is valid, or <code>false</code> if it isn't.
|
||||
*
|
||||
* @param messageContext the message context
|
||||
* @return <code>true</code> if the message is valid; <code>false</code> otherwise
|
||||
* @see #setValidateRequest(boolean)
|
||||
*/
|
||||
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
|
||||
if (validateRequest) {
|
||||
Source requestSource = getValidationRequestSource(messageContext.getRequest());
|
||||
if (requestSource != null) {
|
||||
SAXParseException[] errors;
|
||||
try {
|
||||
errors = validator.validate(requestSource);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new WebServiceIOException("Could not validate response: " + e.getMessage(), e);
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(errors)) {
|
||||
return handleRequestValidationErrors(messageContext, errors);
|
||||
}
|
||||
else if (logger.isDebugEnabled()) {
|
||||
logger.debug("Request message validated");
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that is called when the request message contains validation errors.
|
||||
* <p/>
|
||||
* Default implementation logs all errors, and throws a {@link WebServiceValidationException}. Subclasses can
|
||||
* override this method to customize this behavior.
|
||||
*
|
||||
* @param messageContext the message context
|
||||
* @param errors the validation errors
|
||||
* @return <code>true</code> to continue processing the request, <code>false</code> otherwise
|
||||
*/
|
||||
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) {
|
||||
for (int i = 0; i < errors.length; i++) {
|
||||
logger.error("XML validation error on request: " + errors[i].getMessage());
|
||||
}
|
||||
throw new WebServiceValidationException(errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the response message in the given message context. Validation only occurs if {@link
|
||||
* #setValidateResponse(boolean) validateResponse} is set to <code>true</code>, which is <strong>not</strong> the
|
||||
* default.
|
||||
* <p/>
|
||||
* Returns <code>true</code> if the request is valid, or <code>false</code> if it isn't.
|
||||
*
|
||||
* @param messageContext the message context.
|
||||
* @return <code>true</code> if the response is valid; <code>false</code> otherwise
|
||||
* @see #setValidateResponse(boolean)
|
||||
*/
|
||||
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
|
||||
if (validateResponse) {
|
||||
Source responseSource = getValidationResponseSource(messageContext.getResponse());
|
||||
if (responseSource != null) {
|
||||
SAXParseException[] errors;
|
||||
try {
|
||||
errors = validator.validate(responseSource);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new WebServiceIOException("Could not validate response: " + e.getMessage(), e);
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(errors)) {
|
||||
return handleResponseValidationErrors(messageContext, errors);
|
||||
}
|
||||
else if (logger.isDebugEnabled()) {
|
||||
logger.debug("Response message validated");
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method that is called when the response message contains validation errors.
|
||||
* <p/>
|
||||
* Default implementation logs all errors, and returns <code>false</code>, i.e. do not cot continue to process the
|
||||
* respone interceptor chain.
|
||||
*
|
||||
* @param messageContext the message context
|
||||
* @param errors the validation errors
|
||||
* @return <code>true</code> to continue the reponse interceptor chain, <code>false</code> (the default) otherwise
|
||||
*/
|
||||
protected boolean handleResponseValidationErrors(MessageContext messageContext, SAXParseException[] errors)
|
||||
throws WebServiceValidationException {
|
||||
for (int i = 0; i < errors.length; i++) {
|
||||
logger.warn("XML validation error on response: " + errors[i].getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Does nothing by default. Faults are not validated. */
|
||||
public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract template method that returns the part of the request message that is to be validated.
|
||||
*
|
||||
* @param request the request message
|
||||
* @return the part of the message that is to validated, or <code>null</code> 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.
|
||||
*
|
||||
* @param response the response message
|
||||
* @return the part of the message that is to validated, or <code>null</code> not to validate anything
|
||||
*/
|
||||
protected abstract Source getValidationResponseSource(WebServiceMessage response);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2006 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.client.support.interceptor;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* Client-side interceptor that validates the contents of <code>WebServiceMessage</code>s using a schema. Allows for
|
||||
* both W3C XML and RELAX NG schemas.
|
||||
* <p/>
|
||||
* When the payload is invalid, this interceptor stops processing of the interceptor chain.
|
||||
* <p/>
|
||||
* 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 Stefan Schmidt
|
||||
* @author Arjen Poutsma
|
||||
* @see #setSchema(org.springframework.core.io.Resource)
|
||||
* @see #setSchemas(org.springframework.core.io.Resource[])
|
||||
* @see #setValidateRequest(boolean)
|
||||
* @see #setValidateResponse(boolean)
|
||||
* @since 1.5.4
|
||||
*/
|
||||
public class PayloadValidatingInterceptor extends AbstractValidatingInterceptor {
|
||||
|
||||
/**
|
||||
* Returns the part of the request message that is to be validated. Default
|
||||
*
|
||||
* @param request the request message
|
||||
* @return the part of the message that is to validated, or <code>null</code> not to validate anything
|
||||
*/
|
||||
protected Source getValidationRequestSource(WebServiceMessage request) {
|
||||
return request.getPayloadSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the part of the response message that is to be validated.
|
||||
*
|
||||
* @param response the response message
|
||||
* @return the part of the message that is to validated, or <code>null</code> not to validate anything
|
||||
*/
|
||||
protected Source getValidationResponseSource(WebServiceMessage response) {
|
||||
return response.getPayloadSource();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.springframework.ws.client.support.interceptor;
|
||||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import org.springframework.ws.client.WebServiceClientException;
|
||||
|
||||
/**
|
||||
* Exception thrown whenever a validation error occurs on the client-side.
|
||||
*
|
||||
* @author Stefan Schmidt
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.4
|
||||
*/
|
||||
public class WebServiceValidationException extends WebServiceClientException {
|
||||
|
||||
private SAXParseException[] validationErrors;
|
||||
|
||||
/**
|
||||
* Create a new instance of the <code>WebServiceValidationException</code> class.
|
||||
*
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public WebServiceValidationException(SAXParseException[] validationErrors) {
|
||||
super(createMessage(validationErrors));
|
||||
this.validationErrors = validationErrors;
|
||||
}
|
||||
|
||||
private static String createMessage(SAXParseException[] validationErrors) {
|
||||
StringBuffer buffer = new StringBuffer("XML validation error on response: ");
|
||||
|
||||
for (int i = 0; i < validationErrors.length; i++) {
|
||||
buffer.append(validationErrors[i].getMessage());
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/** Returns the validation errors. */
|
||||
public SAXParseException[] getValidationErrors() {
|
||||
return validationErrors;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides the <code>ClientInterceptor</code> interface.
|
||||
Provides the <code>ClientInterceptor</code> interface, and validating interceptors.
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* Copyright 2006 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.client.support.interceptor;
|
||||
|
||||
import java.io.InputStream;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.soap.MessageFactory;
|
||||
import javax.xml.soap.SOAPConstants;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLTestCase;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.MockWebServiceMessage;
|
||||
import org.springframework.ws.MockWebServiceMessageFactory;
|
||||
import org.springframework.ws.client.WebServiceClientException;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
|
||||
import org.springframework.ws.soap.saaj.support.SaajUtils;
|
||||
import org.springframework.xml.xsd.SimpleXsdSchema;
|
||||
|
||||
public class PayloadValidatingInterceptorTest extends XMLTestCase {
|
||||
|
||||
private PayloadValidatingInterceptor interceptor;
|
||||
|
||||
private MessageContext context;
|
||||
|
||||
private SaajSoapMessageFactory soap11Factory;
|
||||
|
||||
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, getClass()));
|
||||
interceptor.setValidateRequest(true);
|
||||
interceptor.setValidateResponse(true);
|
||||
interceptor.afterPropertiesSet();
|
||||
|
||||
soap11Factory = new SaajSoapMessageFactory(MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL));
|
||||
|
||||
transformer = TransformerFactory.newInstance().newTransformer();
|
||||
}
|
||||
|
||||
public void testHandleInvalidRequest() throws Exception {
|
||||
SoapMessage invalidMessage = (SoapMessage) soap11Factory.createWebServiceMessage();
|
||||
InputStream inputStream = getClass().getResourceAsStream(INVALID_MESSAGE);
|
||||
transformer.transform(new StreamSource(inputStream), invalidMessage.getPayloadResult());
|
||||
context = new DefaultMessageContext(invalidMessage, soap11Factory);
|
||||
|
||||
boolean validated;
|
||||
try {
|
||||
validated = interceptor.handleRequest(context);
|
||||
}
|
||||
catch (WebServiceClientException e) {
|
||||
validated = false;
|
||||
assertNotNull("No exception details provided in WebServiceClientException", e.getMessage());
|
||||
}
|
||||
assertFalse("Invalid response from interceptor", validated);
|
||||
}
|
||||
|
||||
public void testHandlerInvalidRequest() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
request.setPayload(new ClassPathResource(INVALID_MESSAGE, getClass()));
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
|
||||
boolean validated;
|
||||
try {
|
||||
validated = interceptor.handleRequest(context);
|
||||
}
|
||||
catch (WebServiceClientException e) {
|
||||
validated = false;
|
||||
assertNotNull("No exception details provided in WebServiceClientException", e.getMessage());
|
||||
}
|
||||
assertFalse("Invalid response from interceptor", validated);
|
||||
}
|
||||
|
||||
public void testHandleValidRequest() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
request.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
boolean result = interceptor.handleRequest(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
assertFalse("Response set", context.hasResponse());
|
||||
}
|
||||
|
||||
public void testHandleInvalidResponse() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
|
||||
response.setPayload(new ClassPathResource(INVALID_MESSAGE, getClass()));
|
||||
|
||||
boolean result = interceptor.handleResponse(context);
|
||||
assertFalse("Invalid response from interceptor", result);
|
||||
}
|
||||
|
||||
public void testHandleValidResponse() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
|
||||
response.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
|
||||
boolean result = interceptor.handleResponse(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
}
|
||||
|
||||
public void testNamespacesInType() throws Exception {
|
||||
// Make sure we use Xerces for this testcase: the JAXP implementation used internally by JDK 1.5 has a bug
|
||||
// See http://opensource.atlassian.com/projects/spring/browse/SWS-35
|
||||
String previousSchemaFactory =
|
||||
System.getProperty("javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI, "");
|
||||
System.setProperty("javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI,
|
||||
"org.apache.xerces.jaxp.validation.XMLSchemaFactory");
|
||||
try {
|
||||
PayloadValidatingInterceptor interceptor = new PayloadValidatingInterceptor();
|
||||
interceptor.setSchema(new ClassPathResource(SCHEMA2, PayloadValidatingInterceptorTest.class));
|
||||
interceptor.afterPropertiesSet();
|
||||
MessageFactory messageFactory = MessageFactory.newInstance();
|
||||
SOAPMessage saajMessage =
|
||||
SaajUtils.loadMessage(new ClassPathResource(VALID_SOAP_MESSAGE, getClass()), messageFactory);
|
||||
context = new DefaultMessageContext(new SaajSoapMessage(saajMessage),
|
||||
new SaajSoapMessageFactory(messageFactory));
|
||||
|
||||
boolean result = interceptor.handleRequest(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
assertFalse("Response set", context.hasResponse());
|
||||
}
|
||||
finally {
|
||||
// Reset the property
|
||||
System.setProperty("javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI,
|
||||
previousSchemaFactory);
|
||||
}
|
||||
}
|
||||
|
||||
public void testNonExistingSchema() throws Exception {
|
||||
try {
|
||||
interceptor.setSchema(new ClassPathResource("invalid"));
|
||||
interceptor.afterPropertiesSet();
|
||||
fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testHandlerInvalidRequestMultipleSchemas() throws Exception {
|
||||
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
|
||||
new ClassPathResource(SIZE_SCHEMA, getClass())});
|
||||
interceptor.afterPropertiesSet();
|
||||
MockWebServiceMessage request = new MockWebServiceMessage(new ClassPathResource(INVALID_MESSAGE, getClass()));
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
|
||||
boolean validated;
|
||||
try {
|
||||
validated = interceptor.handleRequest(context);
|
||||
}
|
||||
catch (WebServiceClientException e) {
|
||||
validated = false;
|
||||
assertNotNull("No exception details provided in WebServiceClientException", e.getMessage());
|
||||
}
|
||||
assertFalse("Invalid response from interceptor", validated);
|
||||
}
|
||||
|
||||
public void testHandleValidRequestMultipleSchemas() throws Exception {
|
||||
interceptor.setSchemas(new Resource[]{new ClassPathResource(PRODUCT_SCHEMA, getClass()),
|
||||
new ClassPathResource(SIZE_SCHEMA, getClass())});
|
||||
interceptor.afterPropertiesSet();
|
||||
MockWebServiceMessage request = new MockWebServiceMessage(new ClassPathResource(VALID_MESSAGE, getClass()));
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
|
||||
boolean result = interceptor.handleRequest(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
assertFalse("Response set", context.hasResponse());
|
||||
}
|
||||
|
||||
public void testHandleInvalidResponseMultipleSchemas() throws Exception {
|
||||
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(INVALID_MESSAGE, getClass()));
|
||||
boolean result = interceptor.handleResponse(context);
|
||||
assertFalse("Invalid response from interceptor", result);
|
||||
}
|
||||
|
||||
public void testHandleValidResponseMultipleSchemas() throws Exception {
|
||||
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(VALID_MESSAGE, getClass()));
|
||||
boolean result = interceptor.handleResponse(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
}
|
||||
|
||||
public void testXsdSchema() throws Exception {
|
||||
PayloadValidatingInterceptor interceptor = new PayloadValidatingInterceptor();
|
||||
SimpleXsdSchema schema = new SimpleXsdSchema(new ClassPathResource(SCHEMA, getClass()));
|
||||
schema.afterPropertiesSet();
|
||||
interceptor.setXsdSchema(schema);
|
||||
interceptor.setValidateRequest(true);
|
||||
interceptor.setValidateResponse(true);
|
||||
interceptor.afterPropertiesSet();
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
request.setPayload(new ClassPathResource(VALID_MESSAGE, getClass()));
|
||||
context = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
boolean result = interceptor.handleRequest(context);
|
||||
assertTrue("Invalid response from interceptor", result);
|
||||
assertFalse("Response set", context.hasResponse());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<product xmlns="http://www.springframework.org/spring-ws/test/validation" effDate="2006-01-01">
|
||||
<size>20</size>
|
||||
</product>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/validation"
|
||||
xmlns:tns="http://www.springframework.org/spring-ws/test/validation" elementFormDefault="qualified">
|
||||
|
||||
<include schemaLocation="../../../server/endpoint/interceptor/sizeSchema.xsd"/>
|
||||
|
||||
<element name="product" type="tns:ProductType"/>
|
||||
|
||||
<complexType name="ProductType">
|
||||
<sequence>
|
||||
<element name="number" type="integer"/>
|
||||
<element name="size" type="tns:SizeType"/>
|
||||
</sequence>
|
||||
<attribute name="effDate" type="date"/>
|
||||
</complexType>
|
||||
|
||||
</schema>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/validation"
|
||||
xmlns:tns="http://www.springframework.org/spring-ws/test/validation" elementFormDefault="qualified">
|
||||
|
||||
<element name="product" type="tns:ProductType"/>
|
||||
|
||||
<complexType name="ProductType">
|
||||
<sequence>
|
||||
<element name="number" type="integer"/>
|
||||
<element name="size" type="tns:SizeType"/>
|
||||
</sequence>
|
||||
<attribute name="effDate" type="date"/>
|
||||
</complexType>
|
||||
|
||||
<simpleType name="SizeType">
|
||||
<restriction base="integer">
|
||||
<minInclusive value="2"/>
|
||||
<maxInclusive value="18"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</schema>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified"
|
||||
targetNamespace="http://springws.cas.de" xmlns:tns="http://springws.cas.de">
|
||||
|
||||
<complexType name="CasDataType">
|
||||
<sequence>
|
||||
<element name="key" type="long" nillable="false" minOccurs="1" maxOccurs="1"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
|
||||
<complexType name="AddressDataType">
|
||||
<complexContent>
|
||||
<extension base="tns:CasDataType">
|
||||
<sequence>
|
||||
<element name="firstName" nillable="true" minOccurs="0" type="string"/>
|
||||
<element name="lastName" nillable="true" minOccurs="0" type="string"/>
|
||||
</sequence>
|
||||
</extension>
|
||||
</complexContent>
|
||||
</complexType>
|
||||
|
||||
<complexType name="ArrayOfCasDataTypes">
|
||||
<sequence>
|
||||
<element name="casDataType" type="tns:CasDataType" nillable="false" minOccurs="1" maxOccurs="unbounded"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
|
||||
<element name="createDataTypesRequest" type="tns:ArrayOfCasDataTypes"/>
|
||||
|
||||
<element name="createDataTypesResponse" type="tns:ArrayOfCasDataTypes"/>
|
||||
|
||||
</schema>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/validation"
|
||||
xmlns:tns="http://www.springframework.org/spring-ws/test/validation" elementFormDefault="qualified">
|
||||
|
||||
<simpleType name="SizeType">
|
||||
<restriction base="integer">
|
||||
<minInclusive value="2"/>
|
||||
<maxInclusive value="18"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</schema>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<product xmlns="http://www.springframework.org/spring-ws/test/validation" effDate="2006-01-01">
|
||||
<number>42</number>
|
||||
<size>10</size>
|
||||
</product>
|
||||
@@ -0,0 +1,12 @@
|
||||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://springws.cas.de">
|
||||
<SOAP-ENV:Header/>
|
||||
<SOAP-ENV:Body>
|
||||
<ns2:createDataTypesResponse>
|
||||
<ns2:casDataType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:AddressDataType">
|
||||
<ns2:key>123</ns2:key>
|
||||
<ns2:firstName>Fritz</ns2:firstName>
|
||||
<ns2:lastName>Meier</ns2:lastName>
|
||||
</ns2:casDataType>
|
||||
</ns2:createDataTypesResponse>
|
||||
</SOAP-ENV:Body>
|
||||
</SOAP-ENV:Envelope>
|
||||
Reference in New Issue
Block a user