/** * Set the given date under the given header name after formatting it as a string * using the RFC-1123 date-time formatter. The equivalent of * {@link #set(String, String)} but for date headers. * @since 5.0 */ public void setZonedDateTime(String headerName, ZonedDateTime date) { set(headerName, DATE_FORMATTERS[0].format(date)); }
static String formatDate(long date) { Instant instant = Instant.ofEpochMilli(date); ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT); return DATE_FORMATTERS[0].format(time); }
@Override public void serialize(Instant value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(ISO_INSTANT.format(value)); } }
/** * Add a Instant value to the JSON array. * <p> * JSON has no notion of Temporal data so the Instant will be ISOString encoded, and the String added. * * @param value the value * @return a reference to this, so the API can be used fluently */ public JsonArray add(Instant value) { Objects.requireNonNull(value); list.add(ISO_INSTANT.format(value)); return this; }
@Override public String print(TemporalAccessor partial, Locale locale) { return DateTimeContextHolder.getFormatter(this.formatter, locale).format(partial); }
@Override public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { /* * Serialization of timezone data is unwanted (not ISO). Offset is kept, timezone info is thrown away here. * * Keeping timezone info is a new feature which needs to be implemented. */ gen.writeFieldName(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value)); }
private void write(SerializeWriter out, TemporalAccessor object, String format) { DateTimeFormatter formatter; if ("unixtime".equals(format) && object instanceof ChronoZonedDateTime) { long seconds = ((ChronoZonedDateTime) object).toEpochSecond(); out.writeInt((int) seconds); return; } if (format == formatter_iso8601_pattern) { formatter = formatter_iso8601; } else { formatter = DateTimeFormatter.ofPattern(format); } String text = formatter.format((TemporalAccessor) object); out.writeString(text); } }
@Override public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider provider) throws IOException { if (!useTimestamp(provider)) { if (shouldWriteWithZoneId(provider)) { // write with zone g.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value)); return; } } super.serialize(value, g, provider); }
@Test public void shouldHandleLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, null, oneMinuteAgo); }
@Test // SPR-13626 public void shouldHandleIfNoneMatchIfUnmodifiedSince() throws Exception { String etagValue = "\"some-etag\""; servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(etagValue).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, -1); }
@Test public void createDateTimeFormatterWithTimeZone() { factory.setPattern("yyyyMMddHHmmss Z"); factory.setTimeZone(TEST_TIMEZONE); ZoneId dateTimeZone = TEST_TIMEZONE.toZoneId(); ZonedDateTime dateTime = ZonedDateTime.of(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000 " + offset)); }
@Test public void shouldNotFailPreconditionForPutRequests() throws Exception { servletRequest.setMethod("PUT"); ZonedDateTime dateTime = ofEpochMilli(new Date().getTime()).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); long justModified = dateTime.plus(1, ChronoUnit.SECONDS).toEpochSecond() * 1000; ResponseEntity<String> returnValue = ResponseEntity.ok() .lastModified(justModified).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, null, null, justModified); }
@Test public void createDateTimeFormatterInOrderOfPropertyPriority() { factory.setStylePattern("SS"); String value = applyLocale(factory.createDateTimeFormatter()).format(dateTime); assertTrue(value.startsWith("10/21/09")); assertTrue(value.endsWith("12:10 PM")); factory.setIso(ISO.DATE); assertThat(applyLocale(factory.createDateTimeFormatter()).format(dateTime), is("2009-10-21")); factory.setPattern("yyyyMMddHHmmss"); assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000")); }
@Test public void createDateTimeFormatterWithPattern() { factory = new DateTimeFormatterFactory("yyyyMMddHHmmss"); DateTimeFormatter formatter = factory.createDateTimeFormatter(); assertThat(formatter.format(dateTime), is("20091021121000")); }
@Test public void shouldHandleETagAndLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(etagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, etagValue, oneMinuteAgo); }
@Test public void shouldHandleChangedETagAndLastModified() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); String etagValue = "\"deadb33f8badf00d\""; String changedEtagValue = "\"changed-etag-value\""; ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); ResponseEntity<String> returnValue = ResponseEntity.ok() .eTag(changedEtagValue).lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.OK, null, changedEtagValue, oneMinuteAgo); }
@Test public void firstZonedDateTime() { ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT")); headers.setZonedDateTime(HttpHeaders.DATE, date); assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT")); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); headers.clear(); ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT")); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date)); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate)); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // obsolete RFC 850 format headers.clear(); headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // ANSI C's asctime() format headers.clear(); headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); }
@Test public void notModifiedLastModified() { ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES); RenderingResponse responseMono = RenderingResponse.create("bar") .header(HttpHeaders.LAST_MODIFIED, DateTimeFormatter.RFC_1123_DATE_TIME.format(oneMinuteBeforeNow)) .build() .block(); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com") .header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)) .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT); MockServerHttpResponse response = exchange.getResponse(); assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); StepVerifier.create(response.getBody()) .expectError(IllegalStateException.class) .verify(); }
@Test public void notModifiedLastModified() { ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES); EntityResponse<String> responseMono = EntityResponse.fromObject("bar") .lastModified(oneMinuteBeforeNow) .build() .block(); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com") .header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)) .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT); MockServerHttpResponse response = exchange.getResponse(); assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); StepVerifier.create(response.getBody()) .expectError(IllegalStateException.class) .verify(); }
@Test public void notModifiedLastModified() { ZonedDateTime now = ZonedDateTime.now(); ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES); ServerResponse responseMono = ServerResponse.ok() .lastModified(oneMinuteBeforeNow) .syncBody("bar") .block(); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com") .header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)) .build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); responseMono.writeTo(exchange, EMPTY_CONTEXT); MockServerHttpResponse response = exchange.getResponse(); assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); StepVerifier.create(response.getBody()) .expectError(IllegalStateException.class) .verify(); }