You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

The validation component provides schema validation of documents using JAXP 1.3 and XMLSchema or RelaxNG.

The component is pretty simple and straightforward; its configured with a schema document and optionally an error handler implementing the MessageAwareErrorHandler interface. If the inbound document is valid, it continues on its way to the ultimate destination. Otherwise a fault is returned in the message exchange.

ServiceMix configuration using the default error handler:

<sm:activationSpec componentName="SchemaValidator" service="foo:SchemaValidator" endpoint="SchemaValidator">
    <sm:component>
        <bean class="org.apache.servicemix.components.validation.ValidateComponent">
            <property name="schemaResource" value="classpath:org/apache/servicemix/components/validation/schema.xsd"/>
        </bean>
    </sm:component>
</sm:activationSpec>

The default error handler simply counts the number of warnings, errors and fatal errors and if the number of errors and fatal errors is greater than zero then a fault message is returned. Another implementation is available which will capture the messages and output them in an xml format as below:

<rootPath xmlns="namespace">
    <warning><![CDATA[ warning message ]]></warning>
    <error><![CDATA[ error message ]]></error>
    <fatalError><![CDATA[ fatal error message ]]></fatalError>
</rootPath>

Each warning, error or fatal error message will produce a node as shown above. The rootPath represents an arbitrarily deep node path at which to locate the message nodes. The root element also has a default xml namespace attached to it. The ServiceMix configuration for this component is outlined below:

<sm:activationSpec componentName="SchemaValidator" service="foo:SchemaValidator" endpoint="SchemaValidator">
    <sm:component>
        <bean class="org.apache.servicemix.components.validation.ValidateComponent">
            <property name="schemaResource" value="classpath:org/apache/servicemix/components/validation/schema.xsd"/>
            <property name="errorHandler" ref="messageAggregatingErrorHandler"/>
        </bean>
    </sm:component>
</sm:activationSpec>

And here is the bean definition for the MessageAggregatingErrorHandler:

<bean id="messageAggregatingErrorHandler" class="org.apache.servicemix.components.validation.MessageAggregatingErrorHandler">
    <constructor-arg index="0" value="Fault/payload/messages"/>
    <constructor-arg index="1" value="http://www.servicemix.org/fault"/>
    <constructor-arg index="2" value="false"/>
</bean>
</beans>

Constructor arg zero is the rootPath;
constructor arg one is the namespace; and
constructor arg two is an optional boolean value that if true will output stacktraces of the error messages into the fault xml.

  • No labels