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