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:cn.com.ttblog.ssmbootstrap_table.webservice.LicenseHandler.java

@SuppressWarnings("unchecked")
@Override/*w w w .  j av a  2  s  .  co m*/
public boolean handleMessage(SOAPMessageContext context) {
    try {
        Boolean out = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        logger.debug("LicenseHandler:{}", out);
        if (!out) {
            SOAPMessage message = context.getMessage();
            logger.debug("SOAPMessage:{}", ToStringBuilder.reflectionToString(message));
            SOAPEnvelope enve = message.getSOAPPart().getEnvelope();
            SOAPHeader header = enve.getHeader();
            SOAPBody body = enve.getBody();
            Node bn = body.getChildNodes().item(0);
            String partname = bn.getLocalName();
            if ("getUser".equals(partname)) {
                if (header == null) {
                    // ?
                    SOAPFault fault = body.addFault();
                    fault.setFaultString("??!");
                    throw new SOAPFaultException(fault);
                }
                Iterator<SOAPHeaderElement> iterator = header.extractAllHeaderElements();
                if (!iterator.hasNext()) {
                    // ?
                    SOAPFault fault = body.addFault();
                    fault.setFaultString("??!");
                    throw new SOAPFaultException(fault);
                }
                while (iterator.hasNext()) {
                    SOAPHeaderElement ele = iterator.next();
                    System.out.println(ele.getTextContent());
                }
            }
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.hiperium.integration.access.control.SoapSessionHandler.java

/**
 * /*from w  w  w .  jav  a  2 s  .c om*/
 * @param msg
 * @param reason
 */
private void generateFault(SOAPMessage msg, String reason) {
    try {
        SOAPBody body = msg.getSOAPBody();
        SOAPFault fault = body.addFault();
        fault.setFaultString(reason);
        throw new SOAPFaultException(fault);
    } catch (SOAPException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.server.BennuWebServiceHandler.java

private void generateSOAPErrorMessage(SOAPMessage msg, String reason) {
    try {//www. ja v  a 2  s.c om
        SOAPBody soapBody = msg.getSOAPPart().getEnvelope().getBody();
        SOAPFault soapFault = soapBody.addFault();
        soapFault.setFaultString(reason);
        throw new SOAPFaultException(soapFault);
    } catch (SOAPException e) {
    }
}

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 {//from   w  w w .  j a  va  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:be.fedict.eid.idp.protocol.saml2.artifact.ArtifactServiceServerHandler.java

private SOAPFaultException createSOAPFaultException(String faultString) {

    SOAPFault soapFault;
    try {//from w w w  . ja v a 2  s  . c  o m
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        soapFault = soapFactory.createFault();
        soapFault.setFaultString(faultString);
    } catch (SOAPException e) {
        throw new RuntimeException("SOAP error");
    }

    return new SOAPFaultException(soapFault);
}

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);//from  w w  w.  j  a  va 2  s .co m
    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);
    // 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  .  j  a  v a 2  s  . com*/
@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.//from  w  ww.  j  a  v  a2 s  .c  o  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: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);/*ww  w . j  a  v a  2 s . 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.RegistryBSTServlet.java

private SOAPMessage createFaultSOAPMessage(java.lang.Throwable e, SOAPHeader sh) {
    SOAPMessage msg = null;/*from w  w  w . j  a va 2 s .c om*/
    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;
}