Browse Source

Resource.lastModified() propagates 0 value if target resource exists

Includes consistent use of getContentLengthLong over getContentLength.

Issue: SPR-17320
pull/1998/head
Juergen Hoeller 8 years ago
parent
commit
ff0afcff06
  1. 10
      spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java
  2. 14
      spring-core/src/main/java/org/springframework/core/io/AbstractResource.java

10
spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

@ -19,7 +19,6 @@ package org.springframework.core.io;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
@ -66,18 +65,17 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
return false; return false;
} }
} }
if (con.getContentLength() >= 0) { if (con.getContentLengthLong() >= 0) {
return true; return true;
} }
if (httpCon != null) { if (httpCon != null) {
// no HTTP OK status, and no content-length header: give up // No HTTP OK status, and no content-length header: give up
httpCon.disconnect(); httpCon.disconnect();
return false; return false;
} }
else { else {
// Fall back to stream existence: can we open the stream? // Fall back to stream existence: can we open the stream?
InputStream is = getInputStream(); getInputStream().close();
is.close();
return true; return true;
} }
} }
@ -211,7 +209,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
// Try a URL connection content-length header // Try a URL connection content-length header
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
customizeConnection(con); customizeConnection(con);
return con.getContentLength(); return con.getContentLengthLong();
} }
} }

14
spring-core/src/main/java/org/springframework/core/io/AbstractResource.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -57,8 +57,7 @@ public abstract class AbstractResource implements Resource {
catch (IOException ex) { catch (IOException ex) {
// Fall back to stream existence: can we open the stream? // Fall back to stream existence: can we open the stream?
try { try {
InputStream is = getInputStream(); getInputStream().close();
is.close();
return true; return true;
} }
catch (Throwable isEx) { catch (Throwable isEx) {
@ -146,7 +145,7 @@ public abstract class AbstractResource implements Resource {
InputStream is = getInputStream(); InputStream is = getInputStream();
try { try {
long size = 0; long size = 0;
byte[] buf = new byte[255]; byte[] buf = new byte[256];
int read; int read;
while ((read = is.read(buf)) != -1) { while ((read = is.read(buf)) != -1) {
size += read; size += read;
@ -169,10 +168,11 @@ public abstract class AbstractResource implements Resource {
*/ */
@Override @Override
public long lastModified() throws IOException { public long lastModified() throws IOException {
long lastModified = getFileForLastModifiedCheck().lastModified(); File fileToCheck = getFileForLastModifiedCheck();
if (lastModified == 0L) { long lastModified = fileToCheck.lastModified();
if (lastModified == 0L && !fileToCheck.exists()) {
throw new FileNotFoundException(getDescription() + throw new FileNotFoundException(getDescription() +
" cannot be resolved in the file system for resolving its last-modified timestamp"); " cannot be resolved in the file system for checking its last-modified timestamp");
} }
return lastModified; return lastModified;
} }

Loading…
Cancel
Save