@Override public Instant convert(Long source) { return Instant.ofEpochMilli(source); } }
static String formatDate(long date) { Instant instant = Instant.ofEpochMilli(date); ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT); return DATE_FORMATTERS[0].format(time); }
/** * 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 3.2.4 * @see #setZonedDateTime(String, ZonedDateTime) */ public void setDate(String headerName, long date) { setInstant(headerName, Instant.ofEpochMilli(date)); }
private static ZonedDateTime calendarToZonedDateTime(Calendar source) { if (source instanceof GregorianCalendar) { return ((GregorianCalendar) source).toZonedDateTime(); } else { return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()), source.getTimeZone().toZoneId()); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { if (isLegacyTimestamp()) { return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter); } else { return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter); } }
@JsonValue @Override public String toString() { if (isLegacyTimestamp()) { return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(JSON_FORMATTER); } else { return Instant.ofEpochMilli(millis).atZone(ZoneId.of(UTC_KEY.getId())).format(JSON_FORMATTER); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
@JsonValue @Override public String toString() { return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(timeZoneKey.getId())).format(formatter); } }
private boolean validateIfUnmodifiedSince(Instant lastModified) { if (lastModified.isBefore(Instant.EPOCH)) { return false; } long ifUnmodifiedSince = getRequestHeaders().getIfUnmodifiedSince(); if (ifUnmodifiedSince == -1) { return false; } // We will perform this validation... Instant sinceInstant = Instant.ofEpochMilli(ifUnmodifiedSince); this.notModified = sinceInstant.isBefore(lastModified.truncatedTo(ChronoUnit.SECONDS)); return true; }
private boolean validateIfModifiedSince(Instant lastModified) { if (lastModified.isBefore(Instant.EPOCH)) { return false; } long ifModifiedSince = getRequestHeaders().getIfModifiedSince(); if (ifModifiedSince == -1) { return false; } // We will perform this validation... this.notModified = ChronoUnit.SECONDS.between(lastModified, Instant.ofEpochMilli(ifModifiedSince)) >= 0; return true; }
@Nullable String notModified(ServerWebExchange exchange) { if (exchange.checkNotModified(Instant.ofEpochMilli(1000 * 1000))) { return null; } return "body"; }
@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 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 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); }
@Override public final Mono<Void> writeTo(ServerWebExchange exchange, Context context) { writeStatusAndHeaders(exchange.getResponse()); Instant lastModified = Instant.ofEpochMilli(headers().getLastModified()); HttpMethod httpMethod = exchange.getRequest().getMethod(); if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(headers().getETag(), lastModified)) { return exchange.getResponse().setComplete(); } else { return writeToInternal(exchange, context); } }
@Test public void checkNotModifiedTimestampWithLengthPart() throws Exception { long epochTime = dateFormat.parse(CURRENT_TIME).getTime(); String header = "Wed, 09 Apr 2014 09:57:42 GMT; length=13774"; MockServerHttpRequest request = get("/").header("If-Modified-Since", header).build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); assertTrue(exchange.checkNotModified(Instant.ofEpochMilli(epochTime))); assertEquals(304, exchange.getResponse().getStatusCode().value()); assertEquals(epochTime, exchange.getResponse().getHeaders().getLastModified()); }
@Test public void checkModifiedTimestampWithLengthPart() throws Exception { long epochTime = dateFormat.parse(CURRENT_TIME).getTime(); String header = "Tue, 08 Apr 2014 09:57:42 GMT; length=13774"; MockServerHttpRequest request = get("/").header("If-Modified-Since", header).build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); assertFalse(exchange.checkNotModified(Instant.ofEpochMilli(epochTime))); assertNull(exchange.getResponse().getStatusCode()); assertEquals(epochTime, exchange.getResponse().getHeaders().getLastModified()); }