I’m trying to read XML that is being pushed to my java app. I originally had this in my glassfish server working. The working code in glassfish is as follows:
public class XMLPush implements Serializable
{
public void processXML()
{
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try
{
br = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getReader ();
String s = null;
while((s = br.readLine ()) != null)
{
sb.append ( s );
}
//other code to process xml
...........
.............................
}catch(Exception ex)
{
XMLCreator.exceptionOutput ( "processXML","Exception",ex);
}
....
.....
}//processXML
}//class
It works perfect, but my client is unable to have glassfish on their server. I tried grabbing the raw xml from php, but I couldn’t get it to work. I decided to open up a socket and listen for the xml push manually. Here is my code for receiving the push:
public class ListenerService extends Thread
{
private BufferedReader reader = null;
private String line;
public ListenerService ( Socket connection )thows Exception
{
this.reader = new BufferedReader ( new InputStreamReader ( connection.getInputStream () ) );
this.line = null;
}//ListenerService
@Override
public void run ()
{
try
{
while ( (this.line = this.reader.readLine ()) != null)
{
System.out.println ( this.line );
........
}//while
} System.out.println ( ex.toString () );
}
} catch ( Exception ex )
{
...
}//catch
}//run
I haven’t done much socket programing, but from what I read for the past week is that passing the xml into a string is bad. What am I doing wrong and why is it that in glassfish server it works, and when I just open a socket myself it doesn’t?
this is all that I receive from the push:
PUT /?XML_EXPORT_REASON=ResponseLoop&TIMESTAMP=1292559547 HTTP/1.1
Host: ************************
Accept: */*
Content-Length: 470346
Expect: 100-continue
<?xml version="1.0" encoding="UTF-8" ?>
Where did the xml go? Is it because I am placing it in a string? I just need to grab the xml and save it into a file and then process it. Everything else works, but this.Any help would be greatly appreciated.
The first line of the XML had a line terminator (\r\n), but the rest of the XML did not. This is the reason why my threads would get stuck. I used
and casted it into char:
and stuck all the data into an xml file I created and was able to do everything correctly.
The only thing is I had a similar problem at the end, so I had to check to see if I reached the end of the XML document. I just put the last 9 characters of the stream into an arraylist and checking it against the last part of what the XML file is supposed to end with. Works fine now, and the client (and I as well) is very happy. 🙂 Hope this helps.
Oh one final note. I read that JDOM can actual use the input stream directly. I haven’t tested it yet, but that would be more efficient to use, if the line termination will not be a problem either. Maybe someone else has used JDOM with inputstream and has had a line termination issue?