Author: ningjiang
Date: Fri Aug 6 01:55:26 2010
New Revision: 982847
URL: http://svn.apache.org/viewvc?rev=982847&view=rev
Log:
CXF-2931 Polish the code as DanK suggested.
Modified:
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java
Modified:
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java?rev=982847&r1=982846&r2=982847&view=diff
==============================================================================
---
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
(original)
+++
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
Fri Aug 6 01:55:26 2010
@@ -26,13 +26,13 @@ import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
@@ -111,18 +111,11 @@ public class LoggingInInterceptor extend
return originalLogString;
}
- protected void writePayload(StringBuilder builder, CachedOutputStream cos,
String encoding)
+ protected void writePayload(StringBuilder builder, CachedOutputStream cos,
+ String encoding, String contentType)
throws Exception {
- if (isPrettyLogging()) {
-
- TransformerFactory tfactory = TransformerFactory.newInstance();
- try {
- tfactory.setAttribute("indent-number", "2");
- } catch (Exception ex) {
- // ignore
- }
- Transformer serializer;
- serializer = tfactory.newTransformer();
+ if (isPrettyLogging() && (contentType != null &&
contentType.indexOf("xml") >= 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");
@@ -201,7 +194,7 @@ public class LoggingInInterceptor extend
if (bos.size() > limit) {
buffer.getMessage().append("(message truncated to " +
limit + " bytes)\n");
}
- writePayload(buffer.getPayload(), bos, encoding);
+ writePayload(buffer.getPayload(), bos, encoding, ct);
bos.close();
} catch (Exception e) {
Modified:
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java?rev=982847&r1=982846&r2=982847&view=diff
==============================================================================
---
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
(original)
+++
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
Fri Aug 6 01:55:26 2010
@@ -27,12 +27,12 @@ import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.io.CacheAndWriteOutputStream;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.CachedOutputStreamCallback;
@@ -115,18 +115,11 @@ public class LoggingOutInterceptor exten
return originalLogString;
}
- protected void writePayload(StringBuilder builder, CachedOutputStream cos,
String encoding)
+ protected void writePayload(StringBuilder builder, CachedOutputStream cos,
+ String encoding, String contentType)
throws Exception {
- if (isPrettyLogging()) {
-
- TransformerFactory tfactory = TransformerFactory.newInstance();
- try {
- tfactory.setAttribute("indent-number", "2");
- } catch (Exception ex) {
- // ignore
- }
- Transformer serializer;
- serializer = tfactory.newTransformer();
+ if (isPrettyLogging() && (contentType != null &&
contentType.indexOf("xml") >= 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");
@@ -211,7 +204,7 @@ public class LoggingOutInterceptor exten
}
}
try {
- writePayload(buffer.getPayload(), cos, encoding);
+ writePayload(buffer.getPayload(), cos, encoding, ct);
} catch (Exception ex) {
//ignore
}
Modified:
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java?rev=982847&r1=982846&r2=982847&view=diff
==============================================================================
---
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java
(original)
+++
cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java
Fri Aug 6 01:55:26 2010
@@ -28,11 +28,11 @@ import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.io.CacheAndWriteOutputStream;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.CachedOutputStreamCallback;
@@ -80,14 +80,7 @@ public class PrettyLoggingOutInterceptor
public void onClose(CachedOutputStream cos) {
try {
- TransformerFactory tfactory = TransformerFactory.newInstance();
- try {
- tfactory.setAttribute("indent-number", "2");
- } catch (Exception ex) {
- //ignore
- }
- Transformer serializer;
- serializer = tfactory.newTransformer();
+ 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");