Skip to content

Commit 1eaaa9a

Browse files
committed
Add empty location check to ResourceHttpRequestHandler
ResourceHttpRequestHandler now implements InitializingBean and checks for empty locations. Issue: SPR-9186
1 parent c52c78d commit 1eaaa9a

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.http.HttpServletRequest;
2626
import javax.servlet.http.HttpServletResponse;
2727

28+
import org.springframework.beans.factory.InitializingBean;
2829
import org.springframework.core.io.ClassPathResource;
2930
import org.springframework.core.io.Resource;
3031
import org.springframework.http.MediaType;
@@ -38,7 +39,7 @@
3839
import org.springframework.web.servlet.support.WebContentGenerator;
3940

4041
/**
41-
* {@link HttpRequestHandler} that serves static resources optimized for superior browser performance
42+
* {@link HttpRequestHandler} that serves static resources optimized for superior browser performance
4243
* (according to the guidelines of Page Speed, YSlow, etc.) by allowing for flexible cache settings
4344
* ({@linkplain #setCacheSeconds "cacheSeconds" property}, last-modified support).
4445
*
@@ -50,7 +51,7 @@
5051
* (if present) so that a {@code 304} status code will be returned as appropriate, avoiding unnecessary
5152
* overhead for resources that are already cached by the client. The use of {@code Resource} locations
5253
* allows resource requests to easily be mapped to locations other than the web application root. For
53-
* example, resources could be served from a classpath location such as "classpath:/META-INF/public-web-resources/",
54+
* example, resources could be served from a classpath location such as "classpath:/META-INF/public-web-resources/",
5455
* allowing convenient packaging and serving of resources such as a JavaScript library from within jar files.
5556
*
5657
* <p>To ensure that users with a primed browser cache get the latest changes to application-specific
@@ -66,7 +67,7 @@
6667
* @author Juergen Hoeller
6768
* @since 3.0.4
6869
*/
69-
public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler {
70+
public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler, InitializingBean {
7071

7172
private static final boolean jafPresent =
7273
ClassUtils.isPresent("javax.activation.FileTypeMap", ResourceHttpRequestHandler.class.getClassLoader());
@@ -87,6 +88,9 @@ public void setLocations(List<Resource> locations) {
8788
this.locations = locations;
8889
}
8990

91+
public void afterPropertiesSet() throws Exception {
92+
Assert.notEmpty(locations, "Locations list must not be empty");
93+
}
9094

9195
/**
9296
* Processes a resource request.

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class ResourceHttpRequestHandlerTests {
4242

4343
private ResourceHttpRequestHandler handler;
44-
44+
4545
@Before
4646
public void setUp() {
4747
List<Resource> resourcePaths = new ArrayList<Resource>();
@@ -52,7 +52,7 @@ public void setUp() {
5252
handler.setCacheSeconds(3600);
5353
handler.setServletContext(new TestServletContext());
5454
}
55-
55+
5656
@Test
5757
public void getResource() throws Exception {
5858
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -65,11 +65,11 @@ public void getResource() throws Exception {
6565
assertTrue(Long.valueOf(response.getHeader("Expires")) >= System.currentTimeMillis() - 1000 + (3600 * 1000));
6666
assertEquals("max-age=3600, must-revalidate", response.getHeader("Cache-Control"));
6767
assertTrue(response.containsHeader("Last-Modified"));
68-
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
68+
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
6969
new ClassPathResource("test/foo.css", getClass()).getFile().lastModified());
7070
assertEquals("h1 { color:red; }", response.getContentAsString());
7171
}
72-
72+
7373
@Test
7474
public void getResourceWithHtmlMediaType() throws Exception {
7575
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -81,10 +81,10 @@ public void getResourceWithHtmlMediaType() throws Exception {
8181
assertTrue(Long.valueOf(response.getHeader("Expires")) >= System.currentTimeMillis() - 1000 + (3600 * 1000));
8282
assertEquals("max-age=3600, must-revalidate", response.getHeader("Cache-Control"));
8383
assertTrue(response.containsHeader("Last-Modified"));
84-
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
84+
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
8585
new ClassPathResource("test/foo.html", getClass()).getFile().lastModified());
8686
}
87-
87+
8888
@Test
8989
public void getResourceFromAlternatePath() throws Exception {
9090
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -97,11 +97,11 @@ public void getResourceFromAlternatePath() throws Exception {
9797
assertTrue(Long.valueOf(response.getHeader("Expires")) >= System.currentTimeMillis() - 1000 + (3600 * 1000));
9898
assertEquals("max-age=3600, must-revalidate", response.getHeader("Cache-Control"));
9999
assertTrue(response.containsHeader("Last-Modified"));
100-
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
100+
assertEquals(Long.valueOf(response.getHeader("Last-Modified")).longValue(),
101101
new ClassPathResource("testalternatepath/baz.css", getClass()).getFile().lastModified());
102102
assertEquals("h1 { color:red; }", response.getContentAsString());
103103
}
104-
104+
105105
@Test
106106
public void getResourceFromSubDirectory() throws Exception {
107107
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -112,7 +112,7 @@ public void getResourceFromSubDirectory() throws Exception {
112112
assertEquals("text/javascript", response.getContentType());
113113
assertEquals("function foo() { console.log(\"hello world\"); }", response.getContentAsString());
114114
}
115-
115+
116116
@Test
117117
public void getResourceFromSubDirectoryOfAlternatePath() throws Exception {
118118
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -138,7 +138,7 @@ public void getResourceViaDirectoryTraversal() throws Exception {
138138
response = new MockHttpServletResponse();
139139
handler.handleRequest(request, response);
140140
assertEquals(404, response.getStatus());
141-
141+
142142
handler.setLocations(Arrays.<Resource>asList(new ClassPathResource("testsecret/", getClass())));
143143
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "secret.txt");
144144
response = new MockHttpServletResponse();
@@ -158,7 +158,7 @@ public void notModified() throws Exception {
158158
handler.handleRequest(request, response);
159159
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
160160
}
161-
161+
162162
@Test
163163
public void modified() throws Exception {
164164
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -169,9 +169,9 @@ public void modified() throws Exception {
169169
MockHttpServletResponse response = new MockHttpServletResponse();
170170
handler.handleRequest(request, response);
171171
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
172-
assertEquals("h1 { color:red; }", response.getContentAsString());
172+
assertEquals("h1 { color:red; }", response.getContentAsString());
173173
}
174-
174+
175175
@Test
176176
public void directory() throws Exception {
177177
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -191,15 +191,15 @@ public void missingResourcePath() throws Exception {
191191
handler.handleRequest(request, response);
192192
assertEquals(404, response.getStatus());
193193
}
194-
194+
195195
@Test(expected=IllegalStateException.class)
196196
public void noPathWithinHandlerMappingAttribute() throws Exception {
197197
MockHttpServletRequest request = new MockHttpServletRequest();
198198
request.setMethod("GET");
199199
MockHttpServletResponse response = new MockHttpServletResponse();
200200
handler.handleRequest(request, response);
201201
}
202-
202+
203203
@Test(expected=HttpRequestMethodNotSupportedException.class)
204204
public void unsupportedHttpMethod() throws Exception {
205205
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -208,7 +208,7 @@ public void unsupportedHttpMethod() throws Exception {
208208
MockHttpServletResponse response = new MockHttpServletResponse();
209209
handler.handleRequest(request, response);
210210
}
211-
211+
212212
@Test
213213
public void resourceNotFound() throws Exception {
214214
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -219,6 +219,11 @@ public void resourceNotFound() throws Exception {
219219
assertEquals(404, response.getStatus());
220220
}
221221

222+
@Test(expected=IllegalArgumentException.class)
223+
public void locationsNotSet() throws Exception {
224+
new ResourceHttpRequestHandler().afterPropertiesSet();
225+
}
226+
222227

223228
private static class TestServletContext extends MockServletContext {
224229

0 commit comments

Comments
 (0)