From 9ada55dc6bd75059a8790193a137840ad8d20fec Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 22 Jun 2015 11:22:23 +0200 Subject: [PATCH] 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 --- .../servlet/resource/GzipResourceResolver.java | 4 ++-- .../resource/GzipResourceResolverTests.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/GzipResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/GzipResourceResolver.java index 08cc241fa2..dadde77f4c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/GzipResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/GzipResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -48,7 +48,7 @@ public class GzipResourceResolver extends AbstractResourceResolver { List locations, ResourceResolverChain chain) { Resource resource = chain.resolveResource(request, requestPath, locations); - if ((resource == null) || !isGzipAccepted(request)) { + if ((resource == null) || (request != null && !isGzipAccepted(request))) { return resource; } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipResourceResolverTests.java index 7dd3322d9a..78b86d2906 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipResourceResolverTests.java @@ -149,4 +149,21 @@ public class GzipResourceResolverTests { assertFalse("Expected " + resolved + " to *not* be of type " + EncodedResource.class, resolved instanceof EncodedResource); } + + // SPR-13149 + @Test + public void resolveWithNullRequest() throws IOException { + + String file = "js/foo.js"; + String gzFile = file+".gz"; + Resource gzResource = new ClassPathResource("test/"+gzFile, getClass()); + + // resolved resource is now cached in CachingResourceResolver + Resource resolved = resolver.resolveResource(null, file, locations); + + assertEquals(gzResource.getDescription(), resolved.getDescription()); + assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename()); + assertTrue("Expected " + resolved + " to be of type " + EncodedResource.class, + resolved instanceof EncodedResource); + } } -- GitLab