1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31 public class FilesystemResource extends AbstractFilteredResource {
32
33
34 private File resource;
35
36
37
38
39
40
41
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
55
56
57
58
59
60
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
74
75
76
77
78
79
80 public FilesystemResource(String resourcePath, ResourceFilter resourceFilter) throws ResourceException {
81 super(resourceFilter);
82
83 if (DatatypeHelper.isEmpty(resourcePath)) {
84 throw new ResourceException("Resource path may not be null or empty");
85 }
86
87 resource = new File(resourcePath);
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public FilesystemResource(URI resourceURI, ResourceFilter resourceFilter) throws ResourceException {
101 super(resourceFilter);
102
103 if (resourceURI == null) {
104 throw new ResourceException("Resource URI may not be null");
105 }
106
107 resource = new File(resourceURI);
108 }
109
110
111 public boolean exists() throws ResourceException {
112 return resource.exists();
113 }
114
115
116 public InputStream getInputStream() throws ResourceException {
117 try {
118 FileInputStream ins = new FileInputStream(resource);
119 if (getResourceFilter() != null) {
120 return getResourceFilter().applyFilter(ins);
121 } else {
122 return ins;
123 }
124 } catch (FileNotFoundException e) {
125 throw new ResourceException("Resource file does not exist: " + resource.getAbsolutePath());
126 }
127 }
128
129
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());
136 }
137
138
139 public String getLocation() {
140 return resource.getAbsolutePath();
141 }
142
143
144 public String toString() {
145 return getLocation();
146 }
147
148
149 public int hashCode() {
150 return getLocation().hashCode();
151 }
152
153
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 }