source: josm/trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java@ 7242

Last change on this file since 7242 was 7242, checked in by bastiK, 10 years ago

add support for If-Modified-Since header in MirroredInputStream (see #10139)

use this for /maps request (sever support has been added) and change update
intervall from 7 days to 1 day

  • Property svn:eol-style set to native
File size: 20.6 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.Pair;
27import org.openstreetmap.josm.tools.Utils;
28
29/**
30 * Mirrors a file to a local file.
31 * <p>
32 * The file mirrored is only downloaded if it has been more than 7 days since last download
33 */
34public 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
54 InputStream fs = null;
55 File file = null;
56
57 public static final long DEFAULT_MAXTIME = -1L;
58 public static final long DAYS = 24*60*60; // factor to get caching time in days
59
60 /**
61 * Constructs an input stream from a given filename, URL or internal resource.
62 *
63 * @param name can be:<ul>
64 * <li>relative or absolute file name</li>
65 * <li>{@code file:///SOME/FILE} the same as above</li>
66 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
67 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
68 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
69 * @throws IOException when the resource with the given name could not be retrieved
70 */
71 public MirroredInputStream(String name) throws IOException {
72 this(name, null, DEFAULT_MAXTIME, null);
73 }
74
75 /**
76 * Constructs an input stream from a given filename, URL or internal resource.
77 *
78 * @param name can be:<ul>
79 * <li>relative or absolute file name</li>
80 * <li>{@code file:///SOME/FILE} the same as above</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 config directory (since r7058)</li>
83 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
84 * @param maxTime the maximum age of the cache file (in seconds)
85 * @throws IOException when the resource with the given name could not be retrieved
86 */
87 public MirroredInputStream(String name, long maxTime) throws IOException {
88 this(name, null, maxTime, null);
89 }
90
91 /**
92 * Constructs an input stream from a given filename, URL or internal resource.
93 *
94 * @param name can be:<ul>
95 * <li>relative or absolute file name</li>
96 * <li>{@code file:///SOME/FILE} the same as above</li>
97 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
98 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
99 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
100 * @param destDir the destination directory for the cache file. Only applies for URLs.
101 * @throws IOException when the resource with the given name could not be retrieved
102 */
103 public MirroredInputStream(String name, String destDir) throws IOException {
104 this(name, destDir, DEFAULT_MAXTIME, null);
105 }
106
107 /**
108 * Constructs an input stream from a given filename, URL or internal resource.
109 *
110 * @param name can be:<ul>
111 * <li>relative or absolute file name</li>
112 * <li>{@code file:///SOME/FILE} the same as above</li>
113 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
114 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
115 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
116 * @param destDir the destination directory for the cache file. Only applies for URLs.
117 * @param maxTime the maximum age of the cache file (in seconds)
118 * @throws IOException when the resource with the given name could not be retrieved
119 */
120 public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
121 this(name, destDir, maxTime, null);
122 }
123
124 /**
125 * Constructs an input stream from a given filename, URL or internal resource.
126 *
127 * @param name can be:<ul>
128 * <li>relative or absolute file name</li>
129 * <li>{@code file:///SOME/FILE} the same as above</li>
130 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
131 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
132 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
133 * @param destDir the destination directory for the cache file. Only applies for URLs.
134 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs.
135 * @throws IOException when the resource with the given name could not be retrieved
136 * @since 6867
137 */
138 public MirroredInputStream(String name, String destDir, String httpAccept) throws IOException {
139 this(name, destDir, DEFAULT_MAXTIME, httpAccept);
140 }
141
142 /**
143 * Constructs an input stream from a given filename, URL or internal resource.
144 *
145 * @param name can be:<ul>
146 * <li>relative or absolute file name</li>
147 * <li>{@code file:///SOME/FILE} the same as above</li>
148 * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
149 * <li>{@code josmdir://SOME/FILE} file inside josm config directory (since r7058)</li>
150 * <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
151 * @param destDir the destination directory for the cache file. Only applies for URLs.
152 * @param maxTime the maximum age of the cache file (in seconds)
153 * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs.
154 * @throws IOException when the resource with the given name could not be retrieved
155 * @since 6867
156 */
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 {
178 URL url;
179 try {
180 url = new URL(name);
181 if ("file".equals(url.getProtocol())) {
182 file = new File(name.substring("file:/".length()));
183 if (!file.exists()) {
184 file = new File(name.substring("file://".length()));
185 }
186 } else {
187 file = checkLocal(url, destDir, maxTime, httpAccept, caching);
188 }
189 } catch (java.net.MalformedURLException e) {
190 if (name.startsWith("resource://")) {
191 fs = getClass().getResourceAsStream(
192 name.substring("resource:/".length()));
193 if (fs == null)
194 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
195 return;
196 } else if (name.startsWith("josmdir://")) {
197 file = new File(Main.pref.getPreferencesDir(), name.substring("josmdir://".length()));
198 } else {
199 file = new File(name);
200 }
201 }
202 if (file == null)
203 throw new IOException();
204 fs = new FileInputStream(file);
205 }
206
207 /**
208 * Looks for a certain entry inside a zip file and returns the entry path.
209 *
210 * Replies a file in the top level directory of the ZIP file which has an
211 * extension <code>extension</code>. If more than one files have this
212 * extension, the last file whose name includes <code>namepart</code>
213 * is opened.
214 *
215 * @param extension the extension of the file we're looking for
216 * @param namepart the name part
217 * @return The zip entry path of the matching file. Null if this mirrored
218 * input stream doesn't represent a zip file or if there was no matching
219 * file in the ZIP file.
220 */
221 public String findZipEntryPath(String extension, String namepart) {
222 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
223 if (ze == null) return null;
224 return ze.a;
225 }
226
227 /**
228 * Like {@link #findZipEntryPath}, but returns the corresponding InputStream.
229 * @since 6148
230 */
231 public InputStream findZipEntryInputStream(String extension, String namepart) {
232 Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
233 if (ze == null) return null;
234 return ze.b;
235 }
236
237 @SuppressWarnings("resource")
238 private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) {
239 if (file == null)
240 return null;
241 Pair<String, InputStream> res = null;
242 try {
243 ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8);
244 ZipEntry resentry = null;
245 Enumeration<? extends ZipEntry> entries = zipFile.entries();
246 while (entries.hasMoreElements()) {
247 ZipEntry entry = entries.nextElement();
248 if (entry.getName().endsWith("." + extension)) {
249 /* choose any file with correct extension. When more than
250 one file, prefer the one which matches namepart */
251 if (resentry == null || entry.getName().indexOf(namepart) >= 0) {
252 resentry = entry;
253 }
254 }
255 }
256 if (resentry != null) {
257 InputStream is = zipFile.getInputStream(resentry);
258 res = Pair.create(resentry.getName(), is);
259 } else {
260 Utils.close(zipFile);
261 }
262 } catch (Exception e) {
263 if (file.getName().endsWith(".zip")) {
264 Main.warn(tr("Failed to open file with extension ''{2}'' and namepart ''{3}'' in zip file ''{0}''. Exception was: {1}",
265 file.getName(), e.toString(), extension, namepart));
266 }
267 }
268 return res;
269 }
270
271 /**
272 * Replies the local file.
273 * @return The local file on disk
274 */
275 public File getFile() {
276 return file;
277 }
278
279 public static void cleanup(String name) {
280 cleanup(name, null);
281 }
282
283 public static void cleanup(String name, String destDir) {
284 URL url;
285 try {
286 url = new URL(name);
287 if (!"file".equals(url.getProtocol())) {
288 String prefKey = getPrefKey(url, destDir);
289 List<String> localPath = new ArrayList<>(Main.pref.getCollection(prefKey));
290 if (localPath.size() == 2) {
291 File lfile = new File(localPath.get(1));
292 if(lfile.exists()) {
293 lfile.delete();
294 }
295 }
296 Main.pref.putCollection(prefKey, null);
297 }
298 } catch (MalformedURLException e) {
299 Main.warn(e);
300 }
301 }
302
303 /**
304 * get preference key to store the location and age of the cached file.
305 * 2 resources that point to the same url, but that are to be stored in different
306 * directories will not share a cache file.
307 */
308 private static String getPrefKey(URL url, String destDir) {
309 StringBuilder prefKey = new StringBuilder("mirror.");
310 if (destDir != null) {
311 prefKey.append(destDir);
312 prefKey.append(".");
313 }
314 prefKey.append(url.toString());
315 return prefKey.toString().replaceAll("=","_");
316 }
317
318 private File checkLocal(URL url, String destDir, long maxTime, String httpAccept, CachingStrategy caching) throws IOException {
319 String prefKey = getPrefKey(url, destDir);
320 long age = 0L;
321 Long ifModifiedSince = null;
322 File localFile = null;
323 List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey));
324 if (localPathEntry.size() == 2) {
325 localFile = new File(localPathEntry.get(1));
326 if(!localFile.exists())
327 localFile = null;
328 else {
329 if ( maxTime == DEFAULT_MAXTIME
330 || maxTime <= 0 // arbitrary value <= 0 is deprecated
331 ) {
332 maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); // one week
333 }
334 age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0));
335 if (age < maxTime*1000) {
336 return localFile;
337 }
338 if (caching == CachingStrategy.IfModifiedSince) {
339 ifModifiedSince = Long.parseLong(localPathEntry.get(0));
340 }
341 }
342 }
343 if (destDir == null) {
344 destDir = Main.pref.getCacheDirectory().getPath();
345 }
346
347 File destDirFile = new File(destDir);
348 if (!destDirFile.exists()) {
349 destDirFile.mkdirs();
350 }
351
352 String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
353 String localPath = "mirror_" + a;
354 destDirFile = new File(destDir, localPath + ".tmp");
355 try {
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 }
364 try (
365 InputStream bis = new BufferedInputStream(con.getInputStream());
366 OutputStream fos = new FileOutputStream(destDirFile);
367 OutputStream bos = new BufferedOutputStream(fos)
368 ) {
369 byte[] buffer = new byte[4096];
370 int length;
371 while ((length = bis.read(buffer)) > -1) {
372 bos.write(buffer, 0, length);
373 }
374 }
375 localFile = new File(destDir, localPath);
376 if(Main.platform.rename(destDirFile, localFile)) {
377 Main.pref.putCollection(prefKey,
378 Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString()));
379 } else {
380 Main.warn(tr("Failed to rename file {0} to {1}.",
381 destDirFile.getPath(), localFile.getPath()));
382 }
383 } catch (IOException e) {
384 if (age >= maxTime*1000 && age < maxTime*1000*2) {
385 Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", url, e));
386 return localFile;
387 } else {
388 throw e;
389 }
390 }
391
392 return localFile;
393 }
394
395 /**
396 * Opens a connection for downloading a resource.
397 * <p>
398 * Manually follows redirects because
399 * {@link HttpURLConnection#setFollowRedirects(boolean)} fails if the redirect
400 * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.
401 * <p>
402 * This can causes problems when downloading from certain GitHub URLs.
403 *
404 * @param downloadUrl The resource URL to download
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
407 * @return The HTTP connection effectively linked to the resource, after all potential redirections
408 * @throws MalformedURLException If a redirected URL is wrong
409 * @throws IOException If any I/O operation goes wrong
410 * @since 6867
411 */
412 public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, IOException {
413 HttpURLConnection con = null;
414 int numRedirects = 0;
415 while(true) {
416 con = Utils.openHttpConnection(downloadUrl);
417 if (ifModifiedSince != null) {
418 con.setIfModifiedSince(ifModifiedSince);
419 }
420 con.setInstanceFollowRedirects(false);
421 con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
422 con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
423 Main.debug("GET "+downloadUrl);
424 if (httpAccept != null) {
425 Main.debug("Accept: "+httpAccept);
426 con.setRequestProperty("Accept", httpAccept);
427 }
428 try {
429 con.connect();
430 } catch (IOException e) {
431 Main.addNetworkError(downloadUrl, Utils.getRootCause(e));
432 throw e;
433 }
434 switch(con.getResponseCode()) {
435 case HttpURLConnection.HTTP_OK:
436 return con;
437 case HttpURLConnection.HTTP_NOT_MODIFIED:
438 if (ifModifiedSince != null)
439 return con;
440 case HttpURLConnection.HTTP_MOVED_PERM:
441 case HttpURLConnection.HTTP_MOVED_TEMP:
442 case HttpURLConnection.HTTP_SEE_OTHER:
443 String redirectLocation = con.getHeaderField("Location");
444 if (downloadUrl == null) {
445 /* 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());
446 throw new IOException(msg);
447 }
448 downloadUrl = new URL(redirectLocation);
449 // keep track of redirect attempts to break a redirect loops if it happens
450 // to occur for whatever reason
451 numRedirects++;
452 if (numRedirects >= Main.pref.getInteger("socket.maxredirects", 5)) {
453 String msg = tr("Too many redirects to the download URL detected. Aborting.");
454 throw new IOException(msg);
455 }
456 Main.info(tr("Download redirected to ''{0}''", downloadUrl));
457 break;
458 default:
459 String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadUrl, con.getResponseCode());
460 throw new IOException(msg);
461 }
462 }
463 }
464
465 @Override
466 public int available() throws IOException
467 { return fs.available(); }
468 @Override
469 public void close() throws IOException
470 { Utils.close(fs); }
471 @Override
472 public int read() throws IOException
473 { return fs.read(); }
474 @Override
475 public int read(byte[] b) throws IOException
476 { return fs.read(b); }
477 @Override
478 public int read(byte[] b, int off, int len) throws IOException
479 { return fs.read(b,off, len); }
480 @Override
481 public long skip(long n) throws IOException
482 { return fs.skip(n); }
483}
Note: See TracBrowser for help on using the repository browser.