Sebastian Sdorra avatar Sebastian Sdorra committed 84cff32

log only text body

Comments (0)

Files changed (1)

scm-core/src/main/java/sonia/scm/web/filter/LoggingFilter.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.base.Strings;
 import com.google.inject.Singleton;
 
 import org.slf4j.Logger;
   {
     if (logger.isDebugEnabled())
     {
-      boolean logBody = logger.isTraceEnabled();
+      boolean logBody = logger.isTraceEnabled() && isTextRequest(request);
       BufferedHttpServletRequest bufferedRequest =
         new BufferedHttpServletRequest(request, logBody);
       BufferedHttpServletResponse bufferedResponse =
 
     if (logger.isTraceEnabled())
     {
-      logger.trace("Content: ".concat(new String(request.getContentBuffer())));
+      byte[] contentBuffer = request.getContentBuffer();
+
+      if ((contentBuffer != null) && (contentBuffer.length > 0))
+      {
+        logger.trace("Content: ".concat(new String(contentBuffer)));
+      }
     }
   }
 
       logger.debug("Header: {} = {}", header.getKey(), header.getValue());
     }
 
-    if (logger.isTraceEnabled())
+    if (logger.isTraceEnabled() && isTextRequest(orgResponse))
     {
       byte[] content = response.getContentBuffer();
-      ServletOutputStream out = null;
 
-      try
+      if ((content != null) && (content.length > 0))
       {
-        out = orgResponse.getOutputStream();
-        out.write(content);
+        ServletOutputStream out = null;
+
+        try
+        {
+          out = orgResponse.getOutputStream();
+          out.write(content);
+        }
+        finally
+        {
+          out.close();
+        }
+
+        logger.trace("Content: ".concat(new String(content)));
       }
-      finally
-      {
-        out.close();
-      }
-
-      logger.trace("Content: ".concat(new String(content)));
     }
   }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param request
+   *
+   * @return
+   */
+  private boolean isTextRequest(HttpServletRequest request)
+  {
+    return isTextRequest(request.getContentType());
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param response
+   *
+   * @return
+   */
+  private boolean isTextRequest(HttpServletResponse response)
+  {
+    return isTextRequest(response.getContentType());
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param contentType
+   *
+   * @return
+   */
+  private boolean isTextRequest(String contentType)
+  {
+    return !Strings.isNullOrEmpty(contentType)
+           && contentType.toLowerCase().startsWith("text");
+  }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.