user2191296
user2191296

Reputation: 65

Apache Camel XML Validator

I am using apache camel v2.12 with blueprint. In one of my route I am exposing a jetty endpoint which consumes xml via POST request. I want to validate the inbound xml against the xsd file which is in root of another bundle. I have included that dependency in my pom file and specified bundle in features.xml.

Here is my route definition:

<camel:route id="someId">
        <camel:from uri="{{jetty.uri}}"/>
        <camel:to uri="validator:/sample.xsd"/>         
        <camel:convertBodyTo type="java.lang.String"/>      
        <camel:to uri="{{to.target.uri}}" pattern="InOnly"/>            
</camel:route>

I get the following exception in log file

org.apache.camel.FailedToCreateRouteException: Failed to create route someId at: >>> To[validator:/sample.xsd] <<< in route: Route(someId)[[From[{{jetty.uri... because of Failed to resolve endpoint: validator:///sample.xsd due to: Cannot find resource: /sample.xsd in classpath for URI: /sample.xsd
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:912)[143:org.apache.camel.camel-core:2.12.0.redhat-610379]
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:177)[143:org.apache.camel.camel-core:2.12.0.redhat-610379]
at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:778)[143:org.apache.camel.camel-core:2.12.0.redhat-610379]
at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:1955)[143:org.apache.camel.camel-core:2.12.0.redhat-610379]

If I place sample.xsd under working module 'src/main/resources' then camel validator component is able to look up. I do remove slash '/'from front

<to uri="validator:sample.xsd"/>

I want to avoid duplicating schema and just refer to it from another bundle.

I wrote a test java class to check whether resource is loadable or not and its able to find the xsd under jar file

System.out.println(Test.class.getResource("/sample.xsd"));

Please help how to refer xsd file from different bundle.

Upvotes: 0

Views: 2558

Answers (1)

Vassilis
Vassilis

Reputation: 1054

You should be able to export your XSD file, let's say, from bundle A (A has the actual XSD inside its jar) and import it from bundle B (B does not have the XSD in its jar). This should be exactly the same as with import/ export of classes.

Bundle A
<Export-Package>my.package.with.xsd.file,*</Export-Package>

Alternatively, you could try to have bundle A to read the file and export an OSGI service that bundle B will consume. Then the issue could be if Camel Validator Producer can accept directly an InputStream and not an XSD file. Take a look at org.apache.camel.processor.validation.ValidatingProcessor and ValidatorEndpoint. But in any case, you can do the validation manually, in a Java Processor in your Route.

Upvotes: 1

Related Questions