Changeset 7434 in josm for trunk/src/org/openstreetmap/josm/io/CachedFile.java
- Timestamp:
- 2014-08-20T03:07:15+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r7282 r7434 24 24 25 25 import org.openstreetmap.josm.Main; 26 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 27 import org.openstreetmap.josm.tools.Pair; 27 28 import org.openstreetmap.josm.tools.Utils; … … 29 30 /** 30 31 * Downloads a file and caches it on disk in order to reduce network load. 31 * 32 * 32 33 * Supports URLs, local files, and a custom scheme (<code>resource:</code>) to get 33 34 * resources from the current *.jar file. (Local caching is only done for URLs.) … … 49 50 * consider the cache stale and try to download the file again. 50 51 */ 51 MaxAge, 52 MaxAge, 52 53 /** 53 54 * Similar to MaxAge, considers the cache stale when a certain age is … … 56 57 * as a full download. 57 58 */ 58 IfModifiedSince 59 IfModifiedSince 59 60 } 60 61 protected String name; … … 63 64 protected String httpAccept; 64 65 protected CachingStrategy cachingStrategy; 65 66 66 67 protected File cacheFile = null; 67 68 boolean initialized = false; … … 98 99 return this; 99 100 } 100 101 101 102 /** 102 103 * Set maximum age of cache file. Only applies to URLs. … … 201 202 cacheFile = checkLocal(url); 202 203 } 203 } catch ( java.net.MalformedURLException e) {204 } catch (MalformedURLException e) { 204 205 if (name.startsWith("resource://")) { 205 206 return null; … … 211 212 } 212 213 if (cacheFile == null) 213 throw new IOException(); 214 throw new IOException("Unable to get cache file for "+name); 214 215 return cacheFile; 215 216 } 216 217 217 218 /** 218 219 * Looks for a certain entry inside a zip file and returns the entry path. … … 292 293 * Clear the cache for the given resource. 293 294 * This forces a fresh download. 294 * @param name the URL 295 * @param name the URL 295 296 */ 296 297 public static void cleanup(String name) { … … 341 342 private File checkLocal(URL url) throws IOException { 342 343 String prefKey = getPrefKey(url, destDir); 344 String urlStr = url.toExternalForm(); 343 345 long age = 0L; 344 346 long lMaxAge = maxAge; … … 346 348 File localFile = null; 347 349 List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey)); 350 boolean offline = false; 351 try { 352 checkOfflineAccess(urlStr); 353 } catch (OfflineAccessException e) { 354 offline = true; 355 } 348 356 if (localPathEntry.size() == 2) { 349 357 localFile = new File(localPathEntry.get(1)); 350 if(!localFile.exists()) 358 if (!localFile.exists()) { 351 359 localFile = null; 352 else { 360 } else { 353 361 if ( maxAge == DEFAULT_MAXTIME 354 362 || maxAge <= 0 // arbitrary value <= 0 is deprecated … … 357 365 } 358 366 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0)); 359 if (age < lMaxAge*1000) { 367 if (offline || age < lMaxAge*1000) { 360 368 return localFile; 361 369 } … … 373 381 destDirFile.mkdirs(); 374 382 } 375 376 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_"); 383 384 // No local file + offline => nothing to do 385 if (offline) { 386 return null; 387 } 388 389 String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_"); 377 390 String localPath = "mirror_" + a; 378 391 destDirFile = new File(destDir, localPath + ".tmp"); … … 380 393 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince); 381 394 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 382 Main.debug("304 Not Modified ("+url+")"); 383 if (localFile == null) throw new AssertionError(); 384 Main.pref.putCollection(prefKey, 395 if (Main.isDebugEnabled()) { 396 Main.debug("304 Not Modified ("+urlStr+")"); 397 } 398 if (localFile == null) 399 throw new AssertionError(); 400 Main.pref.putCollection(prefKey, 385 401 Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1))); 386 402 return localFile; 387 } 403 } 388 404 try ( 389 405 InputStream bis = new BufferedInputStream(con.getInputStream()); … … 398 414 } 399 415 localFile = new File(destDir, localPath); 400 if(Main.platform.rename(destDirFile, localFile)) { 401 Main.pref.putCollection(prefKey, 416 if (Main.platform.rename(destDirFile, localFile)) { 417 Main.pref.putCollection(prefKey, 402 418 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString())); 403 419 } else { … … 407 423 } catch (IOException e) { 408 424 if (age >= lMaxAge*1000 && age < lMaxAge*1000*2) { 409 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", url, e)); 425 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", urlStr, e)); 410 426 return localFile; 411 427 } else { … … 415 431 416 432 return localFile; 433 } 434 435 private static void checkOfflineAccess(String urlString) { 436 OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlString, Main.getJOSMWebsite()); 437 OnlineResource.OSM_API.checkOfflineAccess(urlString, Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)); 417 438 } 418 439 … … 424 445 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>. 425 446 * <p> 426 * This can cause sproblems when downloading from certain GitHub URLs.447 * This can cause problems when downloading from certain GitHub URLs. 427 448 * 428 449 * @param downloadUrl The resource URL to download … … 432 453 * @throws MalformedURLException If a redirected URL is wrong 433 454 * @throws IOException If any I/O operation goes wrong 455 * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol 434 456 * @since 6867 435 457 */ 436 458 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, IOException { 459 CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl"); 460 String downloadString = downloadUrl.toExternalForm(); 461 462 checkOfflineAccess(downloadString); 463 437 464 HttpURLConnection con = null; 438 465 int numRedirects = 0; 439 466 while(true) { 440 467 con = Utils.openHttpConnection(downloadUrl); 468 if (con == null) { 469 throw new IOException("Cannot open http connection to "+downloadString); 470 } 441 471 if (ifModifiedSince != null) { 442 472 con.setIfModifiedSince(ifModifiedSince); … … 445 475 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 446 476 con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000); 447 Main.debug("GET "+downloadUrl); 477 if (Main.isDebugEnabled()) { 478 Main.debug("GET "+downloadString); 479 } 448 480 if (httpAccept != null) { 449 Main.debug("Accept: "+httpAccept); 481 if (Main.isTraceEnabled()) { 482 Main.trace("Accept: "+httpAccept); 483 } 450 484 con.setRequestProperty("Accept", httpAccept); 451 485 } … … 466 500 case HttpURLConnection.HTTP_SEE_OTHER: 467 501 String redirectLocation = con.getHeaderField("Location"); 468 if (downloadUrl == null) { 469 /* I18n: argument is HTTP response code */ String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header. Can''t redirect. Aborting.", con.getResponseCode()); 502 if (redirectLocation == null) { 503 /* I18n: argument is HTTP response code */ 504 String msg = tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header."+ 505 " Can''t redirect. Aborting.", con.getResponseCode()); 470 506 throw new IOException(msg); 471 507 } 472 508 downloadUrl = new URL(redirectLocation); 509 downloadString = downloadUrl.toExternalForm(); 473 510 // keep track of redirect attempts to break a redirect loops if it happens 474 511 // to occur for whatever reason … … 478 515 throw new IOException(msg); 479 516 } 480 Main.info(tr("Download redirected to ''{0}''", download Url));517 Main.info(tr("Download redirected to ''{0}''", downloadString)); 481 518 break; 482 519 default: 483 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", download Url, con.getResponseCode());520 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadString, con.getResponseCode()); 484 521 throw new IOException(msg); 485 522 } 486 523 } 487 524 } 488 489 525 }
Note:
See TracChangeset
for help on using the changeset viewer.
