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

Last change on this file since 14630 was 14535, checked in by Don-vip, 5 years ago

see #16073 - check response contents
see #16854 - stability of created primitive IDs (accidental commit...)

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