my dog learned polymorphism*
The moose likes Web Services and the fly likes axis2: set SC_BAD_REQUEST with AxisFault Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "axis2: set SC_BAD_REQUEST with AxisFault" Watch "axis2: set SC_BAD_REQUEST with AxisFault" New topic
Author

axis2: set SC_BAD_REQUEST with AxisFault

craig mclellen
Greenhorn

Joined: Feb 19, 2007
Posts: 9
Hi,
Is it possible to have the status code of the response to an axis web service request set to 400 while still having the soap fault in the payload? Could you provide me with the code as the code below does not do the trick .
<code>
MessageContext faultMsgCtx = MessageContextBuilder.createFaultMessageContext(msgCtx, excep);
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
Engine.sendFault(faultMsgCtx);
servlet.destroy();
</code>

Thanks, and much appreciated.
Kindest regards,
Craig
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
  • In SOAP 1.1 a fault is always returned with a 500 "Internal Server Error" HTTP Status.
  • In SOAP 1.2 the 400 "Bad Request" HTTP status is only used if the SOAP envelope is unreadable or if the returned fault code value is {http://www.w3.org/2003/05/soap-envelope}Sender.

  • Therefore:
  • In the case of SOAP 1.1 you shouldn't change the HTTP status code to 400.
  • In the case of SOAP 1.2 the SOAP web service stack should issue 400 "Bad Request" if the SOAP envelope is missing or garbled or if you are returning a SOAP fault with a fault code value of {http://www.w3.org/2003/05/soap-envelope}Sender.

  • So the only way you should be able to effect a 400 "Bad Request" HTTP status code on the HTTP response is if you are
  • Using SOAP 1.2 and
  • Issuing a fault with a {http://www.w3.org/2003/05/soap-envelope}Sender fault code value, e.g.

  • throw new org.apache.axis2.AxisFault("Parameter out of range",new javax.xml.namespace.QName( "http://www.w3.org/2003/05/soap-envelope","Sender" ) );

    W3C SOAP 1.1 Note: 6.2 SOAP HTTP Response
    SOAP HTTP follows the semantics of the HTTP Status codes for communicating status information in HTTP. For example, a 2xx status code indicates that the client's request including the SOAP component was successfully received, understood, and accepted etc.

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


    Basic Profile Version 1.1: 3.3.1 Identifying SOAP Faults
    Some consumer implementations erroneously use only the HTTP status code to determine the presence of a Fault. Because there are situations where the Web infrastructure changes the HTTP status code, and for general reliability, the Profile requires that they examine the envelope. A Fault is an envelope that has a single child element of the soap:Body element, that element being a soap:Fault element.

    R1107 A RECEIVER MUST interpret a SOAP message as a Fault when the soap:Body of the message has a single soap:Fault child.


    SOAP Version 1.2 Part 2: Adjuncts (Second Edition): 7.5.1 Behavior of Responding SOAP Node

    [PR: Corrected SOAP 1.2 namespace]


    "Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
    Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
    craig mclellen
    Greenhorn

    Joined: Feb 19, 2007
    Posts: 9
    Excellent, thank you so much Peer.

    I'm using SOAP 1.2. I've then replaced the code I had before with
    <code>
    resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    throw new org.apache.axis2.AxisFault("Parameter out of range",new javax.xml.namespace.QName( "http://www.w3.org/2002/12/soap-envelope","Sender" ) );
    </code>

    but still getting a 500 error as below - do you know what I may be doing wrong?

    HTTP/1.1 500 Internal Server Error
    Server: Apache-Coyote/1.1
    Content-Type: application/soap+xml; action="urn:helloException";charset=utf-8
    Transfer-Encoding: chunked
    Date: Fri, 27 Feb 2009 14:23:01 GMT
    Connection: close

    1ba
    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><soapenv:Fault xmlns:axis2ns7="http://www.w3.org/2002/12/soap-envelope"><soapenv:Code><soapenv:Value>axis2ns7:Sender</soapenv:Value></soapenv:Code><soapenv:Reason><soapenv:Text xml:lang="en-US">Parameter out of range</soapenv:Text></soapenv:Reason><soapenvetail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>
    0
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2906
    Silly mistake. I was looking at a "Candidate Recommendation" the not the most recent "Recommendation". The SOAP envelope gave it away:

    If I would have given you the correct namespace it should have been soapenv:Sender.

    SOAP Version 1.2 Part 2: Adjuncts (Second Edition): 7.5.1 Behavior of Responding SOAP Node

    Therefore change
    {http://www.w3.org/2002/12/soap-envelope}Sender
    to
    {http://www.w3.org/2003/05/soap-envelope}Sender

    e.g.
    new org.apache.axis2.AxisFault("Parameter out of range",new javax.xml.namespace.QName( "http://www.w3.org/2003/05/soap-envelope","Sender" ) );

    See if that finally works properly ...
    craig mclellen
    Greenhorn

    Joined: Feb 19, 2007
    Posts: 9
    Hi Peer,

    Thanks again for your reply, and sorry for the late response, I've only now managed to log onto my machine and saw your reply. I had tried what you mentioned but strangely, it's still not working - this is the response I'm getting.

    <code>
    HTTP/1.1 500 Internal Server Error
    Server: Apache-Coyote/1.1
    Content-Type: application/soap+xml; action="http://www.w3.org/2005/08/addressing/soap/fault";charset=utf-8
    Transfer-Encoding: chunked
    Date: Sun, 01 Mar 2009 19:23:16 GMT
    Connection: close

    1ba
    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><soapenv:Fault xmlns:axis2ns3="http://www.w3.org/2003/05/soap-envelope"><soapenv:Code><soapenv:Value>axis2ns3:Sender</soapenv:Value></soapenv:Code><soapenv:Reason><soapenv:Text xml:lang="en-US">Parameter out of range</soapenv:Text></soapenv:Reason><soapenvetail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>
    0
    </code>

    Thanks again Peer.
    Eagerly awaiting your response.
    Kindest regards,
    Craig
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2906
    This time you mispelled the namespace.

    It's supposed to be
    http://www.w3.org/2003/05/soap-envelope
    not
    http://www.w3.org/2003/05/soapenvelope
    Note the missing "-" (dash).
    craig mclellen
    Greenhorn

    Joined: Feb 19, 2007
    Posts: 9
    Hi Peer,

    Sorry, it probably seemed that I mispelt it, but the dash is there. I've since learnt to format the xml in coderanch ;) to make it a little more legible. I've run the same piece of code again and captured the output - below is the result. I've also appended the java source incase you are able to pickup something I may be missing.



    The java source


    Thanks again Peer for your help with this, I'm extrememly desperate, and truly appreciate your time.
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2906
    Well, it is supposed to - have a look at the Axis2 1.4.1 source

    org.apache.axis2.transport.http.AxisServlet


  • Get rid of any code relating to the servlet response in your code - for all we know that may alter the fault handling strategy - simply throw the fault
  • Use new org.apache.axis2.AxisFault("Parameter out of range", org.apache.axiom.soap.SOAP12Constants.QNAME_SENDER_FAULTCODE ). I'm a bit puzzled that Axiom creates another namespace prefix when the fault code is clearly in the SOAP 1.2 namespace
  • If all else fails, debug the AxisServlet.handlefault method - that may tell you what you need to do to force the logic to go down the appropriate decision path.
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: axis2: set SC_BAD_REQUEST with AxisFault
     
    Similar Threads
    jsp:setProperty not working for me.
    Constructor injection for Set property
    Generics Dought
    Changing Number, String to more general type Object
    how to get the value saved in request scope in a servlet into a jsp page