Skip to content

Commit

Permalink
Merge httpDate into serverDate as suggested. Removed some unnecessary…
Browse files Browse the repository at this point in the history
… code and fixed a possible synchronization problem.

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4283 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
fuchsi committed Dec 18, 2007
1 parent 5aa8d72 commit f41172f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 154 deletions.
141 changes: 0 additions & 141 deletions source/de/anomic/http/httpDate.java

This file was deleted.

11 changes: 6 additions & 5 deletions source/de/anomic/http/httpHeader.java
Expand Up @@ -71,6 +71,7 @@ keys are not compared by the equal() method, but are always
import java.util.Vector;

import de.anomic.server.serverCore;
import de.anomic.server.serverDate;
import de.anomic.yacy.yacyURL;


Expand Down Expand Up @@ -386,7 +387,7 @@ public String toString() {

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());
}
Expand Down Expand Up @@ -439,10 +440,10 @@ public Date ifModifiedSince() {

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;
Expand Down
69 changes: 61 additions & 8 deletions source/de/anomic/server/serverDate.java
@@ -1,8 +1,9 @@
// serverDate.java
// -------------------------------------------
// (C) by Michael Peter Christen; mc@anomic.de
// (C) by by Bjoern 'Fuchs' Krombholz; fox.box@gmail.com
// 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
Expand Down Expand Up @@ -58,20 +59,77 @@ public final class serverDate {
// standard date formatters
public 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
Expand Down Expand Up @@ -113,10 +171,6 @@ public static Date parseShortSecondTime(String remoteTimeString, String remoteUT
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));
}
Expand All @@ -129,7 +183,6 @@ public static Date parseShortSecondTime(String remoteTimeString, String remoteUT
}
}


// statics
public final static long secondMillis = 1000;
public final static long minuteMillis = 60 * secondMillis;
Expand Down

0 comments on commit f41172f

Please sign in to comment.