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

Last change on this file since 11320 was 11288, checked in by simon04, 7 years ago

see #13376 - Use TimeUnit instead of combinations of 1000/60/60/24

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