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.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URI;
25
26 import org.apache.commons.httpclient.methods.GetMethod;
27 import org.joda.time.DateTime;
28 import org.opensaml.xml.util.DatatypeHelper;
29
30
31
32
33
34
35
36
37
38 public class FileBackedHttpResource extends HttpResource {
39
40
41 private File resourceFile;
42
43
44
45
46
47
48
49 public FileBackedHttpResource(String resource, String backingFile) {
50 super(resource);
51
52 if (DatatypeHelper.isEmpty(backingFile)) {
53 throw new IllegalArgumentException("Backing file path may not be null or empty");
54 }
55
56 resourceFile = new File(backingFile);
57 }
58
59
60
61
62
63
64
65
66
67 public FileBackedHttpResource(String resource, URI backingFile) {
68 super(resource);
69
70 if (backingFile == null) {
71 throw new IllegalArgumentException("Backing file path may not be null or empty");
72 }
73
74 resourceFile = new File(backingFile);
75 }
76
77
78
79
80
81
82
83
84 public FileBackedHttpResource(String resource, String backingFile, ResourceFilter resourceFilter) {
85 super(resource, resourceFilter);
86
87 if (DatatypeHelper.isEmpty(backingFile)) {
88 throw new IllegalArgumentException("Backing file path may not be null or empty");
89 }
90
91 resourceFile = new File(backingFile);
92 }
93
94
95
96
97
98
99
100
101
102
103 public FileBackedHttpResource(String resource, URI backingFile, ResourceFilter resourceFilter) {
104 super(resource, resourceFilter);
105
106 if (backingFile == null) {
107 throw new IllegalArgumentException("Backing file path may not be null or empty");
108 }
109
110 resourceFile = new File(backingFile);
111 }
112
113
114 public boolean exists() throws ResourceException {
115 if (!super.exists()) {
116 return resourceFile.exists();
117 }
118
119 return true;
120 }
121
122
123 public InputStream getInputStream() throws ResourceException {
124 InputStream ins = null;
125 try {
126 GetMethod getMethod = super.getResource();
127 byte[] response = getMethod.getResponseBody();
128 saveToResourceFile(response);
129 ins = getMethod.getResponseBodyAsStream();
130 } catch (Exception e) {
131 try {
132 ins = new FileInputStream(resourceFile);
133 } catch (IOException ioe) {
134 throw new ResourceException("Unable to read resource URL or backing file "
135 + resourceFile.getAbsolutePath(), ioe);
136 }
137 }
138
139 if (getResourceFilter() != null) {
140 return getResourceFilter().applyFilter(ins);
141 } else {
142 return ins;
143 }
144 }
145
146
147 public DateTime getLastModifiedTime() throws ResourceException {
148 try {
149 return super.getLastModifiedTime();
150 } catch (ResourceException e) {
151 long lastModifiedTime = resourceFile.lastModified();
152 if (lastModifiedTime == 0) {
153 throw new ResourceException("URL resource is not reachable and backing file is not readable");
154 }
155
156 return new DateTime(lastModifiedTime);
157 }
158 }
159
160
161 public String getLocation() {
162 return super.getLocation();
163 }
164
165
166
167
168
169
170
171
172 protected void saveToResourceFile(byte[] resource) throws ResourceException {
173 try {
174 FileOutputStream out = new FileOutputStream(resourceFile);
175 out.write(resource);
176 } catch (IOException e) {
177 throw new ResourceException("Unable to write resource to backing file " + resourceFile.getAbsolutePath(), e);
178 }
179 }
180 }