diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 25c9184e0fc..d89bb8fb8c2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -460,6 +460,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator try { StreamUtils.copy(in, response.getOutputStream()); } + catch (NullPointerException ex) { + // ignore, see SPR-13620 + } finally { try { in.close(); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index 176ac6bc041..bb5f502231a 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -478,6 +478,20 @@ public class ResourceHttpRequestHandlerTests { assertEquals(0, this.response.getContentLength()); } + // SPR-13620 + @Test + public void writeContentInputStreamThrowingNullPointerException() throws Exception { + Resource resource = mock(Resource.class); + InputStream in = mock(InputStream.class); + given(resource.getInputStream()).willReturn(in); + given(in.read(any())).willThrow(NullPointerException.class); + + this.handler.writeContent(this.response, resource); + + assertEquals(200, this.response.getStatus()); + assertEquals(0, this.response.getContentLength()); + } + private long dateHeaderAsLong(String responseHeaderName) throws Exception { return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();