Skip to content

Commit 2a83d73

Browse files
committed
Backport support for Cache-Control header.
This is needed for automated testing of systems using Bosk, so they can make sure prior bosk updates have been applied before subsequent actions occur.
1 parent 25075d9 commit 2a83d73

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

bosk-spring-boot-3/src/main/java/works/bosk/spring/boot/ReadContextFilter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import works.bosk.Bosk;
1616
import works.bosk.exceptions.NoReadContextException;
1717

18+
import static org.springframework.http.HttpHeaders.CACHE_CONTROL;
19+
1820
@Component
1921
@ControllerAdvice
2022
@RequiredArgsConstructor
@@ -23,6 +25,16 @@ public class ReadContextFilter extends OncePerRequestFilter {
2325

2426
@Override
2527
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
28+
if ("no-cache".equalsIgnoreCase(request.getHeader(CACHE_CONTROL))) {
29+
// Allow the client to specify that they want their read context to have the latest state.
30+
// We do this even for requests that don't automatically open a read context because
31+
// they might later manually open one.
32+
try {
33+
bosk.driver().flush();
34+
} catch (InterruptedException e) {
35+
throw new ServletException(e);
36+
}
37+
}
2638
if (automaticallyOpenReadContext(request)) {
2739
try (var __ = bosk.readContext()) {
2840
filterChain.doFilter(request, response);
@@ -33,8 +45,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3345
}
3446

3547
/**
36-
* The "safe" HTTP methods won't change server state, so there's no reason not to
37-
* open a
48+
* The "safe" HTTP methods won't change the server state,
49+
* so there's no reason not to open a read context.
3850
*/
3951
private boolean automaticallyOpenReadContext(HttpServletRequest request) {
4052
return switch (request.getMethod()) {

0 commit comments

Comments
 (0)