From 6941bb69b2739a533c2341abc968a262b9c5a775 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Mon, 10 Jan 2022 10:02:01 -0800 Subject: [PATCH] Added support for last-modified using java.time.Instant. --- CHANGELOG.md | 5 ++++ .../proteus/server/ServerResponse.java | 25 ++++++++++++++++++- .../proteus/test/controllers/Tests.java | 15 ++++++++++- .../test/server/StandardEndpointsTest.java | 7 ++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fd3a8c..29e0b5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Added HEIC format** + + +[1aa07d8f82ab1cc](https://github.com/noboomu/proteus/commit/1aa07d8f82ab1cc) Joshua Bauer *2021-12-24 00:44:12* + **Added AVIF format** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java index b7272d1..c4b67b6 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.google.inject.Inject; +import io.sinistral.proteus.protocol.HttpHeaders; import io.sinistral.proteus.protocol.MediaType; import io.sinistral.proteus.server.predicates.ServerPredicates; import io.sinistral.proteus.wrappers.JsonViewWrapper; @@ -20,6 +21,11 @@ import javax.ws.rs.core.Response; import java.net.URI; import java.nio.ByteBuffer; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.Locale; @@ -36,6 +42,10 @@ public class ServerResponse { private static Logger log = LoggerFactory.getLogger(ServerResponse.class.getCanonicalName()); + private final static String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; + + private static final ThreadLocal RFC1123_PATTERN_FORMATTER =ThreadLocal.withInitial( () -> DateTimeFormatter.ofPattern(RFC1123_PATTERN)); + @Inject protected static XmlMapper XML_MAPPER; @@ -176,7 +186,6 @@ public void setStatus(Response.Status status) this.status = status.getStatusCode(); } - public ServerResponse body(ByteBuffer body) { this.body = body; @@ -219,18 +228,32 @@ public ServerResponse method(String method) public ServerResponse lastModified(Date date) { this.headers.put(Headers.LAST_MODIFIED, date.getTime()); + this.hasHeaders = true; return this; } + /** + * @param instant + * the instant to set + */ + public ServerResponse lastModified(Instant instant) + { + this.headers.put(Headers.LAST_MODIFIED,RFC1123_PATTERN_FORMATTER.get().format(ZonedDateTime.ofInstant(instant, ZoneId.of("GMT")))); + this.hasHeaders = true; + return this; + } + public ServerResponse contentLanguage(Locale locale) { this.headers.put(Headers.CONTENT_LANGUAGE, locale.toLanguageTag()); + this.hasHeaders = true; return this; } public ServerResponse contentLanguage(String language) { this.headers.put(Headers.CONTENT_LANGUAGE, language); + this.hasHeaders = true; return this; } diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index 84f2c8a..7278bf3 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -115,6 +115,8 @@ public void exchangeUserJson(HttpServerExchange exchange) { response( new User(123L) ).applicationJson().send(exchange); } + + @GET @Path("exchange/user/xml") @@ -671,7 +673,8 @@ public CompletableFuture>> responseFutureNotFo return future; } - + + @GET @Path("response/future/user") @Produces((MediaType.APPLICATION_JSON)) @@ -1040,5 +1043,15 @@ public ServerResponse> multipartUploadMultipleFiles(ServerReq } + @GET + @Path("headers/last-modified") + @Produces(MediaType.APPLICATION_JSON) + @Blocking + public ServerResponse> lastModifiedResponse(ServerRequest request) + { + return response(Map.of("key","value")).lastModified(Instant.now()); + + } + } diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/StandardEndpointsTest.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/StandardEndpointsTest.java index 3b9b283..2ca06a5 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/StandardEndpointsTest.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/StandardEndpointsTest.java @@ -578,6 +578,13 @@ public void minValueError() } + @Test + public void lastModified() + { + + given().accept(ContentType.JSON).when().get("v1/tests/headers/last-modified").then().statusCode(200).body(containsString("value")).header("last-modified", CoreMatchers.anything()); + } + @Test public void maxValue() {