23
23
import java .io .PrintWriter ;
24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .io .Writer ;
26
- import java .time .Instant ;
27
- import java .time .ZoneId ;
28
- import java .time .ZonedDateTime ;
29
- import java .time .format .DateTimeParseException ;
26
+ import java .text .DateFormat ;
27
+ import java .text .ParseException ;
28
+ import java .text .SimpleDateFormat ;
30
29
import java .util .ArrayList ;
31
30
import java .util .Collection ;
32
31
import java .util .Collections ;
32
+ import java .util .Date ;
33
33
import java .util .List ;
34
34
import java .util .Locale ;
35
35
import java .util .Map ;
36
+ import java .util .TimeZone ;
36
37
import javax .servlet .ServletOutputStream ;
37
38
import javax .servlet .http .Cookie ;
38
39
import javax .servlet .http .HttpServletResponse ;
44
45
import org .springframework .util .StringUtils ;
45
46
import org .springframework .web .util .WebUtils ;
46
47
47
- import static java .time .format .DateTimeFormatter .*;
48
-
49
48
/**
50
49
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
51
50
*
@@ -60,7 +59,9 @@ public class MockHttpServletResponse implements HttpServletResponse {
60
59
61
60
private static final String CHARSET_PREFIX = "charset=" ;
62
61
63
- private static final ZoneId GMT = ZoneId .of ("GMT" );
62
+ private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz" ;
63
+
64
+ private static final TimeZone GMT = TimeZone .getTimeZone ("GMT" );
64
65
65
66
66
67
//---------------------------------------------------------------------
@@ -292,7 +293,7 @@ public void reset() {
292
293
this .characterEncoding = null ;
293
294
this .contentLength = 0 ;
294
295
this .contentType = null ;
295
- this .locale = null ;
296
+ this .locale = Locale . getDefault () ;
296
297
this .cookies .clear ();
297
298
this .headers .clear ();
298
299
this .status = HttpServletResponse .SC_OK ;
@@ -302,9 +303,7 @@ public void reset() {
302
303
@ Override
303
304
public void setLocale (Locale locale ) {
304
305
this .locale = locale ;
305
- if (locale != null ) {
306
- doAddHeaderValue (HttpHeaders .ACCEPT_LANGUAGE , locale .toLanguageTag (), true );
307
- }
306
+ doAddHeaderValue (HttpHeaders .ACCEPT_LANGUAGE , locale .toLanguageTag (), true );
308
307
}
309
308
310
309
@ Override
@@ -507,25 +506,33 @@ public void setDateHeader(String name, long value) {
507
506
setHeaderValue (name , formatDate (value ));
508
507
}
509
508
509
+ @ Override
510
+ public void addDateHeader (String name , long value ) {
511
+ addHeaderValue (name , formatDate (value ));
512
+ }
513
+
510
514
public long getDateHeader (String name ) {
515
+ String headerValue = getHeader (name );
516
+ if (headerValue == null ) {
517
+ return -1 ;
518
+ }
511
519
try {
512
- return ZonedDateTime .parse (getHeader (name ), RFC_1123_DATE_TIME ). toInstant (). toEpochMilli ();
520
+ return newDateFormat () .parse (getHeader (name )). getTime ();
513
521
}
514
- catch (DateTimeParseException ex ) {
522
+ catch (ParseException ex ) {
515
523
throw new IllegalArgumentException (
516
- "Value for header '" + name + "' is not a valid Date: " + getHeader ( name ) );
524
+ "Value for header '" + name + "' is not a valid Date: " + headerValue );
517
525
}
518
526
}
519
527
520
- @ Override
521
- public void addDateHeader (String name , long value ) {
522
- addHeaderValue (name , formatDate (value ));
528
+ private String formatDate (long date ) {
529
+ return newDateFormat ().format (new Date (date ));
523
530
}
524
531
525
- private String formatDate ( long date ) {
526
- Instant instant = Instant . ofEpochMilli ( date );
527
- ZonedDateTime zonedDateTime = ZonedDateTime . ofInstant ( instant , GMT );
528
- return RFC_1123_DATE_TIME . format ( zonedDateTime ) ;
532
+ private DateFormat newDateFormat ( ) {
533
+ SimpleDateFormat dateFormat = new SimpleDateFormat ( DATE_FORMAT , Locale . US );
534
+ dateFormat . setTimeZone ( GMT );
535
+ return dateFormat ;
529
536
}
530
537
531
538
@ Override
@@ -576,7 +583,7 @@ else if (HttpHeaders.ACCEPT_LANGUAGE.equalsIgnoreCase(name)) {
576
583
HttpHeaders headers = new HttpHeaders ();
577
584
headers .add (HttpHeaders .ACCEPT_LANGUAGE , value .toString ());
578
585
List <Locale > locales = headers .getAcceptLanguageAsLocales ();
579
- setLocale ( locales .isEmpty () ? null : locales .get (0 ));
586
+ this . locale = (! locales .isEmpty () ? locales .get (0 ) : Locale . getDefault ( ));
580
587
return true ;
581
588
}
582
589
else {
0 commit comments