Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Spring Ws - Loading Relative Includes in XSD files (Tomcat 8)

I created a Spring web service which creates a dynamic WSDL from a collection of XSD files using the following code:

Resource[] schema = {
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/NarrativeBlock.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/datatypes-base.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/infrastructureRoot.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/multicacheschemas/PRPA_IN201305UV02.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/multicacheschemas/PRPA_IN201306UV02.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/IHE/XCPD_PLQ.xsd"),
            new ClassPathResource(
                    "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/XCPD_PRPA.xsd") };
    CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection(
            schema);
    collection.setInline(true);
    return collection;

The XSD files used to create the dynamic WSDL include other various schema files using include statements like the following:

<xs:include schemaLocation="../coreschemas/voc.xsd"/>
<xs:include schemaLocation="../coreschemas/datatypes.xsd"/>

When I run the code in a Tomcat 8 container, I receive the following exception:

Caused by: java.lang.IllegalArgumentException: The resource path [/../coreschemas/infrastructureRoot.xsd] has been normalized to [null] which is not valid

Spring's URI resolver prepends the "/" in front of the path, even though this file being referenced is a relative path (not an absolute path) and fails when importing the schema.

It should be noted that this application ran fine on Tomcat 7. When trying to migrate it to Tomcat 8, the issues are arising.

Tomcat 8 did change the way how web resources are now being loaded. More Information from Java CodeRanch...

Any ways long story short, is there a way to force Spring that the URI resolver should treat relative files correctly? If I look at the "collectionBaseURI" property on the resolver Spring uses (ClasspathUriResolver), this value is null. Is there a way to set this base URI as well?

EDIT I can fix this issue by converting all relative paths to absolute paths to schemas... however I don't want to apply this fix across hundreds of files.

like image 368
Mike Melusky Avatar asked Jun 11 '15 00:06

Mike Melusky


1 Answers

In case anyone runs into this annoying issue, the ClasspathUriResolver was the culprit, prepending a "/" on relative path includes. I switched it over to use a default URI Resolver (in org.apache.ws.commons.schema.resolver.DefaultURIResolver) and it runs fine without issue on Tomcat 8.

CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection(schema);
collection.setUriResolver(new DefaultURIResolver());
collection.setInline(true);
return collection;
like image 116
Mike Melusky Avatar answered Oct 16 '22 05:10

Mike Melusky