Search in sources :

Example 26 with CachedOutputStream

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

the class LoggingOutInterceptorTest method testFormatting.

@Test
public void testFormatting() throws Exception {
    control.replay();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(baos);
    LoggingOutInterceptor p = new LoggingOutInterceptor(pw);
    // p.setPrettyLogging(true);
    CachedOutputStream cos = new CachedOutputStream();
    String s = "<today><is><the><twenty> <second> <of> <january> <two> <thousand> <and> <nine></nine> " + "</and></thousand></two></january></of></second></twenty></the></is></today>";
    cos.write(s.getBytes());
    Message message = new MessageImpl();
    message.setExchange(new ExchangeImpl());
    message.put(Message.CONTENT_TYPE, "application/xml");
    Logger logger = LogUtils.getL7dLogger(this.getClass());
    LoggingOutInterceptor.LoggingCallback l = p.new LoggingCallback(logger, message, cos);
    l.onClose(cos);
    String str = baos.toString();
    // format has changed
    assertFalse(str.matches(s));
    assertTrue(str.contains("<today>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) PrintWriter(java.io.PrintWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 27 with CachedOutputStream

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

the class WireTapIn method handleInputStream.

private void handleInputStream(Message message, InputStream is) throws IOException {
    CachedOutputStream bos = new CachedOutputStream();
    if (threshold > 0) {
        bos.setThreshold(threshold);
    }
    // use the appropriate input stream and restore it later
    InputStream bis = is instanceof DelegatingInputStream ? ((DelegatingInputStream) is).getInputStream() : is;
    // only copy up to the limit since that's all we need to log
    // we can stream the rest
    IOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);
    bos.flush();
    bis = new SequenceInputStream(bos.getInputStream(), bis);
    // restore the delegating input stream or the input stream
    if (is instanceof DelegatingInputStream) {
        ((DelegatingInputStream) is).setInputStream(bis);
    } else {
        message.setContent(InputStream.class, bis);
    }
    message.setContent(CachedOutputStream.class, bos);
}
Also used : SequenceInputStream(java.io.SequenceInputStream) DelegatingInputStream(org.apache.cxf.io.DelegatingInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) DelegatingInputStream(org.apache.cxf.io.DelegatingInputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 28 with CachedOutputStream

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

the class JSONProvider method marshal.

protected void marshal(Marshaller ms, Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
    OutputStream actualOs = os;
    MessageContext mc = getContext();
    if (mc != null && PropertyUtils.isTrue(mc.get(Marshaller.JAXB_FORMATTED_OUTPUT))) {
        actualOs = new CachedOutputStream();
    }
    XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc, actualOs, isCollection);
    if (namespaceMap.size() > 1 || namespaceMap.size() == 1 && !namespaceMap.containsKey(JSONUtils.XSI_URI)) {
        setNamespaceMapper(ms, namespaceMap);
    }
    org.apache.cxf.common.jaxb.JAXBUtils.setNoEscapeHandler(ms);
    ms.marshal(actualObject, writer);
    writer.close();
    if (os != actualOs) {
        StringIndenter formatter = new StringIndenter(IOUtils.newStringFromBytes(((CachedOutputStream) actualOs).getBytes()));
        Writer outWriter = new OutputStreamWriter(os, enc);
        IOUtils.copy(new StringReader(formatter.result()), outWriter, 2048);
        outWriter.close();
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) StringIndenter(org.codehaus.jettison.util.StringIndenter) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 29 with CachedOutputStream

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

the class MessageModeInInterceptor method doFromSoapMessage.

private void doFromSoapMessage(Message message, Object sm) {
    SOAPMessage m = (SOAPMessage) sm;
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null) {
        list = new MessageContentsList();
        message.setContent(List.class, list);
    }
    Object o = m;
    if (StreamSource.class.isAssignableFrom(type)) {
        try {
            try (CachedOutputStream out = new CachedOutputStream()) {
                XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(new DOMSource(m.getSOAPPart()), xsw);
                xsw.close();
                o = new StreamSource(out.getInputStream());
            }
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (SAXSource.class.isAssignableFrom(type)) {
        o = new StaxSource(new W3CDOMStreamReader(m.getSOAPPart()));
    } else if (Source.class.isAssignableFrom(type)) {
        o = new DOMSource(m.getSOAPPart());
    }
    list.set(0, o);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SAXSource(javax.xml.transform.sax.SAXSource) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxSource(org.apache.cxf.staxutils.StaxSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List)

Example 30 with CachedOutputStream

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

the class LogicalMessageImpl method handleDispatchProviderCase.

private Source handleDispatchProviderCase(Service.Mode mode) {
    Source source = null;
    Message message = msgContext.getWrappedMessage();
    Source obj = message.getContent(Source.class);
    if (message instanceof SoapMessage) {
        // StreamSource may only be used once, need to make a copy
        if (obj instanceof StreamSource) {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                obj = new StreamSource(cos.getInputStream());
                message.setContent(Source.class, new StreamSource(cos.getInputStream()));
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
        if (mode == Service.Mode.PAYLOAD) {
            source = obj;
        } else {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                InputStream in = cos.getInputStream();
                SOAPMessage msg = initSOAPMessage(in);
                source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild());
                in.close();
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
    } else if (message instanceof XMLMessage) {
        if (obj != null) {
            source = obj;
        } else if (message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    }
    return source;
}
Also used : XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) DataSource(javax.activation.DataSource)

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