Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
Merge httpDate into serverDate as suggested. Removed some unnecessary code and fixed a possible synchronization problem.
fuchsif41172f85060
diff --git a/source/de/anomic/http/httpDate.java b/source/de/anomic/http/httpDate.javadeleted file mode 100644index a95615d31..000000000--- a/source/de/anomic/http/httpDate.java+++ /dev/null@@ -1,141 +0,0 @@-// httpDate.java-// -------------------------------// part of YaCy-// (C) by Bjoern 'Fuchs' Krombholz; [email protected]-// first published on http://www.anomic.de-// Frankfurt, Germany, 2005, 2006-//-// This Class was written by Martin Thelian-//-// This program is free software; you can redistribute it and/or modify-// it under the terms of the GNU General Public License as published by-// the Free Software Foundation; either version 2 of the License, or-// (at your option) any later version.-//-// This program is distributed in the hope that it will be useful,-// but WITHOUT ANY WARRANTY; without even the implied warranty of-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-// GNU General Public License for more details.-//-// You should have received a copy of the GNU General Public License-// along with this program; if not, write to the Free Software-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA-//-// Using this software in any meaning (reading, learning, copying, compiling,-// running) means that you agree that the Author(s) is (are) not responsible-// for cost, loss of data or any harm that may be caused directly or indirectly-// by usage of this softare or this documentation. The usage of this software-// is on your own risk. The installation and usage (starting/running) of this-// software may allow other people or application to access your computer and-// any attached devices and is highly dependent on the configuration of the-// software which must be done by the user of the software; the author(s) is-// (are) also not responsible for proper configuration and usage of the-// software, even if provoked by documentation provided together with-// the software.-//-// Any changes to this file according to the GPL as documented in the file-// gpl.txt aside this file in the shipment you received can be done to the-// lines that follows this copyright notice here, but changes must not be-// done inside the copyright notive above. A re-distribution must contain-// the intact and unchanged copyright notice.-// Contributions and changes to the program code must be marked as such.--package de.anomic.http;--import java.text.ParseException;-import java.text.SimpleDateFormat;-import java.util.Calendar;-import java.util.Date;-import java.util.Locale;-import java.util.TimeZone;--import de.anomic.server.logging.serverLog;--/**- * Helper class for parsing HTTP Dates according to RFC 2616 *- */-public final class httpDate {-- public static String PAT_DATE_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";- public static String PAT_DATE_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";- public static String PAT_DATE_ANSI = "EEE MMM d HH:mm:ss yyyy";-- /**- * RFC 2616 requires that HTTP clients are able to parse all 3 different- * formats. All times MUST be in GMT/UTC, but ...- */- public static SimpleDateFormat[] DATE_PARSERS = new SimpleDateFormat[] {- // RFC 1123/822 (Standard) "Mon, 12 Nov 2007 10:11:12 GMT"- new SimpleDateFormat(PAT_DATE_RFC1123, Locale.US),- // RFC 1036/850 (old) "Monday, 12-Nov-07 10:11:12 GMT"- new SimpleDateFormat(PAT_DATE_RFC1036, Locale.US),- // ANSI C asctime() "Mon Nov 12 10:11:12 2007"- new SimpleDateFormat(PAT_DATE_ANSI, Locale.US),- };-- static {- // 2-digit dates are automatically parsed by SimpleDateFormat,- // we need to detect the real year by adding 1900 or 2000 to- // the year value starting with 1990 (before there was no WWW)- Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));- // 01 Jan 1990 00:00:00- c.set(1990, 1, 1, 0, 0, 0);-- for (int i = 0; i < DATE_PARSERS.length; i++) {- SimpleDateFormat f = DATE_PARSERS[i];- // is this necessary?- f.setTimeZone(TimeZone.getTimeZone("GMT"));- f.set2DigitYearStart(c.getTime());- }- }-- private httpDate() {};-- /**- * Parse a HTTP string representation of a date into a Date instance.- * @param s The date String to parse.- * @return The Date instance if successful, <code>null</code> otherwise.- */- public static Date parseHTTPDate(String s) {- try {- return httpDate.parseHTTPDate(s, true);- } catch (ParseException e) {- serverLog.logSevere("HTTPC-header", "DATE ERROR (Parse): " + s);- return null;- } catch (java.lang.NumberFormatException e) {- serverLog.logSevere("HTTPC-header", "DATE ERROR (NumberFormat): " + s);- return null;- }- }-- /**- * Parse a HTTP string representation of a date into a Date instance.- * @param s The date String to parse.- * @param ignoreTimezone parse the timezone? Currently ignored, always parsed.- * @return The Date instance if successful, <code>null</code> otherwise.- * @throws ParseException Thrown, when a parsing problem occured (date String had no leagal format)- * @throws NumberFormatException- */- public static Date parseHTTPDate(String s, boolean /*unused*/ ignoreTimezone) throws ParseException {-- if ((s == null) || (s.length() < 9)) return null;- s = s.trim();-- //Why was this here?- //if (s.indexOf("Mrz") > 0) s = s.replaceAll("Mrz", "March");-- ParseException pe = null;- for(int i = 0; i < DATE_PARSERS.length; i++) {- try {- // if parse() throws an Exception we try the next pattern- return DATE_PARSERS[i].parse(s);- } catch (ParseException e) {- // we re-throw the last Exception when parsing was not possible- pe = e;- }- }-- // no match- throw pe;- }-}diff --git a/source/de/anomic/http/httpHeader.java b/source/de/anomic/http/httpHeader.javaindex 57034b583..1c84412fc 100644--- a/source/de/anomic/http/httpHeader.java+++ b/source/de/anomic/http/httpHeader.java@@ -71,6 +71,7 @@ import java.util.TreeMap;import java.util.Vector;import de.anomic.server.serverCore;+import de.anomic.server.serverDate;import de.anomic.yacy.yacyURL;@@ -386,7 +387,7 @@ public final class httpHeader extends TreeMap implements Map {private Date headerDate(String kind) {if (containsKey(kind)) {- Date parsedDate = httpDate.parseHTTPDate((String) get(kind));+ Date parsedDate = serverDate.parseHTTPDate((String) get(kind));if (parsedDate == null) parsedDate = new Date();return new Date(parsedDate.getTime());}@@ -439,10 +440,10 @@ public final class httpHeader extends TreeMap implements Map {public Object ifRange() {if (containsKey(httpHeader.IF_RANGE)) {- try {- Date rangeDate = httpDate.parseHTTPDate((String) get(httpHeader.IF_RANGE),false);- if (rangeDate != null) return new Date(rangeDate.getTime());- } catch (Exception e) {}+ Date rangeDate = serverDate.parseHTTPDate((String) get(httpHeader.IF_RANGE));+ if (rangeDate != null)+ return new Date(rangeDate.getTime());+return get(httpHeader.IF_RANGE);}return null;diff --git a/source/de/anomic/server/serverDate.java b/source/de/anomic/server/serverDate.javaindex 46b3d068f..1966d1274 100644--- a/source/de/anomic/server/serverDate.java+++ b/source/de/anomic/server/serverDate.java@@ -1,8 +1,9 @@// serverDate.java// -------------------------------------------// (C) by Michael Peter Christen; [email protected]+// (C) by by Bjoern 'Fuchs' Krombholz; [email protected]// first published on http://www.anomic.de-// Frankfurt, Germany, 2005+// Frankfurt, Germany, 2005, 2007// last major change: 14.03.2005//// This program is free software; you can redistribute it and/or modify@@ -58,20 +59,77 @@ public final class serverDate {// standard date formatterspublic static final String shortDayFormatterPattern = "yyyyMMdd";public static final String shortSecondFormatterPattern = "yyyyMMddHHmmss";+ public static final String PAT_DATE_ANSI = "EEE MMM d HH:mm:ss yyyy";+ public static final String PAT_DATE_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";+ public static final String PAT_DATE_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";+public static final SimpleDateFormat shortDayFormatter = new SimpleDateFormat(shortDayFormatterPattern);public static final SimpleDateFormat shortSecondFormatter = new SimpleDateFormat(shortSecondFormatterPattern);public static final SimpleDateFormat longFullFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);- private static TimeZone GMTTimeZone = TimeZone.getTimeZone("GMT");+ private static TimeZone TZ_GMT = TimeZone.getTimeZone("GMT");++ /**+ * RFC 2616 requires that HTTP clients are able to parse all 3 different+ * formats. All times MUST be in GMT/UTC, but ...+ */+ public static SimpleDateFormat[] DATE_PARSERS = new SimpleDateFormat[] {+ // RFC 1123/822 (Standard) "Mon, 12 Nov 2007 10:11:12 GMT"+ new SimpleDateFormat(PAT_DATE_RFC1123, Locale.US),+ // RFC 1036/850 (old) "Monday, 12-Nov-07 10:11:12 GMT"+ new SimpleDateFormat(PAT_DATE_RFC1036, Locale.US),+ // ANSI C asctime() "Mon Nov 12 10:11:12 2007"+ new SimpleDateFormat(PAT_DATE_ANSI, Locale.US),+ };+ static {+ // 2-digit dates are automatically parsed by SimpleDateFormat,+ // we need to detect the real year by adding 1900 or 2000 to+ // the year value starting with 1970+ Calendar c = Calendar.getInstance(TZ_GMT, Locale.US);+ // 01 Jan 1970 00:00:00+ c.set(1970, 1, 1, 0, 0, 0);++ for (int i = 0; i < serverDate.DATE_PARSERS.length; i++) {+ SimpleDateFormat f = serverDate.DATE_PARSERS[i];+ f.setTimeZone(TZ_GMT);+ f.set2DigitYearStart(c.getTime());+ }+ }+public static long nowTime() {return nowDate().getTime();}public static Date nowDate() {- return new GregorianCalendar(GMTTimeZone).getTime();+ return new GregorianCalendar(TZ_GMT).getTime();}+ /**+ * Parse a HTTP string representation of a date into a Date instance.+ * @param s The date String to parse.+ * @return The Date instance if successful, <code>null</code> otherwise.+ */+ public static Date parseHTTPDate(String s) {+ s = s.trim();+ if ((s == null) || (s.length() < 9)) return null;++ for(int i = 0; i < DATE_PARSERS.length; i++) {+ try {+ synchronized (DATE_PARSERS[i]) {+ return DATE_PARSERS[i].parse(s);+ }+ } catch (ParseException e) {+ // on ParseException try again with next parser+ }+ }++ // the method didn't return a Date, so we got an illegal+ serverLog.logSevere("HTTPC-header", "DATE ERROR (Parse): " + s);+ return null;+ }++/** Synchronization of formatters is needed because SimpleDateFormat is not thread-safe.* See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6231579@@ -113,10 +171,6 @@ public final class serverDate {if (remoteTimeString == null || remoteTimeString.length() == 0) { return new Date(); }if (remoteUTCOffset == null || remoteUTCOffset.length() == 0) { return new Date(); }try {- /*- * This synchronized is needed because SimpleDateFormat is not thread-safe.- * See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6231579- */synchronized(serverDate.shortSecondFormatter) {return new Date(serverDate.shortSecondFormatter.parse(remoteTimeString).getTime() - serverDate.UTCDiff() + serverDate.UTCDiff(remoteUTCOffset));}@@ -129,7 +183,6 @@ public final class serverDate {}}-// staticspublic final static long secondMillis = 1000;public final static long minuteMillis = 60 * secondMillis;