From 7343254090dddec210bb84cd08ce1db5dedc0782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 12 Aug 2024 14:46:37 +0200 Subject: [PATCH] Adapt to framework change that makes CacheControl immutable See https://github.com/spring-projects/spring-framework/pull/33366 --- .../boot/autoconfigure/web/WebProperties.java | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebProperties.java index d0ba50a51ca..164b75d2493 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -569,20 +569,32 @@ public class WebProperties { public CacheControl toHttpCacheControl() { PropertyMapper map = PropertyMapper.get(); CacheControl control = createCacheControl(); - map.from(this::getMustRevalidate).whenTrue().toCall(control::mustRevalidate); - map.from(this::getNoTransform).whenTrue().toCall(control::noTransform); - map.from(this::getCachePublic).whenTrue().toCall(control::cachePublic); - map.from(this::getCachePrivate).whenTrue().toCall(control::cachePrivate); - map.from(this::getProxyRevalidate).whenTrue().toCall(control::proxyRevalidate); - map.from(this::getStaleWhileRevalidate) + if (Boolean.TRUE.equals(this.mustRevalidate)) { + control = control.mustRevalidate(); + } + if (Boolean.TRUE.equals(this.noTransform)) { + control = control.noTransform(); + } + if (Boolean.TRUE.equals(this.cachePrivate)) { + control = control.cachePrivate(); + } + if (Boolean.TRUE.equals(this.cachePublic)) { + control = control.cachePublic(); + } + if (Boolean.TRUE.equals(this.proxyRevalidate)) { + control = control.proxyRevalidate(); + } + control = map.from(this::getStaleWhileRevalidate) .whenNonNull() - .to((duration) -> control.staleWhileRevalidate(duration.getSeconds(), TimeUnit.SECONDS)); - map.from(this::getStaleIfError) + .to(control, (instance, duration) -> instance.staleWhileRevalidate(duration.getSeconds(), + TimeUnit.SECONDS)); + control = map.from(this::getStaleIfError) .whenNonNull() - .to((duration) -> control.staleIfError(duration.getSeconds(), TimeUnit.SECONDS)); - map.from(this::getSMaxAge) + .to(control, + (instance, duration) -> instance.staleIfError(duration.getSeconds(), TimeUnit.SECONDS)); + control = map.from(this::getSMaxAge) .whenNonNull() - .to((duration) -> control.sMaxAge(duration.getSeconds(), TimeUnit.SECONDS)); + .to(control, (instance, duration) -> instance.sMaxAge(duration.getSeconds(), TimeUnit.SECONDS)); // check if cacheControl remained untouched if (control.getHeaderValue() == null) { return null;