File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes Sending a Soap error response to Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Sending a Soap error response to Client" Watch "Sending a Soap error response to Client" New topic
Author

Sending a Soap error response to Client

Mat Anthony
Ranch Hand

Joined: May 21, 2008
Posts: 232
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

Joined: Jul 09, 2001
Posts: 1167
Good Day Mat,

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

Regards,
Dan



William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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?


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Mat Anthony
Ranch Hand

Joined: May 21, 2008
Posts: 232
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending a Soap error response to Client
 
Similar Threads
RequestDispatcher working, but ultimately sends an empty HTML page to client
Apache SOAP Client for .NET Web Service
Response filter - doFilter() never returns
Apache SOAP newbie
Adding an error to the ActionError in struts