org.apache.axiom.soap.SOAPFault Java Examples

The following examples show how to use org.apache.axiom.soap.SOAPFault. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public static void setSOAPFault(MessageContext messageContext, String code, 
                                String reason, String detail) {
    SOAPFactory factory = (messageContext.isSOAP11() ?
            OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory());

    OMDocument soapFaultDocument = factory.createOMDocument();
    SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
    soapFaultDocument.addChild(faultEnvelope);

    SOAPFault fault = faultEnvelope.getBody().getFault();
    if (fault == null) {
        fault = factory.createSOAPFault();
    }

    SOAPFaultCode faultCode = factory.createSOAPFaultCode();
    if (messageContext.isSOAP11()) {
        faultCode.setText(new QName(fault.getNamespace().getNamespaceURI(), code));
    } else {
        SOAPFaultValue value = factory.createSOAPFaultValue(faultCode);
        value.setText(new QName(fault.getNamespace().getNamespaceURI(), code));            
    }
    fault.setCode(faultCode);

    SOAPFaultReason faultReason = factory.createSOAPFaultReason();
    if (messageContext.isSOAP11()) {
        faultReason.setText(reason);
    } else {
        SOAPFaultText text = factory.createSOAPFaultText();
        text.setText(reason);
        text.setLang("en");
        faultReason.addSOAPText(text);            
    }
    fault.setReason(faultReason);

    SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
    soapFaultDetail.setText(detail);
    fault.setDetail(soapFaultDetail);
    
    // set the all headers of original SOAP Envelope to the Fault Envelope
    if (messageContext.getEnvelope() != null) {
        SOAPHeader soapHeader = messageContext.getEnvelope().getHeader();
        if (soapHeader != null) {
            for (Iterator iterator = soapHeader.examineAllHeaderBlocks(); iterator.hasNext();) {
                Object o = iterator.next();
                if (o instanceof SOAPHeaderBlock) {
                    SOAPHeaderBlock header = (SOAPHeaderBlock) o;
                    faultEnvelope.getHeader().addChild(header);
                } else if (o instanceof OMElement) {
                    faultEnvelope.getHeader().addChild((OMElement) o);
                }
            }
        }
    }

    try {
        messageContext.setEnvelope(faultEnvelope);
    } catch (AxisFault af) {
        log.error("Error while setting SOAP fault as payload", af);
        return;
    }

    if (messageContext.getFaultTo() != null) {
        messageContext.setTo(messageContext.getFaultTo());
    } else if (messageContext.getReplyTo() != null) {
        messageContext.setTo(messageContext.getReplyTo());
    } else {
        messageContext.setTo(null);
    }

    // set original messageID as relatesTo
    if (messageContext.getMessageID() != null) {
        RelatesTo relatesTo = new RelatesTo(messageContext.getMessageID());
        messageContext.setRelatesTo(new RelatesTo[] { relatesTo });
    }
}