Search in sources :

Example 1 with CachingXmlEventWriter

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

the class JAXRSOutInterceptor method checkCachedStream.

private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled) throws Exception {
    final XMLStreamWriter writer;
    if (enabled) {
        writer = m.getContent(XMLStreamWriter.class);
    } else {
        writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
    }
    if (writer instanceof CachingXmlEventWriter) {
        CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
        if (!cache.getEvents().isEmpty()) {
            XMLStreamWriter origWriter = null;
            try {
                origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
                for (XMLEvent event : cache.getEvents()) {
                    StaxUtils.writeEvent(event, origWriter);
                }
            } finally {
                StaxUtils.close(origWriter);
            }
        }
        m.setContent(XMLStreamWriter.class, null);
        return;
    }
    if (enabled) {
        OutputStream os = m.getContent(OutputStream.class);
        if (os != osOriginal && os instanceof CachedOutputStream) {
            CachedOutputStream cos = (CachedOutputStream) os;
            if (cos.size() != 0) {
                cos.writeCacheTo(osOriginal);
            }
        }
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) XMLEvent(javax.xml.stream.events.XMLEvent) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 2 with CachingXmlEventWriter

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

the class JAXRSOutInterceptor method checkBufferingMode.

private boolean checkBufferingMode(Message m, List<WriterInterceptor> writers, boolean firstTry) {
    if (!firstTry) {
        return false;
    }
    WriterInterceptor last = writers.get(writers.size() - 1);
    MessageBodyWriter<Object> w = ((WriterInterceptorMBW) last).getMBW();
    Object outBuf = m.getContextualProperty(OUT_BUFFERING);
    boolean enabled = PropertyUtils.isTrue(outBuf);
    boolean configurableProvider = w instanceof AbstractConfigurableProvider;
    if (!enabled && outBuf == null && configurableProvider) {
        enabled = ((AbstractConfigurableProvider) w).getEnableBuffering();
    }
    if (enabled) {
        boolean streamingOn = configurableProvider && ((AbstractConfigurableProvider) w).getEnableStreaming();
        if (streamingOn) {
            m.setContent(XMLStreamWriter.class, new CachingXmlEventWriter());
        } else {
            m.setContent(OutputStream.class, new CachedOutputStream());
        }
    }
    return enabled;
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) AbstractConfigurableProvider(org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 3 with CachingXmlEventWriter

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

the class AbstractOutDatabindingInterceptor method writeParts.

protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
    OutputStream out = message.getContent(OutputStream.class);
    XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
    Service service = exchange.getService();
    XMLStreamWriter xmlWriter = origXmlWriter;
    CachingXmlEventWriter cache = null;
    // configure endpoint and operation level schema validation
    setOperationSchemaValidation(message);
    // need to cache the events in case validation fails or buffering is enabled
    if (shouldBuffer(message)) {
        if (!(xmlWriter instanceof CachingXmlEventWriter)) {
            cache = new CachingXmlEventWriter();
            try {
                cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
            } catch (XMLStreamException e) {
            // ignorable, will just get extra namespace decls
            }
            xmlWriter = cache;
        }
        out = null;
    }
    if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.getContextualBoolean(message, DISABLE_OUTPUTSTREAM_OPTIMIZATION, false)) {
        if (xmlWriter != null) {
            try {
                xmlWriter.writeCharacters("");
                xmlWriter.flush();
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
        }
        DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                osWriter.write(o, part, out);
            }
        }
    } else {
        DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, service, XMLStreamWriter.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                NamespaceContext c = null;
                if (!part.isElement() && xmlWriter instanceof CachingXmlEventWriter) {
                    try {
                        c = xmlWriter.getNamespaceContext();
                        xmlWriter.setNamespaceContext(new CachingXmlEventWriter.NSContext(null));
                    } catch (XMLStreamException e) {
                    // ignore
                    }
                }
                Object o = objs.get(part);
                dataWriter.write(o, part, xmlWriter);
                if (c != null) {
                    try {
                        xmlWriter.setNamespaceContext(c);
                    } catch (XMLStreamException e) {
                    // ignore
                    }
                }
            }
        }
    }
    if (cache != null) {
        try {
            for (XMLEvent event : cache.getEvents()) {
                StaxUtils.writeEvent(event, origXmlWriter);
            }
        } catch (XMLStreamException e) {
            throw new Fault(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) NamespaceContext(javax.xml.namespace.NamespaceContext) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 4 with CachingXmlEventWriter

use of org.apache.cxf.staxutils.CachingXmlEventWriter 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;
        }
        final List<MessagePartInfo> parts;
        final boolean output;
        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 5 with CachingXmlEventWriter

use of org.apache.cxf.staxutils.CachingXmlEventWriter in project midpoint by Evolveum.

the class CustomWrappedOutInterceptor method writeParts.

@Override
protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, List<MessagePartInfo> parts) {
    // TODO Auto-generated method stub
    OutputStream out = message.getContent(OutputStream.class);
    XMLStreamWriter origXmlWriter = message.getContent(XMLStreamWriter.class);
    Service service = exchange.getService();
    XMLStreamWriter xmlWriter = origXmlWriter;
    CachingXmlEventWriter cache = null;
    Object en = message.getContextualProperty(OUT_BUFFERING);
    boolean allowBuffer = true;
    boolean buffer = false;
    if (en != null) {
        buffer = Boolean.TRUE.equals(en) || "true".equals(en);
        allowBuffer = !(Boolean.FALSE.equals(en) || "false".equals(en));
    }
    // need to cache the events in case validation fails or buffering is enabled
    if (buffer || (allowBuffer && shouldValidate(message) && !isRequestor(message))) {
        cache = new CachingXmlEventWriter();
        try {
            cache.setNamespaceContext(origXmlWriter.getNamespaceContext());
        } catch (XMLStreamException e) {
        //ignorable, will just get extra namespace decls
        }
        xmlWriter = cache;
        out = null;
    }
    if (out != null && writeToOutputStream(message, operation.getBinding(), service) && !MessageUtils.isTrue(message.getContextualProperty(DISABLE_OUTPUTSTREAM_OPTIMIZATION))) {
        if (xmlWriter != null) {
            try {
                xmlWriter.writeCharacters("");
                xmlWriter.flush();
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
        }
        DataWriter<OutputStream> osWriter = getDataWriter(message, service, OutputStream.class);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                osWriter.write(o, part, out);
            }
        }
    } else {
        DataWriter<XMLStreamWriter> dataWriter = new CustomDataWriter(prismContext);
        for (MessagePartInfo part : parts) {
            if (objs.hasValue(part)) {
                Object o = objs.get(part);
                dataWriter.write(o, part, xmlWriter);
            }
        }
    }
    if (cache != null) {
        try {
            for (XMLEvent event : cache.getEvents()) {
                StaxUtils.writeEvent(event, origXmlWriter);
            }
        } catch (XMLStreamException e) {
            throw new Fault(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) CachingXmlEventWriter(org.apache.cxf.staxutils.CachingXmlEventWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

CachingXmlEventWriter (org.apache.cxf.staxutils.CachingXmlEventWriter)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5 XMLEvent (javax.xml.stream.events.XMLEvent)5 OutputStream (java.io.OutputStream)4 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)3 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)2 Fault (org.apache.cxf.interceptor.Fault)2 WriterInterceptorMBW (org.apache.cxf.jaxrs.impl.WriterInterceptorMBW)2 AbstractConfigurableProvider (org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider)2 Service (org.apache.cxf.service.Service)2 NamespaceContext (javax.xml.namespace.NamespaceContext)1 NSStack (org.apache.cxf.helpers.NSStack)1 MessageContentsList (org.apache.cxf.message.MessageContentsList)1 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)1