1

I need to validate a XML file whose schema URLs (referenced at schemaLocation attribute) return an HTTP 301 error pointing out the correct URL. Moreover, those schemas have schema imports whose URLs return HTTP 301 pointing out the correct URL too.

JDom2 seems unable to resolve the HTTP 301 errors like a simple browser does. Is there a way to force JDom2 to resolve properly such an error?. What JDom2 class/method i must override to do that?. Is there another java XML library that can do that?

FYI: The erroneous URLs are like http://schema-url and the HTTP 301 errors return URLs like

----------------------- some progresses later ------------------------------

A first approach to solve to this problem could be something like this:

JDOM2 - Follow Redirects (HTTP Error 301)

i.e, using the folling snippet:

URL httpurl = new URL(.....);
HTTPURLConnection conn = (HTTPUrlConnection)httpurl.openConnection();
conn.setInstanceFollowRedirects(true);
conn.connect();
Document doc = saxBuilder.build(conn.getInputStream());

But in my case, the application reads the xml from a gzip xml file, so it will not work. I have decided use something like:

final SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
builder.setEntityResolver(new RedirectEntityResolver());
document = builder.build(isXml);

Where isXml is the decompressed XML file input stream and RedirectEntityResolver is coded like:

public class RedirectEntityResolver implements EntityResolver {

    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

        URL httpurl = new URL(systemId);
        HttpURLConnection conn = (HttpURLConnection) httpurl.openConnection();
        conn.setInstanceFollowRedirects(true);
        conn.connect();

        return new InputSource(conn.getInputStream());
    }

}

But it doesn't work. The HttpURLConnection seems unable to resolve the redirects and i get the same error:

org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'Document Moved'.
Community
  • 1
  • 1

1 Answers1

2

The problem is that conn.setInstanceFollowRedirects(true); is not working. View URLConnection Doesn't Follow Redirect anwsers

Instead, using an EntityResolver like the following, it runs perfectly:

public class RedirectEntityResolver implements EntityResolver {


    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

        URL obj = new URL(systemId);
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

        int status = conn.getResponseCode();
        if ((status != HttpURLConnection.HTTP_OK) &&
            (status == HttpURLConnection.HTTP_MOVED_TEMP
            || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER)) {

            String newUrl = conn.getHeaderField("Location");
            conn = (HttpURLConnection) new URL(newUrl).openConnection();
        }

        return new InputSource(conn.getInputStream());

    }

}
Community
  • 1
  • 1