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

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

sonar - fb-contrib - minor performance improvements:

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