1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedInputStream;
|
---|
7 | import java.io.BufferedOutputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.io.OutputStream;
|
---|
14 | import java.net.HttpURLConnection;
|
---|
15 | import java.net.MalformedURLException;
|
---|
16 | import java.net.URL;
|
---|
17 | import java.nio.charset.StandardCharsets;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.Arrays;
|
---|
20 | import java.util.Enumeration;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
23 | import java.util.Map.Entry;
|
---|
24 | import java.util.concurrent.ConcurrentHashMap;
|
---|
25 | import java.util.zip.ZipEntry;
|
---|
26 | import java.util.zip.ZipFile;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
30 | import org.openstreetmap.josm.tools.Pair;
|
---|
31 | import org.openstreetmap.josm.tools.Utils;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Downloads a file and caches it on disk in order to reduce network load.
|
---|
35 | *
|
---|
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.)
|
---|
38 | * <p>
|
---|
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()}.
|
---|
44 | */
|
---|
45 | public class CachedFile {
|
---|
46 |
|
---|
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 | */
|
---|
55 | MaxAge,
|
---|
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 | */
|
---|
62 | IfModifiedSince
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected String name;
|
---|
66 | protected long maxAge;
|
---|
67 | protected String destDir;
|
---|
68 | protected String httpAccept;
|
---|
69 | protected CachingStrategy cachingStrategy;
|
---|
70 |
|
---|
71 | protected File cacheFile = null;
|
---|
72 | protected boolean initialized = false;
|
---|
73 |
|
---|
74 | public static final long DEFAULT_MAXTIME = -1L;
|
---|
75 | public static final long DAYS = 24*60*60; // factor to get caching time in days
|
---|
76 |
|
---|
77 | private Map<String, String> httpHeaders = new ConcurrentHashMap<>();
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Constructs a CachedFile object from a given filename, URL or internal resource.
|
---|
81 | *
|
---|
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>
|
---|
85 | * <li>{@code http://...} a URL. It will be cached on disk.</li>
|
---|
86 | * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
|
---|
87 | * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li>
|
---|
88 | * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul>
|
---|
89 | */
|
---|
90 | public CachedFile(String name) {
|
---|
91 | this.name = name;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Set the name of the resource.
|
---|
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>
|
---|
99 | * <li>{@code http://...} a URL. It will be cached on disk.</li>
|
---|
100 | * <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
|
---|
101 | * <li>{@code josmdir://SOME/FILE} file inside josm user data directory (since r7058)</li>
|
---|
102 | * <li>{@code josmplugindir://SOME/FILE} file inside josm plugin directory (since r7834)</li></ul>
|
---|
103 | * @return this object
|
---|
104 | */
|
---|
105 | public CachedFile setName(String name) {
|
---|
106 | this.name = name;
|
---|
107 | return this;
|
---|
108 | }
|
---|
109 |
|
---|
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 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Set the destination directory for the cache file. Only applies to URLs.
|
---|
124 | * @param destDir the destination directory
|
---|
125 | * @return this object
|
---|
126 | */
|
---|
127 | public CachedFile setDestDir(String destDir) {
|
---|
128 | this.destDir = destDir;
|
---|
129 | return this;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
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
|
---|
136 | */
|
---|
137 | public CachedFile setHttpAccept(String httpAccept) {
|
---|
138 | this.httpAccept = httpAccept;
|
---|
139 | return this;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Set the caching strategy. Only applies to URLs.
|
---|
144 | * @param cachingStrategy caching strategy
|
---|
145 | * @return this object
|
---|
146 | */
|
---|
147 | public CachedFile setCachingStrategy(CachingStrategy cachingStrategy) {
|
---|
148 | this.cachingStrategy = cachingStrategy;
|
---|
149 | return this;
|
---|
150 | }
|
---|
151 |
|
---|
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 |
|
---|
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 |
|
---|
182 | /**
|
---|
183 | * Get InputStream to the requested resource.
|
---|
184 | * @return the InputStream
|
---|
185 | * @throws IOException when the resource with the given name could not be retrieved
|
---|
186 | */
|
---|
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;
|
---|
196 | } else {
|
---|
197 | throw new IOException("No file found for: "+name);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | return new FileInputStream(file);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
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.
|
---|
207 | * @throws IOException when the resource with the given name could not be retrieved
|
---|
208 | */
|
---|
209 | public synchronized File getFile() throws IOException {
|
---|
210 | if (initialized)
|
---|
211 | return cacheFile;
|
---|
212 | initialized = true;
|
---|
213 | URL url;
|
---|
214 | try {
|
---|
215 | url = new URL(name);
|
---|
216 | if ("file".equals(url.getProtocol())) {
|
---|
217 | cacheFile = new File(name.substring("file:/".length() - 1));
|
---|
218 | if (!cacheFile.exists()) {
|
---|
219 | cacheFile = new File(name.substring("file://".length() - 1));
|
---|
220 | }
|
---|
221 | } else {
|
---|
222 | cacheFile = checkLocal(url);
|
---|
223 | }
|
---|
224 | } catch (MalformedURLException e) {
|
---|
225 | if (name.startsWith("resource://")) {
|
---|
226 | return null;
|
---|
227 | } else if (name.startsWith("josmdir://")) {
|
---|
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()));
|
---|
231 | } else {
|
---|
232 | cacheFile = new File(name);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | if (cacheFile == null)
|
---|
236 | throw new IOException("Unable to get cache file for "+name);
|
---|
237 | return cacheFile;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
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>
|
---|
246 | * is opened.
|
---|
247 | *
|
---|
248 | * @param extension the extension of the file we're looking for
|
---|
249 | * @param namepart the name part
|
---|
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
|
---|
252 | * file in the ZIP file.
|
---|
253 | */
|
---|
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.
|
---|
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.
|
---|
267 | * @since 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) {
|
---|
276 | File file = null;
|
---|
277 | try {
|
---|
278 | file = getFile();
|
---|
279 | } catch (IOException ex) {
|
---|
280 | Main.warn(ex, false);
|
---|
281 | }
|
---|
282 | if (file == null)
|
---|
283 | return null;
|
---|
284 | Pair<String, InputStream> res = null;
|
---|
285 | try {
|
---|
286 | ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8);
|
---|
287 | ZipEntry resentry = null;
|
---|
288 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
---|
289 | while (entries.hasMoreElements()) {
|
---|
290 | ZipEntry entry = entries.nextElement();
|
---|
291 | if (entry.getName().endsWith("." + extension)) {
|
---|
292 | /* choose any file with correct extension. When more than
|
---|
293 | one file, prefer the one which matches namepart */
|
---|
294 | if (resentry == null || entry.getName().indexOf(namepart) >= 0) {
|
---|
295 | resentry = entry;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 | if (resentry != null) {
|
---|
300 | InputStream is = zipFile.getInputStream(resentry);
|
---|
301 | res = Pair.create(resentry.getName(), is);
|
---|
302 | } else {
|
---|
303 | Utils.close(zipFile);
|
---|
304 | }
|
---|
305 | } catch (Exception e) {
|
---|
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}",
|
---|
308 | file.getName(), e.toString(), extension, namepart));
|
---|
309 | }
|
---|
310 | }
|
---|
311 | return res;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Clear the cache for the given resource.
|
---|
316 | * This forces a fresh download.
|
---|
317 | * @param name the URL
|
---|
318 | */
|
---|
319 | public static void cleanup(String name) {
|
---|
320 | cleanup(name, null);
|
---|
321 | }
|
---|
322 |
|
---|
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 | */
|
---|
329 | public static void cleanup(String name, String destDir) {
|
---|
330 | URL url;
|
---|
331 | try {
|
---|
332 | url = new URL(name);
|
---|
333 | if (!"file".equals(url.getProtocol())) {
|
---|
334 | String prefKey = getPrefKey(url, destDir);
|
---|
335 | List<String> localPath = new ArrayList<>(Main.pref.getCollection(prefKey));
|
---|
336 | if (localPath.size() == 2) {
|
---|
337 | File lfile = new File(localPath.get(1));
|
---|
338 | if (lfile.exists()) {
|
---|
339 | lfile.delete();
|
---|
340 | }
|
---|
341 | }
|
---|
342 | Main.pref.putCollection(prefKey, null);
|
---|
343 | }
|
---|
344 | } catch (MalformedURLException e) {
|
---|
345 | Main.warn(e);
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Get preference key to store the location and age of the cached file.
|
---|
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) {
|
---|
357 | prefKey.append(destDir).append('.');
|
---|
358 | }
|
---|
359 | prefKey.append(url.toString());
|
---|
360 | return prefKey.toString().replaceAll("=", "_");
|
---|
361 | }
|
---|
362 |
|
---|
363 | private File checkLocal(URL url) throws IOException {
|
---|
364 | String prefKey = getPrefKey(url, destDir);
|
---|
365 | String urlStr = url.toExternalForm();
|
---|
366 | long age = 0L;
|
---|
367 | long lMaxAge = maxAge;
|
---|
368 | Long ifModifiedSince = null;
|
---|
369 | File localFile = null;
|
---|
370 | List<String> localPathEntry = new ArrayList<>(Main.pref.getCollection(prefKey));
|
---|
371 | boolean offline = false;
|
---|
372 | try {
|
---|
373 | checkOfflineAccess(urlStr);
|
---|
374 | } catch (OfflineAccessException e) {
|
---|
375 | offline = true;
|
---|
376 | }
|
---|
377 | if (localPathEntry.size() == 2) {
|
---|
378 | localFile = new File(localPathEntry.get(1));
|
---|
379 | if (!localFile.exists()) {
|
---|
380 | localFile = null;
|
---|
381 | } else {
|
---|
382 | if (maxAge == DEFAULT_MAXTIME
|
---|
383 | || maxAge <= 0 // arbitrary value <= 0 is deprecated
|
---|
384 | ) {
|
---|
385 | lMaxAge = Main.pref.getInteger("mirror.maxtime", 7*24*60*60); // one week
|
---|
386 | }
|
---|
387 | age = System.currentTimeMillis() - Long.parseLong(localPathEntry.get(0));
|
---|
388 | if (offline || age < lMaxAge*1000) {
|
---|
389 | return localFile;
|
---|
390 | }
|
---|
391 | if (cachingStrategy == CachingStrategy.IfModifiedSince) {
|
---|
392 | ifModifiedSince = Long.valueOf(localPathEntry.get(0));
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 | if (destDir == null) {
|
---|
397 | destDir = Main.pref.getCacheDirectory().getPath();
|
---|
398 | }
|
---|
399 |
|
---|
400 | File destDirFile = new File(destDir);
|
---|
401 | if (!destDirFile.exists()) {
|
---|
402 | destDirFile.mkdirs();
|
---|
403 | }
|
---|
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_.-]", "_");
|
---|
411 | String localPath = "mirror_" + a;
|
---|
412 | destDirFile = new File(destDir, localPath + ".tmp");
|
---|
413 | try {
|
---|
414 | HttpURLConnection con = connectFollowingRedirect(url, httpAccept, ifModifiedSince, httpHeaders);
|
---|
415 | if (ifModifiedSince != null && con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
---|
416 | if (Main.isDebugEnabled()) {
|
---|
417 | Main.debug("304 Not Modified ("+urlStr+")");
|
---|
418 | }
|
---|
419 | if (localFile == null)
|
---|
420 | throw new AssertionError();
|
---|
421 | Main.pref.putCollection(prefKey,
|
---|
422 | Arrays.asList(Long.toString(System.currentTimeMillis()), localPathEntry.get(1)));
|
---|
423 | return localFile;
|
---|
424 | }
|
---|
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 | }
|
---|
435 | }
|
---|
436 | localFile = new File(destDir, localPath);
|
---|
437 | if (Main.platform.rename(destDirFile, localFile)) {
|
---|
438 | Main.pref.putCollection(prefKey,
|
---|
439 | Arrays.asList(Long.toString(System.currentTimeMillis()), localFile.toString()));
|
---|
440 | } else {
|
---|
441 | Main.warn(tr("Failed to rename file {0} to {1}.",
|
---|
442 | destDirFile.getPath(), localFile.getPath()));
|
---|
443 | }
|
---|
444 | } catch (IOException e) {
|
---|
445 | if (age >= lMaxAge*1000 && age < lMaxAge*1000*2) {
|
---|
446 | Main.warn(tr("Failed to load {0}, use cached file and retry next time: {1}", urlStr, e));
|
---|
447 | return localFile;
|
---|
448 | } else {
|
---|
449 | throw e;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | return localFile;
|
---|
454 | }
|
---|
455 |
|
---|
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 |
|
---|
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
|
---|
466 | * is going from a http to a https URL, see <a href="https://bugs.openjdk.java.net/browse/JDK-4620571">bug report</a>.
|
---|
467 | * <p>
|
---|
468 | * This can cause problems when downloading from certain GitHub URLs.
|
---|
469 | *
|
---|
470 | * @param downloadUrl The resource URL to download
|
---|
471 | * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}
|
---|
472 | * @param ifModifiedSince The download time of the cache file, optional
|
---|
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
|
---|
476 | * @throws OfflineAccessException if resource is accessed in offline mode, in any protocol
|
---|
477 | * @since 6867
|
---|
478 | */
|
---|
479 | public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince)
|
---|
480 | throws MalformedURLException, IOException {
|
---|
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 | */
|
---|
502 | public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince,
|
---|
503 | Map<String, String> headers) throws MalformedURLException, IOException {
|
---|
504 | CheckParameterUtil.ensureParameterNotNull(downloadUrl, "downloadUrl");
|
---|
505 | String downloadString = downloadUrl.toExternalForm();
|
---|
506 |
|
---|
507 | checkOfflineAccess(downloadString);
|
---|
508 |
|
---|
509 | int numRedirects = 0;
|
---|
510 | while (true) {
|
---|
511 | HttpURLConnection con = Utils.openHttpConnection(downloadUrl);
|
---|
512 | if (ifModifiedSince != null) {
|
---|
513 | con.setIfModifiedSince(ifModifiedSince);
|
---|
514 | }
|
---|
515 | if (headers != null) {
|
---|
516 | for (Entry<String, String> header: headers.entrySet()) {
|
---|
517 | con.setRequestProperty(header.getKey(), header.getValue());
|
---|
518 | }
|
---|
519 | }
|
---|
520 | con.setInstanceFollowRedirects(false);
|
---|
521 | con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000);
|
---|
522 | con.setReadTimeout(Main.pref.getInteger("socket.timeout.read", 30)*1000);
|
---|
523 | if (Main.isDebugEnabled()) {
|
---|
524 | Main.debug("GET "+downloadString);
|
---|
525 | }
|
---|
526 | if (httpAccept != null) {
|
---|
527 | if (Main.isTraceEnabled()) {
|
---|
528 | Main.trace("Accept: "+httpAccept);
|
---|
529 | }
|
---|
530 | con.setRequestProperty("Accept", httpAccept);
|
---|
531 | }
|
---|
532 | try {
|
---|
533 | con.connect();
|
---|
534 | } catch (IOException e) {
|
---|
535 | Main.addNetworkError(downloadUrl, Utils.getRootCause(e));
|
---|
536 | throw e;
|
---|
537 | }
|
---|
538 | switch(con.getResponseCode()) {
|
---|
539 | case HttpURLConnection.HTTP_OK:
|
---|
540 | return con;
|
---|
541 | case HttpURLConnection.HTTP_NOT_MODIFIED:
|
---|
542 | if (ifModifiedSince != null)
|
---|
543 | return con;
|
---|
544 | case HttpURLConnection.HTTP_MOVED_PERM:
|
---|
545 | case HttpURLConnection.HTTP_MOVED_TEMP:
|
---|
546 | case HttpURLConnection.HTTP_SEE_OTHER:
|
---|
547 | String redirectLocation = con.getHeaderField("Location");
|
---|
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());
|
---|
552 | throw new IOException(msg);
|
---|
553 | }
|
---|
554 | downloadUrl = new URL(redirectLocation);
|
---|
555 | downloadString = downloadUrl.toExternalForm();
|
---|
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)) {
|
---|
560 | String msg = tr("Too many redirects to the download URL detected. Aborting.");
|
---|
561 | throw new IOException(msg);
|
---|
562 | }
|
---|
563 | Main.info(tr("Download redirected to ''{0}''", downloadString));
|
---|
564 | break;
|
---|
565 | default:
|
---|
566 | String msg = tr("Failed to read from ''{0}''. Server responded with status code {1}.", downloadString, con.getResponseCode());
|
---|
567 | throw new IOException(msg);
|
---|
568 | }
|
---|
569 | }
|
---|
570 | }
|
---|
571 | }
|
---|