Example usage for javax.xml.soap SOAPFault addChildElement

List of usage examples for javax.xml.soap SOAPFault addChildElement

Introduction

In this page you can find the example usage for javax.xml.soap SOAPFault addChildElement.

Prototype

public SOAPElement addChildElement(Name name) throws SOAPException;

Source Link

Document

Creates a new SOAPElement object initialized with the given Name object and adds the new element to this SOAPElement object.

Usage

From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java

private void _fixFaultElements(SOAPEnvelope env) {
    try {// ww w  .  ja  v a2s.c  o  m
        // If we have a SOAP 1.2 envelope, then there's nothing to do.
        if (env.getNamespaceURI().equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
            return;
        }

        SOAPBody body = env.getBody();
        if (body != null && !body.hasFault()) {
            if (log.isDebugEnabled()) {
                log.debug("No fault found.  No conversion necessary.");
            }
            return;
        } else if (body != null && body.hasFault()) {
            if (log.isDebugEnabled()) {
                log.debug("A fault was found.  Converting the fault child elements to SOAP 1.1 format");
            }

            SOAPFault fault = body.getFault();

            Iterator itr = fault.getChildElements();
            while (itr.hasNext()) {
                SOAPElement se = (SOAPElement) itr.next();
                if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Converting: faultcode");
                    }
                    // Axis2 SAAJ stores the acutal faultcode text under a SOAPFaultValue object, so we have to 
                    // get that and add it as a text node under the original element.
                    Node value = se.getFirstChild();
                    if (value != null && value instanceof org.apache.axis2.saaj.SOAPElementImpl) {
                        org.apache.axis2.saaj.SOAPElementImpl valueElement = (org.apache.axis2.saaj.SOAPElementImpl) value;
                        ElementImpl e = valueElement.getElement();
                        String content = e.getText();

                        SOAPElement child = fault.addChildElement(
                                new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_CODE_LOCAL_NAME));
                        child.addTextNode(content);

                        se.detachNode();
                    }
                } else if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Converting: detail");
                    }
                    se.setElementQName(
                            new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_DETAIL_LOCAL_NAME));
                } else if (se.getLocalName().equals(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Converting: faultstring");
                    }
                    se.setElementQName(
                            new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_STRING_LOCAL_NAME));
                    // Axis2 SAAJ stores the acutal faultstring text under a SOAPFaultValue object, so we have to 
                    // get that and add it as a text node under the original element.
                    Node value = se.getFirstChild();
                    if (value != null && value instanceof org.apache.axis2.saaj.SOAPElementImpl) {
                        org.apache.axis2.saaj.SOAPElementImpl valueElement = (org.apache.axis2.saaj.SOAPElementImpl) value;
                        ElementImpl e = valueElement.getElement();
                        String content = e.getText();

                        SOAPElement child = fault.addChildElement(
                                new QName(se.getNamespaceURI(), SOAP11Constants.SOAP_FAULT_STRING_LOCAL_NAME));
                        child.addTextNode(content);

                        se.detachNode();
                    }
                }
            }
        }
    } catch (SOAPException e) {
        if (log.isDebugEnabled()) {
            log.debug("An error occured while converting fault elements: " + e.getMessage());
        }
        throw ExceptionFactory.makeWebServiceException(e);
    }
}