Search in sources :

Example 1 with FragmentStreamReader

use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.

the class XMLStreamDataReader method resetForStreaming.

private XMLStreamReader resetForStreaming(XMLStreamReader input) throws XMLStreamException {
    // is not closed and additional parts are not read and such
    if (message != null) {
        if (message.getInterceptorChain() != null) {
            message.getInterceptorChain().remove(StaxInEndingInterceptor.INSTANCE);
            message.getInterceptorChain().add(new StaxInEndingInterceptor(Phase.POST_INVOKE));
        }
        message.removeContent(XMLStreamReader.class);
        final InputStream ins = message.getContent(InputStream.class);
        message.removeContent(InputStream.class);
        input = new FragmentStreamReader(input, true) {

            boolean closed;

            public boolean hasNext() throws XMLStreamException {
                boolean b = super.hasNext();
                if (!b && !closed) {
                    close();
                }
                return b;
            }

            public void close() throws XMLStreamException {
                closed = true;
                try {
                    super.close();
                } catch (XMLStreamException e) {
                // ignore
                }
                if (ins != null) {
                    try {
                        ins.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
        };
    }
    return input;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) StaxInEndingInterceptor(org.apache.cxf.interceptor.StaxInEndingInterceptor) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader) IOException(java.io.IOException)

Example 2 with FragmentStreamReader

use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.

the class DocumentType method readObject.

@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    try {
        XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
        // we need to eat the surrounding element.
        reader.nextTag();
        Object tree = StaxUtils.read(null, new FragmentStreamReader(reader), true);
        // eat the end tag.
        reader.nextTag();
        return tree;
    } catch (XMLStreamException e) {
        throw new DatabindingException("Could not parse xml.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader)

Example 3 with FragmentStreamReader

use of org.apache.cxf.staxutils.FragmentStreamReader in project cxf by apache.

the class Soap12FaultInInterceptor method unmarshalFault.

public static SoapFault unmarshalFault(SoapMessage message, XMLStreamReader reader) {
    String exMessage = null;
    QName faultCode = null;
    List<QName> subCodes = null;
    String role = null;
    String node = null;
    Element detail = null;
    String lang = null;
    Map<String, String> ns = new HashMap<>();
    ns.put("s", Soap12.SOAP_NAMESPACE);
    XPathUtils xu = new XPathUtils(ns);
    try {
        Node mainNode = message.getContent(Node.class);
        Node fault = null;
        if (reader instanceof W3CDOMStreamReader) {
            W3CDOMStreamReader dr = (W3CDOMStreamReader) reader;
            fault = dr.getCurrentElement();
            dr.consumeFrame();
        } else if (mainNode != null) {
            Node bodyNode = (Node) xu.getValue("//s:Body", mainNode, XPathConstants.NODE);
            StaxUtils.readDocElements(bodyNode.getOwnerDocument(), bodyNode, new FragmentStreamReader(reader), false, false);
            fault = (Element) xu.getValue("//s:Fault", bodyNode, XPathConstants.NODE);
        } else {
            fault = StaxUtils.read(new FragmentStreamReader(reader));
        }
        fault = DOMUtils.getDomElement(fault);
        Element el = (Element) xu.getValue("//s:Fault/s:Code/s:Value", fault, XPathConstants.NODE);
        if (el != null) {
            faultCode = DOMUtils.createQName(el.getTextContent(), el);
        }
        el = (Element) xu.getValue("//s:Fault/s:Code/s:Subcode", fault, XPathConstants.NODE);
        if (el != null) {
            subCodes = new LinkedList<QName>();
            NodeList vlist = el.getElementsByTagNameNS(Soap12.SOAP_NAMESPACE, "Value");
            for (int i = 0; i < vlist.getLength(); i++) {
                Node v = vlist.item(i);
                subCodes.add(DOMUtils.createQName(v.getTextContent(), v));
            }
        }
        exMessage = (String) xu.getValue("//s:Fault/s:Reason/s:Text/text()", fault, XPathConstants.STRING);
        lang = (String) xu.getValue("//s:Fault/s:Reason/s:Text/@xml:lang", fault, XPathConstants.STRING);
        Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail", fault, XPathConstants.NODE);
        if (detailNode != null) {
            detail = (Element) detailNode;
        }
        role = (String) xu.getValue("//s:Fault/s:Role/text()", fault, XPathConstants.STRING);
        node = (String) xu.getValue("//s:Fault/s:Node/text()", fault, XPathConstants.STRING);
    } catch (XMLStreamException e) {
        throw new SoapFault("Could not parse message.", e, message.getVersion().getSender());
    }
    // if the fault's content is invalid and fautlCode is not found, blame the receiver
    if (faultCode == null) {
        faultCode = Soap12.getInstance().getReceiver();
        exMessage = new Message("INVALID_FAULT", LOG).toString();
    }
    SoapFault fault = new SoapFault(exMessage, faultCode);
    fault.setSubCodes(subCodes);
    fault.setDetail(detail);
    fault.setRole(role);
    fault.setNode(node);
    fault.setLang(lang);
    return fault;
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XPathUtils(org.apache.cxf.helpers.XPathUtils) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader)

Example 4 with FragmentStreamReader

use of org.apache.cxf.staxutils.FragmentStreamReader 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

XMLStreamException (javax.xml.stream.XMLStreamException)4 FragmentStreamReader (org.apache.cxf.staxutils.FragmentStreamReader)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 Element (org.w3c.dom.Element)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 DatabindingException (org.apache.cxf.aegis.DatabindingException)1 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)1 SoapFault (org.apache.cxf.binding.soap.SoapFault)1 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)1 XMLFault (org.apache.cxf.binding.xml.XMLFault)1 Message (org.apache.cxf.common.i18n.Message)1 XPathUtils (org.apache.cxf.helpers.XPathUtils)1 Fault (org.apache.cxf.interceptor.Fault)1 StaxInEndingInterceptor (org.apache.cxf.interceptor.StaxInEndingInterceptor)1 Message (org.apache.cxf.message.Message)1 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)1 W3CDOMStreamReader (org.apache.cxf.staxutils.W3CDOMStreamReader)1