cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject cxf git commit: [CXF-6286] Switch to XMLStreamReader and fix limit set
Date Thu, 26 Mar 2015 19:52:24 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 0313a3c7f -> 24b4ed27b


[CXF-6286] Switch to XMLStreamReader and fix limit set


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/24b4ed27
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/24b4ed27
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/24b4ed27

Branch: refs/heads/master
Commit: 24b4ed27bf096d657f11320b83ef8449ca366f63
Parents: 0313a3c
Author: Christian Schneider <chris@die-schneider.net>
Authored: Thu Mar 26 20:51:45 2015 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Thu Mar 26 20:51:45 2015 +0100

----------------------------------------------------------------------
 .../ext/logging/AbstractLoggingInterceptor.java |  6 +-
 .../apache/cxf/ext/logging/LoggingFeature.java  | 12 ++--
 .../org/apache/cxf/ext/logging/WireTapIn.java   | 16 ++++--
 .../ext/logging/event/PrettyLoggingFilter.java  | 60 +++++++++++++-------
 .../apache/cxf/ext/logging/SOAPLoggingTest.java |  2 +
 5 files changed, 62 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/24b4ed27/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
index 6dd2c7d..5fd1bde 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/AbstractLoggingInterceptor.java
@@ -40,15 +40,15 @@ abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor<Messa
     }
     
     public void setLimit(int lim) {
-        limit = lim;
+        this.limit = lim;
     }
 
     public int getLimit() {
-        return limit;
+        return this.limit;
     }
 
     public void setInMemThreshold(long t) {
-        threshold = t;
+        this.threshold = t;
     }
 
     public long getInMemThreshold() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/24b4ed27/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
index c42b51e..51f1885 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/LoggingFeature.java
@@ -43,23 +43,23 @@ import org.apache.cxf.interceptor.InterceptorProvider;
  */
 @NoJSR250Annotations
 public class LoggingFeature extends AbstractFeature {
-    private int limit = AbstractLoggingInterceptor.DEFAULT_LIMIT;
-    private long inMemThreshold;
     private LogEventSender sender;
     private LoggingInInterceptor in;
     private LoggingOutInterceptor out;
+    private WireTapIn wireTapIn;
     private PrettyLoggingFilter prettyFilter;
-    
+
     public LoggingFeature() {
         this.sender = new Slf4jEventSender();
         prettyFilter = new PrettyLoggingFilter(sender);
+        wireTapIn = new WireTapIn();
         in = new LoggingInInterceptor(prettyFilter);
         out = new LoggingOutInterceptor(prettyFilter);
     }
     
     @Override
     protected void initializeProvider(InterceptorProvider provider, Bus bus) {
-        WireTapIn wireTapIn = new WireTapIn(inMemThreshold, limit);
+        
         provider.getInInterceptors().add(wireTapIn);
         provider.getInInterceptors().add(in);
         provider.getInFaultInterceptors().add(in);
@@ -68,14 +68,16 @@ public class LoggingFeature extends AbstractFeature {
         provider.getOutFaultInterceptors().add(out);
     }
 
-    public void setLimit(int lim) {
+    public void setLimit(int limit) {
         in.setLimit(limit);
         out.setLimit(limit);
+        wireTapIn.setLimit(limit);
     }
     
     public void setInMemThreshold(long inMemThreshold) {
         in.setInMemThreshold(inMemThreshold);
         out.setInMemThreshold(inMemThreshold);
+        wireTapIn.setThreshold(inMemThreshold);
     }
     
     public void setSender(LogEventSender sender) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/24b4ed27/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/WireTapIn.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/WireTapIn.java b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/WireTapIn.java
index ae7ee66..527b80e 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/WireTapIn.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/WireTapIn.java
@@ -33,8 +33,8 @@ import org.apache.cxf.phase.AbstractPhaseInterceptor;
 import org.apache.cxf.phase.Phase;
 
 public class WireTapIn extends AbstractPhaseInterceptor<Message> {
-    private long threshold;
-    private int limit;
+    private long threshold = -1;
+    private int limit = AbstractLoggingInterceptor.DEFAULT_LIMIT;
 
     /**
      * Instantiates a new WireTapIn
@@ -42,10 +42,8 @@ public class WireTapIn extends AbstractPhaseInterceptor<Message>
{
      *
      * @param logMessageContent the log message content
      */
-    public WireTapIn(long threshold, int limit) {
+    public WireTapIn() {
         super(Phase.RECEIVE);
-        this.threshold = threshold;
-        this.limit = limit;
     }
 
     @Override
@@ -97,4 +95,12 @@ public class WireTapIn extends AbstractPhaseInterceptor<Message>
{
         message.setContent(CachedOutputStream.class, bos);
 
     }
+    
+    public void setLimit(int limit) {
+        this.limit = limit;
+    }
+    
+    public void setThreshold(long threshold) {
+        this.threshold = threshold;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/24b4ed27/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
index 71742d8..bb99ff2 100644
--- a/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
+++ b/rt/features/logging/src/main/java/org/apache/cxf/ext/logging/event/PrettyLoggingFilter.java
@@ -18,54 +18,72 @@
  */
 package org.apache.cxf.ext.logging.event;
 
-import java.io.StringReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringWriter;
 
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-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.staxutils.PrettyPrintXMLStreamWriter;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class PrettyLoggingFilter implements LogEventSender {
+    private static final Logger LOG = LoggerFactory.getLogger(PrettyLoggingFilter.class);
     private LogEventSender next;
     private boolean prettyLogging;
-    private TransformerFactory transformerFactory;
 
     public PrettyLoggingFilter(LogEventSender next) {
         this.next = next;
         this.prettyLogging = false;
-        transformerFactory = TransformerFactory.newInstance();
     }
 
     @Override
     public void send(LogEvent event) {
         if (shouldPrettyPrint(event)) {
-            event.setPayload(getPrettyMessage(event.getPayload()));
+            event.setPayload(getPrettyMessage(event.getPayload(), event.getEncoding()));
         }
         next.send(event);
     }
 
     private boolean shouldPrettyPrint(LogEvent event) {
+        String contentType = event.getContentType(); 
         return prettyLogging 
-            && event.getContentType() != null 
-            && event.getContentType().indexOf("xml") >= 0 
+            && contentType != null 
+            && contentType.indexOf("xml") >= 0
+            && contentType.toLowerCase().indexOf("multipart/related") < 0
             && event.getPayload().length() > 0;
     }
 
-    public String getPrettyMessage(String message) {
+    public String getPrettyMessage(String message, String encoding) {
+        StringWriter swriter = new StringWriter();
         try {
-            Transformer serializer = transformerFactory.newTransformer();
-            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(message)), new StreamResult(swriter));
-            return swriter.toString();
-        } catch (TransformerException e) {
-            return message;
+            // Using XMLStreamWriter instead of Transformer as it works with non well formed
xml
+            // that can occur when we set a limit and cur the rest off
+            XMLStreamWriter xwriter = StaxUtils.createXMLStreamWriter(swriter);
+            xwriter = new PrettyPrintXMLStreamWriter(xwriter, 2);
+            InputStream in = new ByteArrayInputStream(message.getBytes(encoding));
+            try {
+                StaxUtils.copy(new StreamSource(in), xwriter);
+            } catch (XMLStreamException xse) {
+                //ignore
+            } finally {
+                try {
+                    xwriter.flush();
+                    xwriter.close();
+                } catch (XMLStreamException xse2) {
+                    //ignore
+                }
+                in.close();
+            }
+        } catch (IOException e) {
+            LOG.debug("Error while pretty printing cxf message, returning what we got till
now.", e);
         }
+        return swriter.toString();
     }
 
     public void setNext(LogEventSender next) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/24b4ed27/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
----------------------------------------------------------------------
diff --git a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
index 17f0c37..80dbdec 100644
--- a/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
+++ b/rt/features/logging/src/test/java/org/apache/cxf/ext/logging/SOAPLoggingTest.java
@@ -49,6 +49,8 @@ public class SOAPLoggingTest {
         TestService serviceImpl = new TestServiceImplementation();
         LoggingFeature loggingFeature = new LoggingFeature();
         loggingFeature.setPrettyLogging(true);
+        // Setting the limit should omit parts of the body but the result should still be
well formed xml
+        loggingFeature.setLimit(140);
         Endpoint ep = Endpoint.publish(SERVICE_URI, serviceImpl, loggingFeature);
         TestService client = createTestClient(loggingFeature);
         client.echo("test");


Mime
View raw message