Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Streaming Xml and well formedness

 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am creating a REST style webservice, and I am using jibx xml binding framework to create the response. I configured jibx to use StAX as its parser. I have run into an issue however, and would like to know how other people solve it:

When there is an exception thrown during the binding process, the client could get back an incomplete xml response, since its being streamed.
Something like the following:



What I would really like to do is just have an error response returned, ie catch the exception and modify the response. Currently the way I am solving this issue is by having jibx write to a StringWriter, then I spit that back, and if there is an exception I can clear it out and add my error response:



Is there a better way? The above solution fixes the problem, but now I am creating the entire response in memory. How do other people handle this problem? I'd rather not send back invalid xml...

It seems like a choice between being memory intensive for completeness, or being incomplete for better performance

Also keep in mind that this is not an exception in the business logic, and it should never happen.
[ July 09, 2007: Message edited by: joe black ]
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me elaborate.

The following is an example taken from

http://www.ibm.com/developerworks/xml/library/x-tipstx4/

Which has been modified to show my point. It uses stax, and when an
error is thrown
while writing to the stream, I can recover gracefully and send back well
formed xml with
an error element.



Notice I am throwing an Exception. This would be similar to a
JibxException being thrown during marshalling.
However, I catch this exception, add an error element as the end
element, then call writeEndDocument which
closes all open structures. Can something similar be done with jibx?
instead of sending back xml that is
not well formed? How does any xml to object binding framework handle this use case?

Peter
[ July 09, 2007: Message edited by: joe black ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic