forked from spdx/Spdx-Java-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadCache.java
More file actions
477 lines (441 loc) · 22.2 KB
/
Copy pathDownloadCache.java
File metadata and controls
477 lines (441 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* SPDX-FileCopyrightText: Copyright (c) 2023 Peter Monks
* SPDX-FileType: SOURCE
* SPDX-License-Identifier: Apache-2.0
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spdx.utility;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spdx.Configuration;
/**
* Flexible download cache for the rest of the library
* <p>
* This singleton class provides a flexible download cache for the rest of the library. If enabled, URLs that are
* requested using this class will have their content automatically cached locally on disk (in a directory that adheres
* to the XDG Base Directory Specification - <a href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">...</a>),
* and any subsequent requests will be served out of that cache. Cache entries will also be automatically checked every
* so often for staleness using HTTP ETag requests (which are more efficient than full HTTP requests). The interval
* between such checks is configurable (and can even be turned off, which makes every download request re-check the URL
* for staleness).
* <p>
* The cache is configured via these Configuration options:
* * org.spdx.storage.listedlicense.enableCache:
* Controls whether the cache is enabled or not. Defaults to false i.e. the cache is disabled.
* * org.spdx.storage.listedlicense.cacheCheckIntervalSecs:
* How many seconds should the cache wait between issuing ETag requests to determine whether cached content is
* stale? Defaults to 86,400 seconds (24 hours).
*
* @author Gary O'Neall
*/
public final class DownloadCache {
private static final Logger logger = LoggerFactory.getLogger(DownloadCache.class);
private static final int READ_TIMEOUT = 5000;
private static final int IO_BUFFER_SIZE = 8192;
private static final long DEFAULT_CACHE_CHECK_INTERVAL_SECS = 86400; // 24 hours, in seconds
static final List<String> WHITE_LIST = Collections.unmodifiableList(Arrays.asList(
"spdx.org", "spdx.dev", "spdx.com", "spdx.info")); // Allowed host names for the SPDX listed licenses
private static DownloadCache singleton;
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
private final String cacheDir = ((System.getenv("XDG_CACHE_HOME") == null ||
System.getenv("XDG_CACHE_HOME").trim().isEmpty()) ?
System.getProperty("user.home") + File.separator + ".cache" :
System.getenv("XDG_CACHE_HOME")) +
File.separator + "Spdx-Java-Library";
private static final String CONFIG_PROPERTY_CACHE_ENABLED = "org.spdx.downloadCacheEnabled";
private static final String CONFIG_PROPERTY_CACHE_CHECK_INTERVAL_SECS = "org.spdx.downloadCacheCheckIntervalSecs";
private final boolean cacheEnabled;
private final long cacheCheckIntervalSecs;
private final DateTimeFormatter iso8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000'Z'").withZone(ZoneOffset.UTC);
/**
* This class is a singleton - use getInstance() to obtain the instance.
*/
private DownloadCache() {
boolean tmpCacheEnabled = Boolean.parseBoolean(Configuration.getInstance().getProperty(CONFIG_PROPERTY_CACHE_ENABLED, "false"));
if (tmpCacheEnabled) {
try {
final File cacheDirectory = new File(cacheDir);
Files.createDirectories(cacheDirectory.toPath());
} catch (IOException ioe) {
logger.warn("Unable to create cache directory '{}'; continuing with cache disabled.", cacheDir, ioe);
tmpCacheEnabled = false;
}
}
cacheEnabled = tmpCacheEnabled;
long tmpCacheCheckIntervalSecs = DEFAULT_CACHE_CHECK_INTERVAL_SECS;
try {
tmpCacheCheckIntervalSecs = Long.parseLong(Configuration.getInstance().getProperty(CONFIG_PROPERTY_CACHE_CHECK_INTERVAL_SECS));
} catch(NumberFormatException nfe) {
// Ignore parse failures - in this case we use the default value of 24 hours
}
cacheCheckIntervalSecs = tmpCacheCheckIntervalSecs;
}
/**
* @return The singleton instance of the DownloadCache class.
*/
public static DownloadCache getInstance() {
if (singleton == null) {
singleton = new DownloadCache();
}
return singleton;
}
/**
* Recursively removes a directory. USE WITH CAUTION!
*
* @param dir The directory to delete.
* @throws IOException on IO error
*/
private static void rmdir(final File dir) throws IOException {
if (Objects.isNull(dir) || !dir.exists()) {
return;
}
File[] contents = dir.listFiles();
if (Objects.nonNull(contents)) {
for (final File f : contents) {
rmdir(f);
}
}
Files.delete(dir.toPath());
}
/**
* Resets (deletes) the local cache
* @throws IOException on IO error
*/
public void resetCache() throws IOException {
final File cacheDirectory = new File(cacheDir);
rmdir(cacheDirectory);
Files.createDirectories(cacheDirectory.toPath());
}
/**
* @param url The URL to get an input stream for. Notes: redirects issued by this url are restricted to known
* SPDX hosts; redirects to other hosts will cause an IOException to be thrown. This method may
* synchronize on this argument, so callers should avoid using it for synchronization.
* @return An InputStream for url, or null if url is null. Note that this InputStream may be of different concrete
* types, depending on whether the content is being served out of cache or not.
* @throws IOException When an IO error of some kind occurs.
*/
public InputStream getUrlInputStream(final URL url) throws IOException {
return getUrlInputStream(url, true);
}
/**
* @param url The URL to get an input stream for. Note: this method may synchronize on this argument, so callers
* should avoid using it for synchronization.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @return An InputStream for url, or null if url is null. Note that this InputStream may be of different concrete
* types, depending on whether the content is being served out of cache or not.
* @throws IOException When an IO error of some kind occurs.
*/
public InputStream getUrlInputStream(final URL url, final boolean restrictRedirects) throws IOException {
InputStream result = null;
if (url != null) {
if (cacheEnabled) {
// Per-URL critical section to prevent cache stampede
synchronized (url) {
result = getUrlInputStreamThroughCache(url, restrictRedirects);
}
} else {
result = getUrlInputStreamDirect(url, restrictRedirects);
}
}
return result;
}
/**
* @param url The URL to get an input stream for, ignoring the local cache.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @return An InputStream for url, or null if url is null.
* @throws IOException When an IO error of some kind occurs.
*/
private InputStream getUrlInputStreamDirect(URL url, boolean restrictRedirects) throws IOException {
InputStream result;
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setReadTimeout(READ_TIMEOUT);
final URL redirectUrl = processPossibleRedirect(connection, restrictRedirects);
if (redirectUrl != null) {
url = redirectUrl;
connection = (HttpURLConnection)redirectUrl.openConnection();
connection.setReadTimeout(READ_TIMEOUT);
}
final int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
result = connection.getInputStream();
} else {
throw new IOException("Unexpected HTTP status code from " + url + ": " + status);
}
return result;
}
/**
* @param url The URL to get an input stream for, leveraging the local cache.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @return An InputStream for url, or null if url is null. Note that this InputStream may be of different concrete
* types, depending on whether the content is being served out of cache or not.
* @throws IOException When an IO error of some kind occurs.
*/
private InputStream getUrlInputStreamThroughCache(final URL url, boolean restrictRedirects) throws IOException {
synchronized (url) {
final String cacheKey = base64Encode(url);
final File cachedFile = new File(cacheDir, cacheKey);
final File cachedMetadataFile = new File(cacheDir, cacheKey + ".metadata.json");
if (cachedFile.exists() && cachedMetadataFile.exists()) {
try {
checkCache(url, restrictRedirects);
} catch (IOException ioe) {
// We know we have a locally cached file here, so if we happen to get an exception we can safely ignore
// it and fall back on the (possibly stale) cached content file. This makes the code more robust in the
// presence of network errors when the cache has previously been populated.
}
} else {
cacheMiss(url, restrictRedirects);
}
// At this point the cached file definitely exists
return new BufferedInputStream(Files.newInputStream(cachedFile.toPath()));
}
}
/**
* Checks the cache for content from the given url, and brings the cached content up to date if it's stale.
* @param url The url to check.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @throws IOException When an IO error of some kind occurs.
*/
private void checkCache(final URL url, boolean restrictRedirects) throws IOException {
final String cacheKey = base64Encode(url);
final File cachedMetadataFile = new File(cacheDir, cacheKey + ".metadata.json");
final HashMap<String,String> cachedMetadata = readMetadataFile(cachedMetadataFile);
if (cachedMetadata != null) {
final Instant lastChecked = parseISO8601String(cachedMetadata.get("lastChecked"));
final long difference = lastChecked != null ? Math.abs(ChronoUnit.SECONDS.between(Instant.now(), lastChecked)) : Long.MAX_VALUE;
if (difference > cacheCheckIntervalSecs) {
// It's been a while since we checked the cached download of this URL for staleness, so make an ETag request
logger.debug("Cache check interval exceeded; checking for updates to {}", url);
final String eTag = cachedMetadata.get("eTag");
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestProperty("If-None-Match", eTag);
final int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_NOT_MODIFIED) {
// The content of the URL has changed, which we handle the same as a cache miss (i.e. we re-download
// the content, and write a new metadata file from scratch)
cacheMiss(url, connection, restrictRedirects);
} else {
// The content hasn't changed, so just update the lastChecked metadata but otherwise do nothing
logger.debug("Cache hit for {}", url);
cachedMetadata.put("lastChecked", iso8601.format(Instant.now()));
writeMetadataFile(cachedMetadataFile, cachedMetadata);
}
} else {
// We checked recently, so don't need to do anything - the cached content will be used
logger.debug("Within cache check interval; skipping check of updates to {}", url);
}
} else {
// Metadata doesn't exist - treat it as a cache miss
cacheMiss(url, restrictRedirects);
}
}
/**
* Process a cache miss, which involves downloading the content from the given url, and writing out an associated
* metadata file (in JSON format) containing sufficient information for the cache to check for staleness in the
* future.
* @param connection The open HTTP connection to download and cache.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @throws IOException When an IO error of some kind occurs.
*/
private void cacheMiss(URL url, HttpURLConnection connection, boolean restrictRedirects) throws IOException {
logger.debug("Cache miss for {}", url);
final URL redirectUrl = processPossibleRedirect(connection, restrictRedirects);
if (redirectUrl != null) {
url = redirectUrl;
connection = (HttpURLConnection)redirectUrl.openConnection();
}
final int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
final String cacheKey = base64Encode(url);
final File cachedFile = new File(cacheDir, cacheKey);
writeContentFile(connection.getInputStream(), cachedFile);
final File cachedMetadataFile = new File(cacheDir, cacheKey + ".metadata.json");
final HashMap<String, String> metadata = new HashMap<>();
metadata.put("eTag", connection.getHeaderField("ETag"));
metadata.put("downloadedAt", iso8601.format(Instant.now()));
metadata.put("lastChecked", iso8601.format(Instant.now()));
metadata.put("sourceUrl", url.toString());
writeMetadataFile(cachedMetadataFile, metadata);
} else {
throw new IOException("Unexpected HTTP status code from " + url.toString() + ": " + status);
}
}
/**
* Process a cache miss, which involves downloading the content from the given url, and writing out an associated
* metadata file (in JSON format) containing sufficient information for the cache to check for staleness in the
* future.
* @param url The url to download and cache.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @throws IOException When an IO error of some kind occurs.
*/
private void cacheMiss(final URL url, boolean restrictRedirects) throws IOException {
final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setReadTimeout(READ_TIMEOUT);
cacheMiss(url, connection, restrictRedirects);
}
/**
* Processes an HTTP redirect (if any) returned by the given connection, returning the URL
* @param connection The connection to check for a redirect.
* @param restrictRedirects A flag that controls whether redirects returned by url are restricted to known SPDX
* hosts or not. Defaults to true. USE EXTREME CAUTION WHEN TURNING THIS OFF!
* @return The redirect URL, or null if there wasn't one.
* @throws IOException When an IO error of some kind occurs.
*/
private URL processPossibleRedirect(final HttpURLConnection connection, final boolean restrictRedirects) throws IOException {
URL result = null;
final int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
// redirect
final String redirectUrlStr = connection.getHeaderField("Location");
if (Objects.isNull(redirectUrlStr) || redirectUrlStr.isEmpty()) {
throw new IOException("Empty redirect URL response");
}
try {
result = new URL(redirectUrlStr);
} catch (Exception ex) {
throw new IOException("Invalid redirect URL", ex);
}
if (!result.getProtocol().toLowerCase().startsWith("http")) {
throw new IOException("Invalid redirect protocol");
}
if (restrictRedirects && !WHITE_LIST.contains(result.getHost())) {
throw new IOException("Invalid redirect host - not on the allowed 'white list'");
}
}
return result;
}
/**
* Reads a metadata file out of local cache.
* @param metadataFile The metadata file to read.
* @return The metadata read from the file, or null if the file doesn't exist or there was an error while reading
* it.
*/
private HashMap<String,String> readMetadataFile(final File metadataFile) {
HashMap<String,String> result;
try {
final Reader r = new BufferedReader(new FileReader(metadataFile));
result = new Gson().fromJson(r, new TypeToken<HashMap<String, String>>(){}.getType());
}
catch (IOException ioe) {
result = null; // Treat metadata read errors as a cache miss
}
return result;
}
/**
* Writes a metadata file to the local cache.
* @param metadataFile The metadata file to write. Note: if it already exists it will be silently overwritten.
* @param metadata The metadata to write to the file.
* @throws IOException When an IO error of some kind occurs.
*/
private void writeMetadataFile(final File metadataFile, HashMap<String,String> metadata) throws IOException {
try (final Writer w = new BufferedWriter(new FileWriter(metadataFile))) {
new Gson().toJson(metadata, new TypeToken<HashMap<String, String>>(){}.getType(), w);
w.flush();
}
}
/**
* Writes a content file to the local cache.
* @param is The InputStream to read the content from. Note: this InputStream must be open at the time this
* method is called, and will be fully consumed and closed by this method.
* @param cachedFile The content file to write to. Note: if it already exists it will be silently overwritten.
* @throws IOException When an IO error of some kind occurs.
*/
private void writeContentFile(final InputStream is, final File cachedFile) throws IOException {
try (final OutputStream cacheFileOutputStream = new BufferedOutputStream(Files.newOutputStream(cachedFile.toPath()))) {
byte[] ioBuffer = new byte[IO_BUFFER_SIZE];
int length;
while ((length = is.read(ioBuffer)) != -1) {
cacheFileOutputStream.write(ioBuffer, 0, length);
}
cacheFileOutputStream.flush();
}
is.close();
}
/**
* Attempts to parse s as if it were an ISO8601 formatted String.
* @param s The string to attempt to parse.
* @return The Instant for that ISO8601 value if parsing succeeded, or null if it didn't.
*/
private Instant parseISO8601String(final String s) {
Instant result = null;
if (s != null) {
try {
result = Instant.parse(s);
} catch (final DateTimeParseException dtpe) {
//noinspection DataFlowIssue
result = null;
}
}
return result;
}
/**
* @param s The String to BASE64 encode.
* @return The BASE64 encoding of s (as UTF-8).
*/
private String base64Encode(final String s) {
String result = null;
if (s != null) {
result = Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
}
return result;
}
/**
* @param u The URL to BASE64 encode.
* @return The BASE64 encoding of u (as a UTF-8 encoded String).
*/
private String base64Encode(final URL u) {
String result = null;
if (u != null) {
result = base64Encode(u.toString());
}
return result;
}
}