Search in sources :

Example 71 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class FormUtils method restoreForm.

public static void restoreForm(FormEncodingProvider<Form> provider, Form form, Message message) throws Exception {
    CachedOutputStream os = new CachedOutputStream();
    writeForm(provider, form, os);
    message.setContent(InputStream.class, os.getInputStream());
}
Also used : CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 72 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class MessageModeOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop != null && !bindingName.equals(bop.getBinding().getName())) {
        return;
    }
    if (saajOut != null) {
        doSoap(message);
    } else if (DataSource.class.isAssignableFrom(type)) {
        // datasource stuff, must check if multi-source
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        DataSource ds = (DataSource) list.get(0);
        String ct = ds.getContentType();
        if (ct.toLowerCase().contains("multipart/related")) {
            Message msg = new MessageImpl();
            msg.setExchange(message.getExchange());
            msg.put(Message.CONTENT_TYPE, ct);
            try {
                msg.setContent(InputStream.class, ds.getInputStream());
                AttachmentDeserializer deser = new AttachmentDeserializer(msg);
                deser.initializeAttachments();
            } catch (IOException ex) {
                throw new Fault(ex);
            }
            message.setAttachments(msg.getAttachments());
            final InputStream in = msg.getContent(InputStream.class);
            final String ct2 = (String) msg.get(Message.CONTENT_TYPE);
            list.set(0, new DataSource() {

                public String getContentType() {
                    return ct2;
                }

                public InputStream getInputStream() throws IOException {
                    return in;
                }

                public String getName() {
                    return ct2;
                }

                public OutputStream getOutputStream() throws IOException {
                    return null;
                }
            });
        } else if (!ct.toLowerCase().contains("xml")) {
            // not XML based, need to stream out directly.  This is a bit tricky as
            // we don't want the stax stuff triggering and such
            OutputStream out = message.getContent(OutputStream.class);
            message.put(Message.CONTENT_TYPE, ct);
            try {
                InputStream in = ds.getInputStream();
                IOUtils.copy(in, out);
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new Fault(e);
            }
            list.remove(0);
            out = new CachedOutputStream();
            message.setContent(OutputStream.class, out);
            XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
            message.setContent(XMLStreamWriter.class, writer);
        }
    } else if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && Source.class.isAssignableFrom(type)) {
        // if schema validation is on, we'll end up converting to a DOMSource anyway,
        // let's convert and check for a fault
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        Source ds = (Source) list.get(0);
        if (!(ds instanceof DOMSource)) {
            try {
                ds = new DOMSource(StaxUtils.read(ds));
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
            list.set(0, ds);
            validatePossibleFault(message, bop, ((DOMSource) ds).getNode());
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) DataSource(javax.activation.DataSource) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 73 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class AttachmentDeserializer method cache.

private void cache(DelegatingInputStream input) throws IOException {
    if (loaded.contains(input)) {
        return;
    }
    loaded.add(input);
    InputStream origIn = input.getInputStream();
    try (CachedOutputStream out = new CachedOutputStream()) {
        AttachmentUtil.setStreamedAttachmentProperties(message, out);
        IOUtils.copy(input, out);
        input.setInputStream(out.getInputStream());
        origIn.close();
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 74 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class AttachmentDataSource method cache.

public void cache(Message message) throws IOException {
    if (cache == null) {
        cache = new CachedOutputStream();
        AttachmentUtil.setStreamedAttachmentProperties(message, cache);
        try {
            IOUtils.copyAndCloseInput(ins, cache);
            cache.lockOutputStream();
            if (delegate != null) {
                delegate.setInputStream(cache.getInputStream());
            }
        } catch (CacheSizeExceededException | IOException cee) {
            cache.close();
            cache = null;
            throw cee;
        } finally {
            ins = null;
        }
    }
}
Also used : CacheSizeExceededException(org.apache.cxf.io.CacheSizeExceededException) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 75 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class XSLTOutInterceptor method transformOS.

protected void transformOS(Message message, OutputStream out) {
    CachedOutputStream wrapper = new CachedOutputStream();
    CachedOutputStreamCallback callback = new XSLTCachedOutputStreamCallback(getXSLTTemplate(), out);
    wrapper.registerCallback(callback);
    message.setContent(OutputStream.class, wrapper);
}
Also used : CachedOutputStreamCallback(org.apache.cxf.io.CachedOutputStreamCallback) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Aggregations

CachedOutputStream (org.apache.cxf.io.CachedOutputStream)105 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 Test (org.junit.Test)24 Message (org.apache.cxf.message.Message)22 OutputStream (java.io.OutputStream)18 Fault (org.apache.cxf.interceptor.Fault)18 MessageImpl (org.apache.cxf.message.MessageImpl)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)10 PrintWriter (java.io.PrintWriter)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 StreamSource (javax.xml.transform.stream.StreamSource)6 Endpoint (org.apache.cxf.endpoint.Endpoint)6 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)6 RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Source (javax.xml.transform.Source)5 ArrayList (java.util.ArrayList)4