INT-1515 initial round of refactoring to turn validating-router into validating-filter
This commit is contained in:
@@ -34,7 +34,7 @@ public class IntegrationXmlNamespaceHandler extends AbstractIntegrationNamespace
|
||||
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
|
||||
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());
|
||||
registerBeanDefinitionParser("xpath-splitter", new XPathMessageSplitterParser());
|
||||
registerBeanDefinitionParser("validating-router", new XmlPayloadValidatingRouterParser());
|
||||
registerBeanDefinitionParser("validating-filter", new XmlPayloadValidatingFilterParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.integration.xml.config;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class XmlPayloadValidatingFilterParser extends AbstractConsumerEndpointParser {
|
||||
private static String SELECTOR =
|
||||
"org.springframework.integration.xml.selector.SchemaValidatingMessageSelector";
|
||||
private static String FILTER =
|
||||
"org.springframework.integration.config.FilterFactoryBean";
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.genericBeanDefinition(FILTER);
|
||||
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(filterBuilder, element, "discard-channel");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(filterBuilder, element, "throw-exception-on-rejection");
|
||||
|
||||
BeanDefinitionBuilder selectorBuilder = BeanDefinitionBuilder.genericBeanDefinition(SELECTOR);
|
||||
selectorBuilder.addConstructorArgValue(element.getAttribute("schema-location"));
|
||||
selectorBuilder.addPropertyValue("schemaType", element.getAttribute("schema-type"));
|
||||
|
||||
filterBuilder.addPropertyValue("targetObject", selectorBuilder.getBeanDefinition());
|
||||
return filterBuilder;
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.integration.xml.config;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
|
||||
import org.springframework.integration.xml.router.SchemaValidator;
|
||||
import org.springframework.integration.xml.router.XmlPayloadValidatingRouter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XmlPayloadValidatingRouterParser extends
|
||||
AbstractConsumerEndpointParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseHandler(Element element,
|
||||
ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.genericBeanDefinition();
|
||||
builder.getBeanDefinition().setBeanClass(
|
||||
XmlPayloadValidatingRouter.class);
|
||||
String channelResolver = element.getAttribute("channel-resolver");
|
||||
|
||||
String validChannelName = element.getAttribute("valid-channel");
|
||||
String invalidChannelName = element.getAttribute("invalid-channel");
|
||||
String schemaType = element.getAttribute("schema-type");
|
||||
String schemaLocation = element.getAttribute("schema-location");
|
||||
|
||||
Assert.state(schemaType.equals("xml-schema")
|
||||
|| schemaType.equals("relax-ng"), "Unrecognised schema type "
|
||||
+ schemaType);
|
||||
|
||||
|
||||
Assert.state(StringUtils.hasText(invalidChannelName)
|
||||
&& StringUtils.hasText(validChannelName),
|
||||
"valid-channel and invalid-channel must both be specified");
|
||||
|
||||
builder.addConstructorArgValue(validChannelName);
|
||||
builder.addConstructorArgValue(invalidChannelName);
|
||||
|
||||
|
||||
|
||||
BeanDefinition validatorBeanDefinition;
|
||||
if (schemaType.equals("xml-schema")) {
|
||||
validatorBeanDefinition = createValidator(XMLConstants.W3C_XML_SCHEMA_NS_URI, schemaLocation);
|
||||
} else {
|
||||
validatorBeanDefinition = createValidator(XMLConstants.RELAXNG_NS_URI, schemaLocation);
|
||||
}
|
||||
builder.addConstructorArgValue(validatorBeanDefinition);
|
||||
|
||||
|
||||
if (StringUtils.hasText(channelResolver)) {
|
||||
builder.addPropertyReference("channelResolver", channelResolver);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
protected BeanDefinition createValidator(String schemaType, String schemaLocation){
|
||||
BeanDefinitionBuilder xmlValidator = BeanDefinitionBuilder
|
||||
.genericBeanDefinition();
|
||||
xmlValidator.getBeanDefinition().setBeanClass(SchemaValidator.class);
|
||||
xmlValidator.addConstructorArgValue(schemaLocation);
|
||||
xmlValidator.addConstructorArgValue(schemaType);
|
||||
|
||||
return xmlValidator.getBeanDefinition();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.integration.xml.router;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.router.AbstractSingleChannelNameRouter;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
|
||||
public class XmlPayloadValidatingRouter extends AbstractSingleChannelNameRouter{
|
||||
|
||||
private final String validMessageChannelName;
|
||||
|
||||
private final String invalidMessageChannelName;
|
||||
|
||||
private final XmlValidator xmlValidator;
|
||||
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
|
||||
public XmlPayloadValidatingRouter(String validMessageChannelName,
|
||||
String invalidMessageChannelName, XmlValidator xmlValidator) {
|
||||
super();
|
||||
this.validMessageChannelName = validMessageChannelName;
|
||||
this.invalidMessageChannelName = invalidMessageChannelName;
|
||||
this.xmlValidator = xmlValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converter used to convert payloads prior to validation
|
||||
*
|
||||
* @param converter
|
||||
*/
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String determineTargetChannelName(Message<?> message) {
|
||||
return xmlValidator.isValid(converter.convertToSource(message.getPayload())) ? validMessageChannelName : invalidMessageChannelName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.integration.xml.selector;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.core.MessageSelector;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
import org.xml.sax.SAXParseException;
|
||||
/**
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class SchemaValidatingMessageSelector implements MessageSelector{
|
||||
|
||||
private final XmlValidator xmlValidator;
|
||||
private volatile String schemaType = XmlValidatorFactory.SCHEMA_W3C_XML;
|
||||
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
|
||||
public SchemaValidatingMessageSelector(Resource schema) throws Exception{
|
||||
Assert.notNull(schema, "You must provide XML schema location to perform validation");
|
||||
this.xmlValidator = XmlValidatorFactory.createValidator(schema, schemaType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converter used to convert payloads prior to validation
|
||||
*
|
||||
* @param converter
|
||||
*/
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public void setSchemaType(String schemaType) {
|
||||
this.schemaType = schemaType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Message<?> message) {
|
||||
// TODO Need to figure out how the exceptions could be propagated since the return from this method is true/false
|
||||
// and 'throw-exception-on-rejection'is actually set on the filter
|
||||
try {
|
||||
SAXParseException[] validationExceptions = xmlValidator.validate(converter.convertToSource(message.getPayload()));
|
||||
return validationExceptions.length == 0 ? true : false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new MessageHandlingException(message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -510,11 +510,11 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="validating-router">
|
||||
<xsd:element name="validating-filter">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a validating router.
|
||||
Defines a validating filter.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
@@ -530,17 +530,8 @@
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="channel-resolver" type="xsd:string" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.ChannelResolver"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="valid-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="invalid-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="output-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="discard-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="schema-location" use="required" />
|
||||
<xsd:attribute name="schema-type" default="xml-schema">
|
||||
<xsd:simpleType>
|
||||
|
||||
Reference in New Issue
Block a user