Author: ningjiang
Date: Mon Aug  2 13:51:38 2010
New Revision: 981508

URL: http://svn.apache.org/viewvc?rev=981508&view=rev
Log:
CXF-2923 Logging{In|Out}Intererceptor should check the encoding before writing 
the log

Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
    
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
    
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java?rev=981508&r1=981507&r2=981508&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java 
(original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/io/CachedOutputStream.java Mon 
Aug  2 13:51:38 2010
@@ -279,7 +279,12 @@ public class CachedOutputStream extends 
             IOUtils.copyAndCloseInput(fin, out);
         }
     }
+    
     public void writeCacheTo(StringBuilder out, int limit) throws IOException {
+        writeCacheTo(out, "UTF-8", limit);
+    }
+    
+    public void writeCacheTo(StringBuilder out, String charsetName, int limit) 
throws IOException {
         flush();
         if (totalLength < limit
             || limit == -1) {
@@ -291,7 +296,7 @@ public class CachedOutputStream extends 
         if (inmem) {
             if (currentStream instanceof ByteArrayOutputStream) {
                 byte bytes[] = 
((ByteArrayOutputStream)currentStream).toByteArray();
-                out.append(IOUtils.newStringFromBytes(bytes, 0, limit));
+                out.append(IOUtils.newStringFromBytes(bytes, charsetName, 0, 
limit));
             } else {
                 throw new IOException("Unknown format of currentStream");
             }
@@ -304,7 +309,7 @@ public class CachedOutputStream extends 
                 if ((count + x) > limit) {
                     x = limit - count;
                 }
-                out.append(IOUtils.newStringFromBytes(bytes, 0, x));
+                out.append(IOUtils.newStringFromBytes(bytes, charsetName, 0, 
x));
                 count += x;
 
                 if (count >= limit) {
@@ -316,12 +321,17 @@ public class CachedOutputStream extends 
             fin.close();
         }
     }
+    
     public void writeCacheTo(StringBuilder out) throws IOException {
+        writeCacheTo(out, "UTF-8");
+    }
+    
+    public void writeCacheTo(StringBuilder out, String charsetName) throws 
IOException {
         flush();
         if (inmem) {
             if (currentStream instanceof ByteArrayOutputStream) {
                 byte[] bytes = 
((ByteArrayOutputStream)currentStream).toByteArray();
-                out.append(IOUtils.newStringFromBytes(bytes));
+                out.append(IOUtils.newStringFromBytes(bytes, charsetName));
             } else {
                 throw new IOException("Unknown format of currentStream");
             }
@@ -331,7 +341,7 @@ public class CachedOutputStream extends 
             byte bytes[] = new byte[1024];
             int x = fin.read(bytes);
             while (x != -1) {
-                out.append(IOUtils.newStringFromBytes(bytes, 0, x));
+                out.append(IOUtils.newStringFromBytes(bytes, charsetName, 0, 
x));
                 x = fin.read(bytes);
             }
             fin.close();

Modified: 
cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java?rev=981508&r1=981507&r2=981508&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java 
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java 
Mon Aug  2 13:51:38 2010
@@ -38,38 +38,65 @@ public final class IOUtils {
     }
     
     /**
-     * Use this function instead of new String(byte[]) to avoid surprises from 
non-standard default encodings.
+     * Use this function instead of new String(byte[], String) to avoid 
surprises from 
+     * non-standard default encodings.
      * @param bytes
+     * @param charsetName
      * @return
      */
-    public static String newStringFromBytes(byte[] bytes) {
+    public static String newStringFromBytes(byte[] bytes, String charsetName) {
         try {
-            return new String(bytes, UTF8_CHARSET.name());
+            return new String(bytes, charsetName);
         } catch (UnsupportedEncodingException e) {
             throw 
-                new RuntimeException("Impossible failure: 
Charset.forName(\"utf-8\") returns invalid name.");
+                new RuntimeException("Impossible failure: Charset.forName(\""
+                                     + charsetName + "\") returns invalid 
name.");
 
         }
     }
-
+    
+    
     /**
-     * Use this function instead of new String(byte[], int, int) 
+     * Use this function instead of new String(byte[]) to avoid surprises from 
non-standard default encodings.
+     * @param bytes
+     * @return
+     */
+    public static String newStringFromBytes(byte[] bytes) {
+        return newStringFromBytes(bytes, UTF8_CHARSET.name());        
+    }
+    
+    /**
+     * Use this function instead of new String(byte[], int, int, String) 
      * to avoid surprises from non-standard default encodings.
      * @param bytes
+     * @param charsetName
      * @param start
      * @param length
      * @return
      */
-    public static String newStringFromBytes(byte[] bytes, int start, int 
length) {
+    public static String newStringFromBytes(byte[] bytes, String charsetName, 
int start, int length) {
         try {
-            return new String(bytes, start, length, UTF8_CHARSET.name());
+            return new String(bytes, start, length, charsetName);
         } catch (UnsupportedEncodingException e) {
             throw 
-                new RuntimeException("Impossible failure: 
Charset.forName(\"utf-8\") returns invalid name.");
+                new RuntimeException("Impossible failure: Charset.forName(\""
+                                     + charsetName + "\") returns invalid 
name.");
 
         }
     }
 
+    /**
+     * Use this function instead of new String(byte[], int, int) 
+     * to avoid surprises from non-standard default encodings.
+     * @param bytes
+     * @param start
+     * @param length
+     * @return
+     */
+    public static String newStringFromBytes(byte[] bytes, int start, int 
length) {
+        return newStringFromBytes(bytes, UTF8_CHARSET.name(), start, length);
+    }
+
     public static int copy(final InputStream input, final OutputStream output)
         throws IOException {
         return copy(input, output, DEFAULT_BUFFER_SIZE);

Modified: 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java?rev=981508&r1=981507&r2=981508&view=diff
==============================================================================
--- 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
 (original)
+++ 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
 Mon Aug  2 13:51:38 2010
@@ -24,6 +24,7 @@ import java.io.PrintWriter;
 import java.util.logging.Level;
 
 import org.apache.cxf.common.injection.NoJSR250Annotations;
+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;
@@ -116,7 +117,11 @@ public class LoggingInInterceptor extend
                 if (bos.size() > limit) {
                     buffer.getMessage().append("(message truncated to " + 
limit + " bytes)\n");
                 }
-                bos.writeCacheTo(buffer.getPayload(), limit);
+                if (StringUtils.isEmpty(encoding)) {
+                    bos.writeCacheTo(buffer.getPayload(), limit);
+                } else {
+                    bos.writeCacheTo(buffer.getPayload(), encoding, limit);
+                }
                     
                 bos.close();
             } catch (IOException e) {

Modified: 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
URL: 
http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java?rev=981508&r1=981507&r2=981508&view=diff
==============================================================================
--- 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
 (original)
+++ 
cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/LoggingOutInterceptor.java
 Mon Aug  2 13:51:38 2010
@@ -24,6 +24,7 @@ import java.io.PrintWriter;
 import java.util.logging.Level;
 
 import org.apache.cxf.common.injection.NoJSR250Annotations;
+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;
@@ -133,7 +134,11 @@ public class LoggingOutInterceptor exten
                 }
             }
             try {
-                cos.writeCacheTo(buffer.getPayload(), limit);
+                if (StringUtils.isEmpty(encoding)) {
+                    cos.writeCacheTo(buffer.getPayload(), limit);
+                } else {                    
+                    cos.writeCacheTo(buffer.getPayload(), encoding, limit);
+                }
             } catch (Exception ex) {
                 //ignore
             }


Reply via email to