Browse Source

Merge branch '6.2.x'

pull/34996/head
Brian Clozel 8 months ago
parent
commit
79a793e44c
  1. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 6
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

8
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

@ -514,7 +514,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator @@ -514,7 +514,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* If the resource exists, the request will be checked for the presence of the
* {@code Last-Modified} header, and its value will be compared against the last-modified
* timestamp of the given resource, returning a {@code 304} status code if the
* {@code Last-Modified} value is greater. If the resource is newer than the
* {@code Last-Modified} value is greater. If the resource is newer than the
* {@code Last-Modified} value, or the header is not present, the content resource
* of the resource will be written to the response with caching headers
* set to expire one year in the future.
@ -538,6 +538,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator @@ -538,6 +538,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
// Supported methods and required session
checkRequest(request);
// Apply cache settings, if any
prepareResponse(response);
// Header phase
String eTagValue = (this.getEtagGenerator() != null) ? this.getEtagGenerator().apply(resource) : null;
long lastModified = (this.isUseLastModified()) ? resource.lastModified() : -1;
@ -546,9 +549,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator @@ -546,9 +549,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
return;
}
// Apply cache settings, if any
prepareResponse(response);
// Check the media type for the resource
MediaType mediaType = getMediaType(request, resource);
setHeaders(response, resource, mediaType);

6
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@ -465,11 +465,13 @@ class ResourceHttpRequestHandlerTests { @@ -465,11 +465,13 @@ class ResourceHttpRequestHandlerTests {
@Test
void shouldRespondWithNotModifiedWhenModifiedSince() throws Exception {
this.handler.setCacheSeconds(3600);
this.handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css"));
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
}
@Test
@ -484,12 +486,14 @@ class ResourceHttpRequestHandlerTests { @@ -484,12 +486,14 @@ class ResourceHttpRequestHandlerTests {
@Test
void shouldRespondWithNotModifiedWhenEtag() throws Exception {
this.handler.setCacheSeconds(3600);
this.handler.setEtagGenerator(resource -> "testEtag");
this.handler.afterPropertiesSet();
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
this.request.addHeader("If-None-Match", "\"testEtag\"");
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED);
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
}
@Test

Loading…
Cancel
Save