A small bug in processing soapfault in axis2

Source: Internet
Author: User


A small bug in processing soapfault in axis2
Gjs0064114 updated: 2006-04-27 21:56:07 $ version

To meet project requirements, the axis2 client must be able to process the soap fault returned by the server. However, it is found that axis2 has a problem with soapfault processing.
First, (1) Specify the format of the soapfault message received by the axis2 engine:

HTTP/1.1 500 Internal Server Error

Service: gsoap/2.7

Content-Type: text/XML; charset = UTF-8

Content-Length: 1916

Connection: Close

<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: wsu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-
SOAP-ENV: Body>
SOAP-ENV: Fault>
<Faultcode> SOAP-ENV: server </faultcode>
<Faultstring> the value of structure ns__ register is null </faultstring>
<Detail>
<A: egoerror xmlns: A = "http://www.ego.com/">
<A: A> error string </a: A>
<A: B> Error Code </a: B>
</A: egoerror>
</Detail>
SOAP-ENV: Fault>
SOAP-ENV: Body>
SOAP-ENV: envelope>
(2) encapsulate the soapfault message in axis2 to Code As follows:
Org. Apache. axis2.clientapi. outmepclient. Java
.....

Public messagecontext invokeblocking (operationdescription axisop,
Final messagecontext msgctx)
Throws axisfault {
Prepareinvocation (axisop, msgctx );

// The Message ID is sent all the time
String messageid = string. valueof (system. currenttimemillis ());
Msgctx. setmessageid (messageid );
//
If (useseparatelistener ){
// This mean doing a request-response invocation using two channel. If
// Transport is two way transport (e.g. http) only one channel is used (e.g. In HTTP Cases
// 202 OK is sent to Say No repsone avalible). axis2 get blocked return when the response is avalible.

Synccallback callback = new synccallback ();
// This method call two channel Non blocking method to do the work and wait on the callbck
Invokenonblocking (axisop, msgctx, callback );
Long Index = timeoutinmilliseconds/100;
While (! Callback. iscomplete ()){
// Wait till the reponse arrives
If (index --> = 0 ){
Try {
Thread. Sleep (100 );
} Catch (interruptedexception e ){
Throw new axisfault (E );
}
} Else {
Throw new axisfault (messages. getmessage ("responsetimeout "));
}
}
// Process the resule of the invocation
If (callback. envelope! = NULL ){
Messagecontext resmsgctx =
New messagecontext (servicecontext. getenginecontext ());
Resmsgctx. setenvelope (callback. envelope );
Return resmsgctx;
} Else {
If (callback. Error instanceof axisfault ){
Throw (axisfault) callback. error;
} Else {
Throw new axisfault (callback. Error );
}
}
} Else {
// This is the usual request-response sync Implemetation
Msgctx. setto ();
Msgctx. setservicecontext (servicecontext );
Configurationcontext syscontext = servicecontext. getenginecontext ();
Msgctx. setconfigurationcontext (syscontext );

Checktransport (msgctx );

// Find and set the operation Context
Configurationcontext syscontext = servicecontext. getenginecontext ();
Axisconfiguration registry = syscontext. getaxisconfiguration ();
Msgctx. setoperationcontext (operationcontextfactory. createoperationcontext (wsdlconstants. mep_constant_in_out,
Axisop,
Servicecontext ));
// Send the SOAP message and receive a response
Messagecontext response =
Twowaytransportbasedsender. Send (msgctx, listenertransport );

// Check for a fault and return the result
Soapenvelope resenvelope = response. getenvelope ();
If (resenvelope. getbody (). hasfault ()){
Soapfault = resenvelope. getbody (). getfault ();
Exception EX = soapfault. getexception ();

If (isexceptiontobethrownonsoapfault ){
// Does the soapfault has a detail element for excpetion
If (ex! = NULL ){
Throw new axisfault (Ex );
} Else {
// If detail element not present create a new exception from the detail
String message = "";

message = message + "code =" + soapfault. getcode () = NULL? "":
soapfault. getcode (). getvalue () = NULL? "": Soapfault. getcode (). getvalue (). gettext ();

Message = message + "reason =" + soapfault. getreason () = NULL? "":
Soapfault. getreason (). getsoaptext () = NULL? "": Soapfault. getreason (). getsoaptext (). gettext ();
System. Out. println ("message" + message );
Throw new axisfault (Message );
}
}
}
Return response;
}
}

.....
Should be changed
Message = message + "code =" + (soapfault. getcode () = NULL? "":
Soapfault. getcode (). getvalue () = NULL? "": Soapfault. getcode (). getvalue (). gettext ());
System. Out. println ("" + message );
Iterator T = soapfault. getdetail (). getalldetailentries ();
Loop (T, 1 );
Org. Apache. axis2.soap. impl. rawm. soapfaultdetailimpl;

Message = message + "reason =" + (soapfault. getreason () = NULL? "":
Soapfault. getreason (). getsoaptext () = NULL? "": Soapfault. getreason (). getsoaptext (). gettext ());
This Code only processes <faultcode> <faultstring>
<Detail> not handled

3) You can modify the <detail> part of the code on your own.

Public void loop (iterator T, int I ){
Omelementimpl ome;
Omtextimpl OMT;
Object TX;
Char [] sapace1 = new char [I];
String sapace = new string (sapace1 );
Omchildreniterator tor = (omchildreniterator) T;
While (T. hasnext ()){
Tx = T. Next ();
System. Out. println (sapace + Tx. getclass (). getname ());
If (TX instanceof omelementimpl ){
Ome = (omelementimpl) TX;
System. Out. println (sapace + OME. gettext ());
Loop (OME. getchildren (), I + 1 );
} Else if (TX instanceof omtextimpl)
{OMT = (omtextimpl) TX;
System. Out. println (sapace + OMT. gettext ());
} Else {
System. Out. println (sapace + "other type ");
}
}
}

and call this function at the bottom
If (ex! = NULL) {
throw new axisfault (Ex );
}else {
// If detail element not present create a new exception from the detail
string message = "";

Message = message + "code =" + (soapfault. getcode () = NULL? "":
Soapfault. getcode (). getvalue () = NULL? "": Soapfault. getcode (). getvalue (). gettext ());
System. Out. println ("" + message );
Iterator T = soapfault. getdetail (). getalldetailentries ();
Loop (T, 1 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.