Example usage for javax.xml.soap SOAPFault setFaultString

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

Introduction

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

Prototype

public void setFaultString(String faultString) throws SOAPException;

Source Link

Document

Sets the fault string for this SOAPFault object to the given string.

Usage

From source file:it.cnr.icar.eric.server.interfaces.soap.RegistrySAMLServlet.java

/**
 * This method is a copy of the respective method from RegistrySOAPServlet.
 * The SAML-based Servlet returns X.509 certificate base SOAP messages.
 * /*from w w w .  j a v a2  s.  c om*/
 */

private SOAPMessage createFaultSOAPMessage(java.lang.Throwable e, SOAPHeader sh) {
    SOAPMessage msg = null;

    if (log.isDebugEnabled()) {
        log.debug("Creating Fault SOAP Message with Throwable:", e);
    }

    try {
        // Will this method be "legacy" ebRS 3.0 spec-compliant and
        // return a URN as the <faultcode/> value? Default expectation
        // is of a an older client. Overridden to instead be SOAP
        // 1.1-compliant and return a QName as the faultcode value when
        // we know (for sure) client supports new approach.
        boolean legacyFaultCode = true;

        // get SOAPHeaderElement list from the received message
        // TODO: if additional capabilities are needed, move code to
        // elsewhere
        if (null != sh) {
            Iterator<?> headers = sh.examineAllHeaderElements();
            while (headers.hasNext()) {
                Object obj = headers.next();

                // confirm expected Iterator content
                if (obj instanceof SOAPHeaderElement) {
                    SOAPHeaderElement header = (SOAPHeaderElement) obj;
                    Name headerName = header.getElementName();

                    // check this SOAP header for relevant capability
                    // signature
                    if (headerName.getLocalName().equals(BindingUtility.SOAP_CAPABILITY_HEADER_LocalName)
                            && headerName.getURI().equals(BindingUtility.SOAP_CAPABILITY_HEADER_Namespace)
                            && header.getValue().equals(BindingUtility.SOAP_CAPABILITY_ModernFaultCodes)) {
                        legacyFaultCode = false;
                        // only interested in one client capability
                        break;
                    }
                }
            }
        }

        msg = MessageFactory.newInstance().createMessage();
        SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
        SOAPFault fault = msg.getSOAPBody().addFault();

        // set faultCode
        String exceptionName = e.getClass().getName();

        // TODO: SAAJ 1.3 has introduced preferred QName interfaces
        Name name = env.createName(exceptionName, "ns1", BindingUtility.SOAP_FAULT_PREFIX);
        fault.setFaultCode(name);
        if (legacyFaultCode) {
            // we now have an element child, munge its text (hack alert)
            Node faultCode = fault.getElementsByTagName("faultcode").item(0);

            // Using Utility.setTextContent() implementation since Java
            // WSDP 1.5 (containing an earlier DOM API) does not
            // support Node.setTextContent().
            Utility.setTextContent(faultCode, BindingUtility.SOAP_FAULT_PREFIX + ":" + exceptionName);
        }

        // set faultString
        String errorMsg = e.getMessage();
        if (errorMsg == null) {
            errorMsg = "NULL";
        }
        fault.setFaultString(errorMsg);

        // create faultDetail with one entry
        Detail det = fault.addDetail();

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String str = sw.toString();

        name = env.createName("StackTrace", "rs", "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0");

        DetailEntry de = det.addDetailEntry(name);
        de.setValue(str);
        // de.addTextNode(str);

        // TODO: Need to put baseURL for this registry here

        msg.saveChanges();

    } catch (SOAPException ex) {
        log.warn(ex, ex);
        // otherwise ignore the problem updating part of the message
    }

    return msg;
}

From source file:de.unibi.techfak.bibiserv.BiBiTools.java

/**
 * Returns a SOAPFaultException with specified
 *
 * @param faultcode/*  w  ww.  j  a va  2 s  . com*/
 * @param faultstring
 * @param hobitstatuscode
 * @param hobitstatusdescription
 * @return
 */
public static SOAPFaultException createSOAPFaultException(String faultcode, String faultstring,
        String hobitstatuscode, String hobitstatusdescription) {
    SOAPFault fault = null;
    try {
        SOAPFactory sfi = SOAPFactory.newInstance();
        fault = sfi.createFault();

        fault.setFaultCode(new QName("http://schemas.xmlsoap.org/soap/envelope/", faultcode, "soap"));
        fault.setFaultString(faultstring);
        if (hobitstatuscode != null && hobitstatusdescription != null) {
            Detail detail = fault.addDetail();
            DetailEntry detailentry = detail.addDetailEntry(new QName(
                    "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "hobitStatuscode", "status"));

            SOAPElement statuscode = detailentry.addChildElement(
                    new QName("http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "statuscode", "status"));
            statuscode.addTextNode(hobitstatuscode);

            SOAPElement description = detailentry.addChildElement(new QName(
                    "http://hobit.sourceforge.net/xsds/hobitStatuscode.xsd", "description", "status"));
            description.addTextNode(hobitstatusdescription);
        }

    } catch (SOAPException e) {
        log.fatal("SOAPException occured : " + e.getMessage());
    }

    return new SOAPFaultException(fault);

}

From source file:org.apache.cxf.ws.security.sts.provider.SecurityTokenServiceProvider.java

public Source invoke(Source request) {
    Source response = null;//from   w  ww  .  j a  v a2s. c  om
    try {
        RequestSecurityTokenType rst = convertToJAXBObject(request);
        Object operationImpl = null;
        List<?> objectList = rst.getAny();
        for (int i = 0; i < objectList.size(); i++) {
            Object obj = objectList.get(i);
            if (obj instanceof JAXBElement) {
                QName qname = ((JAXBElement<?>) obj).getName();
                if (qname.equals(new QName(WSTRUST_13_NAMESPACE, WSTRUST_REQUESTTYPE_ELEMENTNAME))) {
                    operationImpl = operationMap.get(((JAXBElement<?>) obj).getValue().toString());
                    break;
                }

            }
        }

        if (operationImpl == null) {
            throw new Exception("Implementation for this operation not found.");
        }
        Method[] methods = operationImpl.getClass().getMethods();
        for (int x = 0; x < methods.length; x++) {
            Class<?>[] paramClass = methods[x].getParameterTypes();
            if (paramClass.length == 1 && paramClass[0].equals(rst.getClass())) {
                RequestSecurityTokenResponseCollectionType tokenResponse = (RequestSecurityTokenResponseCollectionType) methods[x]
                        .invoke(operationImpl, rst);
                if (tokenResponse == null) {
                    throw new Exception("Error in implementation class.");
                }

                response = new JAXBSource(jaxbContext,
                        new ObjectFactory().createRequestSecurityTokenResponseCollection(tokenResponse));
                return response;
            }
        }

    } catch (Exception e) {
        LOG.error(e);
        try {
            SOAPFault fault = soapFactory.createFault();
            if (e.getMessage() == null) {
                fault.setFaultString(e.getCause().getMessage());
            } else {
                fault.setFaultString(e.getMessage());
            }
            Detail detail = fault.addDetail();
            detail = fault.getDetail();
            QName qName = new QName(WSTRUST_13_NAMESPACE, "Fault", "ns");
            DetailEntry de = detail.addDetailEntry(qName);
            qName = new QName(WSTRUST_13_NAMESPACE, "ErrorCode", "ns");
            SOAPElement errorElement = de.addChildElement(qName);
            StackTraceElement[] ste = e.getStackTrace();
            errorElement.setTextContent(ste[0].toString());
            throw new SOAPFaultException(fault);
        } catch (SOAPException e1) {
            LOG.error(e1);
        }

    }

    return response;
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSOAPUtils.java

/**
 *
 * Creates a SOAP Fault message including the fault code and fault string.
 * @param faultString detailed error message
 * @param faultcode/*from www.  j a  v  a2s. c om*/
 * @return
 */
public static String createSOAPFault(String faultString, String faultcode)
        throws TransformerException, SOAPException {
    SOAPMessage soapMsg;
    MessageFactory factory = MessageFactory.newInstance();
    soapMsg = factory.createMessage();
    SOAPPart part = soapMsg.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
    SOAPBody body = envelope.getBody();
    SOAPFault fault = body.addFault();
    fault.setFaultString(faultString);
    fault.setFaultCode(new QName(SAMLECPConstants.SOAPNamespaceURI.SOAP_NAMESPACE_URI, faultcode));
    return convertSOAPMsgToString(soapMsg).replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
}