Java Code Examples for org.apache.cxf.interceptor.Fault#getCause()

The following examples show how to use org.apache.cxf.interceptor.Fault#getCause() . 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: SonosFaultInterceptor.java    From airsonic with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = (Fault) message.getContent(Exception.class);
    LOG.warn("Error: " + fault, fault);

    if (fault.getCause() instanceof SonosSoapFault) {
        SonosSoapFault cause = (SonosSoapFault) fault.getCause();
        fault.setFaultCode(new QName(cause.getFaultCode()));
        fault.setMessage(cause.getFaultCode());

        Document document = DOMUtils.createDocument();
        Element details = document.createElement("detail");
        fault.setDetail(details);

        details.appendChild(document.createElement("ExceptionInfo"));

        Element sonosError = document.createElement("SonosError");
        sonosError.setTextContent(String.valueOf(cause.getSonosError()));
        details.appendChild(sonosError);
    }
}
 
Example 2
Source File: SonosFaultInterceptor.java    From subsonic with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = (Fault) message.getContent(Exception.class);
    LOG.warn("Error: " + fault, fault);

    if (fault.getCause() instanceof SonosSoapFault) {
        SonosSoapFault cause = (SonosSoapFault) fault.getCause();
        fault.setFaultCode(new QName(cause.getFaultCode()));
        fault.setMessage(cause.getFaultCode());

        Document document = DOMUtils.createDocument();
        Element details = document.createElement("detail");
        fault.setDetail(details);

        details.appendChild(document.createElement("ExceptionInfo"));

        Element sonosError = document.createElement("SonosError");
        sonosError.setTextContent(String.valueOf(cause.getSonosError()));
        details.appendChild(sonosError);
    }
}
 
Example 3
Source File: SecurityOutFaultInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    Fault fault = (Fault)message.getContent(Exception.class);
    Throwable ex = fault.getCause();
    if (!(ex instanceof SecurityException)) {
        throw new RuntimeException("Security Exception is expected");
    }

    HttpServletResponse response = (HttpServletResponse)message.getExchange().getInMessage()
        .get(AbstractHTTPDestination.HTTP_RESPONSE);
    int status = ex instanceof AccessDeniedException ? 403 : 401;
    response.setStatus(status);
    try {
        response.getOutputStream().write(ex.getMessage().getBytes());
        response.getOutputStream().flush();
    } catch (IOException iex) {
        // ignore
    }

    message.getInterceptorChain().abort();
}
 
Example 4
Source File: ClientImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * TODO This is SOAP specific code and should not be in cxf core
 * @param fault
 */
private void enrichFault(Fault fault) {
    if (fault.getCause().getCause() instanceof IOException
            || fault.getCause() instanceof IOException) {
        String soap11NS = "http://schemas.xmlsoap.org/soap/envelope/";
        String soap12NS = "http://www.w3.org/2003/05/soap-envelope";
        QName faultCode = fault.getFaultCode();
        //for SoapFault, if it's underlying cause is IOException,
        //it means something like server is down or can't create
        //connection, according to soap spec we should set fault as
        //Server Fault
        if (faultCode.getNamespaceURI().equals(
                soap11NS)
                && "Client".equals(faultCode.getLocalPart())) {
            faultCode = new QName(soap11NS, "Server");
            fault.setFaultCode(faultCode);
        }
        if (faultCode.getNamespaceURI().equals(
                soap12NS)
                && "Sender".equals(faultCode.getLocalPart())) {
            faultCode = new QName(soap12NS, "Receiver");
            fault.setFaultCode(faultCode);
        }
    }
}
 
Example 5
Source File: SonosFaultInterceptor.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleMessage(SoapMessage message) throws Fault {
    Fault fault = (Fault) message.getContent(Exception.class);
    LOG.warn("Error with Soap message", fault);

    if (fault.getCause() instanceof SonosSoapFault) {
        SonosSoapFault cause = (SonosSoapFault) fault.getCause();
        fault.setFaultCode(new QName(cause.getFaultCode()));
        fault.setMessage(cause.getFaultCode());

        Document document = DOMUtils.createDocument();
        Element details = document.createElement("detail");
        fault.setDetail(details);

        if (cause instanceof TokenRefreshRequired) {
            try {
                marshaller.marshal(((TokenRefreshRequired) cause).getRefreshTokens(), details);
            } catch (JAXBException e) {
                LOG.warn("Could not marshal Sonos refresh tokens", e);
            }
        } else {
            details.appendChild(document.createElement("ExceptionInfo"));

            Element sonosError = document.createElement("SonosError");
            sonosError.setTextContent(String.valueOf(cause.getSonosError()));
            details.appendChild(sonosError);
        }
    }
}
 
Example 6
Source File: CustomSoapFaultInterceptor.java    From tutorial-soap-spring-boot-cxf with MIT License 5 votes vote down vote up
@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
    Fault fault = (Fault) soapMessage.getContent(Exception.class);
    Throwable faultCause = fault.getCause();
    String faultMessage = fault.getMessage();

    if (containsFaultIndicatingNotSchemeCompliantXml(faultCause, faultMessage)) { 
    	LOG.schemaValidationError(FaultConst.SCHEME_VALIDATION_ERROR, faultMessage);
    	WeatherSoapFaultHelper.buildWeatherFaultAndSet2SoapMessage(soapMessage, FaultConst.SCHEME_VALIDATION_ERROR);
    } else if (containsFaultIndicatingSyntacticallyIncorrectXml(faultCause)) {
    	LOG.schemaValidationError(FaultConst.SYNTACTICALLY_INCORRECT_XML_ERROR, faultMessage);
    	WeatherSoapFaultHelper.buildWeatherFaultAndSet2SoapMessage(soapMessage, FaultConst.SYNTACTICALLY_INCORRECT_XML_ERROR);	        
    }
}
 
Example 7
Source File: WebFaultInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    Exception ex = message.getContent(Exception.class);
    if (ex != null) {
        message.put(Message.RESPONSE_CODE, Integer.valueOf(500));
    }

    if (ex instanceof Fault) {
        Fault f = (Fault) ex;
        ex = (Exception) f.getCause();
    }
    if (ex == null) {
        return;
    }


    QName faultName = this.getFaultName(ex);
    if (faultName == null) {
        return;
    }

    BindingOperationInfo boi = message.getExchange().getBindingOperationInfo();
    MessagePartInfo part = getFaultMessagePart(faultName, boi.getOperationInfo());

    if (part != null) {
        message.setContent(Exception.class, ex);
    }
}
 
Example 8
Source File: XMLFault.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static XMLFault createFault(Fault f) {
    if (f instanceof XMLFault) {
        return (XMLFault) f;
    }
    Throwable th = f.getCause();
    XMLFault xmlFault = new XMLFault(new Message(f.getMessage(), (ResourceBundle) null), th);
    xmlFault.setDetail(f.getDetail());
    return xmlFault;
}
 
Example 9
Source File: JAXRSDefaultFaultOutInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void handleMessage(Message message) throws Fault {
    if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
        return;
    }
    final Fault f = (Fault) message.getContent(Exception.class);

    Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
    if (r != null) {
        JAXRSUtils.setMessageContentType(message, r);
        message.setContent(List.class, new MessageContentsList(r));
        if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
            message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
        }
        new JAXRSOutInterceptor().handleMessage(message);
        return;
    }

    ServerProviderFactory.releaseRequestState(message);
    if (mustPropogateException(message)) {
        throw f;
    }

    new StaxOutInterceptor().handleMessage(message);
    message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
    NSStack nsStack = new NSStack();
    nsStack.push();

    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    try {
        nsStack.add("http://cxf.apache.org/bindings/xformat");
        String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "XMLFault",
                                    "http://cxf.apache.org/bindings/xformat");
        StaxUtils.writeStartElement(writer, prefix, "faultstring",
                                    "http://cxf.apache.org/bindings/xformat");
        Throwable t = f.getCause();
        writer.writeCharacters(t == null ? f.getMessage() : t.toString());
        // fault string
        writer.writeEndElement();
        // call StaxUtils to write Fault detail.

        if (f.getDetail() != null) {
            StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
            StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE),
                                writer, false);
            writer.writeEndElement();
        }
        // fault root
        writer.writeEndElement();
        writer.flush();
    } catch (XMLStreamException xe) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
    }
}