1919import java .time .Duration ;
2020import java .time .temporal .ChronoUnit ;
2121import java .util .concurrent .TimeUnit ;
22+ import java .util .function .Consumer ;
2223
2324import org .springframework .boot .context .properties .ConfigurationProperties ;
2425import org .springframework .boot .context .properties .bind .convert .DefaultDurationUnit ;
@@ -48,16 +49,16 @@ public class ResourceProperties {
4849 private String [] staticLocations = CLASSPATH_RESOURCE_LOCATIONS ;
4950
5051 /**
51- * Cache period for the resources served by the resource handler.
52- * If a duration suffix is not specified, seconds will be used.
53- * Can be overridden by the {@code cache-control} property.
52+ * Cache period for the resources served by the resource handler. If a duration suffix
53+ * is not specified, seconds will be used. Can be overridden by the 'cache-control'
54+ * property.
5455 */
5556 @ DefaultDurationUnit (ChronoUnit .SECONDS )
5657 private Duration cachePeriod ;
5758
5859 /**
59- * Cache control HTTP headers, only allows valid directive combinations.
60- * Overrides the {@code cache-period} property.
60+ * Cache control HTTP headers, only allows valid directive combinations. Overrides the
61+ * ' cache-period' property.
6162 */
6263 private CacheControlProperties cacheControl = new CacheControlProperties ();
6364
@@ -145,9 +146,8 @@ public static class Chain {
145146 /**
146147 * Return whether the resource chain is enabled. Return {@code null} if no
147148 * specific settings are present.
148- *
149- * @return whether the resource chain is enabled or {@code null} if no specified settings are
150- * present.
149+ * @return whether the resource chain is enabled or {@code null} if no specified
150+ * settings are present.
151151 */
152152 public Boolean getEnabled () {
153153 return getEnabled (getStrategy ().getFixed ().isEnabled (),
@@ -225,7 +225,7 @@ public static class Content {
225225 /**
226226 * Comma-separated list of patterns to apply to the Version Strategy.
227227 */
228- private String [] paths = new String []{ "/**" };
228+ private String [] paths = new String [] { "/**" };
229229
230230 public boolean isEnabled () {
231231 return this .enabled ;
@@ -258,7 +258,7 @@ public static class Fixed {
258258 /**
259259 * Comma-separated list of patterns to apply to the Version Strategy.
260260 */
261- private String [] paths = new String []{ "/**" };
261+ private String [] paths = new String [] { "/**" };
262262
263263 /**
264264 * Version string to use for the Version Strategy.
@@ -297,15 +297,15 @@ public void setVersion(String version) {
297297 public static class CacheControlProperties {
298298
299299 /**
300- * Maximum time the response should be cached,
301- * in seconds if no duration suffix is not specified.
300+ * Maximum time the response should be cached, in seconds if no duration suffix is
301+ * not specified.
302302 */
303303 @ DefaultDurationUnit (ChronoUnit .SECONDS )
304304 private Duration maxAge ;
305305
306306 /**
307- * Indicate that the cached response can be reused only
308- * if re-validated with the server.
307+ * Indicate that the cached response can be reused only if re-validated with the
308+ * server.
309309 */
310310 private Boolean noCache ;
311311
@@ -315,14 +315,14 @@ public static class CacheControlProperties {
315315 private Boolean noStore ;
316316
317317 /**
318- * Indicate that once it has become stale, a cache must not use
319- * the response without re-validating it with the server.
318+ * Indicate that once it has become stale, a cache must not use the response
319+ * without re-validating it with the server.
320320 */
321321 private Boolean mustRevalidate ;
322322
323323 /**
324- * Indicate intermediaries (caches and others) that they should
325- * not transform the response content.
324+ * Indicate intermediaries (caches and others) that they should not transform the
325+ * response content.
326326 */
327327 private Boolean noTransform ;
328328
@@ -332,34 +332,34 @@ public static class CacheControlProperties {
332332 private Boolean cachePublic ;
333333
334334 /**
335- * Indicate that the response message is intended for a single user
336- * and must not be stored by a shared cache.
335+ * Indicate that the response message is intended for a single user and must not
336+ * be stored by a shared cache.
337337 */
338338 private Boolean cachePrivate ;
339339
340340 /**
341- * Same meaning as the "must-revalidate" directive,
342- * except that it does not apply to private caches.
341+ * Same meaning as the "must-revalidate" directive, except that it does not apply
342+ * to private caches.
343343 */
344344 private Boolean proxyRevalidate ;
345345
346346 /**
347- * Maximum time the response can be served after it becomes stale,
348- * in seconds if no duration suffix is not specified.
347+ * Maximum time the response can be served after it becomes stale, in seconds if
348+ * no duration suffix is not specified.
349349 */
350350 @ DefaultDurationUnit (ChronoUnit .SECONDS )
351351 private Duration staleWhileRevalidate ;
352352
353353 /**
354- * Maximum time the response may be used when errors are encountered,
355- * in seconds if no duration suffix is not specified.
354+ * Maximum time the response may be used when errors are encountered, in seconds
355+ * if no duration suffix is not specified.
356356 */
357357 @ DefaultDurationUnit (ChronoUnit .SECONDS )
358358 private Duration staleIfError ;
359359
360360 /**
361- * Maximum time the response should be cached by shared caches,
362- * in seconds if no duration suffix is not specified.
361+ * Maximum time the response should be cached by shared caches, in seconds if no
362+ * duration suffix is not specified.
363363 */
364364 @ DefaultDurationUnit (ChronoUnit .SECONDS )
365365 private Duration sMaxAge ;
@@ -453,44 +453,43 @@ public void setsMaxAge(Duration sMaxAge) {
453453 }
454454
455455 public CacheControl toHttpCacheControl () {
456- CacheControl cc ;
457- if (Boolean .TRUE .equals (this .noStore )) {
458- cc = CacheControl .noStore ();
459- }
460- else if (Boolean .TRUE .equals (this .noCache )) {
461- cc = CacheControl .noCache ();
462- }
463- else if (this .maxAge != null ) {
464- cc = CacheControl .maxAge (this .maxAge .getSeconds (), TimeUnit .SECONDS );
465- }
466- else {
467- cc = CacheControl .empty ();
468- }
469- if (Boolean .TRUE .equals (this .mustRevalidate )) {
470- cc .mustRevalidate ();
471- }
472- if (Boolean .TRUE .equals (this .noTransform )) {
473- cc .noTransform ();
456+ CacheControl cacheControl = createCacheControl ();
457+ callIfTrue (this .mustRevalidate , cacheControl , CacheControl ::mustRevalidate );
458+ callIfTrue (this .noTransform , cacheControl , CacheControl ::noTransform );
459+ callIfTrue (this .cachePublic , cacheControl , CacheControl ::cachePublic );
460+ callIfTrue (this .cachePrivate , cacheControl , CacheControl ::cachePrivate );
461+ callIfTrue (this .proxyRevalidate , cacheControl , CacheControl ::proxyRevalidate );
462+ if (this .staleWhileRevalidate != null ) {
463+ cacheControl .staleWhileRevalidate (this .staleWhileRevalidate .getSeconds (),
464+ TimeUnit .SECONDS );
474465 }
475- if (Boolean .TRUE .equals (this .cachePublic )) {
476- cc .cachePublic ();
466+ if (this .staleIfError != null ) {
467+ cacheControl .staleIfError (this .staleIfError .getSeconds (),
468+ TimeUnit .SECONDS );
477469 }
478- if (Boolean . TRUE . equals ( this .cachePrivate ) ) {
479- cc . cachePrivate ( );
470+ if (this .sMaxAge != null ) {
471+ cacheControl . sMaxAge ( this . sMaxAge . getSeconds (), TimeUnit . SECONDS );
480472 }
481- if (Boolean .TRUE .equals (this .proxyRevalidate )) {
482- cc .proxyRevalidate ();
473+ return cacheControl ;
474+ }
475+
476+ private CacheControl createCacheControl () {
477+ if (Boolean .TRUE .equals (this .noStore )) {
478+ return CacheControl .noStore ();
483479 }
484- if (this .staleWhileRevalidate != null ) {
485- cc . staleWhileRevalidate ( this . staleWhileRevalidate . getSeconds (), TimeUnit . SECONDS );
480+ if (Boolean . TRUE . equals ( this .noCache ) ) {
481+ return CacheControl . noCache ( );
486482 }
487- if (this .staleIfError != null ) {
488- cc . staleIfError (this .staleIfError .getSeconds (), TimeUnit .SECONDS );
483+ if (this .maxAge != null ) {
484+ return CacheControl . maxAge (this .maxAge .getSeconds (), TimeUnit .SECONDS );
489485 }
490- if (this .sMaxAge != null ) {
491- cc .sMaxAge (this .sMaxAge .getSeconds (), TimeUnit .SECONDS );
486+ return CacheControl .empty ();
487+ }
488+
489+ private <T > void callIfTrue (Boolean property , T instance , Consumer <T > call ) {
490+ if (Boolean .TRUE .equals (property )) {
491+ call .accept (instance );
492492 }
493- return cc ;
494493 }
495494
496495 }
0 commit comments