[#1384] Cache HttpHeaderDateFormat in ThreadLocal
This commit is contained in:
parent
97332f6436
commit
6e1c5926ee
@ -132,7 +132,7 @@ public class CookieDecoder {
|
||||
} else if (CookieHeaderNames.EXPIRES.equalsIgnoreCase(name)) {
|
||||
try {
|
||||
long maxAgeMillis =
|
||||
new HttpHeaderDateFormat().parse(value).getTime() -
|
||||
HttpHeaderDateFormat.get().parse(value).getTime() -
|
||||
System.currentTimeMillis();
|
||||
|
||||
maxAge = (int) (maxAgeMillis / 1000) +
|
||||
|
@ -110,7 +110,7 @@ public class CookieEncoder {
|
||||
if (cookie.getMaxAge() != Integer.MIN_VALUE) {
|
||||
if (cookie.getVersion() == 0) {
|
||||
addUnquoted(sb, CookieHeaderNames.EXPIRES,
|
||||
new HttpHeaderDateFormat().format(
|
||||
HttpHeaderDateFormat.get().format(
|
||||
new Date(System.currentTimeMillis() +
|
||||
cookie.getMaxAge() * 1000L)));
|
||||
} else {
|
||||
|
@ -37,11 +37,23 @@ final class HttpHeaderDateFormat extends SimpleDateFormat {
|
||||
private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
|
||||
private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
|
||||
|
||||
private static final ThreadLocal<HttpHeaderDateFormat> FORMAT_THREAD_LOCAL =
|
||||
new ThreadLocal<HttpHeaderDateFormat>() {
|
||||
@Override
|
||||
protected HttpHeaderDateFormat initialValue() {
|
||||
return new HttpHeaderDateFormat();
|
||||
}
|
||||
};
|
||||
|
||||
public static HttpHeaderDateFormat get() {
|
||||
return FORMAT_THREAD_LOCAL.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard date format<p>
|
||||
* 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);
|
||||
setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class CookieDecoderTest {
|
||||
@Test
|
||||
public void testDecodingSingleCookieV0() {
|
||||
String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
|
||||
cookieString = cookieString.replace("XXX", new HttpHeaderDateFormat().format(new Date(System.currentTimeMillis() + 50000)));
|
||||
cookieString = cookieString.replace("XXX", HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000)));
|
||||
|
||||
CookieDecoder cookieDecoder = new CookieDecoder();
|
||||
Set<Cookie> cookies = cookieDecoder.decode(cookieString);
|
||||
|
@ -26,7 +26,7 @@ public class CookieEncoderTest {
|
||||
@Test
|
||||
public void testEncodingSingleCookieV0() {
|
||||
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");
|
||||
CookieEncoder encoder = new CookieEncoder(true);
|
||||
encoder.addCookie(cookie);
|
||||
|
@ -32,7 +32,7 @@ public class HttpHeaderDateFormatTest {
|
||||
|
||||
@Test
|
||||
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");
|
||||
Assert.assertNotNull(parsedDateWithSingleDigitDay);
|
||||
@ -61,7 +61,7 @@ public class HttpHeaderDateFormatTest {
|
||||
|
||||
@Test
|
||||
public void testFormat() {
|
||||
HttpHeaderDateFormat format = new HttpHeaderDateFormat();
|
||||
HttpHeaderDateFormat format = HttpHeaderDateFormat.get();
|
||||
|
||||
final String formatted = format.format(DATE);
|
||||
Assert.assertNotNull(formatted);
|
||||
|
Loading…
Reference in New Issue
Block a user