Skip to content

Commit

Permalink
Resource.isFile() and JAF MediaTypeFactory
Browse files Browse the repository at this point in the history
Issue: SPR-14484
  • Loading branch information
jhoeller committed Jul 19, 2016
1 parent 7287bae commit 8bb34bc
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 262 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,6 +38,20 @@
*/
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.
Expand Down Expand Up @@ -72,7 +86,25 @@ protected File getFileForLastModifiedCheck() throws IOException {
}

/**
* 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)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public boolean isOpen() {
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -85,7 +85,6 @@ public final String getPath() {
return this.path;
}


/**
* This implementation returns whether the underlying file exists.
* @see java.io.File#exists()
Expand Down Expand Up @@ -153,6 +152,14 @@ public URI getURI() throws IOException {
return this.file.toURI();
}

/**
* This implementation always indicates a file.
*/
@Override
public boolean isFile() {
return true;
}

/**
* This implementation returns the underlying File reference.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ public URI getURI() throws IOException {
return this.path.toUri();
}

/**
* This implementation always indicates a file.
*/
@Override
public boolean isFile() {
return true;
}

/**
* This implementation returns the underlying File reference.
*/
Expand All @@ -180,8 +188,8 @@ public File getFile() throws IOException {
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");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,31 +47,47 @@
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.
*/
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.
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -198,6 +199,16 @@ public URI getURI() throws IOException {
}
}

@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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,16 +30,18 @@
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
* that the resource content cannot be modified.
* @see #getOutputStream()
* @see #isReadable()
*/
boolean isWritable();
default boolean isWritable() {
return true;
}

/**
* Return an {@link OutputStream} for the underlying resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,13 +26,8 @@
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;

/**
Expand All @@ -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. "&#42;/&#42;").
*/
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -322,23 +284,6 @@ public static List<MimeType> parseMimeTypes(String mimeTypes) {
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
Expand Down

0 comments on commit 8bb34bc

Please sign in to comment.