cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r982506 - in /cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor: LoggingInInterceptor.java LoggingOutInterceptor.java
Date Thu, 05 Aug 2010 08:46:05 GMT
Author: ningjiang
Date: Thu Aug  5 08:46:04 2010
New Revision: 982506

URL: http://svn.apache.org/viewvc?rev=982506&view=rev
Log:
CXF-2931 Fixed the build error

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

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=982506&r1=982505&r2=982506&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
Thu Aug  5 08:46:04 2010
@@ -20,10 +20,18 @@ package org.apache.cxf.interceptor;
 
 import java.io.InputStream;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.logging.Level;
 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.io.CachedOutputStream;
 import org.apache.cxf.message.Message;
@@ -40,6 +48,7 @@ public class LoggingInInterceptor extend
 
     private int limit = 100 * 1024;
     private PrintWriter writer;
+    private boolean prettyLogging;
     
     
     public LoggingInInterceptor() {
@@ -60,6 +69,14 @@ public class LoggingInInterceptor extend
         this.writer = w;
     }
     
+    public void setPrettyLogging(boolean flag) {
+        prettyLogging = flag;
+    }
+    
+    public boolean isPrettyLogging() {
+        return prettyLogging;
+    }
+    
     public void setPrintWriter(PrintWriter w) {
         writer = w;
     }
@@ -92,7 +109,42 @@ public class LoggingInInterceptor extend
      */
     protected String transform(String originalLogString) {
         return originalLogString;
-    } 
+    }
+    
+    protected void writePayload(StringBuilder builder, CachedOutputStream cos, String encoding)
+        throws Exception {
+        if (isPrettyLogging()) {
+
+            TransformerFactory tfactory = TransformerFactory.newInstance();
+            try {
+                tfactory.setAttribute("indent-number", "2");
+            } catch (Exception ex) {
+                // ignore
+            }
+            Transformer serializer;
+            serializer = tfactory.newTransformer();
+            // 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));
+            String result = swriter.toString();
+            if (result.length() < limit || limit == -1) {
+                builder.append(swriter.toString());
+            } else {
+                builder.append(swriter.toString().substring(0, limit));
+            }
+
+        } else {
+            if (StringUtils.isEmpty(encoding)) {
+                cos.writeCacheTo(builder, limit);
+            } else {
+                cos.writeCacheTo(builder, encoding, limit);
+            }
+
+        }
+    }
 
     private void logging(Message message) throws Fault {
         if (message.containsKey(LoggingMessage.ID_KEY)) {

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=982506&r1=982505&r2=982506&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
Thu Aug  5 08:46:04 2010
@@ -21,10 +21,18 @@ package org.apache.cxf.interceptor;
 
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.logging.Level;
 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.io.CacheAndWriteOutputStream;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.io.CachedOutputStreamCallback;
@@ -42,6 +50,7 @@ public class LoggingOutInterceptor exten
 
     private int limit = 100 * 1024;
     private PrintWriter writer;
+    private boolean prettyLogging;
     
     public LoggingOutInterceptor(String phase) {
         super(phase);
@@ -60,13 +69,21 @@ public class LoggingOutInterceptor exten
         this.writer = w;
     }
     
+    public void setPrettyLogging(boolean flag) {
+        prettyLogging = flag;
+    }
+    
+    public boolean isPrettyLogging() {
+        return prettyLogging;
+    }
+    
     public void setLimit(int lim) {
         limit = lim;
     }
     
     public int getLimit() {
         return limit;
-    }    
+    }
 
     public void handleMessage(Message message) throws Fault {
         final OutputStream os = message.getContent(OutputStream.class);
@@ -96,7 +113,42 @@ public class LoggingOutInterceptor exten
      */
     protected String transform(String originalLogString) {
         return originalLogString;
-    } 
+    }
+    
+    protected void writePayload(StringBuilder builder, CachedOutputStream cos, String encoding)
+        throws Exception {
+        if (isPrettyLogging()) {
+
+            TransformerFactory tfactory = TransformerFactory.newInstance();
+            try {
+                tfactory.setAttribute("indent-number", "2");
+            } catch (Exception ex) {
+                // ignore
+            }
+            Transformer serializer;
+            serializer = tfactory.newTransformer();
+            // 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));
+            String result = swriter.toString();
+            if (result.length() < limit || limit == -1) {
+                builder.append(swriter.toString());
+            } else {
+                builder.append(swriter.toString().substring(0, limit));
+            }
+
+        } else {
+            if (StringUtils.isEmpty(encoding)) {
+                cos.writeCacheTo(builder, limit);
+            } else {
+                cos.writeCacheTo(builder, encoding, limit);
+            }
+
+        }
+    }
 
     class LoggingCallback implements CachedOutputStreamCallback {
         



Mime
View raw message