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

Last change on this file since 8512 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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