[#1384] Cache HttpHeaderDateFormat in ThreadLocal

This commit is contained in:
Norman Maurer 2013-05-23 11:39:50 +02:00
parent 9c925b104a
commit 83dcf829d6
8 changed files with 24 additions and 13 deletions

View File

@ -117,7 +117,7 @@ public final class CookieDecoder {
} else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) { } else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) {
try { try {
long maxAgeMillis = long maxAgeMillis =
new HttpHeaderDateFormat().parse(value).getTime() - HttpHeaderDateFormat.get().parse(value).getTime() -
System.currentTimeMillis(); System.currentTimeMillis();
maxAge = maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0); maxAge = maxAgeMillis / 1000 + (maxAgeMillis % 1000 != 0? 1 : 0);

View File

@ -304,10 +304,10 @@ public class DefaultHttpHeaders extends HttpHeaders {
return value.toString(); return value.toString();
} }
if (value instanceof Date) { if (value instanceof Date) {
return new HttpHeaderDateFormat().format((Date) value); return HttpHeaderDateFormat.get().format((Date) value);
} }
if (value instanceof Calendar) { if (value instanceof Calendar) {
return new HttpHeaderDateFormat().format(((Calendar) value).getTime()); return HttpHeaderDateFormat.get().format(((Calendar) value).getTime());
} }
return value.toString(); return value.toString();
} }

View File

@ -37,11 +37,22 @@ final class HttpHeaderDateFormat extends SimpleDateFormat {
private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1(); private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2(); private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
private static ThreadLocal<HttpHeaderDateFormat> dateFormatThreadLocal = new ThreadLocal<HttpHeaderDateFormat>() {
@Override
protected HttpHeaderDateFormat initialValue() {
return new HttpHeaderDateFormat();
}
};
static HttpHeaderDateFormat get() {
return dateFormatThreadLocal.get();
}
/** /**
* Standard date format<p> * Standard date format<p>
* Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z * Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
*/ */
HttpHeaderDateFormat() { private HttpHeaderDateFormat() {
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
setTimeZone(TimeZone.getTimeZone("GMT")); setTimeZone(TimeZone.getTimeZone("GMT"));
} }

View File

@ -750,7 +750,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
if (value == null) { if (value == null) {
throw new ParseException("header not found: " + name, 0); throw new ParseException("header not found: " + name, 0);
} }
return new HttpHeaderDateFormat().parse(value); return HttpHeaderDateFormat.get().parse(value);
} }
/** /**
@ -768,7 +768,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
} }
try { try {
return new HttpHeaderDateFormat().parse(value); return HttpHeaderDateFormat.get().parse(value);
} catch (ParseException e) { } catch (ParseException e) {
return defaultValue; return defaultValue;
} }
@ -782,7 +782,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*/ */
public static void setDateHeader(HttpMessage message, String name, Date value) { public static void setDateHeader(HttpMessage message, String name, Date value) {
if (value != null) { if (value != null) {
message.headers().set(name, new HttpHeaderDateFormat().format(value)); message.headers().set(name, HttpHeaderDateFormat.get().format(value));
} else { } else {
message.headers().set(name, null); message.headers().set(name, null);
} }
@ -947,7 +947,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*/ */
public static void setDate(HttpMessage message, Date value) { public static void setDate(HttpMessage message, Date value) {
if (value != null) { if (value != null) {
message.headers().set(Names.DATE, new HttpHeaderDateFormat().format(value)); message.headers().set(Names.DATE, HttpHeaderDateFormat.get().format(value));
} else { } else {
message.headers().set(Names.DATE, null); message.headers().set(Names.DATE, null);
} }

View File

@ -54,7 +54,7 @@ public final class ServerCookieEncoder {
if (cookie.getMaxAge() != Long.MIN_VALUE) { if (cookie.getMaxAge() != Long.MIN_VALUE) {
if (cookie.getVersion() == 0) { if (cookie.getVersion() == 0) {
addUnquoted(buf, CookieHeaderNames.EXPIRES, addUnquoted(buf, CookieHeaderNames.EXPIRES,
new HttpHeaderDateFormat().format( HttpHeaderDateFormat.get().format(
new Date(System.currentTimeMillis() + new Date(System.currentTimeMillis() +
cookie.getMaxAge() * 1000L))); cookie.getMaxAge() * 1000L)));
} else { } else {

View File

@ -30,7 +30,7 @@ public class CookieDecoderTest {
public void testDecodingSingleCookieV0() { public void testDecodingSingleCookieV0() {
String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;"; String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
cookieString = cookieString.replace("XXX", cookieString = cookieString.replace("XXX",
new HttpHeaderDateFormat().format(new Date(System.currentTimeMillis() + 50000))); HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000)));
Set<Cookie> cookies = CookieDecoder.decode(cookieString); Set<Cookie> cookies = CookieDecoder.decode(cookieString);
assertEquals(1, cookies.size()); assertEquals(1, cookies.size());

View File

@ -27,7 +27,7 @@ public class CookieEncoderTest {
@Test @Test
public void testEncodingSingleCookieV0() { public void testEncodingSingleCookieV0() {
String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure"; String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";
DateFormat df = new HttpHeaderDateFormat(); DateFormat df = HttpHeaderDateFormat.get();
Cookie cookie = new DefaultCookie("myCookie", "myValue"); Cookie cookie = new DefaultCookie("myCookie", "myValue");
cookie.setComment("this is a Comment"); cookie.setComment("this is a Comment");
cookie.setCommentUrl("http://aurl.com"); cookie.setCommentUrl("http://aurl.com");

View File

@ -32,7 +32,7 @@ public class HttpHeaderDateFormatTest {
@Test @Test
public void testParse() throws ParseException { public void testParse() throws ParseException {
HttpHeaderDateFormat format = new HttpHeaderDateFormat(); HttpHeaderDateFormat format = HttpHeaderDateFormat.get();
final Date parsedDateWithSingleDigitDay = format.parse("Sun, 6 Nov 1994 08:49:37 GMT"); final Date parsedDateWithSingleDigitDay = format.parse("Sun, 6 Nov 1994 08:49:37 GMT");
Assert.assertNotNull(parsedDateWithSingleDigitDay); Assert.assertNotNull(parsedDateWithSingleDigitDay);
@ -61,7 +61,7 @@ public class HttpHeaderDateFormatTest {
@Test @Test
public void testFormat() { public void testFormat() {
HttpHeaderDateFormat format = new HttpHeaderDateFormat(); HttpHeaderDateFormat format = HttpHeaderDateFormat.get();
final String formatted = format.format(DATE); final String formatted = format.format(DATE);
Assert.assertNotNull(formatted); Assert.assertNotNull(formatted);