Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import java.io.PrintWriter;

/**
* Sets {@code Cache-Control} only when HTTP cache is enabled and the request path plus
* query parameters match a mapping exactly. Unmatched requests get no cache header.
* When HTTP cache is enabled, sets {@code Cache-Control} on GET responses that do not
* already have one: a matching mapping's value, otherwise
* {@code no-store, no-cache, must-revalidate}.
* <p>
* Implemented as a filter (not a {@code HandlerInterceptor}) so the header is applied
* before the response body is written. {@code postHandle} runs after {@code @ResponseBody}
Expand All @@ -28,6 +29,8 @@
@Component
public class HttpCacheControlFilter extends OncePerRequestFilter {

static final String NO_CACHE = "no-store, no-cache, must-revalidate";

private final OgcApiProperties ogcApiProperties;

public HttpCacheControlFilter(OgcApiProperties ogcApiProperties) {
Expand Down Expand Up @@ -75,19 +78,19 @@ void applyIfEligible() {
return;
}
applied = true;
if (getStatus() != HttpStatus.OK.value()) {
return;
}
if (containsHeader(HttpHeaders.CACHE_CONTROL)) {
return;
}
String path = request.getRequestURI();
for (OgcApiProperties.Mapping mapping : httpCache.mappings()) {
if (mapping.matches(path, request.getParameterMap())) {
setHeader(HttpHeaders.CACHE_CONTROL, mapping.cacheControlHeader());
return;
if (getStatus() == HttpStatus.OK.value()) {
String path = request.getRequestURI();
for (OgcApiProperties.Mapping mapping : httpCache.mappings()) {
if (mapping.matches(path, request.getParameterMap())) {
setHeader(HttpHeaders.CACHE_CONTROL, mapping.cacheControlHeader());
return;
}
}
}
setHeader(HttpHeaders.CACHE_CONTROL, NO_CACHE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,49 +77,49 @@ void setsHeaderOnExactMatchBeforeBodyWrite() throws Exception {
}

@Test
void noHeaderWhenPathDiffers() throws Exception {
void noCacheWhenPathDiffers() throws Exception {
HttpCacheControlFilter filter = new HttpCacheControlFilter(properties(true));
MockHttpServletRequest request = matchingGet();
request.setRequestURI("/api/v1/ogc/collections/other");
MockHttpServletResponse response = new MockHttpServletResponse();

run(filter, request, response, statusThenWrite(200));

assertNull(response.getHeader(HttpHeaders.CACHE_CONTROL));
assertEquals(HttpCacheControlFilter.NO_CACHE, response.getHeader(HttpHeaders.CACHE_CONTROL));
}

@Test
void noHeaderWhenExtraQueryParamPresent() throws Exception {
void noCacheWhenExtraQueryParamPresent() throws Exception {
HttpCacheControlFilter filter = new HttpCacheControlFilter(properties(true));
MockHttpServletRequest request = matchingGet();
request.setParameter("q", "fish");
MockHttpServletResponse response = new MockHttpServletResponse();

run(filter, request, response, statusThenWrite(200));

assertNull(response.getHeader(HttpHeaders.CACHE_CONTROL));
assertEquals(HttpCacheControlFilter.NO_CACHE, response.getHeader(HttpHeaders.CACHE_CONTROL));
}

@Test
void noHeaderWhenExpectedParamValueDiffers() throws Exception {
void noCacheWhenExpectedParamValueDiffers() throws Exception {
HttpCacheControlFilter filter = new HttpCacheControlFilter(properties(true));
MockHttpServletRequest request = matchingGet();
request.setParameter("sortby", "-score");
MockHttpServletResponse response = new MockHttpServletResponse();

run(filter, request, response, statusThenWrite(200));

assertNull(response.getHeader(HttpHeaders.CACHE_CONTROL));
assertEquals(HttpCacheControlFilter.NO_CACHE, response.getHeader(HttpHeaders.CACHE_CONTROL));
}

@Test
void noHeaderWhenNotOk() throws Exception {
void noCacheWhenNotOk() throws Exception {
HttpCacheControlFilter filter = new HttpCacheControlFilter(properties(true));
MockHttpServletResponse response = new MockHttpServletResponse();

run(filter, matchingGet(), response, statusThenWrite(500));

assertNull(response.getHeader(HttpHeaders.CACHE_CONTROL));
assertEquals(HttpCacheControlFilter.NO_CACHE, response.getHeader(HttpHeaders.CACHE_CONTROL));
}

@Test
Expand Down
Loading