Repository: cxf Updated Branches: refs/heads/2.7.x-fixes b9d8aee62 -> 41e11d892
[CXF-5761] When pretty writing, ignore any errors as they could be caused by the truncation Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/88c98e8a Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/88c98e8a Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/88c98e8a Branch: refs/heads/2.7.x-fixes Commit: 88c98e8a3e300040007bc6552a2986b142204347 Parents: b9d8aee Author: Daniel Kulp <[email protected]> Authored: Thu Jun 12 10:53:42 2014 -0400 Committer: Daniel Kulp <[email protected]> Committed: Thu Jun 12 11:03:14 2014 -0400 ---------------------------------------------------------------------- .../interceptor/AbstractLoggingInterceptor.java | 44 ++++++++++++-------- 1 file changed, 27 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/88c98e8a/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java b/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java index 8ba74dd..d724f5b 100644 --- a/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java +++ b/api/src/main/java/org/apache/cxf/interceptor/AbstractLoggingInterceptor.java @@ -20,6 +20,7 @@ package org.apache.cxf.interceptor; import java.io.File; import java.io.FileWriter; +import java.io.InputStream; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; @@ -30,27 +31,26 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.stream.StreamResult; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.stream.StreamSource; import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.endpoint.Endpoint; -import org.apache.cxf.helpers.XMLUtils; import org.apache.cxf.io.CachedOutputStream; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.service.model.InterfaceInfo; +import org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter; +import org.apache.cxf.staxutils.StaxUtils; /** * A simple logging handler which outputs the bytes of the message to the * Logger. */ public abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor<Message> { - protected static final String BINARY_CONTENT_MESSAGE = "--- Binary Content ---"; private static final List<String> BINARY_CONTENT_MEDIA_TYPES; static { @@ -155,13 +155,25 @@ public abstract class AbstractLoggingInterceptor extends AbstractPhaseIntercepto // Just transform the XML message when the cos has content if (isPrettyLogging() && (contentType != null && contentType.indexOf("xml") >= 0 && contentType.toLowerCase().indexOf("multipart/related") < 0) && cos.size() > 0) { - Transformer serializer = XMLUtils.newTransformer(2); - // Setup indenting to "pretty print" - serializer.setOutputProperty(OutputKeys.INDENT, "yes"); - serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter swriter = new StringWriter(); - serializer.transform(new StreamSource(cos.getInputStream()), new StreamResult(swriter)); + XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter); + xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2); + InputStream in = cos.getInputStream(); + try { + StaxUtils.copy(new StreamSource(in), xwriter); + } catch (XMLStreamException xse) { + //ignore + } finally { + try { + xwriter.flush(); + xwriter.close(); + } catch (XMLStreamException xse2) { + //ignore + } + in.close(); + } + String result = swriter.toString(); if (result.length() < limit || limit == -1) { builder.append(swriter.toString()); @@ -175,7 +187,6 @@ public abstract class AbstractLoggingInterceptor extends AbstractPhaseIntercepto } else { cos.writeCacheTo(builder, encoding, limit); } - } } protected void writePayload(StringBuilder builder, @@ -187,14 +198,13 @@ public abstract class AbstractLoggingInterceptor extends AbstractPhaseIntercepto && contentType != null && contentType.indexOf("xml") >= 0 && stringWriter.getBuffer().length() > 0) { - Transformer serializer = XMLUtils.newTransformer(2); - // Setup indenting to "pretty print" - serializer.setOutputProperty(OutputKeys.INDENT, "yes"); - serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter swriter = new StringWriter(); - serializer.transform(new StreamSource(new StringReader(stringWriter.getBuffer().toString())), - new StreamResult(swriter)); + XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter); + xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2); + StaxUtils.copy(new StreamSource(new StringReader(stringWriter.getBuffer().toString())), xwriter); + xwriter.close(); + String result = swriter.toString(); if (result.length() < limit || limit == -1) { builder.append(swriter.toString());
