Search in sources :

Example 1 with XMLFault

use of org.apache.cxf.binding.xml.XMLFault in project cxf by apache.

the class XMLFaultOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (mustPropogateException(message)) {
        throw (Fault) message.getContent(Exception.class);
    }
    Fault f = (Fault) message.getContent(Exception.class);
    message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
    NSStack nsStack = new NSStack();
    nsStack.push();
    XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
    XMLFault xmlFault = XMLFault.createFault(f);
    try {
        nsStack.add(XMLConstants.NS_XML_FORMAT);
        String prefix = nsStack.getPrefix(XMLConstants.NS_XML_FORMAT);
        StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_ROOT, XMLConstants.NS_XML_FORMAT);
        StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_STRING, XMLConstants.NS_XML_FORMAT);
        Throwable t = xmlFault.getCause();
        writer.writeCharacters(t == null ? xmlFault.getMessage() : t.toString());
        // fault string
        writer.writeEndElement();
        if (xmlFault.getDetail() != null) {
            Element detail = xmlFault.getDetail();
            StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_DETAIL, XMLConstants.NS_XML_FORMAT);
            Node node = detail.getFirstChild();
            while (node != null) {
                StaxUtils.writeNode(node, writer, false);
                node = node.getNextSibling();
            }
            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);
    }
}
Also used : Message(org.apache.cxf.message.Message) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XMLFault(org.apache.cxf.binding.xml.XMLFault) Fault(org.apache.cxf.interceptor.Fault) XMLStreamException(javax.xml.stream.XMLStreamException) NSStack(org.apache.cxf.helpers.NSStack) XMLFault(org.apache.cxf.binding.xml.XMLFault) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 2 with XMLFault

use of org.apache.cxf.binding.xml.XMLFault in project cxf by apache.

the class XMLFaultInterceptorsTest method testRuntimeExceptionOfImpl.

@Test
public void testRuntimeExceptionOfImpl() throws Exception {
    String ns = "http://apache.org/hello_world_xml_http/wrapped";
    common("/wsdl/hello_world_xml_wrapped.wsdl", new QName(ns, "XMLPort"), MyComplexStructType.class);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    xmlMessage.setContent(OutputStream.class, baos);
    xmlMessage.setContent(XMLStreamWriter.class, StaxUtils.createXMLStreamWriter(baos));
    xmlMessage.setContent(Exception.class, new Fault(new RuntimeException("dummy exception")));
    XMLFaultOutInterceptor xfo = new XMLFaultOutInterceptor("phase1");
    chain.add(xfo);
    InHelpInterceptor ih = new InHelpInterceptor("phase2");
    ClientFaultConverter cfc = new ClientFaultConverter("phase3");
    XMLFaultInInterceptor xfi = new XMLFaultInInterceptor("phase3");
    chain.add(ih);
    chain.add(cfc);
    chain.add(xfi);
    chain.doIntercept(xmlMessage);
    assertNotNull(xmlMessage.getContent(Exception.class));
    assertTrue(xmlMessage.getContent(Exception.class) instanceof XMLFault);
    XMLFault xfault = (XMLFault) xmlMessage.getContent(Exception.class);
    assertTrue("check message expected - dummy exception", xfault.getMessage().indexOf("dummy exception") >= 0);
}
Also used : XMLFault(org.apache.cxf.binding.xml.XMLFault) QName(javax.xml.namespace.QName) XMLFault(org.apache.cxf.binding.xml.XMLFault) Fault(org.apache.cxf.interceptor.Fault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientFaultConverter(org.apache.cxf.interceptor.ClientFaultConverter) Test(org.junit.Test)

Example 3 with XMLFault

use of org.apache.cxf.binding.xml.XMLFault in project cxf by apache.

the class XMLFaultOutInterceptorTest method testFault.

@Test
public void testFault() throws Exception {
    FaultDetail detail = new FaultDetail();
    detail.setMajor((short) 2);
    detail.setMinor((short) 1);
    PingMeFault fault = new PingMeFault("TEST_FAULT", detail);
    XMLFault xmlFault = XMLFault.createFault(new Fault(fault));
    Element el = xmlFault.getOrCreateDetail();
    JAXBContext ctx = JAXBContext.newInstance(FaultDetail.class);
    Marshaller m = ctx.createMarshaller();
    m.marshal(detail, el);
    OutputStream outputStream = new ByteArrayOutputStream();
    xmlMessage.setContent(OutputStream.class, outputStream);
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream);
    xmlMessage.setContent(XMLStreamWriter.class, writer);
    xmlMessage.setContent(Exception.class, xmlFault);
    out.handleMessage(xmlMessage);
    outputStream.flush();
    XMLStreamReader reader = getXMLReader();
    DepthXMLStreamReader dxr = new DepthXMLStreamReader(reader);
    dxr.nextTag();
    StaxUtils.toNextElement(dxr);
    assertEquals(XMLConstants.NS_XML_FORMAT, dxr.getNamespaceURI());
    assertEquals(XMLFault.XML_FAULT_ROOT, dxr.getLocalName());
    dxr.nextTag();
    StaxUtils.toNextElement(dxr);
    assertEquals(XMLFault.XML_FAULT_STRING, dxr.getLocalName());
    assertEquals(fault.toString(), dxr.getElementText());
    dxr.nextTag();
    StaxUtils.toNextElement(dxr);
    assertEquals(XMLFault.XML_FAULT_DETAIL, dxr.getLocalName());
    dxr.nextTag();
    StaxUtils.toNextElement(dxr);
    assertEquals("faultDetail", dxr.getLocalName());
}
Also used : PingMeFault(org.apache.hello_world_doc_lit.PingMeFault) Marshaller(javax.xml.bind.Marshaller) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) XMLFault(org.apache.cxf.binding.xml.XMLFault) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FaultDetail(org.apache.hello_world_doc_lit.types.FaultDetail) Element(org.w3c.dom.Element) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLFault(org.apache.cxf.binding.xml.XMLFault) Fault(org.apache.cxf.interceptor.Fault) PingMeFault(org.apache.hello_world_doc_lit.PingMeFault) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Test(org.junit.Test)

Example 4 with XMLFault

use of org.apache.cxf.binding.xml.XMLFault in project cxf by apache.

the class XMLFaultInInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    XMLStreamReader xsr = message.getContent(XMLStreamReader.class);
    DepthXMLStreamReader reader = new DepthXMLStreamReader(xsr);
    try {
        reader.nextTag();
        if (!StaxUtils.toNextElement(reader)) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("ILLEGAL_XMLFAULT_FORMAT", BUNDLE));
        }
        String exMessage = reader.getElementText();
        Fault fault = new XMLFault(exMessage);
        reader.nextTag();
        if (StaxUtils.toNextElement(reader)) {
            // handling detail
            Element detail = StaxUtils.read(new FragmentStreamReader(reader)).getDocumentElement();
            fault.setDetail(detail);
        }
        message.setContent(Exception.class, fault);
    } catch (XMLStreamException xse) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_READ_EXC", BUNDLE));
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Message(org.apache.cxf.message.Message) Element(org.w3c.dom.Element) XMLFault(org.apache.cxf.binding.xml.XMLFault) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) XMLFault(org.apache.cxf.binding.xml.XMLFault) XMLStreamException(javax.xml.stream.XMLStreamException) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader)

Aggregations

XMLFault (org.apache.cxf.binding.xml.XMLFault)4 Fault (org.apache.cxf.interceptor.Fault)4 Element (org.w3c.dom.Element)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2 Message (org.apache.cxf.message.Message)2 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)2 Test (org.junit.Test)2 OutputStream (java.io.OutputStream)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 QName (javax.xml.namespace.QName)1 NSStack (org.apache.cxf.helpers.NSStack)1 ClientFaultConverter (org.apache.cxf.interceptor.ClientFaultConverter)1 FragmentStreamReader (org.apache.cxf.staxutils.FragmentStreamReader)1 PingMeFault (org.apache.hello_world_doc_lit.PingMeFault)1 FaultDetail (org.apache.hello_world_doc_lit.types.FaultDetail)1 Node (org.w3c.dom.Node)1