Question

We are in the process of migrating functionality from a Weblogic Server 8,1 sp5 with java 1.4 to 10.3.6 with java 1.7.

The case that is described below is working properly in the old server, however we are facing an issue when transferring the handling to the new one. The problem lies while retrieving and parsing an XML response that was retrieved from an external system through SOAP calls.

The following libraries and procedure are used in the method:

  1. java.net.HttpURLConnection to make the connection
  2. java.io.OutputStream to send the request
  3. java.io.InputStream to get the response
  4. byte[] to store the result before transforming in to String
  5. javax.xml.parsers.DocumentBuilder, java.io.StringReader and org.xml.sax.InputSource to transform the String into org.w3c.dom.Document
  6. The following exception is thrown: "org.xml.sax.SAXParseException - Content is not allowed in trailing section."

When opening the logs of the application with notepad++ many null characters appear after the end of the file which seem to cause the issue. I repeat that no such cases appear when executing the request from the old server.

The respective code is the following:

//Creating the connection
URL u = new URL(default_server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(requestMethod);
connection.setRequestProperty("SOAPAction", soap_action);

OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);

wout.write(xmlString);
wout.flush();
wout.close();

InputStream in = connection.getInputStream();

int c = in.available();
byte r[] = new byte[c];
in.read(r);
String response = new String(r);
connection.disconnect();

//Transorming the String to XML Doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader theReader = new StringReader(response);
InputSource theInputSource = new InputSource();
theInputSource.setCharacterStream(theReader);
Document doc = builder.parse(theInputSource); 
//Here occurs org.xml.sax.SAXParseException-Content is not allowed in trailing section

return doc;

I know that i can solve the issue by getting the stripping the response from junk characters but this not a safe resolution. Do you have any information to share on the matter? Do you think it is a java version issue or maybe a server configuration issue? Thank you in advance your time.

Best Regards, George

Was it helpful?

Solution

I see two issues

  • in.available() as per the javadoc: Returns an estimate of the number of bytes... Do not rely on this. Loop over buffers of 8K to read the stream until you reach the end or even better, do not re-invent the wheel, use commons-io from Apache and use a single call to ÌOUtils.read
  • String response = new String(r); doing this you are assuming that the bytes received are encoded using the same charset as your platform encoding/charset. It is unlikely to be the case if you are on Windows or OSX. You must pass the charset and use the constructor String(byte[] bytes, Charset charset).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top