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>"));
}
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);
}
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();
}
}
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);
}
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;
}
Aggregations