Skip to content

Commit b3f0199

Browse files
committed
Fix NPE in GzipResourceResolver
This change fixes a NullPointerException in GzipResourceResolver, which assumed that calls to the `resolveResource` method were made with only non-null values for request. This is not the case for the VersionResourceResolver, which tries to resolve resources that aren't requested per se by the HTTP request. Issue: SPR-13149
1 parent 8dd182f commit b3f0199

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ protected Resource resolveResourceInternal(HttpServletRequest request, String re
4848
List<? extends Resource> locations, ResourceResolverChain chain) {
4949

5050
Resource resource = chain.resolveResource(request, requestPath, locations);
51-
if ((resource == null) || !isGzipAccepted(request)) {
51+
if ((resource == null) || (request != null && !isGzipAccepted(request))) {
5252
return resource;
5353
}
5454

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,21 @@ public void resolveFromCacheWithEncodingVariants() throws IOException {
149149
assertFalse("Expected " + resolved + " to *not* be of type " + EncodedResource.class,
150150
resolved instanceof EncodedResource);
151151
}
152+
153+
// SPR-13149
154+
@Test
155+
public void resolveWithNullRequest() throws IOException {
156+
157+
String file = "js/foo.js";
158+
String gzFile = file+".gz";
159+
Resource gzResource = new ClassPathResource("test/"+gzFile, getClass());
160+
161+
// resolved resource is now cached in CachingResourceResolver
162+
Resource resolved = resolver.resolveResource(null, file, locations);
163+
164+
assertEquals(gzResource.getDescription(), resolved.getDescription());
165+
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
166+
assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class,
167+
resolved instanceof EncodedResource);
168+
}
152169
}

0 commit comments

Comments
 (0)