Rainer
Rainer

Reputation: 11

XMLInputFactory Error on IPhone App (not on Browser)

I have a confusing error on a different behaviour between IPhone Conflunce App Version 1.37.0.1 and Web-Browser.

Technical the macro receives Data (XML) from a Redmine server. The XML is stored in a ByteArray Class.
Storing in a ByteArray allows to cache the Redmine server response for a while.

For parsing it was wrapped by ByteArray Stream:

public InputStream httpGetAsInputStream(String url) throws IOException {
   byte[] result = httpGetAsString(url).getBytes(UTF_8);
   return new ByteArrayInputStream(result);
}

private static void parse_xml(InputStream in, TicketState t) throws Exception {
   try {
      // ---> THIS FAILS  on IPhone only
       XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
            ...
    }   ...
}

And this is how the methods are invoked:

try {
     InputStream strm =  ....httpGetAsInputStream(tmpurl);
     parse_xml(strm, t);
     strm.close();
}

The XML Output is parsed and rendered as a HTML-Table.

What is realy confusing to me is that the Webbrowser works like expected, but the IPhone App throws the Exception: Provider for Class javax.xml.stream.XMLInputFactory cannot be created.

enter image description here

Well that is the server side … there should be no difference between IPhone App and Browser.

Upvotes: 0

Views: 30

Answers (2)

Rainer
Rainer

Reputation: 11

The Issue is about the class loader. How ever for IPhone App access to the conflunce server the class loader has to be set to the conflunce class loader instead of Java System class loader.

private static XMLInputFactory xmlf = null;
private final static Object xmlf_mutex = new Object();

/* Create a XMLInputFactory with Confluence Class Loader instead of system class loader
 * This is required by iPhone Confluence App Web-API. */
private static XMLInputFactory getXmlFConfl()throws FactoryConfigurationError {
    if(xmlf == null) { synchronized (xmlf_mutex) {
        ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
        ClassLoader webappClassLoader = ConfluenceEvent.class.getClassLoader();
        Thread.currentThread().setContextClassLoader(webappClassLoader);
        xmlf = XMLInputFactory.newInstance();
        Thread.currentThread().setContextClassLoader(currentCL);
    }}
   return xmlf;
}

After that just replace the XMLInputFactory call:

-  XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
+  XMLStreamReader reader = getXmlFConfl().createXMLStreamReader(in);

Upvotes: 0

lance-java
lance-java

Reputation: 27994

I'm guessing there's an issue with encoding (iphone sending the response using a different encoding to browser).

Why are you reading the HTTP response into a string just to convert it to a byte array? Why don't you read the response bytes directly into a byte array?

Upvotes: 0

Related Questions