Skip to content

Commit

Permalink
Added support for last-modified using java.time.Instant.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jan 10, 2022
1 parent 1aa07d8 commit 6941bb6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,6 +42,10 @@ public class ServerResponse<T>
{
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<DateTimeFormatter> RFC1123_PATTERN_FORMATTER =ThreadLocal.withInitial( () -> DateTimeFormatter.ofPattern(RFC1123_PATTERN));

@Inject
protected static XmlMapper XML_MAPPER;

Expand Down Expand Up @@ -176,7 +186,6 @@ public void setStatus(Response.Status status)
this.status = status.getStatusCode();
}


public ServerResponse<T> body(ByteBuffer body)
{
this.body = body;
Expand Down Expand Up @@ -219,18 +228,32 @@ public ServerResponse<T> method(String method)
public ServerResponse<T> lastModified(Date date)
{
this.headers.put(Headers.LAST_MODIFIED, date.getTime());
this.hasHeaders = true;
return this;
}

/**
* @param instant
* the instant to set
*/
public ServerResponse<T> 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<T> contentLanguage(Locale locale)
{
this.headers.put(Headers.CONTENT_LANGUAGE, locale.toLanguageTag());
this.hasHeaders = true;
return this;
}

public ServerResponse<T> contentLanguage(String language)
{
this.headers.put(Headers.CONTENT_LANGUAGE, language);
this.hasHeaders = true;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public void exchangeUserJson(HttpServerExchange exchange)
{
response( new User(123L) ).applicationJson().send(exchange);
}



@GET
@Path("exchange/user/xml")
Expand Down Expand Up @@ -671,7 +673,8 @@ public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureNotFo

return future;
}



@GET
@Path("response/future/user")
@Produces((MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -1040,5 +1043,15 @@ public ServerResponse<Map<String,Object>> multipartUploadMultipleFiles(ServerReq

}

@GET
@Path("headers/last-modified")
@Produces(MediaType.APPLICATION_JSON)
@Blocking
public ServerResponse<Map<String,String>> lastModifiedResponse(ServerRequest request)
{
return response(Map.of("key","value")).lastModified(Instant.now());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 6941bb6

Please sign in to comment.