Skip to content

Commit

Permalink
fix(core): make request/response headers optional...
Browse files Browse the repository at this point in the history
...when checking/using cache-control headers
  • Loading branch information
cmark committed Apr 8, 2024
1 parent 3566465 commit 7aecfe9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Request<BranchContext, R> nextWithCaching() {

// before executing the request, check whether we have an If-None-Match header set in the incoming request
// if yes, check the current ETag with any of the attached values, if none matches evaluate the query otherwise send back HTTP 304
String ifNoneMatchHeaderValue = ctx.service(RequestHeaders.class).header(ApiConfiguration.IF_NONE_MATCH_HEADER);
String ifNoneMatchHeaderValue = ctx.optionalService(RequestHeaders.class).orElse(RequestHeaders.EMPTY).header(ApiConfiguration.IF_NONE_MATCH_HEADER);
if (!Strings.isNullOrEmpty(ifNoneMatchHeaderValue) && Objects.equals(ifNoneMatchHeaderValue.replaceAll("\"", ""), eTag)) {
throw new NotModifiedException();
}
Expand All @@ -88,10 +88,11 @@ private Request<BranchContext, R> nextWithCaching() {

// once we have the response ready calculate Cache-Control and ETag headers
final ApiConfiguration apiConfiguration = ctx.service(ApiConfiguration.class);
final ResponseHeaders responseHeaders = ctx.service(ResponseHeaders.class);
responseHeaders.set(ApiConfiguration.ETAG_HEADER, eTag);
// configure HTTP Cache-Control headers here using the currently configured global api.cache_control value
responseHeaders.set(ApiConfiguration.CACHE_CONTROL_HEADER, apiConfiguration.getCacheControl());
ctx.optionalService(ResponseHeaders.class).ifPresent(responseHeaders -> {
responseHeaders.set(ApiConfiguration.ETAG_HEADER, eTag);
// configure HTTP Cache-Control headers here using the currently configured global api.cache_control value
responseHeaders.set(ApiConfiguration.CACHE_CONTROL_HEADER, apiConfiguration.getCacheControl());
});

return response;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2019-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,13 +18,17 @@
import java.util.Collections;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableMap;

/**
* @since 7.2
*/
public final class RequestHeaders {

@JsonIgnore
public static final RequestHeaders EMPTY = new RequestHeaders(Map.of());

private final Map<String, String> headers;

public RequestHeaders(Map<String, String> headers) {
Expand Down

0 comments on commit 7aecfe9

Please sign in to comment.