Search in sources :

Example 1 with NSStack

use of org.apache.cxf.helpers.NSStack in project cxf by apache.

the class RPCOutInterceptor method handleMessage.

public void handleMessage(Message message) {
    XMLStreamWriter origXmlWriter = null;
    try {
        NSStack nsStack = new NSStack();
        nsStack.push();
        BindingOperationInfo operation = message.getExchange().getBindingOperationInfo();
        assert operation.getName() != null;
        XMLStreamWriter xmlWriter = getXMLStreamWriter(message);
        CachingXmlEventWriter cache = null;
        // need to cache the events in case validation fails or buffering is enabled
        if (shouldBuffer(message)) {
            origXmlWriter = xmlWriter;
            cache = new CachingXmlEventWriter();
            try {
                cache.setNamespaceContext(xmlWriter.getNamespaceContext());
            } catch (XMLStreamException e) {
            // ignorable, will just get extra namespace decls
            }
            message.setContent(XMLStreamWriter.class, cache);
            xmlWriter = cache;
        }
        List<MessagePartInfo> parts = null;
        boolean output = false;
        if (!isRequestor(message)) {
            if (operation.getOutput() == null) {
                return;
            }
            parts = operation.getOutput().getMessageParts();
            output = true;
        } else {
            parts = operation.getInput().getMessageParts();
            output = false;
        }
        MessageContentsList objs = MessageContentsList.getContentsList(message);
        if (objs == null) {
            addOperationNode(nsStack, message, xmlWriter, output, operation);
            xmlWriter.writeEndElement();
            return;
        }
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                if (o == null) {
                    // WSI-BP R2211 - RPC/Lit parts are not allowed to be xsi:nil
                    throw new Fault(new org.apache.cxf.common.i18n.Message("BP_2211_RPCLIT_CANNOT_BE_NULL", LOG, part.getConcreteName()));
                }
            // WSI-BP R2737  -RPC/LIG part name space is empty
            // part.setConcreteName(new QName("", part.getConcreteName().getLocalPart()));
            }
        }
        addOperationNode(nsStack, message, xmlWriter, output, operation);
        writeParts(message, message.getExchange(), operation, objs, parts);
        // Finishing the writing.
        xmlWriter.writeEndElement();
        if (cache != null) {
            try {
                for (XMLEvent event : cache.getEvents()) {
                    StaxUtils.writeEvent(event, origXmlWriter);
                }
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
        }
    } catch (XMLStreamException e) {
        throw new Fault(e);
    } finally {
        if (origXmlWriter != null) {
            message.setContent(XMLStreamWriter.class, origXmlWriter);
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) NSStack(org.apache.cxf.helpers.NSStack) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 2 with NSStack

use of org.apache.cxf.helpers.NSStack 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 3 with NSStack

use of org.apache.cxf.helpers.NSStack in project cxf by apache.

the class JAXRSDefaultFaultOutInterceptor method handleMessage.

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();
        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);
    }
}
Also used : StaxOutInterceptor(org.apache.cxf.interceptor.StaxOutInterceptor) MessageContentsList(org.apache.cxf.message.MessageContentsList) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault) XMLStreamException(javax.xml.stream.XMLStreamException) Response(javax.ws.rs.core.Response) NSStack(org.apache.cxf.helpers.NSStack) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)3 NSStack (org.apache.cxf.helpers.NSStack)3 Fault (org.apache.cxf.interceptor.Fault)3 Message (org.apache.cxf.message.Message)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 Response (javax.ws.rs.core.Response)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 XMLFault (org.apache.cxf.binding.xml.XMLFault)1 StaxOutInterceptor (org.apache.cxf.interceptor.StaxOutInterceptor)1 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 CachingXmlEventWriter (org.apache.cxf.staxutils.CachingXmlEventWriter)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1