Retrieving an HTTP status code from a SOAP response

How can I get the HTTP status from the result of SOAPConnection.call() ?

+6
source share
4 answers

Adapted from W3C Note in SOAP (Section 6.2)

HTTP SOAP follows the semantics of HTTP status codes to convey status information in HTTP. For example, the status 2xx code indicates that a client request that includes the SOAP component has been successfully accepted, understood and accepted, etc.

In the event of a SOAP error when processing a SOAP request, the HTTP server MUST send an HTTP 500 response "Internal Server Error" and include the SOAP message in the response containing the SOAP Fault element (see section 4.4), indicating a SOAP processing error.

And from the SOAPFault documentation in the API

An element of a SOAPBody object containing error and / or status information . This information may relate to errors in the SOAPMessage of the object or problems that are not related to the content in the message itself.

So a possible answer might be

 SoapMessage soapMessage = null; soapMessage = MySOAPConnection.call(...); soapMessage.getSOAPPart().getEnvelope().getBody().getFault().getFaultCode(); 

Some links that helped me create this answer:

+8
source

The simple answer is: you cannot. Expressing the HttpSOAPConnection code, the local instance of the HttpURLConnection object is used to actually communicate with the target service. This does the httpResponse code, but it more or less completely hides it from the caller. All you conclude is that if you don't get an exception, but the returned SOAPMessage contains SOAPFault, then the return code was HttpURLConnection.HTTP_INTERNAL_ERROR (i.e. 500). No exception and SOAPFault does not mean that the return code ranged from 200 to 206, all of which are "SUCCESS" - unfortunately, the status record from the HTTP headers in the HttpURLConnection object is not explicitly copied to the MIMEHeaders in the returned SOAPMessage ...

 // Header field 0 is the status line so we skip it. 

Everything else will throw an exception, and the code will start after the open parenthesis in the exception message field and will probably be three-digit, it is difficult to be precise, because someone forgot the closed parenthesis or any other delimiter before the message ...

 throw new SOAPExceptionImpl( "Bad response: (" + responseCode + httpConnection.getResponseMessage()); 

For instance:

 com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (502internal error - server connection terminated 

It terribly relies on formatting the text message in the exception, but the response code no longer displays.

+4
source

You can access HTTP headers through the MessageContext interface.

http://docs.oracle.com/javaee/5/api/javax/xml/ws/handler/MessageContext.html

The easiest way is probably to implement a SOAPHandler that will give you access to the MessageContext:

http://docs.oracle.com/cd/E15051_01/wls/docs103/webserv_adv/handlers.html#wp222394

However, SOAP applications should generally not interoperate with HTTP status codes, as they are transport specific.

+1
source

I have a similar requirement mentioned in this question, a business analyst wants to register every HTTP response code associated with each incoming and outgoing soap calls. My answer is valid for Apache CXF 2.7.5.

You can access the http status through the MessageContext interface using the code snippet below in the javax.xml.ws.handler.soap.SoapHandler interface implementation.

 int status = (( javax.servlet.http.HttpServletResponse)messageContext.get("HTTP.RESPONSE")).getStatus(); 
0
source

All Articles