View Javadoc

1   /*
2    * Copyright 2006 University Corporation for Advanced Internet Development, Inc.
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.opensaml.util.resource;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.InputStream;
23  import java.net.URI;
24  
25  import org.joda.time.DateTime;
26  import org.opensaml.xml.util.DatatypeHelper;
27  
28  /**
29   * A resource representing a file on the local filesystem.
30   */
31  public class FilesystemResource extends AbstractFilteredResource {
32  
33      /** The file represented by this resource. */
34      private File resource;
35  
36      /**
37       * Constructor.
38       * 
39       * @param resourcePath the path to the file for this resource
40       * 
41       * @throws ResourceException thrown if the resource path is null or empty
42       */
43      public FilesystemResource(String resourcePath) throws ResourceException {
44          super();
45  
46          if (DatatypeHelper.isEmpty(resourcePath)) {
47              throw new ResourceException("Resource path may not be null or empty");
48          }
49  
50          resource = new File(resourcePath);
51      }
52  
53      /**
54       * Constructor.
55       * 
56       * @param resourceURI file: URI to the file
57       * 
58       * @throws ResourceException thrown if the resource path is null or empty
59       * 
60       * @since 1.2.0
61       */
62      public FilesystemResource(URI resourceURI) throws ResourceException {
63          super();
64  
65          if (resourceURI == null) {
66              throw new ResourceException("Resource URL may not be null");
67          }
68  
69          resource = new File(resourceURI);
70      }
71  
72      /**
73       * Constructor.
74       * 
75       * @param resourcePath the path to the file for this resource
76       * @param resourceFilter filter to apply to this resource
77       * 
78       * @throws ResourceException thrown if the resource path is null or empty
79       * 
80       * @deprecated use {@link #setResourceFilter(ResourceFilter)} instead
81       */
82      public FilesystemResource(String resourcePath, ResourceFilter resourceFilter) throws ResourceException {
83          super(resourceFilter);
84  
85          if (DatatypeHelper.isEmpty(resourcePath)) {
86              throw new ResourceException("Resource path may not be null or empty");
87          }
88  
89          resource = new File(resourcePath);
90      }
91  
92      /**
93       * Constructor.
94       * 
95       * @param resourceURI the file: URI to the file for this resource
96       * @param resourceFilter filter to apply to this resource
97       * 
98       * @throws ResourceException thrown if the resource path is null or empty
99       * 
100      * @since 1.2.0
101      * @deprecated use {@link #setResourceFilter(ResourceFilter)} instead
102      */
103     public FilesystemResource(URI resourceURI, ResourceFilter resourceFilter) throws ResourceException {
104         super(resourceFilter);
105 
106         if (resourceURI == null) {
107             throw new ResourceException("Resource URI may not be null");
108         }
109 
110         resource = new File(resourceURI);
111     }
112 
113     /** {@inheritDoc} */
114     public boolean exists() throws ResourceException {
115         return resource.exists();
116     }
117 
118     /** {@inheritDoc} */
119     public InputStream getInputStream() throws ResourceException {
120         try {
121             FileInputStream ins = new FileInputStream(resource);
122             return applyFilter(ins);
123         } catch (FileNotFoundException e) {
124             throw new ResourceException("Resource file does not exist: " + resource.getAbsolutePath());
125         }
126     }
127 
128     /** {@inheritDoc} */
129     public DateTime getLastModifiedTime() throws ResourceException {
130         if (!resource.exists()) {
131             throw new ResourceException("Resource file does not exist: " + resource.getAbsolutePath());
132         }
133 
134         return new DateTime(resource.lastModified());
135     }
136 
137     /** {@inheritDoc} */
138     public String getLocation() {
139         return resource.getAbsolutePath();
140     }
141 
142     /** {@inheritDoc} */
143     public String toString() {
144         return getLocation();
145     }
146 
147     /** {@inheritDoc} */
148     public int hashCode() {
149         return getLocation().hashCode();
150     }
151 
152     /** {@inheritDoc} */
153     public boolean equals(Object o) {
154         if (o == this) {
155             return true;
156         }
157 
158         if (o instanceof FilesystemResource) {
159             return getLocation().equals(((FilesystemResource) o).getLocation());
160         }
161 
162         return false;
163     }
164 }