Resource.isFile() and JAF MediaTypeFactory
Issue: SPR-14484
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -38,6 +38,20 @@ import org.springframework.util.ResourceUtils;
|
||||
*/
|
||||
public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
try {
|
||||
URL url = getURL();
|
||||
if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
|
||||
return VfsResourceDelegate.getResource(url).isFile();
|
||||
}
|
||||
return ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns a File reference for the underlying class path
|
||||
* resource, provided that it refers to a file in the file system.
|
||||
@@ -72,7 +86,25 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns a File reference for the underlying class path
|
||||
* This implementation returns a File reference for the given URI-identified
|
||||
* resource, provided that it refers to a file in the file system.
|
||||
* @since 5.0
|
||||
* @see #getFile(URI)
|
||||
*/
|
||||
protected boolean isFile(URI uri) {
|
||||
try {
|
||||
if (uri.getScheme().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
|
||||
return VfsResourceDelegate.getResource(uri).isFile();
|
||||
}
|
||||
return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns a File reference for the given URI-identified
|
||||
* resource, provided that it refers to a file in the file system.
|
||||
* @see org.springframework.util.ResourceUtils#getFile(java.net.URI, String)
|
||||
*/
|
||||
|
||||
@@ -81,6 +81,14 @@ public abstract class AbstractResource implements Resource {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation always returns {@code false}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation throws a FileNotFoundException, assuming
|
||||
* that the resource cannot be resolved to a URL.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -85,7 +85,6 @@ public class FileSystemResource extends AbstractResource implements WritableReso
|
||||
return this.path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This implementation returns whether the underlying file exists.
|
||||
* @see java.io.File#exists()
|
||||
@@ -153,6 +152,14 @@ public class FileSystemResource extends AbstractResource implements WritableReso
|
||||
return this.file.toURI();
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation always indicates a file.
|
||||
*/
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns the underlying File reference.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -49,7 +49,6 @@ public interface InputStreamSource {
|
||||
* that each {@code getInputStream()} call returns a fresh stream.
|
||||
* @return the input stream for the underlying resource (must not be {@code null})
|
||||
* @throws IOException if the stream could not be opened
|
||||
* @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
|
||||
*/
|
||||
InputStream getInputStream() throws IOException;
|
||||
|
||||
|
||||
@@ -171,6 +171,14 @@ public class PathResource extends AbstractResource implements WritableResource {
|
||||
return this.path.toUri();
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation always indicates a file.
|
||||
*/
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns the underlying File reference.
|
||||
*/
|
||||
@@ -180,8 +188,8 @@ public class PathResource extends AbstractResource implements WritableResource {
|
||||
return this.path.toFile();
|
||||
}
|
||||
catch (UnsupportedOperationException ex) {
|
||||
// only Paths on the default file system can be converted to a File
|
||||
// do exception translation for cases where conversion is not possible
|
||||
// Only paths on the default file system can be converted to a File:
|
||||
// Do exception translation for cases where conversion is not possible.
|
||||
throw new FileNotFoundException(this.path + " cannot be resolved to " + "absolute file path");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -47,7 +47,7 @@ import java.net.URL;
|
||||
public interface Resource extends InputStreamSource {
|
||||
|
||||
/**
|
||||
* Return whether this resource actually exists in physical form.
|
||||
* Determine whether this resource actually exists in physical form.
|
||||
* <p>This method performs a definitive existence check, whereas the
|
||||
* existence of a {@code Resource} handle only guarantees a
|
||||
* valid descriptor handle.
|
||||
@@ -55,23 +55,39 @@ public interface Resource extends InputStreamSource {
|
||||
boolean exists();
|
||||
|
||||
/**
|
||||
* Return whether the contents of this resource can be read,
|
||||
* e.g. via {@link #getInputStream()} or {@link #getFile()}.
|
||||
* Indicate whether the contents of this resource can be read via
|
||||
* {@link #getInputStream()}.
|
||||
* <p>Will be {@code true} for typical resource descriptors;
|
||||
* note that actual content reading may still fail when attempted.
|
||||
* However, a value of {@code false} is a definitive indication
|
||||
* that the resource content cannot be read.
|
||||
* @see #getInputStream()
|
||||
*/
|
||||
boolean isReadable();
|
||||
default boolean isReadable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether this resource represents a handle with an open
|
||||
* stream. If true, the InputStream cannot be read multiple times,
|
||||
* Indicate whether this resource represents a handle with an open stream.
|
||||
* If {@code true}, the InputStream cannot be read multiple times,
|
||||
* and must be read and closed to avoid resource leaks.
|
||||
* <p>Will be {@code false} for typical resource descriptors.
|
||||
*/
|
||||
boolean isOpen();
|
||||
default boolean isOpen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether this resource represents a file in a file system.
|
||||
* A value of {@code true} strongly suggests (but does not guarantee)
|
||||
* that a {@link #getFile()} call will succeed.
|
||||
* <p>This is conservatively {@code false} by default.
|
||||
* @since 5.0
|
||||
* @see #getFile()
|
||||
*/
|
||||
default boolean isFile() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a URL handle for this resource.
|
||||
@@ -84,6 +100,7 @@ public interface Resource extends InputStreamSource {
|
||||
* Return a URI handle for this resource.
|
||||
* @throws IOException if the resource cannot be resolved as URI,
|
||||
* i.e. if the resource is not available as descriptor
|
||||
* @since 2.5
|
||||
*/
|
||||
URI getURI() throws IOException;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -61,6 +61,7 @@ public class UrlResource extends AbstractFileResolvingResource {
|
||||
* Create a new {@code UrlResource} based on the given URI object.
|
||||
* @param uri a URI
|
||||
* @throws MalformedURLException if the given URL path is not valid
|
||||
* @since 2.5
|
||||
*/
|
||||
public UrlResource(URI uri) throws MalformedURLException {
|
||||
Assert.notNull(uri, "URI must not be null");
|
||||
@@ -198,6 +199,16 @@ public class UrlResource extends AbstractFileResolvingResource {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
if (this.uri != null) {
|
||||
return super.isFile(this.uri);
|
||||
}
|
||||
else {
|
||||
return super.isFile();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation returns a File reference for the underlying URL/URI,
|
||||
* provided that it refers to a file in the file system.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -30,8 +30,8 @@ import java.io.OutputStream;
|
||||
public interface WritableResource extends Resource {
|
||||
|
||||
/**
|
||||
* Return whether the contents of this resource can be modified,
|
||||
* e.g. via {@link #getOutputStream()} or {@link #getFile()}.
|
||||
* Indicate whether the contents of this resource can be written
|
||||
* via {@link #getOutputStream()}.
|
||||
* <p>Will be {@code true} for typical resource descriptors;
|
||||
* note that actual content writing may still fail when attempted.
|
||||
* However, a value of {@code false} is a definitive indication
|
||||
@@ -39,7 +39,9 @@ public interface WritableResource extends Resource {
|
||||
* @see #getOutputStream()
|
||||
* @see #isReadable()
|
||||
*/
|
||||
boolean isWritable();
|
||||
default boolean isWritable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an {@link OutputStream} for the underlying resource,
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.ArrayList;
|
||||
@@ -28,13 +26,8 @@ import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import javax.activation.FileTypeMap;
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.MimeType.SpecificityComparator;
|
||||
|
||||
/**
|
||||
@@ -56,12 +49,6 @@ public abstract class MimeTypeUtils {
|
||||
|
||||
private static Charset US_ASCII = Charset.forName("US-ASCII");
|
||||
|
||||
private static final FileTypeMap fileTypeMap;
|
||||
|
||||
static {
|
||||
fileTypeMap = initFileTypeMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Public constant mime type that includes all media ranges (i.e. "*/*").
|
||||
*/
|
||||
@@ -220,31 +207,6 @@ public abstract class MimeTypeUtils {
|
||||
TEXT_XML = MimeType.valueOf(TEXT_XML_VALUE);
|
||||
}
|
||||
|
||||
private static FileTypeMap initFileTypeMap() {
|
||||
// See if we can find the extended mime.types from the context-support module...
|
||||
Resource mappingLocation = new ClassPathResource("org/springframework/mail/javamail/mime.types");
|
||||
if (mappingLocation.exists()) {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = mappingLocation.getInputStream();
|
||||
return new MimetypesFileTypeMap(inputStream);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return FileTypeMap.getDefaultFileTypeMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given String into a single {@code MimeType}.
|
||||
@@ -322,23 +284,6 @@ public abstract class MimeTypeUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code MimeType} of the given file name, using the Java Activation
|
||||
* Framework.
|
||||
* @param filename the filename whose mime type is to be found
|
||||
* @return the mime type, if any
|
||||
* @since 5.0
|
||||
*/
|
||||
public static Optional<MimeType> getMimeType(String filename) {
|
||||
if (filename != null) {
|
||||
String mimeType = fileTypeMap.getContentType(filename);
|
||||
if (StringUtils.hasText(mimeType)) {
|
||||
return Optional.of(parseMimeType(mimeType));
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string representation of the given list of {@code MimeType} objects.
|
||||
* @param mimeTypes the string to parse
|
||||
|
||||
@@ -18,18 +18,12 @@ package org.springframework.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.DescriptiveResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Utility methods for resolving resource locations to files in the
|
||||
* file system. Mainly for internal use within the framework.
|
||||
@@ -235,6 +229,7 @@ public abstract class ResourceUtils {
|
||||
* @return a corresponding File object
|
||||
* @throws FileNotFoundException if the URL cannot be resolved to
|
||||
* a file in the file system
|
||||
* @since 2.5
|
||||
*/
|
||||
public static File getFile(URI resourceUri) throws FileNotFoundException {
|
||||
return getFile(resourceUri, "URI");
|
||||
@@ -249,6 +244,7 @@ public abstract class ResourceUtils {
|
||||
* @return a corresponding File object
|
||||
* @throws FileNotFoundException if the URL cannot be resolved to
|
||||
* a file in the file system
|
||||
* @since 2.5
|
||||
*/
|
||||
public static File getFile(URI resourceUri, String description) throws FileNotFoundException {
|
||||
Assert.notNull(resourceUri, "Resource URI must not be null");
|
||||
@@ -296,32 +292,6 @@ public abstract class ResourceUtils {
|
||||
url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION));
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the given resource has a file, so that {@link
|
||||
* Resource#getFile()}
|
||||
* can be called without an {@link java.io.IOException}.
|
||||
* @param resource the resource to check
|
||||
* @return {@code true} if the given resource has a file; {@code false} otherwise
|
||||
* @since 5.0
|
||||
*/
|
||||
public static boolean hasFile(Resource resource) {
|
||||
Assert.notNull(resource, "'resource' must not be null");
|
||||
|
||||
// the following Resource implementations do not support getURI/getFile
|
||||
if (resource instanceof ByteArrayResource ||
|
||||
resource instanceof DescriptiveResource ||
|
||||
resource instanceof InputStreamResource) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
URI resourceUri = resource.getURI();
|
||||
return URL_PROTOCOL_FILE.equals(resourceUri.getScheme());
|
||||
}
|
||||
catch (IOException ignored) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the URL for the actual jar file from the given URL
|
||||
* (which may point to a resource in a jar file or to a jar file itself).
|
||||
|
||||
@@ -75,10 +75,10 @@ public class CollectionToCollectionConverterTests {
|
||||
conversionService.addConverterFactory(new StringToNumberConverterFactory());
|
||||
assertTrue(conversionService.canConvert(sourceType, targetType));
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
|
||||
List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
|
||||
assertFalse(list.equals(result));
|
||||
assertEquals(9, result.get(0));
|
||||
assertEquals(37, result.get(1));
|
||||
assertEquals(9, result.get(0).intValue());
|
||||
assertEquals(37, result.get(1).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -303,6 +303,11 @@ public class CollectionToCollectionConverterTests {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFile() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() throws IOException {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user