View Javadoc

1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.core.io;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URI;
23  import java.net.URL;
24  
25  /**
26   * Interface for a resource descriptor that abstracts from the actual
27   * type of underlying resource, such as a file or class path resource.
28   *
29   * <p>An InputStream can be opened for every resource if it exists in
30   * physical form, but a URL or File handle can just be returned for
31   * certain resources. The actual behavior is implementation-specific.
32   *
33   * @author Juergen Hoeller
34   * @since 28.12.2003
35   * @see #getInputStream()
36   * @see #getURL()
37   * @see #getURI()
38   * @see #getFile()
39   * @see WritableResource
40   * @see ContextResource
41   * @see FileSystemResource
42   * @see ClassPathResource
43   * @see UrlResource
44   * @see ByteArrayResource
45   * @see InputStreamResource
46   */
47  public interface Resource extends InputStreamSource {
48  
49  	/**
50  	 * Return whether this resource actually exists in physical form.
51  	 * <p>This method performs a definitive existence check, whereas the
52  	 * existence of a <code>Resource</code> handle only guarantees a
53  	 * valid descriptor handle.
54  	 */
55  	boolean exists();
56  
57  	/**
58  	 * Return whether the contents of this resource can be read,
59  	 * e.g. via {@link #getInputStream()} or {@link #getFile()}.
60  	 * <p>Will be <code>true</code> for typical resource descriptors;
61  	 * note that actual content reading may still fail when attempted.
62  	 * However, a value of <code>false</code> is a definitive indication
63  	 * that the resource content cannot be read.
64  	 * @see #getInputStream()
65  	 */
66  	boolean isReadable();
67  
68  	/**
69  	 * Return whether this resource represents a handle with an open
70  	 * stream. If true, the InputStream cannot be read multiple times,
71  	 * and must be read and closed to avoid resource leaks.
72  	 * <p>Will be <code>false</code> for typical resource descriptors.
73  	 */
74  	boolean isOpen();
75  
76  	/**
77  	 * Return a URL handle for this resource.
78  	 * @throws IOException if the resource cannot be resolved as URL,
79  	 * i.e. if the resource is not available as descriptor
80  	 */
81  	URL getURL() throws IOException;
82  
83  	/**
84  	 * Return a URI handle for this resource.
85  	 * @throws IOException if the resource cannot be resolved as URI,
86  	 * i.e. if the resource is not available as descriptor
87  	 */
88  	URI getURI() throws IOException;
89  
90  	/**
91  	 * Return a File handle for this resource.
92  	 * @throws IOException if the resource cannot be resolved as absolute
93  	 * file path, i.e. if the resource is not available in a file system
94  	 */
95  	File getFile() throws IOException;
96  
97  	/**
98  	 * Determine the content length for this resource.
99  	 * @throws IOException if the resource cannot be resolved
100 	 * (in the file system or as some other known physical resource type)
101 	 */
102 	long contentLength() throws IOException;
103 
104 	/**
105 	 * Determine the last-modified timestamp for this resource.
106 	 * @throws IOException if the resource cannot be resolved
107 	 * (in the file system or as some other known physical resource type)
108 	 */
109 	long lastModified() throws IOException;
110 
111 	/**
112 	 * Create a resource relative to this resource.
113 	 * @param relativePath the relative path (relative to this resource)
114 	 * @return the resource handle for the relative resource
115 	 * @throws IOException if the relative resource cannot be determined
116 	 */
117 	Resource createRelative(String relativePath) throws IOException;
118 
119 	/**
120 	 * Determine a filename for this resource, i.e. typically the last
121 	 * part of the path: for example, "myfile.txt".
122 	 * <p>Returns <code>null</code> if this type of resource does not
123 	 * have a filename.
124 	 */
125 	String getFilename();
126 
127 	/**
128 	 * Return a description for this resource,
129 	 * to be used for error output when working with the resource.
130 	 * <p>Implementations are also encouraged to return this value
131 	 * from their <code>toString</code> method.
132 	 * @see java.lang.Object#toString()
133 	 */
134 	String getDescription();
135 
136 }