Example usage for javax.xml.soap SOAPFault setFaultCode

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

Introduction

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

Prototype

public void setFaultCode(String faultCode) throws SOAPException;

Source Link

Document

Sets this SOAPFault object with the give fault code.

Usage

From source file:de.drv.dsrv.spoc.web.webservice.jax.ExtraSchemaValidationHandler.java

private boolean handleException(final SOAPBody soapBody, final String errorText,
        final ExtraErrorReasonType reason) {

    try {/*w  w  w .  j  a  v a  2  s .c  o  m*/
        // Bisherigen Inhalt des SOAP-Bodys entfernen
        soapBody.removeContents();

        // SOAP-Fault erzeugen
        final SOAPFault fault = soapBody.addFault();
        fault.setFaultString(this.soapFaultString);
        fault.setFaultCode(new QName(SOAPConstants.URI_NS_SOAP_ENVELOPE, SOAP_FAULT_CODE));

        final ExtraJaxbMarshaller extraJaxbMarshaller = new ExtraJaxbMarshaller();
        final ExtraErrorType extraError = ExtraHelper.generateError(reason, this.extraErrorCode, errorText);
        extraJaxbMarshaller.marshalExtraError(extraError, fault.addDetail());

        return false;
    } catch (final Exception e) {
        LOG.error("Fehler bei Exception-Behandlung.", e);
        throw new WebServiceException(resourceBundle.getString(Messages.ERROR_NON_EXTRA_TEXT));
    }
}

From source file:de.drv.dsrv.spoc.web.webservice.spring.SpocMessageDispatcherServlet.java

private void createExtraErrorAndWriteResponse(final HttpServletResponse httpServletResponse,
        final String errorText)
        throws SOAPException, JAXBException, DatatypeConfigurationException, IOException {

    final MessageFactory factory = MessageFactory.newInstance();
    final SOAPMessage message = factory.createMessage();
    final SOAPBody body = message.getSOAPBody();

    final SOAPFault fault = body.addFault();
    final QName faultName = new QName(SOAPConstants.URI_NS_SOAP_ENVELOPE, FaultCode.CLIENT.toString());
    fault.setFaultCode(faultName);
    fault.setFaultString(this.soapFaultString);

    final Detail detail = fault.addDetail();

    final ExtraJaxbMarshaller extraJaxbMarshaller = new ExtraJaxbMarshaller();
    final ExtraErrorReasonType reason = ExtraErrorReasonType.INVALID_REQUEST;
    final ExtraErrorType extraError = ExtraHelper.generateError(reason, this.extraErrorCode, errorText);
    extraJaxbMarshaller.marshalExtraError(extraError, detail);

    // Schreibt die SOAPMessage in einen String.
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    message.writeTo(out);//from  w  w w  .j  a  v  a2 s. c o m
    // Das Encoding, in dem sich die Message rausschreibt, kann man als
    // Property abfragen.
    final Object encodingProperty = message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
    String soapMessageEncoding = "UTF-8";
    if (encodingProperty != null) {
        soapMessageEncoding = encodingProperty.toString();
    }
    final String errorXml = out.toString(soapMessageEncoding);

    httpServletResponse.setStatus(HttpServletResponse.SC_OK);
    httpServletResponse.setContentType("text/xml");
    httpServletResponse.getWriter().write(errorXml);
    httpServletResponse.getWriter().flush();
    httpServletResponse.getWriter().close();
}

From source file:org.cleverbus.core.reqres.RequestResponseTest.java

/**
 * Test saving synchronous request/response where response is failed SOAP fault exception.
 *///from  w  w  w. ja  v a2 s. c  om
@Test
public void testSavingRequestWithSOAPFaultResponse() throws Exception {

    final String soapFault = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<SOAP-ENV:Fault xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "     <faultcode>SOAP-ENV:Server</faultcode>"
            + "     <faultstring>There is one error</faultstring>" + "</SOAP-ENV:Fault>";

    // prepare target route
    prepareTargetRoute(TARGET_URI, new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getOut().setBody(soapFault);

            // create SOAP Fault message
            SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            SOAPBody soapBody = soapEnvelope.getBody();

            // Set fault code and fault string
            SOAPFault fault = soapBody.addFault();
            fault.setFaultCode(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"));
            fault.setFaultString("There is one error");
            SoapMessage message = new SaajSoapMessage(soapMessage);
            throw new SoapFaultClientException(message);
        }
    });

    // action
    mock.expectedMessageCount(0);

    try {
        producer.sendBody(REQUEST);
        fail("Target route was thrown exception.");
    } catch (CamelExecutionException ex) {
        assertRequestResponse(REQUEST, null,
                "org.springframework.ws.soap.client.SoapFaultClientException: There is one error", soapFault);
    }
}

From source file:edu.duke.cabig.c3pr.webservice.studyimportexport.impl.StudyImportExportImpl.java

/**
 * Creates the soap fault./*  ww w . ja  v a  2s  .  co m*/
 *
 * @param msg the msg
 * @return the sOAP fault
 */
private SOAPFault createSOAPFault(String msg) {
    try {
        SOAPFactory factory = SOAPFactory.newInstance();
        SOAPFault fault = factory.createFault();
        fault.setFaultString(msg);
        fault.setFaultCode(new QName(SOAP_NS, SOAP_FAULT_CODE));
        Detail detail = fault.addDetail();
        final Element detailEntry = detail.getOwnerDocument().createElementNS(SERVICE_NS, STUDY_IMPORT_FAULT);
        detail.appendChild(detailEntry);

        final Element detailMsg = detail.getOwnerDocument().createElementNS(SERVICE_NS, FAULT_MESSAGE);
        detailMsg.setTextContent(msg);
        detailEntry.appendChild(detailMsg);
        return fault;
    } catch (SOAPException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
        throw new WebServiceException(e);
    }

}

From source file:com.centurylink.mdw.hub.servlet.SoapServlet.java

/**
 * Allow version specific factory passed in to support SOAP 1.1 and 1.2
 * <b>Important</b> Faults are treated differently for 1.1 and 1.2 For 1.2
 * you can't use the elementName otherwise it throws an exception
 *
 * @see http://docs.oracle.com/cd/E19159-01/819-3669/bnbip/index.html
 *
 * @param factory//from  ww  w  . j  a v a  2  s.  c o  m
 * @param code
 * @param message
 * @return Xml fault string
 * @throws SOAPException
 * @throws TransformerException
 */
protected String createSoapFaultResponse(String soapVersion, String code, String message)
        throws SOAPException, TransformerException {

    SOAPMessage soapMessage = getSoapMessageFactory(soapVersion).createMessage();
    SOAPBody soapBody = soapMessage.getSOAPBody();
    /**
     * Faults are treated differently for 1.1 and 1.2 For 1.2 you can't use
     * the elementName otherwise it throws an exception
     *
     * @see http://docs.oracle.com/cd/E19159-01/819-3669/bnbip/index.html
     */
    SOAPFault fault = null;
    if (soapVersion.equals(SOAPConstants.SOAP_1_1_PROTOCOL)) {
        // existing 1.1 functionality
        fault = soapBody.addFault(soapMessage.getSOAPHeader().getElementName(), message);
        if (code != null)
            fault.setFaultCode(code);
    } else if (soapVersion.equals(SOAPConstants.SOAP_1_2_PROTOCOL)) {
        /**
         * For 1.2 there are only a set number of allowed codes, so we can't
         * just use any one like what we did in 1.1. The recommended one to
         * use is SOAPConstants.SOAP_RECEIVER_FAULT
         */
        fault = soapBody.addFault(SOAPConstants.SOAP_RECEIVER_FAULT,
                code == null ? message : code + " : " + message);

    }
    return DomHelper.toXml(soapMessage.getSOAPPart().getDocumentElement());

}

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

private SOAPMessage createFaultSOAPMessage(java.lang.Throwable e, SOAPHeader sh) {
    SOAPMessage msg = null;/*  w w w  .  j  av a  2 s  .c  o m*/
    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:org.jasig.portal.security.provider.saml.SAMLDelegatedAuthenticationService.java

private Document createSOAPFaultDocument(String faultString) throws SOAPException {
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();
    SOAPPart sp = message.getSOAPPart();
    SOAPEnvelope se = sp.getEnvelope();
    se.setPrefix(SOAP_PREFIX);//from w  w  w .j a va2s.c o m
    se.getHeader().detachNode();
    se.addHeader();
    se.getBody().detachNode();
    SOAPBody body = se.addBody();
    SOAPFault fault = body.addFault();
    Name faultCode = se.createName("Client", null, SOAPConstants.URI_NS_SOAP_ENVELOPE);
    fault.setFaultCode(faultCode);
    fault.setFaultString(faultString);
    return se.getOwnerDocument();
}

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.
 * /* w  w w  . ja  va2 s  .c  o  m*/
 */

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//from  ww w.  j  av a  2 s . c om
 * @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.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/* w  w  w . jav  a  2s .com*/
 * @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\"?>", "");
}