[LOGBack-dev] svn commit: r596 - in logback/trunk/logback-access/src: main/java/ch/qos/logback/access/jetty main/java/ch/qos/logback/access/spi test/java/ch/qos/logback/access/pattern test/java/ch/qos/logback/access/pattern/helpers

noreply.seb at qos.ch noreply.seb at qos.ch
Wed Sep 20 17:03:16 CEST 2006


Author: seb
Date: Wed Sep 20 17:03:16 2006
New Revision: 596

Added:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/JettyServerAdapter.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/ServerAdapter.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyValuesAdapter.java
Modified:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java

Log:
- added a ServerAdapter interface to allow the server-independent AccessEvent to reach server-specific methods
- impemented this interface in JettyServerAdapter and DummyValuesAdapter, the latter used for testing only.
- updated ConverterTest.

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/JettyServerAdapter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/JettyServerAdapter.java	Wed Sep 20 17:03:16 2006
@@ -0,0 +1,35 @@
+package ch.qos.logback.access.jetty;
+
+import org.mortbay.jetty.Request;
+import org.mortbay.jetty.Response;
+
+import ch.qos.logback.access.spi.ServerAdapter;
+
+/**
+ * A jetty specific implementation of the {@link ServerAdapter} interface.
+ *
+ * @author Sébastien Pennec
+ */
+public class JettyServerAdapter implements ServerAdapter {
+  
+  Request request;
+  Response response;
+
+  public JettyServerAdapter(Request jettyRequest, Response jettyResponse) {
+    this.request = jettyRequest;
+    this.response = jettyResponse;
+  }
+  
+  public long getContentLength() {
+    return response.getContentCount();
+  }
+
+  public int getStatusCode() {
+    return response.getStatus();
+  }
+  
+  public String getResponseHeader(String key) {
+    return response.getHeader(key);
+  }
+
+}

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java	Wed Sep 20 17:03:16 2006
@@ -27,7 +27,8 @@
   String filename;
 
   public void log(Request jettyRequest, Response jettyResponse) {
-    AccessEvent accessEvent = new AccessEvent(jettyRequest, jettyResponse);
+    JettyServerAdapter adapter = new JettyServerAdapter(jettyRequest, jettyResponse);
+    AccessEvent accessEvent = new AccessEvent(jettyRequest, jettyResponse, adapter);
     // TODO better exception handling
     aai.appendLoopOnAppenders(accessEvent);
   }

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java	Wed Sep 20 17:03:16 2006
@@ -9,8 +9,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.mortbay.jetty.Response;
-
 import ch.qos.logback.access.pattern.AccessConverter;
 
 public class AccessEvent implements Serializable {
@@ -31,26 +29,27 @@
   String protocol;
   String method;
   String serverName;
-  
-  Map requestHeaderMap;
+
+  Map<String, Object> requestHeaderMap;
 
   long contentLength = SENTINEL;
   int statusCode = SENTINEL;
   int localPort = SENTINEL;
 
+  ServerAdapter serverAdapter;
+
   /**
    * The number of milliseconds elapsed from 1/1/1970 until logging event was
    * created.
    */
   private long timeStamp = 0;
 
- 
-
   public AccessEvent(HttpServletRequest httpRequest,
-      HttpServletResponse httpResponse) {
+      HttpServletResponse httpResponse, ServerAdapter adapter) {
     this.httpRequest = httpRequest;
     this.httpResponse = httpResponse;
     this.timeStamp = System.currentTimeMillis();
+    this.serverAdapter = adapter;
   }
 
   public long getTimeStamp() {
@@ -159,7 +158,6 @@
     return serverName;
   }
 
-
   public String getRemoteAddr() {
     if (remoteAddr == null) {
       if (httpRequest != null) {
@@ -190,25 +188,18 @@
   }
 
   public void buildRequestHeaderMap() {
-    requestHeaderMap = new HashMap();
+    requestHeaderMap = new HashMap<String, Object>();
     Enumeration e = httpRequest.getHeaderNames();
-    while(e.hasMoreElements()) {
+    while (e.hasMoreElements()) {
       String key = (String) e.nextElement();
       requestHeaderMap.put(key, httpRequest.getHeader(key));
     }
   }
-  
+
   public String getResponseHeader(String key) {
-    //TODO buildMap
-    if (httpResponse instanceof org.mortbay.jetty.Response) {
-      return ((org.mortbay.jetty.Response)httpResponse).getHeader(key);
-    }
-    if (httpResponse instanceof ch.qos.logback.access.pattern.helpers.DummyResponse) {
-      return ((ch.qos.logback.access.pattern.helpers.DummyResponse)httpResponse).getHeader(key);
-    }
-    
-    return null;
+    return serverAdapter.getResponseHeader(key);
   }
+
   /**
    * Attributes are not serialized
    * 
@@ -248,15 +239,20 @@
   public long getContentLength() {
     if (contentLength == SENTINEL) {
       if (httpResponse != null) {
-        if (httpResponse instanceof org.mortbay.jetty.Response) {
-          // TODO
-        } else if (httpResponse instanceof com.caucho.server.connection.AbstractHttpResponse) {
-          contentLength = ((com.caucho.server.connection.AbstractHttpResponse) httpResponse)
-              .getContentLength();
-        } else if (httpResponse instanceof org.apache.catalina.connector.Response) {
-          contentLength = ((org.apache.catalina.connector.Response) httpResponse)
-              .getContentLength();
-        }
+        return serverAdapter.getContentLength();
+        // if (httpResponse instanceof org.mortbay.jetty.Response) {
+        // // TODO
+        // } else if (httpResponse instanceof
+        // com.caucho.server.connection.AbstractHttpResponse) {
+        // contentLength = ((com.caucho.server.connection.AbstractHttpResponse)
+        // httpResponse)
+        // .getContentLength();
+        // } else if (httpResponse instanceof
+        // org.apache.catalina.connector.Response) {
+        // contentLength = ((org.apache.catalina.connector.Response)
+        // httpResponse)
+        // .getContentLength();
+        // }
       }
     }
     return contentLength;
@@ -265,15 +261,19 @@
   public int getStatusCode() {
     if (statusCode == SENTINEL) {
       if (httpResponse != null) {
-        if (httpResponse instanceof org.mortbay.jetty.Response) {
-          statusCode = ((org.mortbay.jetty.Response) httpResponse).getStatus();
-        } else if (httpResponse instanceof com.caucho.server.connection.AbstractHttpResponse) {
-          statusCode = ((com.caucho.server.connection.AbstractHttpResponse) httpResponse)
-              .getStatusCode();
-        } else if (httpResponse instanceof org.apache.catalina.connector.Response) {
-          statusCode = ((org.apache.catalina.connector.Response) httpResponse)
-              .getStatus();
-        }
+        return serverAdapter.getStatusCode();
+        // if (httpResponse instanceof org.mortbay.jetty.Response) {
+        // statusCode = ((org.mortbay.jetty.Response) httpResponse).getStatus();
+        // } else if (httpResponse instanceof
+        // com.caucho.server.connection.AbstractHttpResponse) {
+        // statusCode = ((com.caucho.server.connection.AbstractHttpResponse)
+        // httpResponse)
+        // .getStatusCode();
+        // } else if (httpResponse instanceof
+        // org.apache.catalina.connector.Response) {
+        // statusCode = ((org.apache.catalina.connector.Response) httpResponse)
+        // .getStatus();
+        // }
       }
     }
     return statusCode;
@@ -288,4 +288,8 @@
     }
     return localPort;
   }
+
+  public ServerAdapter getServerAdapter() {
+    return serverAdapter;
+  }
 }
\ No newline at end of file

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/ServerAdapter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/ServerAdapter.java	Wed Sep 20 17:03:16 2006
@@ -0,0 +1,15 @@
+package ch.qos.logback.access.spi;
+
+/**
+ * An interface to access server-specific methods from
+ * the server-independent AccessEvent.
+ *
+ * @author Ceki G&uuml;lc&uuml;
+ * @author S&eacute;bastien Pennec
+ */
+public interface ServerAdapter {
+
+  long getContentLength();
+  int getStatusCode();
+  String getResponseHeader(String key);
+}

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/ConverterTest.java	Wed Sep 20 17:03:16 2006
@@ -10,6 +10,7 @@
 import junit.framework.TestCase;
 import ch.qos.logback.access.pattern.helpers.DummyRequest;
 import ch.qos.logback.access.pattern.helpers.DummyResponse;
+import ch.qos.logback.access.pattern.helpers.DummyValuesAdapter;
 import ch.qos.logback.access.spi.AccessEvent;
 
 public class ConverterTest extends TestCase {
@@ -33,7 +34,10 @@
   }
 
   public void testContentLengthConverter() {
-    // TODO when AccessEvent has been modified
+    ContentLengthConverter converter = new ContentLengthConverter();
+    converter.start();
+    String result = converter.convert(event);
+    assertEquals(Long.toString(event.getServerAdapter().getContentLength()), result);
   }
 
   public void testDateConverter() {
@@ -150,11 +154,15 @@
   }
   
   public void testStatusCodeConverter() {
-    //TODO 
+    StatusCodeConverter converter = new StatusCodeConverter();
+    converter.start();
+    String result = converter.convert(event);
+    assertEquals(Integer.toString(event.getServerAdapter().getStatusCode()), result);
   }
 
   private AccessEvent createEvent() {
-    AccessEvent ae = new AccessEvent(request, response);
+    DummyValuesAdapter dummyAdapter = new DummyValuesAdapter(request, response);
+    AccessEvent ae = new AccessEvent(request, response, dummyAdapter);
     return ae;
   }
 

Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyValuesAdapter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyValuesAdapter.java	Wed Sep 20 17:03:16 2006
@@ -0,0 +1,35 @@
+package ch.qos.logback.access.pattern.helpers;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import ch.qos.logback.access.spi.ServerAdapter;
+
+/**
+ * A test-only implementation of the {@link ServerAdapter} interface.
+ *
+ * @author S&eacute;bastien Pennec
+ */
+public class DummyValuesAdapter implements ServerAdapter {
+
+  DummyRequest request;
+  DummyResponse response;
+  
+  public DummyValuesAdapter(HttpServletRequest request, HttpServletResponse response) {
+    this.request = (DummyRequest)request;
+    this.response = (DummyResponse)response;
+  }
+  
+  public long getContentLength() {
+    return 123L;
+  }
+
+  public String getResponseHeader(String key) {
+    return response.getHeader(key);
+  }
+
+  public int getStatusCode() {
+    return 1;
+  }
+
+}



More information about the logback-dev mailing list