Consistently throw FileNotFoundException even for NIO access

Issue: SPR-16334
This commit is contained in:
Juergen Hoeller
2018-01-07 23:18:28 +01:00
parent 37f0e8c6e5
commit 1a154c3e4c
6 changed files with 186 additions and 130 deletions

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import org.springframework.util.ResourceUtils;
@@ -42,6 +43,68 @@ import org.springframework.util.ResourceUtils;
*/
public abstract class AbstractFileResolvingResource extends AbstractResource {
@Override
public boolean exists() {
try {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution
return getFile().exists();
}
else {
// Try a URL connection content-length header
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon =
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) {
int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
return true;
}
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
}
if (con.getContentLength() >= 0) {
return true;
}
if (httpCon != null) {
// no HTTP OK status, and no content-length header: give up
httpCon.disconnect();
return false;
}
else {
// Fall back to stream existence: can we open the stream?
InputStream is = getInputStream();
is.close();
return true;
}
}
}
catch (IOException ex) {
return false;
}
}
@Override
public boolean isReadable() {
try {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution
File file = getFile();
return (file.canRead() && !file.isDirectory());
}
else {
return true;
}
}
catch (IOException ex) {
return false;
}
}
@Override
public boolean isFile() {
try {
@@ -123,81 +186,20 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
* This implementation returns a FileChannel for the given URI-identified
* resource, provided that it refers to a file in the file system.
* @since 5.0
* @see #getFile(URI)
* @see #getFile()
*/
@Override
public ReadableByteChannel readableChannel() throws IOException {
if (isFile()) {
try {
// Try file system channel
return FileChannel.open(getFile().toPath(), StandardOpenOption.READ);
}
else {
catch (FileNotFoundException | NoSuchFileException ex) {
// Fall back to InputStream adaptation in superclass
return super.readableChannel();
}
}
@Override
public boolean exists() {
try {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution
return getFile().exists();
}
else {
// Try a URL connection content-length header
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon =
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) {
int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
return true;
}
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
}
if (con.getContentLength() >= 0) {
return true;
}
if (httpCon != null) {
// no HTTP OK status, and no content-length header: give up
httpCon.disconnect();
return false;
}
else {
// Fall back to stream existence: can we open the stream?
InputStream is = getInputStream();
is.close();
return true;
}
}
}
catch (IOException ex) {
return false;
}
}
@Override
public boolean isReadable() {
try {
URL url = getURL();
if (ResourceUtils.isFileURL(url)) {
// Proceed with file system resolution
File file = getFile();
return (file.canRead() && !file.isDirectory());
}
else {
return true;
}
}
catch (IOException ex) {
return false;
}
}
@Override
public long contentLength() throws IOException {
URL url = getURL();
@@ -231,7 +233,6 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
return con.getLastModified();
}
/**
* Customize the given {@link URLConnection}, obtained in the course of an
* {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.core.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -26,6 +27,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import org.springframework.util.Assert;
@@ -115,12 +117,17 @@ public class FileSystemResource extends AbstractResource implements WritableReso
}
/**
* This implementation opens a FileInputStream for the underlying file.
* This implementation opens a NIO file stream for the underlying file.
* @see java.io.FileInputStream
*/
@Override
public InputStream getInputStream() throws IOException {
return Files.newInputStream(this.file.toPath());
try {
return Files.newInputStream(this.file.toPath());
}
catch (NoSuchFileException ex) {
throw new FileNotFoundException(ex.getMessage());
}
}
/**
@@ -183,7 +190,12 @@ public class FileSystemResource extends AbstractResource implements WritableReso
*/
@Override
public ReadableByteChannel readableChannel() throws IOException {
return FileChannel.open(this.file.toPath(), StandardOpenOption.READ);
try {
return FileChannel.open(this.file.toPath(), StandardOpenOption.READ);
}
catch (NoSuchFileException ex) {
throw new FileNotFoundException(ex.getMessage());
}
}
/**

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -26,11 +26,13 @@ import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.springframework.lang.Nullable;
import org.springframework.util.ResourceUtils;
/**
* Subclass of {@link UrlResource} which assumes file resolution, to the degree
* of implementing the {@link WritableResource} interface for it.
* of implementing the {@link WritableResource} interface for it. This resource
* variant also caches resolved {@link File} handles from {@link #getFile()}.
*
* <p>This is the class resolved by {@link DefaultResourceLoader} for a "file:..."
* URL location, allowing a downcast to {@link WritableResource} for it.
@@ -44,6 +46,10 @@ import org.springframework.util.ResourceUtils;
*/
public class FileUrlResource extends UrlResource implements WritableResource {
@Nullable
private volatile File file;
/**
* Create a new {@code FileUrlResource} based on the given URL object.
* <p>Note that this does not enforce "file" as URL protocol. If a protocol
@@ -71,11 +77,14 @@ public class FileUrlResource extends UrlResource implements WritableResource {
@Override
public Resource createRelative(String relativePath) throws MalformedURLException {
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
public File getFile() throws IOException {
File file = this.file;
if (file != null) {
return file;
}
return new FileUrlResource(new URL(getURL(), relativePath));
file = super.getFile();
this.file = file;
return file;
}
@Override
@@ -106,4 +115,12 @@ public class FileUrlResource extends UrlResource implements WritableResource {
return FileChannel.open(getFile().toPath(), StandardOpenOption.WRITE);
}
@Override
public Resource createRelative(String relativePath) throws MalformedURLException {
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
}
return new FileUrlResource(new URL(getURL(), relativePath));
}
}

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -202,7 +203,12 @@ public class PathResource extends AbstractResource implements WritableResource {
*/
@Override
public ReadableByteChannel readableChannel() throws IOException {
return Files.newByteChannel(this.path, StandardOpenOption.READ);
try {
return Files.newByteChannel(this.path, StandardOpenOption.READ);
}
catch (NoSuchFileException ex) {
throw new FileNotFoundException(ex.getMessage());
}
}
/**
@@ -215,7 +221,7 @@ public class PathResource extends AbstractResource implements WritableResource {
}
/**
* This implementation returns the underlying File's length.
* This implementation returns the underlying file's length.
*/
@Override
public long contentLength() throws IOException {