import java.util.*;
import javax.xml.soap.*;
import javax.xml.messaging.*;
public class SampleMsgSV4retFault implements ReqRespListener
{
public SampleMsgSV4retFault () { }
public SOAPMessage onMessage( SOAPMessage message )
{
try{
boolean isFault = false;
String beName = null;
SOAPEnvelope env = message.getSOAPPart().getEnvelope();
SOAPBody body = env.getBody();
Iterator it = body.getChildElements();
while(it.hasNext()) {
SOAPElement elm = (SOAPElement)it.next();
beName = elm.getElementName().getLocalName();
if( !beName.equals("RequestBody")) {
isFault = true;
break;
}
}
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage resp = mf.createMessage();
env = resp.getSOAPPart().getEnvelope();
body = env.getBody();
//**(1)**
if( isFault ){
SOAPFault fault = body.addFault();
fault.setFaultCode("BadElementName");
fault.setFaultString("BodyElement name is invalid.");
Detail detail = fault.addDetail();
DetailEntry entry = detail.addDetailEntry(
env.createName( "FaultDetail", "m", "urn:SampleMsg" ));
entry.addChildElement( env.createName( "Reason")).
addTextNode("BodyElement name is unexpected:" + beName);
}
else{
SOAPBodyElement elm = body.addBodyElement(
env.createName( "ResponseBody", "m", "urn:SampleMsg" ));
elm.addChildElement( env.createName("Response")).
addTextNode("response string...");
elm.addNamespaceDeclaration( "m", "urn:SampleMsg" );
}
resp.saveChanges();
return resp;
} catch( Exception e ) {
e.printStackTrace();
return null;
}
}
} |