source: josm/trunk/src/org/openstreetmap/josm/io/CachedFile.java@ 13802

Last change on this file since 13802 was 13741, checked in by wiktorn, 6 years ago

PMD fixes

  • Property svn:eol-style set to native
File size: 22.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.ByteArrayOutputStream;
8import java.io.Closeable;
9import java.io.File;
10import java.io.IOException;
11import java.io.InputStream;
12import java.math.BigInteger;
13import java.net.HttpURLConnection;
14import java.net.MalformedURLException;
15import java.net.URL;
16import java.nio.charset.StandardCharsets;
17import java.nio.file.Files;
18import java.nio.file.StandardCopyOption;
19import java.security.MessageDigest;
20import java.security.NoSuchAlgorithmException;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Enumeration;
24import java.util.List;
25import java.util.Map;
26import java.util.concurrent.ConcurrentHashMap;
27import java.util.concurrent.TimeUnit;
28import java.util.zip.ZipEntry;
29import java.util.zip.ZipFile;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.spi.preferences.Config;
33import org.openstreetmap.josm.tools.HttpClient;
34import org.openstreetmap.josm.tools.Logging;
35import org.openstreetmap.josm.tools.Pair;
36import org.openstreetmap.josm.tools.Utils;
37
38/**
39 * Downloads a file and caches it on disk in order to reduce network load.
40 *
41 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get
42 * resources from the current *.jar file. (Local caching is only done for URLs.)
43 * <p>
44 * The mirrored file is only downloaded if it has been more than 7 days since
45 * last download. (Time can be configured.)
46 * <p>
47 * The file content is normally accessed with {@link #getInputStream()}, but
48 * you can also get the mirrored copy with {@link #getFile()}.
49 */
50public class CachedFile implements Closeable {
51
52 /**
53 * Caching strategy.
54 */
55 public enum CachingStrategy {
56 /**
57 * If cached file on disk is older than a certain time (7 days by default),
58 * consider the cache stale and try to download the file again.
59 */
60 MaxAge,
61 /**
62 * Similar to MaxAge, considers the cache stale when a certain age is
63 * exceeded. In addition, a If-Modified-Since HTTP header is added.
64 * When the server replies "304 Not Modified", this is considered the same
65 * as a full download.
66 */
67 IfModifiedSince
68 }
69
70 protected String name;
71 protected long maxAge;
72 protected String destDir;
73 protected String httpAccept;
74 protected CachingStrategy cachingStrategy;
75
76 private boolean fastFail;
77 private HttpClient activeConnection;
78 protected File cacheFile;
79 protected boolean initialized;
80 protected String parameter;
81
82 public static final long DEFAULT_MAXTIME = -1L;
83 public static final long DAYS = TimeUnit.DAYS.toSeconds(1); // factor to get caching time in days
84
85 private final Map<String, String> httpHeaders = new ConcurrentHashMap<>();
86
87 /**
88 * Constructs a CachedFile object from a given filename, URL or internal resource.
89 *
90 * @param name can be:<ul>
91 * <li>relative or absolute file name</li>
92 * <li>{@code file:///SOME/FILE} the same as above</li>
93 * <li>{@code http://...} a URL. It will be cached on disk.</li>
94 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
95 * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li>
96 * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul>
97 */
98 public CachedFile(String name) {
99 this.name = name;
100 }
101
102 /**
103 * Set the name of the resource.
104 * @param name can be:<ul>
105 * <li>relative or absolute file name</li>
106 * <li>{@code file:///SOME/FILE} the same as above</li>
107 * <li>{@code http://...} a URL. It will be cached on disk.</li>
108 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
109 * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li>
110 * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul>
111 * @return this object
112 */
113 public CachedFile setName(String name) {
114 this.name = name;
115 return this;
116 }
117
118 /**
119 * Set maximum age of cache file. Only applies to URLs.
120 * When this time has passed after the last download of the file, the
121 * cache is considered stale and a new download will be attempted.
122 * @param maxAge the maximum cache age in seconds
123 * @return this object
124 */
125 public CachedFile setMaxAge(long maxAge) {
126 this.maxAge = maxAge;
127 return this;
128 }
129
130 /**
131 * Set the destination directory for the cache file. Only applies to URLs.
132 * @param destDir the destination directory
133 * @return this object
134 */
135 public CachedFile setDestDir(String destDir) {
136 this.destDir = destDir;
137 return this;
138 }
139
140 /**
141 * Set the accepted MIME types sent in the HTTP Accept header. Only applies to URLs.
142 * @param httpAccept the accepted MIME types
143 * @return this object
144 */
145 public CachedFile setHttpAccept(String httpAccept) {
146 this.httpAccept = httpAccept;
147 return this;
148 }
149
150 /**
151 * Set the caching strategy. Only applies to URLs.
152 * @param cachingStrategy caching strategy
153 * @return this object
154 */
155 public CachedFile setCachingStrategy(CachingStrategy cachingStrategy) {
156 this.cachingStrategy = cachingStrategy;
157 return this;
158 }
159
160 /**
161 * Sets the http headers. Only applies to URL pointing to http or https resources
162 * @param headers that should be sent together with request
163 * @return this object
164 */
165 public CachedFile setHttpHeaders(Map<String, String> headers) {
166 this.httpHeaders.putAll(headers);
167 return this;
168 }
169
170 /**
171 * Sets whether opening HTTP connections should fail fast, i.e., whether a
172 * {@link HttpClient#setConnectTimeout(int) low connect timeout} should be used.
173 * @param fastFail whether opening HTTP connections should fail fast
174 */
175 public void setFastFail(boolean fastFail) {
176 this.fastFail = fastFail;
177 }
178
179 /**
180 * Sets additional URL parameter (used e.g. for maps)
181 * @param parameter the URL parameter
182 * @since 13536
183 */
184 public void setParam(String parameter) {
185 this.parameter = parameter;
186 }
187
188 public String getName() {
189 if (parameter != null)
190 return name.replaceAll("%<(.*)>", "");
191 return name;
192 }
193
194 /**
195 * Returns maximum age of cache file. Only applies to URLs.
196 * When this time has passed after the last download of the file, the
197 * cache is considered stale and a new download will be attempted.
198 * @return the maximum cache age in seconds
199 */
200 public long getMaxAge() {
201 return maxAge;
202 }
203
204 public String getDestDir() {
205 return destDir;
206 }
207
208 public String getHttpAccept() {
209 return httpAccept;
210 }
211
212 public CachingStrategy getCachingStrategy() {
213 return cachingStrategy;
214 }
215
216 /**
217 * Get InputStream to the requested resource.
218 * @return the InputStream
219 * @throws IOException when the resource with the given name could not be retrieved
220 */
221 public InputStream getInputStream() throws IOException {
222 File file = getFile();
223 if (file == null) {
224 if (name != null && name.startsWith("resource://")) {
225 InputStream is = getClass().getResourceAsStream(
226 name.substring("resource:/".length()));
227 if (is == null)
228 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
229 return is;
230 } else {
231 throw new IOException("No file found for: "+name);
232 }
233 }
234 return Files.newInputStream(file.toPath());
235 }
236
237 /**
238 * Get the full content of the requested resource as a byte array.
239 * @return the full content of the requested resource as byte array
240 * @throws IOException in case of an I/O error
241 */
242 public byte[] getByteContent() throws IOException {
243 try (InputStream is = getInputStream()) {
244 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
245 int nRead;
246 byte[] data = new byte[8192];
247 while ((nRead = is.read(data, 0, data.length)) != -1) {
248 buffer.write(data, 0, nRead);
249 }
250 buffer.flush();
251 return buffer.toByteArray();
252 }
253 }
254
255 /**
256 * Returns {@link #getInputStream()} wrapped in a buffered reader.
257 * <p>
258 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
259 *
260 * @return buffered reader
261 * @throws IOException if any I/O error occurs
262 * @since 9411
263 */
264 public BufferedReader getContentReader() throws IOException {
265 return new BufferedReader(UTFInputStreamReader.create(getInputStream()));
266 }
267
268 /**
269 * Get local file for the requested resource.
270 * @return The local cache file for URLs. If the resource is a local file,
271 * returns just that file.
272 * @throws IOException when the resource with the given name could not be retrieved
273 */
274 public synchronized File getFile() throws IOException {
275 if (initialized)
276 return cacheFile;
277 initialized = true;
278 URL url;
279 try {
280 url = new URL(name);
281 if ("file".equals(url.getProtocol())) {
282 cacheFile = new File(name.substring("file:/".length() - 1));
283 if (!cacheFile.exists()) {
284 cacheFile = new File(name.substring("file://".length() - 1));
285 }
286 } else {
287 try {
288 cacheFile = checkLocal(url);
289 } catch (SecurityException e) {
290 throw new IOException(e);
291 }
292 }
293 } catch (MalformedURLException e) {
294 if (name == null || name.startsWith("resource://")) {
295 return null;
296 } else if (name.startsWith("josmdir://")) {
297 cacheFile = new File(Config.getDirs().getUserDataDirectory(false), name.substring("josmdir://".length()));
298 } else if (name.startsWith("josmplugindir://")) {
299 cacheFile = new File(Main.pref.getPluginsDirectory(), name.substring("josmplugindir://".length()));
300 } else {
301 cacheFile = new File(name);
302 }
303 }
304 if (cacheFile == null)
305 throw new IOException("Unable to get cache file for "+getName());
306 return cacheFile;
307 }
308
309 /**
310 * Looks for a certain entry inside a zip file and returns the entry path.
311 *
312 * Replies a file in the top level directory of the ZIP file which has an
313 * extension <code>extension</code>. If more than one files have this
314 * extension, the last file whose name includes <code>namepart</code>
315 * is opened.
316 *
317 * @param extension the extension of the file we're looking for
318 * @param namepart the name part
319 * @return The zip entry path of the matching file. <code>null</code> if this cached file
320 * doesn't represent a zip file or if there was no matching
321 * file in the ZIP file.
322 */
323 public String findZipEntryPath(String extension, String namepart) {
324 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
325 if (ze == null) return null;
326 return ze.a;
327 }
328
329 /**
330 * Like {@link #findZipEntryPath}, but returns the corresponding InputStream.
331 * @param extension the extension of the file we're looking for
332 * @param namepart the name part
333 * @return InputStream to the matching file. <code>null</code> if this cached file
334 * doesn't represent a zip file or if there was no matching
335 * file in the ZIP file.
336 * @since 6148
337 */
338 public InputStream findZipEntryInputStream(String extension, String namepart) {
339 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
340 if (ze == null) return null;
341 return ze.b;
342 }
343
344 private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) {
345 File file = null;
346 try {
347 file = getFile();
348 } catch (IOException ex) {
349 Logging.log(Logging.LEVEL_WARN, ex);
350 }
351 if (file == null)
352 return null;
353 Pair<String, InputStream> res = null;
354 try {
355 ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8);
356 ZipEntry resentry = null;
357 Enumeration<? extends ZipEntry> entries = zipFile.entries();
358 while (entries.hasMoreElements()) {
359 ZipEntry entry = entries.nextElement();
360 // choose any file with correct extension. When more than one file, prefer the one which matches namepart
361 if (entry.getName().endsWith('.' + extension) && (resentry == null || entry.getName().indexOf(namepart) >= 0)) {
362 resentry = entry;
363 }
364 }
365 if (resentry != null) {
366 InputStream is = zipFile.getInputStream(resentry);
367 res = Pair.create(resentry.getName(), is);
368 } else {
369 Utils.close(zipFile);
370 }
371 } catch (IOException e) {
372 if (file.getName().endsWith(".zip")) {
373 Logging.log(Logging.LEVEL_WARN,
374 tr("Failed to open file with extension ''{2}'' and namepart ''{3}'' in zip file ''{0}''. Exception was: {1}",
375 file.getName(), e.toString(), extension, namepart), e);
376 }
377 }
378 return res;
379 }
380
381 /**
382 * Clear the cache for the given resource.
383 * This forces a fresh download.
384 * @param name the URL
385 */
386 public static void cleanup(String name) {
387 cleanup(name, null);
388 }
389
390 /**
391 * Clear the cache for the given resource.
392 * This forces a fresh download.
393 * @param name the URL
394 * @param destDir the destination directory (see {@link #setDestDir(java.lang.String)})
395 */
396 public static void cleanup(String name, String destDir) {
397 URL url;
398 try {
399 url = new URL(name);
400 if (!"file".equals(url.getProtocol())) {
401 String prefKey = getPrefKey(url, destDir, null);
402 List<String> localPath = new ArrayList<>(Config.getPref().getList(prefKey));
403 if (localPath.size() == 2) {
404 File lfile = new File(localPath.get(1));
405 if (lfile.exists()) {
406 Utils.deleteFile(lfile);
407 }
408 }
409 Config.getPref().putList(prefKey, null);
410 }
411 } catch (MalformedURLException e) {
412 Logging.warn(e);
413 }
414 }
415
416 /**
417 * Get preference key to store the location and age of the cached file.
418 * 2 resources that point to the same url, but that are to be stored in different
419 * directories will not share a cache file.
420 * @param url URL
421 * @param destDir destination directory
422 * @param parameter additional URL parameter (used e.g. for maps)
423 * @return Preference key
424 */
425 private static String getPrefKey(URL url, String destDir, String parameter) {
426 StringBuilder prefKey = new StringBuilder("mirror.");
427 if (destDir != null) {
428 prefKey.append(destDir).append('.');
429 }
430 if (parameter != null) {
431 prefKey.append(url.toString().replaceAll("%<(.*)>", ""));
432 } else {
433 prefKey.append(url.toString());
434 }
435 return prefKey.toString().replaceAll("=", "_");
436 }
437
438 private File checkLocal(URL url) throws IOException {
439 String prefKey = getPrefKey(url, destDir, parameter);
440 String urlStr = url.toExternalForm();
441 if (parameter != null)
442 urlStr = urlStr.replaceAll("%<(.*)>", "");
443 long age = 0L;
444 long maxAgeMillis = TimeUnit.SECONDS.toMillis(maxAge);
445 Long ifModifiedSince = null;
446 File localFile = null;
447 List<String> localPathEntry = new ArrayList<>(Config.getPref().getList(prefKey));
448 boolean offline = false;
449 try {
450 checkOfflineAccess(urlStr);
451 } catch (OfflineAccessException e) {
452 Logging.trace(e);
453 offline = true;
454 }
455 if (localPathEntry.size() == 2) {
456 localFile = new File(localPathEntry.get(1));
457 if (!localFile.exists()) {
458 localFile = null;
459 } else {
460 if (maxAge == DEFAULT_MAXTIME
461 || maxAge <= 0 // arbitrary value <= 0 is deprecated
462 ) {
463 maxAgeMillis = TimeUnit.SECONDS.toMillis(Config.getPref().getLong("mirror.maxtime", TimeUnit.DAYS.toSeconds(7)));
464 }
465 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0));
466 if (offline || age < maxAgeMillis) {
467 return localFile;
468 }
469 if (cachingStrategy == CachingStrategy.IfModifiedSince) {
470 ifModifiedSince = Long.valueOf(localPathEntry.get(0));
471 }
472 }
473 }
474 if (destDir == null) {
475 destDir = Config.getDirs().getCacheDirectory(true).getPath();
476 }
477
478 File destDirFile = new File(destDir);
479 if (!destDirFile.exists()) {
480 Utils.mkDirs(destDirFile);
481 }
482
483 // No local file + offline => nothing to do
484 if (offline) {
485 return null;
486 }
487
488 if (parameter != null) {
489 String u = url.toExternalForm();
490 String uc;
491 if (parameter.isEmpty()) {
492 uc = u.replaceAll("%<(.*)>", "");
493 } else {
494 uc = u.replaceAll("%<(.*)>", "$1" + Utils.encodeUrl(parameter));
495 }
496 if (!uc.equals(u))
497 url = new URL(uc);
498 }
499
500 String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_");
501 String localPath = "mirror_" + a;
502 localPath = truncatePath(destDir, localPath);
503 destDirFile = new File(destDir, localPath + ".tmp");
504 try {
505 activeConnection = HttpClient.create(url)
506 .setAccept(httpAccept)
507 .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince)
508 .setHeaders(httpHeaders);
509 if (fastFail) {
510 activeConnection.setReadTimeout(1000);
511 }
512 final HttpClient.Response con = activeConnection.connect();
513 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
514 Logging.debug("304 Not Modified ({0})", urlStr);
515 if (localFile == null)
516 throw new AssertionError();
517 Config.getPref().putList(prefKey,
518 Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1)));
519 return localFile;
520 } else if (con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
521 throw new IOException(tr("The requested URL {0} was not found", urlStr));
522 }
523 try (InputStream is = con.getContent()) {
524 Files.copy(is, destDirFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
525 }
526 activeConnection = null;
527 localFile = new File(destDir, localPath);
528 if (Main.platform.rename(destDirFile, localFile)) {
529 Config.getPref().putList(prefKey,
530 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString()));
531 } else {
532 Logging.warn(tr("Failed to rename file {0} to {1}.",
533 destDirFile.getPath(), localFile.getPath()));
534 }
535 } catch (IOException e) {
536 if (age >= maxAgeMillis && age < maxAgeMillis*2) {
537 Logging.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", urlStr, e));
538 return localFile;
539 } else {
540 throw e;
541 }
542 }
543
544 return localFile;
545 }
546
547 private static void checkOfflineAccess(String urlString) {
548 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlString, Main.getJOSMWebsite());
549 OnlineResource.OSM_API.checkOfflineAccess(urlString, OsmApi.getOsmApi().getServerUrl());
550 }
551
552 private static String truncatePath(String directory, String fileName) {
553 if (directory.length() + fileName.length() > 255) {
554 // Windows doesn't support paths longer than 260, leave 5 chars as safe buffer, 4 will be used by ".tmp"
555 // TODO: what about filename size on other systems? 255?
556 if (directory.length() > 191 && Main.isPlatformWindows()) {
557 // digest length + name prefix == 64
558 // 255 - 64 = 191
559 // TODO: use this check only on Windows?
560 throw new IllegalArgumentException("Path " + directory + " too long to cached files");
561 }
562
563 MessageDigest md;
564 try {
565 md = MessageDigest.getInstance("SHA-256");
566 md.update(fileName.getBytes(StandardCharsets.UTF_8));
567 String digest = String.format("%064x", new BigInteger(1, md.digest()));
568 return fileName.substring(0, Math.min(fileName.length(), 32)) + digest.substring(0, 32);
569 } catch (NoSuchAlgorithmException e) {
570 Logging.error(e);
571 // TODO: what better can we do here?
572 throw new IllegalArgumentException("Missing digest algorithm SHA-256", e);
573 }
574 }
575 return fileName;
576 }
577
578 /**
579 * Attempts to disconnect an URL connection.
580 * @see HttpClient#disconnect()
581 * @since 9411
582 */
583 @Override
584 public void close() {
585 if (activeConnection != null) {
586 activeConnection.disconnect();
587 }
588 }
589
590 /**
591 * Clears the cached file
592 * @throws IOException if any I/O error occurs
593 * @since 10993
594 */
595 public void clear() throws IOException {
596 URL url;
597 try {
598 url = new URL(name);
599 if ("file".equals(url.getProtocol())) {
600 return; // this is local file - do not delete it
601 }
602 } catch (MalformedURLException e) {
603 return; // if it's not a URL, then it still might be a local file - better not to delete
604 }
605 File f = getFile();
606 if (f != null && f.exists()) {
607 Utils.deleteFile(f);
608 }
609 }
610}
Note: See TracBrowser for help on using the repository browser.