001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.BufferedInputStream; 007import java.io.BufferedReader; 008import java.io.Closeable; 009import java.io.File; 010import java.io.FileInputStream; 011import java.io.IOException; 012import java.io.InputStream; 013import java.net.HttpURLConnection; 014import java.net.MalformedURLException; 015import java.net.URL; 016import java.nio.charset.StandardCharsets; 017import java.nio.file.Files; 018import java.nio.file.StandardCopyOption; 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.Enumeration; 022import java.util.List; 023import java.util.Map; 024import java.util.concurrent.ConcurrentHashMap; 025import java.util.zip.ZipEntry; 026import java.util.zip.ZipFile; 027 028import org.openstreetmap.josm.Main; 029import org.openstreetmap.josm.tools.HttpClient; 030import org.openstreetmap.josm.tools.Pair; 031import org.openstreetmap.josm.tools.Utils; 032 033/** 034 * Downloads a file and caches it on disk in order to reduce network load. 035 * 036 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get 037 * resources from the current *.jar file. (Local caching is only done for URLs.) 038 * <p> 039 * The mirrored file is only downloaded if it has been more than 7 days since 040 * last download. (Time can be configured.) 041 * <p> 042 * The file content is normally accessed with {@link #getInputStream()}, but 043 * you can also get the mirrored copy with {@link #getFile()}. 044 */ 045public class CachedFile implements Closeable { 046 047 /** 048 * Caching strategy. 049 */ 050 public enum CachingStrategy { 051 /** 052 * If cached file on disk is older than a certain time (7 days by default), 053 * consider the cache stale and try to download the file again. 054 */ 055 MaxAge, 056 /** 057 * Similar to MaxAge, considers the cache stale when a certain age is 058 * exceeded. In addition, a If-Modified-Since HTTP header is added. 059 * When the server replies "304 Not Modified", this is considered the same 060 * as a full download. 061 */ 062 IfModifiedSince 063 } 064 065 protected String name; 066 protected long maxAge; 067 protected String destDir; 068 protected String httpAccept; 069 protected CachingStrategy cachingStrategy; 070 071 private boolean fastFail; 072 private HttpClient activeConnection; 073 protected File cacheFile; 074 protected boolean initialized; 075 076 public static final long DEFAULT_MAXTIME = -1L; 077 public static final long DAYS = 24*60*60; // factor to get caching time in days 078 079 private final Map<String, String> httpHeaders = new ConcurrentHashMap<>(); 080 081 /** 082 * Constructs a CachedFile object from a given filename, URL or internal resource. 083 * 084 * @param name can be:<ul> 085 * <li>relative or absolute file name</li> 086 * <li>{@code file:///SOME/FILE} the same as above</li> 087 * <li>{@code http://...} a URL. It will be cached on disk.</li> 088 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 089 * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li> 090 * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul> 091 */ 092 public CachedFile(String name) { 093 this.name = name; 094 } 095 096 /** 097 * Set the name of the resource. 098 * @param name can be:<ul> 099 * <li>relative or absolute file name</li> 100 * <li>{@code file:///SOME/FILE} the same as above</li> 101 * <li>{@code http://...} a URL. It will be cached on disk.</li> 102 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 103 * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li> 104 * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul> 105 * @return this object 106 */ 107 public CachedFile setName(String name) { 108 this.name = name; 109 return this; 110 } 111 112 /** 113 * Set maximum age of cache file. Only applies to URLs. 114 * When this time has passed after the last download of the file, the 115 * cache is considered stale and a new download will be attempted. 116 * @param maxAge the maximum cache age in seconds 117 * @return this object 118 */ 119 public CachedFile setMaxAge(long maxAge) { 120 this.maxAge = maxAge; 121 return this; 122 } 123 124 /** 125 * Set the destination directory for the cache file. Only applies to URLs. 126 * @param destDir the destination directory 127 * @return this object 128 */ 129 public CachedFile setDestDir(String destDir) { 130 this.destDir = destDir; 131 return this; 132 } 133 134 /** 135 * Set the accepted MIME types sent in the HTTP Accept header. Only applies to URLs. 136 * @param httpAccept the accepted MIME types 137 * @return this object 138 */ 139 public CachedFile setHttpAccept(String httpAccept) { 140 this.httpAccept = httpAccept; 141 return this; 142 } 143 144 /** 145 * Set the caching strategy. Only applies to URLs. 146 * @param cachingStrategy caching strategy 147 * @return this object 148 */ 149 public CachedFile setCachingStrategy(CachingStrategy cachingStrategy) { 150 this.cachingStrategy = cachingStrategy; 151 return this; 152 } 153 154 /** 155 * Sets the http headers. Only applies to URL pointing to http or https resources 156 * @param headers that should be sent together with request 157 * @return this object 158 */ 159 public CachedFile setHttpHeaders(Map<String, String> headers) { 160 this.httpHeaders.putAll(headers); 161 return this; 162 } 163 164 /** 165 * Sets whether opening HTTP connections should fail fast, i.e., whether a 166 * {@link HttpClient#setConnectTimeout(int) low connect timeout} should be used. 167 * @param fastFail whether opening HTTP connections should fail fast 168 */ 169 public void setFastFail(boolean fastFail) { 170 this.fastFail = fastFail; 171 } 172 173 public String getName() { 174 return name; 175 } 176 177 public long getMaxAge() { 178 return maxAge; 179 } 180 181 public String getDestDir() { 182 return destDir; 183 } 184 185 public String getHttpAccept() { 186 return httpAccept; 187 } 188 189 public CachingStrategy getCachingStrategy() { 190 return cachingStrategy; 191 } 192 193 /** 194 * Get InputStream to the requested resource. 195 * @return the InputStream 196 * @throws IOException when the resource with the given name could not be retrieved 197 */ 198 public InputStream getInputStream() throws IOException { 199 File file = getFile(); 200 if (file == null) { 201 if (name.startsWith("resource://")) { 202 InputStream is = getClass().getResourceAsStream( 203 name.substring("resource:/".length())); 204 if (is == null) 205 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name)); 206 return is; 207 } else { 208 throw new IOException("No file found for: "+name); 209 } 210 } 211 return new FileInputStream(file); 212 } 213 214 /** 215 * Returns {@link #getInputStream()} wrapped in a buffered reader. 216 * <p> 217 * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}. 218 * 219 * @return buffered reader 220 * @throws IOException if any I/O error occurs 221 * @since 9411 222 */ 223 public BufferedReader getContentReader() throws IOException { 224 return new BufferedReader(UTFInputStreamReader.create(getInputStream())); 225 } 226 227 /** 228 * Get local file for the requested resource. 229 * @return The local cache file for URLs. If the resource is a local file, 230 * returns just that file. 231 * @throws IOException when the resource with the given name could not be retrieved 232 */ 233 public synchronized File getFile() throws IOException { 234 if (initialized) 235 return cacheFile; 236 initialized = true; 237 URL url; 238 try { 239 url = new URL(name); 240 if ("file".equals(url.getProtocol())) { 241 cacheFile = new File(name.substring("file:/".length() - 1)); 242 if (!cacheFile.exists()) { 243 cacheFile = new File(name.substring("file://".length() - 1)); 244 } 245 } else { 246 cacheFile = checkLocal(url); 247 } 248 } catch (MalformedURLException e) { 249 if (name.startsWith("resource://")) { 250 return null; 251 } else if (name.startsWith("josmdir://")) { 252 cacheFile = new File(Main.pref.getUserDataDirectory(), name.substring("josmdir://".length())); 253 } else if (name.startsWith("josmplugindir://")) { 254 cacheFile = new File(Main.pref.getPluginsDirectory(), name.substring("josmplugindir://".length())); 255 } else { 256 cacheFile = new File(name); 257 } 258 } 259 if (cacheFile == null) 260 throw new IOException("Unable to get cache file for "+name); 261 return cacheFile; 262 } 263 264 /** 265 * Looks for a certain entry inside a zip file and returns the entry path. 266 * 267 * Replies a file in the top level directory of the ZIP file which has an 268 * extension <code>extension</code>. If more than one files have this 269 * extension, the last file whose name includes <code>namepart</code> 270 * is opened. 271 * 272 * @param extension the extension of the file we're looking for 273 * @param namepart the name part 274 * @return The zip entry path of the matching file. Null if this cached file 275 * doesn't represent a zip file or if there was no matching 276 * file in the ZIP file. 277 */ 278 public String findZipEntryPath(String extension, String namepart) { 279 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart); 280 if (ze == null) return null; 281 return ze.a; 282 } 283 284 /** 285 * Like {@link #findZipEntryPath}, but returns the corresponding InputStream. 286 * @param extension the extension of the file we're looking for 287 * @param namepart the name part 288 * @return InputStream to the matching file. Null if this cached file 289 * doesn't represent a zip file or if there was no matching 290 * file in the ZIP file. 291 * @since 6148 292 */ 293 public InputStream findZipEntryInputStream(String extension, String namepart) { 294 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart); 295 if (ze == null) return null; 296 return ze.b; 297 } 298 299 private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) { 300 File file = null; 301 try { 302 file = getFile(); 303 } catch (IOException ex) { 304 Main.warn(ex, false); 305 } 306 if (file == null) 307 return null; 308 Pair<String, InputStream> res = null; 309 try { 310 ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8); 311 ZipEntry resentry = null; 312 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 313 while (entries.hasMoreElements()) { 314 ZipEntry entry = entries.nextElement(); 315 if (entry.getName().endsWith('.' + extension)) { 316 /* choose any file with correct extension. When more than 317 one file, prefer the one which matches namepart */ 318 if (resentry == null || entry.getName().indexOf(namepart) >= 0) { 319 resentry = entry; 320 } 321 } 322 } 323 if (resentry != null) { 324 InputStream is = zipFile.getInputStream(resentry); 325 res = Pair.create(resentry.getName(), is); 326 } else { 327 Utils.close(zipFile); 328 } 329 } catch (Exception e) { 330 if (file.getName().endsWith(".zip")) { 331 Main.warn(tr("Failed to open file with extension ''{2}'' and namepart ''{3}'' in zip file ''{0}''. Exception was: {1}", 332 file.getName(), e.toString(), extension, namepart)); 333 } 334 } 335 return res; 336 } 337 338 /** 339 * Clear the cache for the given resource. 340 * This forces a fresh download. 341 * @param name the URL 342 */ 343 public static void cleanup(String name) { 344 cleanup(name, null); 345 } 346 347 /** 348 * Clear the cache for the given resource. 349 * This forces a fresh download. 350 * @param name the URL 351 * @param destDir the destination directory (see {@link #setDestDir(java.lang.String)}) 352 */ 353 public static void cleanup(String name, String destDir) { 354 URL url; 355 try { 356 url = new URL(name); 357 if (!"file".equals(url.getProtocol())) { 358 String prefKey = getPrefKey(url, destDir); 359 List<String> localPath = new ArrayList<>(Main.pref.getCollection(prefKey)); 360 if (localPath.size() == 2) { 361 File lfile = new File(localPath.get(1)); 362 if (lfile.exists()) { 363 Utils.deleteFile(lfile); 364 } 365 } 366 Main.pref.putCollection(prefKey, null); 367 } 368 } catch (MalformedURLException e) { 369 Main.warn(e); 370 } 371 } 372 373 /** 374 * Get preference key to store the location and age of the cached file. 375 * 2 resources that point to the same url, but that are to be stored in different 376 * directories will not share a cache file. 377 * @param url URL 378 * @param destDir destination directory 379 * @return Preference key 380 */ 381 private static String getPrefKey(URL url, String destDir) { 382 StringBuilder prefKey = new StringBuilder("mirror."); 383 if (destDir != null) { 384 prefKey.append(destDir).append('.'); 385 } 386 prefKey.append(url.toString()); 387 return prefKey.toString().replaceAll("=", "_"); 388 } 389 390 private File checkLocal(URL url) throws IOException { 391 String prefKey = getPrefKey(url, destDir); 392 String urlStr = url.toExternalForm(); 393 long age = 0L; 394 long lMaxAge = maxAge; 395 Long ifModifiedSince = null; 396 File localFile = null; 397 List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey)); 398 boolean offline = false; 399 try { 400 checkOfflineAccess(urlStr); 401 } catch (OfflineAccessException e) { 402 offline = true; 403 } 404 if (localPathEntry.size() == 2) { 405 localFile = new File(localPathEntry.get(1)); 406 if (!localFile.exists()) { 407 localFile = null; 408 } else { 409 if (maxAge == DEFAULT_MAXTIME 410 || maxAge <= 0 // arbitrary value <= 0 is deprecated 411 ) { 412 lMaxAge = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); // one week 413 } 414 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0)); 415 if (offline || age < lMaxAge*1000) { 416 return localFile; 417 } 418 if (cachingStrategy == CachingStrategy.IfModifiedSince) { 419 ifModifiedSince = Long.valueOf(localPathEntry.get(0)); 420 } 421 } 422 } 423 if (destDir == null) { 424 destDir = Main.pref.getCacheDirectory().getPath(); 425 } 426 427 File destDirFile = new File(destDir); 428 if (!destDirFile.exists()) { 429 Utils.mkDirs(destDirFile); 430 } 431 432 // No local file + offline => nothing to do 433 if (offline) { 434 return null; 435 } 436 437 String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_"); 438 String localPath = "mirror_" + a; 439 destDirFile = new File(destDir, localPath + ".tmp"); 440 try { 441 activeConnection = HttpClient.create(url) 442 .setAccept(httpAccept) 443 .setIfModifiedSince(ifModifiedSince == null ? 0L : ifModifiedSince) 444 .setHeaders(httpHeaders); 445 if (fastFail) { 446 activeConnection.setReadTimeout(1000); 447 } 448 final HttpClient.Response con = activeConnection.connect(); 449 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 450 if (Main.isDebugEnabled()) { 451 Main.debug("304 Not Modified ("+urlStr+')'); 452 } 453 if (localFile == null) 454 throw new AssertionError(); 455 Main.pref.putCollection(prefKey, 456 Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1))); 457 return localFile; 458 } 459 try (InputStream bis = new BufferedInputStream(con.getContent())) { 460 Files.copy(bis, destDirFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 461 } 462 activeConnection = null; 463 localFile = new File(destDir, localPath); 464 if (Main.platform.rename(destDirFile, localFile)) { 465 Main.pref.putCollection(prefKey, 466 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString())); 467 } else { 468 Main.warn(tr("Failed to rename file {0} to {1}.", 469 destDirFile.getPath(), localFile.getPath())); 470 } 471 } catch (IOException e) { 472 if (age >= lMaxAge*1000 && age < lMaxAge*1000*2) { 473 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", urlStr, e)); 474 return localFile; 475 } else { 476 throw e; 477 } 478 } 479 480 return localFile; 481 } 482 483 private static void checkOfflineAccess(String urlString) { 484 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlString, Main.getJOSMWebsite()); 485 OnlineResource.OSM_API.checkOfflineAccess(urlString, OsmApi.getOsmApi().getServerUrl()); 486 } 487 488 /** 489 * Attempts to disconnect an URL connection. 490 * @see HttpClient#disconnect() 491 * @since 9411 492 */ 493 @Override 494 public void close() { 495 if (activeConnection != null) { 496 activeConnection.disconnect(); 497 } 498 } 499}