chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1182044 - in /chemistry/opencmis/trunk/chemistry-opencmis-server: chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/ chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/
Date Tue, 11 Oct 2011 19:25:36 GMT
Author: jens
Date: Tue Oct 11 19:25:35 2011
New Revision: 1182044

URL: http://svn.apache.org/viewvc?rev=1182044&view=rev
Log:
Improve Logging filter [CMIS-442]

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml?rev=1182044&r1=1182043&r2=1182044&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/webapp/WEB-INF/web.xml
Tue Oct 11 19:25:35 2011
@@ -54,6 +54,10 @@
    		   <param-value>true</param-value>
   		 </init-param>
    		<init-param>
+   		   <param-name>LogHeader</param-name>
+   		   <param-value>true</param-value>
+  		 </init-param>
+   		<init-param>
    		   <param-name>Indent</param-name>
    		   <param-value>4</param-value>
   		 </init-param>
@@ -63,7 +67,7 @@
    		<filter-name>LoggingFilter</filter-name>
    		<servlet-name>cmisatom</servlet-name> 
 	</filter-mapping>
-	-->
+    -->
 
 	<servlet>
 		<servlet-name>cmisws</servlet-name>

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java?rev=1182044&r1=1182043&r2=1182044&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
Tue Oct 11 19:25:35 2011
@@ -25,6 +25,12 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
 import java.util.Scanner;
 
 import javax.servlet.Filter;
@@ -35,6 +41,7 @@ import javax.servlet.ServletInputStream;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
@@ -53,44 +60,37 @@ public class LoggingFilter implements Fi
 
     private static final Log log = LogFactory.getLog(LoggingFilter.class);
     private static int REQUEST_NO = 0;
-    private static String OVERRIDE_NAME;
-    private static int OVERRIDE_INDENT = -1;
-    private static String OVERRIDE_LOG_DIR;
-    private static Boolean OVERRIDE_PRETTY_PRINT;
+    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("EEE MMM dd hh:mm:ss
a z yyyy", Locale.US);
     private String logDir;
     private boolean prettyPrint = true;
+    private boolean logHeaders = true;
     private int indent = -1;
 
     public void init(FilterConfig cfg) throws ServletException {
         
         String val; 
-        if (null == OVERRIDE_LOG_DIR) {
-            logDir = cfg.getInitParameter("LogDir");
-            if (null == logDir)
-                logDir = System.getProperty("java.io.tmpdir");
-            if (null == logDir)
-                logDir = "." + File.separator;
-        } else
-            logDir = OVERRIDE_LOG_DIR;
-       
+        logDir = cfg.getInitParameter("LogDir");
+        if (null == logDir)
+            logDir = System.getProperty("java.io.tmpdir");
+        if (null == logDir)
+            logDir = "." + File.separator;
+
         if (!logDir.endsWith(File.separator))
             logDir += File.separator;
-        
-        if (OVERRIDE_INDENT < 0) {
-            val = cfg.getInitParameter("Indent");
-            if (null != val)
-                indent = Integer.parseInt(val);
-            if (indent < 0)
-                indent = 4;
-        } else
-            indent = OVERRIDE_INDENT;
-        
-        if (null == OVERRIDE_PRETTY_PRINT) {
-            val = cfg.getInitParameter("PrettyPrint");
-            if (null != val)
-                prettyPrint = Boolean.parseBoolean(val);
-        } else 
-            prettyPrint = OVERRIDE_PRETTY_PRINT;
+
+        val = cfg.getInitParameter("Indent");
+        if (null != val)
+            indent = Integer.parseInt(val);
+        if (indent < 0)
+            indent = 4;
+
+        val = cfg.getInitParameter("PrettyPrint");
+        if (null != val)
+            prettyPrint = Boolean.parseBoolean(val);
+
+        val = cfg.getInitParameter("LogHeaders");
+        if (null != val)
+            logHeaders = Boolean.parseBoolean(val);
     }
 
     public void destroy() {
@@ -108,28 +108,45 @@ public class LoggingFilter implements Fi
             String requestFileName = getRequestFileName(reqNo);
             String cType = logReq.getContentType();
             String xmlRequest = logReq.getPayload();
+            StringBuffer sb = new StringBuffer();
+            
+            if (logHeaders)
+                logHeaders(logReq, sb);
+
             if (xmlRequest != null && xmlRequest.length() > 0) {
                 if (prettyPrint)
                     xmlRequest = prettyPrint(xmlRequest, indent);
             } else
                 xmlRequest = "";
 
+            xmlRequest = sb.toString() + xmlRequest;
             log.debug("Found request: " + requestFileName + ": " + xmlRequest);
             writeTextToFile(requestFileName, xmlRequest);
             
             chain.doFilter(logReq, logResponse);
 
+            sb = new StringBuffer();
             cType = logResponse.getContentType();
             String xmlResponse;
             String responseFileName = getResponseFileName(reqNo);
-            if(cType != null && cType.endsWith("xml")) {
+            if (logHeaders) {
+                logHeaders(logResponse, req.getProtocol(), sb);
+            }
+            if (cType != null && cType.contains("xml")) {
                 if (prettyPrint)
                     xmlResponse = prettyPrint(logResponse.getPayload(), indent);
                 else
                     xmlResponse = logResponse.getPayload();
                 
+                xmlResponse = sb.toString() + xmlResponse;
+                log.debug("Found response: " + responseFileName  + ": " + xmlResponse);
+                writeTextToFile(responseFileName, xmlResponse);
+            } else if (cType != null && cType.contains("html")) {
+                xmlResponse = sb.toString() + logResponse.getPayload();
                 log.debug("Found response: " + responseFileName  + ": " + xmlResponse);
                 writeTextToFile(responseFileName, xmlResponse);
+            } else {
+                writeTextToFile(responseFileName, "Unknown reponse content format: " + cType);
             }
         } else {            
             chain.doFilter(req, resp);
@@ -179,41 +196,53 @@ public class LoggingFilter implements Fi
             throw new RuntimeException(e); // simple exception handling, please review it
         }
     }
+    
+    @SuppressWarnings("rawtypes")
+    private void logHeaders(LoggingRequestWrapper req, StringBuffer sb) {
+        sb.append(req.getMethod());
+        sb.append(" ");
+        sb.append(req.getRequestURI());
+        sb.append(" ");
+        sb.append(req.getProtocol());
+        sb.append("\n");
+        Enumeration headerNames = req.getHeaderNames();
+        while (headerNames.hasMoreElements()) {
+            String headerName = headerNames.nextElement().toString();
+            headerName = headerName.substring(0, 1).toUpperCase() + headerName.substring(1);
+            sb.append(headerName + ": ");
+            sb.append(req.getHeader(headerName));
+            sb.append("\n");
+        }
+        sb.append("\n");
+    }
+
+    private void logHeaders(LoggingResponseWrapper resp, String protocol, StringBuffer sb)
{
+        sb.append(protocol);
+        sb.append(" ");
+        sb.append(String.valueOf(resp.getStatus()));
+        sb.append("\n");
+        Map<String, String> headers = resp.getHeaders();
+        for ( Map.Entry<String, String> header: headers.entrySet()) {
+            sb.append(header.getKey());
+            sb.append(": ");
+            sb.append(header.getValue());
+            sb.append("\n");            
+        }
+        sb.append("\n");
+    }
 
     private String getRequestFileName(int no) {
-        if (OVERRIDE_NAME == null)
-            return logDir + String.format("%05d-request.log", no);
-        else
-            return OVERRIDE_NAME + "-request";
+        return logDir + String.format("%05d-request.log", no);
     }
     
     private String getResponseFileName(int no) {
-        if (OVERRIDE_NAME == null)
-            return logDir + String.format("%05d-response.log", no);
-        else
-            return OVERRIDE_NAME + "-request";
+        return logDir + String.format("%05d-response.log", no);
     }
     
     private static synchronized int getNextRequestNumber() {
         return REQUEST_NO++;
     }
     
-    public static void setFileName(String name) {
-        OVERRIDE_NAME = name;
-    }
-    
-    public static void setLogDir(String dir) {
-        OVERRIDE_LOG_DIR = dir;
-    }
-
-    public static void setIndent(int indent) {
-        OVERRIDE_INDENT = indent;
-    }
-
-    public static void setPrettyPrint(boolean pp) {
-        OVERRIDE_PRETTY_PRINT = pp;
-    }
-
     private class LoggingRequestWrapper extends HttpServletRequestWrapper {
         
         private LoggingInputStream is;
@@ -288,7 +317,10 @@ public class LoggingFilter implements Fi
      private class LoggingResponseWrapper extends HttpServletResponseWrapper {
          
          private LoggingOutputStream os;
-    
+         private int statusCode;
+         private Map<String, String> headers = new HashMap<String, String>();
+         String encoding;
+         
          public LoggingResponseWrapper(HttpServletResponse response) throws IOException {
             super(response);
             this.os = new LoggingOutputStream(response.getOutputStream());
@@ -302,6 +334,136 @@ public class LoggingFilter implements Fi
          public String getPayload() {
             return os.getPayload();
          }
+         
+         @Override
+         public void addCookie(Cookie cookie) {
+             super.addCookie(cookie);
+             String value;
+             if (headers.containsKey("Cookie")) {
+                 value = headers.get("Cookie") + "; " + cookie.toString();
+             } else
+                 value = cookie.toString();
+             headers.put("Cookie", value);
+         }
+         
+         @Override
+         public void setContentType(String type) {
+             super.setContentType(type);
+             if (headers.containsKey("Content-Type")) {
+                 String cType = headers.get("Content-Type");
+                 int pos = cType.indexOf(";charset=");
+                 if (pos < 0 && encoding != null)
+                     type = cType + ";charset=" + encoding;
+                 else if (pos >= 0)
+                     encoding = null;                 
+             }
+             headers.put("Content-Type", type);             
+         }
+         
+         @Override         
+         public void setCharacterEncoding(java.lang.String charset) {
+             super.setCharacterEncoding(charset);
+             encoding = charset;
+             if (headers.containsKey("Content-Type")) {
+                 String cType = headers.get("Content-Type");
+                 int pos = cType.indexOf(";charset=");
+                 if (pos >=0)
+                     cType = cType.substring(0, pos) + ";charset=" + encoding;
+                 else
+                     cType = cType + ";charset=" + encoding;
+                 headers.put("Content-Type", cType);
+             }
+         }
+         
+         @Override
+         public void setContentLength(int len) {
+             super.setContentLength(len);
+             headers.put("Content-Length", String.valueOf(len));                        
 
+         }
+         
+         private String getDateString(long date) {
+             return FORMAT.format(new Date(date));             
+         }
+         
+         @Override
+         public void setDateHeader(String name, long date) {
+             super.setDateHeader(name, date);
+             headers.put(name, String.valueOf(getDateString(date)));
+         }
+         
+         @Override
+         public void addDateHeader(String name, long date) {
+             super.addDateHeader(name, date);
+             if (headers.containsKey(name)) {
+                 headers.put(name, headers.get(name) + "; " + getDateString(date));
+             } else {
+                 headers.put(name, String.valueOf(getDateString(date)));
+             }
+         }
+         
+         @Override
+         public void setHeader(String name, String value) {
+             super.setHeader(name, value);
+             headers.put(name, String.valueOf(value));
+         }
+
+         @Override
+         public void addHeader(String name, String value) {
+             super.addHeader(name, value);
+             if (headers.containsKey(name)) {
+                 headers.put(name, headers.get(name) + "; " + value);
+             } else {
+                 headers.put(name, String.valueOf(value));
+             }
+         }
+         
+         @Override
+         public void setIntHeader(String name, int value) {
+             super.setIntHeader(name, value);
+             headers.put(name, String.valueOf(value));
+         }
+         
+         @Override
+         public void addIntHeader(String name, int value) {
+             super.addIntHeader(name, value);
+             if (headers.containsKey(name)) {
+                 headers.put(name, headers.get(name) + "; " + String.valueOf(value));
+             } else {
+                 headers.put(name, String.valueOf(value));
+             }
+         }
+         
+         @Override
+         public void sendError(int sc) throws IOException {
+             statusCode = sc;
+             super.sendError(sc);
+         }
+
+         @Override
+         public void sendError(int sc, String msg) throws IOException {
+             statusCode = sc;
+             super.sendError(sc, msg);
+         }
+
+         @Override
+         public void sendRedirect(String location) throws IOException {
+             statusCode = 302;
+             super.sendRedirect(location);
+         }
+
+         @Override
+         public void setStatus(int sc) {
+             statusCode = sc;
+             super.setStatus(sc);
+         }
+
+         public int getStatus() {
+             return statusCode;
+         }
+
+         public Map<String, String> getHeaders() {
+             return headers;
+         }
       }
     
       private class LoggingOutputStream extends ServletOutputStream {



Mime
View raw message