Changeset 7242 in josm
- Timestamp:
- 2014-06-12T00:50:05+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java ¶
r7089 r7242 33 33 */ 34 34 public class MirroredInputStream extends InputStream { 35 36 /** 37 * Caching strategy. 38 */ 39 public enum CachingStrategy { 40 /** 41 * If cached file on disk is older than a certain time (7 days by default), 42 * consider the cache stale and try to download the file again. 43 */ 44 MaxAge, 45 /** 46 * Similar to MaxAge, considers the cache stale when a certain age is 47 * exceeded. In addition, a If-Modified-Since HTTP header is added. 48 * When the server replies "304 Not Modified", this is considered the same 49 * as a full download. 50 */ 51 IfModifiedSince 52 } 53 35 54 InputStream fs = null; 36 55 File file = null; 37 56 38 57 public static final long DEFAULT_MAXTIME = -1L; 58 public static final long DAYS = 24*60*60; // factor to get caching time in days 39 59 40 60 /** … … 136 156 */ 137 157 public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept) throws IOException { 158 this(name, destDir, maxTime, httpAccept, CachingStrategy.MaxAge); 159 } 160 161 /** 162 * Constructs an input stream from a given filename, URL or internal resource. 163 * 164 * @param name can be:<ul> 165 * <li>relative or absolute file name</li> 166 * <li>{@code file:///SOME/FILE} the same as above</li> 167 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li> 168 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li> 169 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul> 170 * @param destDir the destination directory for the cache file. Only applies for URLs. 171 * @param maxTime the maximum age of the cache file (in seconds) 172 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs. 173 * @param caching the caching strategy 174 * @throws IOException when the resource with the given name could not be retrieved 175 * @since 6867 176 */ 177 public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException { 138 178 URL url; 139 179 try { … … 145 185 } 146 186 } else { 147 file = checkLocal(url, destDir, maxTime, httpAccept); 187 file = checkLocal(url, destDir, maxTime, httpAccept, caching); 148 188 } 149 189 } catch (java.net.MalformedURLException e) { … … 276 316 } 277 317 278 private File checkLocal(URL url, String destDir, long maxTime, String httpAccept) throws IOException { 318 private File checkLocal(URL url, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException { 279 319 String prefKey = getPrefKey(url, destDir); 280 320 long age = 0L; 321 Long ifModifiedSince = null; 281 322 File localFile = null; 282 323 List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey)); … … 295 336 return localFile; 296 337 } 338 if (caching == CachingStrategy.IfModifiedSince) { 339 ifModifiedSince = Long.parseLong(localPathEntry.get(0)); 340 } 297 341 } 298 342 } … … 305 349 destDirFile.mkdirs(); 306 350 } 307 351 308 352 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_"); 309 353 String localPath = "mirror_" + a; 310 354 destDirFile = new File(destDir, localPath + ".tmp"); 311 355 try { 312 HttpURLConnection con = connectFollowingRedirect(url, httpAccept); 356 HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince); 357 if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 358 Main.debug("304 Not Modified ("+url+")"); 359 if (localFile == null) throw new AssertionError(); 360 Main.pref.putCollection(prefKey, 361 Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1))); 362 return localFile; 363 } 313 364 try ( 314 365 InputStream bis = new BufferedInputStream(con.getInputStream()); … … 324 375 localFile = new File(destDir, localPath); 325 376 if(Main.platform.rename(destDirFile, localFile)) { 326 Main.pref.putCollection(prefKey, Arrays.asList(new String[]327 {Long.toString(System.currentTimeMillis()), localFile.toString()}));377 Main.pref.putCollection(prefKey, 378 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString())); 328 379 } else { 329 380 Main.warn(tr("Failed to rename file {0} to {1}.", … … 353 404 * @param downloadUrl The resource URL to download 354 405 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null} 406 * @param ifModifiedSince The download time of the cache file, optional 355 407 * @return The HTTP connection effectively linked to the resource, after all potential redirections 356 408 * @throws MalformedURLException If a redirected URL is wrong … … 358 410 * @since 6867 359 411 */ 360 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept) throws MalformedURLException, IOException { 412 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, IOException { 361 413 HttpURLConnection con = null; 362 414 int numRedirects = 0; 363 415 while(true) { 364 416 con = Utils.openHttpConnection(downloadUrl); 417 if (ifModifiedSince != null) { 418 con.setIfModifiedSince(ifModifiedSince); 419 } 365 420 con.setInstanceFollowRedirects(false); 366 421 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); … … 380 435 case HttpURLConnection.HTTP_OK: 381 436 return con; 437 case HttpURLConnection.HTTP_NOT_MODIFIED: 438 if (ifModifiedSince != null) 439 return con; 382 440 case HttpURLConnection.HTTP_MOVED_PERM: 383 441 case HttpURLConnection.HTTP_MOVED_TEMP: -
TabularUnified trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java ¶
r7186 r7242 50 50 SAXParserFactory factory = SAXParserFactory.newInstance(); 51 51 factory.setNamespaceAware(true); 52 try (InputStream in = new MirroredInputStream(source)) { 52 try (InputStream in = new MirroredInputStream(source, null, 1*MirroredInputStream.DAYS, null, 53 MirroredInputStream.CachingStrategy.IfModifiedSince)) { 53 54 InputSource is = new InputSource(UTFInputStreamReader.create(in)); 54 55 factory.newSAXParser().parse(is, parser); -
TabularUnified trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java ¶
r7033 r7242 125 125 URL url = new URL(pi.downloadlink); 126 126 synchronized(this) { 127 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES); 127 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null); 128 128 } 129 129 try (
Note:
See TracChangeset
for help on using the changeset viewer.