Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending a Soap error response to Client

 
Mat Anthony
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I currently use a filter to capture any ServletRequest coming into my system.
The request is processed and sent onto an Axis2 engine that then calls my web service.

If an exception occurs within the filter I need to be able to transmit a soap message back to the calling client.

Is the following an ideal approach:-

private void soapErrorResponse(String errorMessage, HttpServletRequest request, HttpServletResponse response )throws IOException
{
MyResponseMsg resp = new MyResponseMsg();
resp.setStatus(MyResponseMsg.ERROR);
ArrayList error = new ArrayList();
error.add("This is the error"+errorMessage);
resp.setFaults(error);
String xmlResults = new XStream().toXML(resp);
response.setContentType("text/xml");
ServletOutputStream out =response.getOutputStream();
out.print(xmlResults);
out.close();
}
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day Mat,

I suggest to read the Basic Profile Version 1.0 section about faults at SOAP Fault Syntax.

Regards,
Dan


 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the limited information you have given, I would say NO. It's not an ideal approach. You should use handlers. Handlers are to a web services what filters are to a servlet. They can intercep SOAP request and response and do things with them. Is there any reason why you must use filters?
 
Mat Anthony
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan/Chintan,

thanks for both your replys. Dan I'm currently using a filter because I'm setting up the user via to acegi security within the filter then passing
the request on to my service.

I have tried creating the soap response as below, but get the following error:-
org.apache.xmlbeans.impl.soap.SOAPException: Unable to create message factory for SOAP: null

every time the following line is called:-
MessageFactory messageFactory = MessageFactory.newInstance();


import org.apache.xmlbeans.impl.soap.MessageFactory;
import org.apache.xmlbeans.impl.soap.SOAPMessage;
import org.apache.xmlbeans.impl.soap.SOAPPart;
import org.apache.xmlbeans.impl.soap.SOAPException;
import javax.xml.transform.stream.StreamSource;

private void soapErrorResponse(String errorMessage, HttpServletRequest request, HttpServletResponse response )throws IOException
{
MyResponseMsg resp = new MyResponseMsg();
resp.setStatus(MyResponseMsg.ERROR);
ArrayList error = new ArrayList();
error.add("This is the error"+errorMessage);
resp.setFaults(error);
String xmlResults = new XStream().toXML(resp);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();

//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();

StringBufferInputStream buffer = new StringBufferInputStream(xmlResults);

StreamSource preppedMsgSrc = new StreamSource(buffer);
soapPart.setContent(preppedMsgSrc);

//Save the message
message.saveChanges();

//Output soap error message back to the user
ServletOutputStream out =response.getOutputStream();
message.writeTo(out);
out.close();

}

Has anybody seen this before

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic