hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r366832 - in /jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol: HttpDateGenerator.java ResponseDate.java
Date Sat, 07 Jan 2006 16:57:25 GMT
Author: olegk
Date: Sat Jan  7 08:57:14 2006
New Revision: 366832

URL: http://svn.apache.org/viewcvs?rev=366832&view=rev
Log:
* Added Http date generator class
* Optimized the ResponseDate interceptor for better performance

Added:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/HttpDateGenerator.java
Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/ResponseDate.java

Added: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/HttpDateGenerator.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/HttpDateGenerator.java?rev=366832&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/HttpDateGenerator.java
(added)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/HttpDateGenerator.java
Sat Jan  7 08:57:14 2006
@@ -0,0 +1,71 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.protocol;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+import org.apache.http.util.DateUtils;
+
+/**
+ * <p>
+ * </p>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Revision: $
+ * 
+ * @since 4.0
+ */
+public class HttpDateGenerator {
+
+    private final DateFormat dateformat;
+    
+    private long dateAsLong = 0L;
+    private String dateAsText = null;
+
+    public HttpDateGenerator() {
+        super();
+        this.dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123, Locale.US);
+        this.dateformat.setTimeZone(DateUtils.GMT);
+    }
+    
+    public synchronized String getCurrentDate() {
+        long now = System.currentTimeMillis();
+        if (now - this.dateAsLong > 1000) {
+            // Generate new date string
+            this.dateAsText = this.dateformat.format(new Date(now));
+            this.dateAsLong = now;
+        }
+        return this.dateAsText;
+    }
+    
+}

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/ResponseDate.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/ResponseDate.java?rev=366832&r1=366831&r2=366832&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/ResponseDate.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/protocol/ResponseDate.java
Sat Jan  7 08:57:14 2006
@@ -30,17 +30,12 @@
 package org.apache.http.protocol;
 
 import java.io.IOException;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
 
 import org.apache.http.Header;
 import org.apache.http.HttpException;
 import org.apache.http.HttpMutableResponse;
 import org.apache.http.HttpResponseInterceptor;
 import org.apache.http.HttpStatus;
-import org.apache.http.util.DateUtils;
 
 /**
  * <p>
@@ -55,20 +50,12 @@
 
     private static final String DATE_DIRECTIVE = "Date";
     
-    private final DateFormat rfc1123; 
+    private static final HttpDateGenerator DATE_GENERATOR = new HttpDateGenerator(); 
     
     public ResponseDate() {
         super();
-        this.rfc1123 = new SimpleDateFormat(DateUtils.PATTERN_RFC1123, Locale.US);
-        this.rfc1123.setTimeZone(DateUtils.GMT);
     }
 
-    private String getCurrentDate() {
-        synchronized (this.rfc1123) {
-            return this.rfc1123.format(new Date());
-        }
-    }
-    
     public void process(final HttpMutableResponse response, final HttpContext context) 
         throws HttpException, IOException {
         if (response == null) {
@@ -76,7 +63,8 @@
         }
         int status = response.getStatusLine().getStatusCode();
         if (status >= HttpStatus.SC_OK) {
-            response.setHeader(new Header(DATE_DIRECTIVE, getCurrentDate(), true)); 
+            String httpdate = DATE_GENERATOR.getCurrentDate();
+            response.setHeader(new Header(DATE_DIRECTIVE, httpdate, true)); 
         }
     }
     



Mime
View raw message