tags:

views:

30

answers:

2

Java journeyman but Web service noob

I have a class that unmarshals xml from a 3rd party source (I have no control over the content). Here is the snippet that unmarshals:

JAXBContext jContext = JAXBContext.newInstance("com.optimumlightpath.it.aspenoss.xsd"); 
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
StringReader xmlStr = new StringReader(str.value);
Connections conns =(Connections) unmarshaller.unmarshal(xmlStr); 

Connections is a class generated dtd->xsd->class using xjc. the package com.optimumlightpath.it.aspenoss.xsd contains all such classes.

The xml I recieve contains a relative path in the DOCTYPE. Basically str.value above contains:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE Connections SYSTEM "./dtd/Connections.dtd">
<Connections>
...
</Connections>

This runs successfully as a java 1.5 application. In order to avoid the error above, I had to create a ./dtd directory off the project root and include all the dtd files (not sure why I had to do this but we'll get to that).

I've since created a web service on Tomcat5.5 that uses the above class. I am getting [org.xml.sax.SAXParseException: Relative URI "./dtd/Connections.dtd"; can not be resolved without a document URI.] on the unmarshal line. I have tried creating ./dtd in every relavant folder (project root, WebContent, WEB-INF, tomcat working dir, etc) to no avail.

Question #1: Where can I locate ./dtd so that the class can find it when run as a tomcat webservice? Is there any tomcat or service config I need to do in order to get the directory recognized?

Question #2: Why does the class even need the dtd file in the first place? Doesn't it have all the information it needs to unmarshal in the annotations of the dtd->xsd->class? I've read many posts about disabling validation, setting EntityResource, and other solutions, but this class isn't always deployed as a web-service and I don't want to have two code trains.

Bill

A: 

Question #2: Why does the class even need the dtd file in the first place?

It is not the JAXB implementation that is looking for the DTD, it is the underlying parser.

Question #1: Where can I locate ./dtd so that the class can find it when run as a tomcat webservice?

I'm not sure, but below I'll demonstrate a way you can make this work using the MOXy JAXB implementation (I'm the tech lead) that will work in multiple environments.

Proposed Solution

Create an EntityResolver that loads the DTD from the classpath. This way you can package the DTD with your application and you will always know where it is regardless of the deployment environment.

public class DtdEntityResolver implements EntityResolver {

    public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
        InputStream dtd = getClass().getClassLoader().getResourceAsStream("dtd/Connections.dtd");
        return new InputSource(dtd);
    }

}

Then using the MOXy JAXB implementation you can cast down to the underlying implementation and set the EntityResolver.

import org.eclipse.persistence.jaxb.JAXBHelper;
...
JAXBContext jContext = JAXBContext.newInstance("com.optimumlightpath.it.aspenoss.xsd");
Unmarshaller unmarshaller = jContext.createUnmarshaller() ;
JAXBHelper.getUnmarshaller(unmarshaller).getXMLUnmarshaller().setEntityResolver(new DtdEntityResolver());
StringReader xmlStr = new StringReader(str.value);
Connections conns =(Connections) unmarshaller.unmarshal(xmlStr);
Blaise Doughan
Blaise - ty for taking the time to respond. Initially, JAXBHelper complained that the unmarshaller was not an eclipselink unmarshaller. So I replaced javax.xml.bind.JAXBContext with org.eclipse.persistence.jaxb.JAXBContext and javax.xml.bind.Unmarshaller with org.eclipse.persistence.jaxb.JAXBUnmarshaller. However, the eclipselink JAXBContext returns a javax JAXBContext type. JAXBUnmarshaller requires a eclipselink type and I am getting cast exceptions if I try to re-cast. Any ideas?
Bill Dolan
You need to add a file named jaxb.properties file in with your model classes with the following entry: javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Blaise Doughan
I may try that at another time. I was able to make Jorn's answer work although it will require more code changes. As you say it is the most portable. Thank you both very much!! When I get a 15 rep, I can vote up your answer as well.
Bill Dolan
+2  A: 

When unmarshalling from an InputStream or Reader the parser does not know the systemId (uri / location) of the document, so it can not resolve relative paths. It seems the parser tries to resolve references using the current working directory, which only works when running from the ide or command line. In order to override this behaviour and do the resolving yourself you need to implement an EntityResolver, as Blaise Doughan mentioned.

After some experimenting I found a standard way of doing this. You need to unmarshal from a SAXSource, which is in turn constructed from an XMLReader and an InputSource. In this example the dtd is located next to the annotated class and so can be found in the classpath.

Main.java

public class Main {
    private static final String FEATURE_NAMESPACES = "http://xml.org/sax/features/namespaces";
    private static final String FEATURE_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";

    public static void main(String[] args) throws JAXBException, IOException, SAXException {
        JAXBContext ctx = JAXBContext.newInstance(Root.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();

        XMLReader xmlreader = XMLReaderFactory.createXMLReader();
        xmlreader.setFeature(FEATURE_NAMESPACES, true);
        xmlreader.setFeature(FEATURE_NAMESPACE_PREFIXES, true);
        xmlreader.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                // TODO: Check if systemId really references root.dtd
                return new InputSource(Root.class.getResourceAsStream("root.dtd"));
            }
        });

        String xml = "<!DOCTYPE root SYSTEM './root.dtd'><root><element>test</element></root>";
        InputSource input = new InputSource(new StringReader(xml));
        Source source = new SAXSource(xmlreader, input);

        Root root = (Root)unmarshaller.unmarshal(source);
        System.out.println(root.getElement());
    }
}

Root.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
    @XmlElement
    private String element;

    public String getElement() {
        return element;
    }

    public void setElement(String element) {
        this.element = element;
    }
}

root.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT root (element)>
<!ELEMENT element (#PCDATA)>
Jörn Horstmann
Jorn - ty for your response. I am trying Blaise's suggestion first as it requires the least code changes. But you both have provided very helpful answers. Is there a way I can credit both?
Bill Dolan
My approach will work, but the advantage of Jorn's approach is that you stay JAXB implementation agnostic which is the most portable solution.
Blaise Doughan