Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix; |
2 | ||
3 | import java.io.BufferedOutputStream; | |
4 | import java.io.File; | |
5 | import java.io.FileInputStream; | |
57f02339 | 6 | import java.io.FileNotFoundException; |
08fe2e33 NR |
7 | import java.io.FileOutputStream; |
8 | import java.io.IOException; | |
9 | import java.io.InputStream; | |
6e06d2cc | 10 | import java.io.OutputStreamWriter; |
08fe2e33 NR |
11 | import java.net.CookieHandler; |
12 | import java.net.CookieManager; | |
13 | import java.net.CookiePolicy; | |
14 | import java.net.CookieStore; | |
15 | import java.net.HttpCookie; | |
16 | import java.net.HttpURLConnection; | |
08fe2e33 NR |
17 | import java.net.URL; |
18 | import java.net.URLConnection; | |
6e06d2cc | 19 | import java.net.URLEncoder; |
08fe2e33 NR |
20 | import java.util.Date; |
21 | import java.util.Map; | |
22 | import java.util.zip.GZIPInputStream; | |
23 | ||
24 | import javax.imageio.ImageIO; | |
25 | ||
26 | import be.nikiroo.fanfix.bundles.Config; | |
27 | import be.nikiroo.fanfix.supported.BasicSupport; | |
28 | import be.nikiroo.utils.IOUtils; | |
326093dc | 29 | import be.nikiroo.utils.ImageUtils; |
08fe2e33 | 30 | import be.nikiroo.utils.MarkableFileInputStream; |
08fe2e33 NR |
31 | |
32 | /** | |
33 | * This cache will manage Internet (and local) downloads, as well as put the | |
34 | * downloaded files into a cache. | |
35 | * <p> | |
36 | * As long the cached resource is not too old, it will use it instead of | |
37 | * retrieving the file again. | |
38 | * | |
39 | * @author niki | |
40 | */ | |
41 | public class Cache { | |
42 | private File dir; | |
43 | private String UA; | |
44 | private long tooOldChanging; | |
45 | private long tooOldStable; | |
46 | private CookieManager cookies; | |
47 | ||
48 | /** | |
49 | * Create a new {@link Cache} object. | |
50 | * | |
51 | * @param dir | |
52 | * the directory to use as cache | |
53 | * @param UA | |
54 | * the User-Agent to use to download the resources | |
55 | * @param hoursChanging | |
56 | * the number of hours after which a cached file that is thought | |
57 | * to change ~often is considered too old (or -1 for | |
58 | * "never too old") | |
59 | * @param hoursStable | |
60 | * the number of hours after which a LARGE cached file that is | |
61 | * thought to change rarely is considered too old (or -1 for | |
62 | * "never too old") | |
63 | * | |
64 | * @throws IOException | |
65 | * in case of I/O error | |
66 | */ | |
67 | public Cache(File dir, String UA, int hoursChanging, int hoursStable) | |
68 | throws IOException { | |
69 | this.dir = dir; | |
70 | this.UA = UA; | |
71 | this.tooOldChanging = 1000 * 60 * 60 * hoursChanging; | |
72 | this.tooOldStable = 1000 * 60 * 60 * hoursStable; | |
73 | ||
74 | if (dir != null) { | |
75 | if (!dir.exists()) { | |
76 | dir.mkdirs(); | |
77 | } | |
78 | } | |
79 | ||
80 | if (dir == null || !dir.exists()) { | |
81 | throw new IOException("Cannot create the cache directory: " | |
82 | + (dir == null ? "null" : dir.getAbsolutePath())); | |
83 | } | |
84 | ||
85 | cookies = new CookieManager(); | |
86 | cookies.setCookiePolicy(CookiePolicy.ACCEPT_ALL); | |
87 | CookieHandler.setDefault(cookies); | |
88 | } | |
89 | ||
6e06d2cc NR |
90 | /** |
91 | * Clear all the cookies currently in the jar. | |
92 | */ | |
93 | public void clearCookies() { | |
94 | cookies.getCookieStore().removeAll(); | |
95 | } | |
96 | ||
08fe2e33 NR |
97 | /** |
98 | * Open a resource (will load it from the cache if possible, or save it into | |
99 | * the cache after downloading if not). | |
100 | * | |
101 | * @param url | |
102 | * the resource to open | |
103 | * @param support | |
104 | * the support to use to download the resource | |
105 | * @param stable | |
106 | * TRUE for more stable resources, FALSE when they often change | |
107 | * | |
595dfa7a | 108 | * @return the opened resource, NOT NULL |
08fe2e33 NR |
109 | * |
110 | * @throws IOException | |
111 | * in case of I/O error | |
112 | */ | |
113 | public InputStream open(URL url, BasicSupport support, boolean stable) | |
114 | throws IOException { | |
595dfa7a | 115 | // MUST NOT return null |
08fe2e33 NR |
116 | return open(url, support, stable, url); |
117 | } | |
118 | ||
119 | /** | |
120 | * Open a resource (will load it from the cache if possible, or save it into | |
121 | * the cache after downloading if not). | |
122 | * <p> | |
123 | * The cached resource will be assimilated to the given original {@link URL} | |
124 | * | |
125 | * @param url | |
126 | * the resource to open | |
127 | * @param support | |
128 | * the support to use to download the resource | |
129 | * @param stable | |
130 | * TRUE for more stable resources, FALSE when they often change | |
131 | * @param originalUrl | |
132 | * the original {@link URL} used to locate the cached resource | |
133 | * | |
595dfa7a | 134 | * @return the opened resource, NOT NULL |
08fe2e33 NR |
135 | * |
136 | * @throws IOException | |
137 | * in case of I/O error | |
138 | */ | |
139 | public InputStream open(URL url, BasicSupport support, boolean stable, | |
140 | URL originalUrl) throws IOException { | |
595dfa7a | 141 | // MUST NOT return null |
08fe2e33 NR |
142 | try { |
143 | InputStream in = load(originalUrl, false, stable); | |
144 | if (in == null) { | |
145 | try { | |
146 | save(url, support, originalUrl); | |
147 | } catch (IOException e) { | |
148 | throw new IOException("Cannot save the url: " | |
149 | + (url == null ? "null" : url.toString()), e); | |
150 | } | |
151 | ||
595dfa7a | 152 | // Was just saved, can load old, so, will not be null |
08fe2e33 NR |
153 | in = load(originalUrl, true, stable); |
154 | } | |
155 | ||
156 | return in; | |
157 | } catch (IOException e) { | |
158 | throw new IOException("Cannot open the url: " | |
159 | + (url == null ? "null" : url.toString()), e); | |
160 | } | |
161 | } | |
162 | ||
6e06d2cc NR |
163 | /** |
164 | * Open the given {@link URL} without using the cache, but still using and | |
165 | * updating the cookies. | |
166 | * | |
167 | * @param url | |
168 | * the {@link URL} to open | |
169 | * @param support | |
170 | * the {@link BasicSupport} used for the cookies | |
171 | * | |
172 | * @return the {@link InputStream} of the opened page | |
173 | * | |
174 | * @throws IOException | |
175 | * in case of I/O error | |
176 | */ | |
177 | public InputStream openNoCache(URL url, BasicSupport support) | |
178 | throws IOException { | |
179 | return openNoCache(url, support, url, null); | |
180 | } | |
181 | ||
182 | /** | |
183 | * Open the given {@link URL} without using the cache, but still using and | |
184 | * updating the cookies. | |
185 | * | |
186 | * @param url | |
187 | * the {@link URL} to open | |
188 | * @param support | |
189 | * the {@link BasicSupport} used for the cookies | |
190 | * @param postParams | |
191 | * the POST parameters | |
192 | * | |
193 | * @return the {@link InputStream} of the opened page | |
194 | * | |
195 | * @throws IOException | |
196 | * in case of I/O error | |
197 | */ | |
198 | public InputStream openNoCache(URL url, BasicSupport support, | |
199 | Map<String, String> postParams) throws IOException { | |
200 | return openNoCache(url, support, url, postParams); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Open the given {@link URL} without using the cache, but still using and | |
205 | * updating the cookies. | |
206 | * | |
207 | * @param url | |
208 | * the {@link URL} to open | |
209 | * @param support | |
210 | * the {@link BasicSupport} used for the cookies | |
211 | * @param originalUrl | |
212 | * the original {@link URL} before any redirection occurs | |
213 | * @param postParams | |
214 | * the POST parameters | |
215 | * | |
216 | * @return the {@link InputStream} of the opened page | |
217 | * | |
218 | * @throws IOException | |
219 | * in case of I/O error | |
220 | */ | |
221 | private InputStream openNoCache(URL url, BasicSupport support, | |
222 | final URL originalUrl, Map<String, String> postParams) | |
223 | throws IOException { | |
224 | ||
225 | URLConnection conn = openConnectionWithCookies(url, support); | |
13285ff8 | 226 | if (postParams != null && conn instanceof HttpURLConnection) { |
6e06d2cc NR |
227 | StringBuilder postData = new StringBuilder(); |
228 | for (Map.Entry<String, String> param : postParams.entrySet()) { | |
229 | if (postData.length() != 0) | |
230 | postData.append('&'); | |
231 | postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); | |
232 | postData.append('='); | |
233 | postData.append(URLEncoder.encode( | |
234 | String.valueOf(param.getValue()), "UTF-8")); | |
235 | } | |
236 | ||
237 | conn.setDoOutput(true); | |
13285ff8 NR |
238 | ((HttpURLConnection) conn).setRequestMethod("POST"); |
239 | conn.setRequestProperty("Content-Type", | |
240 | "application/x-www-form-urlencoded"); | |
241 | conn.setRequestProperty("charset", "utf-8"); | |
6e06d2cc NR |
242 | |
243 | OutputStreamWriter writer = new OutputStreamWriter( | |
244 | conn.getOutputStream()); | |
245 | ||
246 | writer.write(postData.toString()); | |
247 | writer.flush(); | |
248 | writer.close(); | |
249 | } | |
250 | ||
251 | conn.connect(); | |
252 | ||
253 | // Check if redirect | |
254 | if (conn instanceof HttpURLConnection | |
255 | && ((HttpURLConnection) conn).getResponseCode() / 100 == 3) { | |
256 | String newUrl = conn.getHeaderField("Location"); | |
257 | return openNoCache(new URL(newUrl), support, originalUrl, | |
258 | postParams); | |
259 | } | |
260 | ||
261 | InputStream in = conn.getInputStream(); | |
262 | if ("gzip".equals(conn.getContentEncoding())) { | |
263 | in = new GZIPInputStream(in); | |
264 | } | |
265 | ||
266 | return in; | |
267 | } | |
268 | ||
08fe2e33 NR |
269 | /** |
270 | * Refresh the resource into cache if needed. | |
271 | * | |
272 | * @param url | |
273 | * the resource to open | |
274 | * @param support | |
275 | * the support to use to download the resource | |
276 | * @param stable | |
277 | * TRUE for more stable resources, FALSE when they often change | |
278 | * | |
08fe2e33 NR |
279 | * @throws IOException |
280 | * in case of I/O error | |
281 | */ | |
282 | public void refresh(URL url, BasicSupport support, boolean stable) | |
283 | throws IOException { | |
284 | File cached = getCached(url); | |
285 | if (cached.exists() && !isOld(cached, stable)) { | |
286 | return; | |
287 | } | |
288 | ||
289 | open(url, support, stable).close(); | |
290 | } | |
291 | ||
292 | /** | |
293 | * Check the resource to see if it is in the cache. | |
294 | * | |
295 | * @param url | |
296 | * the resource to check | |
297 | * | |
298 | * @return TRUE if it is | |
299 | * | |
300 | */ | |
301 | public boolean check(URL url) { | |
302 | return getCached(url).exists(); | |
303 | } | |
304 | ||
305 | /** | |
0efd25e3 NR |
306 | * Save the given resource as an image on disk using the default image |
307 | * format for content. | |
08fe2e33 NR |
308 | * |
309 | * @param url | |
0efd25e3 NR |
310 | * the resource |
311 | * @param target | |
312 | * the target file | |
08fe2e33 NR |
313 | * |
314 | * @throws IOException | |
315 | * in case of I/O error | |
316 | */ | |
317 | public void saveAsImage(URL url, File target) throws IOException { | |
595dfa7a | 318 | URL cachedUrl = new URL(url.toString()); |
08fe2e33 NR |
319 | File cached = getCached(cachedUrl); |
320 | ||
321 | if (!cached.exists() || isOld(cached, true)) { | |
595dfa7a | 322 | InputStream imageIn = open(url, null, true); |
326093dc | 323 | ImageIO.write(ImageUtils.fromStream(imageIn), Instance.getConfig() |
08fe2e33 NR |
324 | .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(), |
325 | cached); | |
326 | } | |
327 | ||
328 | IOUtils.write(new FileInputStream(cached), target); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Manually add this item to the cache. | |
333 | * | |
334 | * @param in | |
335 | * the input data | |
336 | * @param uniqueID | |
337 | * a unique ID for this resource | |
338 | * | |
9252c65e | 339 | * @return the resulting {@link File} |
08fe2e33 NR |
340 | * |
341 | * @throws IOException | |
342 | * in case of I/O error | |
343 | */ | |
344 | public File addToCache(InputStream in, String uniqueID) throws IOException { | |
57f02339 | 345 | File file = getCached(uniqueID); |
08fe2e33 NR |
346 | IOUtils.write(in, file); |
347 | return file; | |
348 | } | |
349 | ||
57f02339 NR |
350 | /** |
351 | * Return the {@link InputStream} corresponding to the given unique ID, or | |
352 | * NULL if none found. | |
353 | * | |
354 | * @param uniqueID | |
355 | * the unique ID | |
0efd25e3 | 356 | * |
57f02339 NR |
357 | * @return the content or NULL |
358 | */ | |
359 | public InputStream getFromCache(String uniqueID) { | |
360 | File file = getCached(uniqueID); | |
361 | if (file.exists()) { | |
362 | try { | |
363 | return new MarkableFileInputStream(new FileInputStream(file)); | |
364 | } catch (FileNotFoundException e) { | |
365 | } | |
366 | } | |
367 | ||
368 | return null; | |
369 | } | |
370 | ||
08fe2e33 NR |
371 | /** |
372 | * Clean the cache (delete the cached items). | |
373 | * | |
374 | * @param onlyOld | |
375 | * only clean the files that are considered too old | |
376 | * | |
377 | * @return the number of cleaned items | |
378 | */ | |
379 | public int cleanCache(boolean onlyOld) { | |
380 | int num = 0; | |
381 | for (File file : dir.listFiles()) { | |
382 | if (!onlyOld || isOld(file, true)) { | |
383 | if (file.delete()) { | |
384 | num++; | |
385 | } else { | |
386 | System.err.println("Cannot delete temporary file: " | |
387 | + file.getAbsolutePath()); | |
388 | } | |
389 | } | |
390 | } | |
391 | return num; | |
392 | } | |
393 | ||
394 | /** | |
395 | * Open a resource from the cache if it exists. | |
396 | * | |
397 | * @param url | |
398 | * the resource to open | |
595dfa7a NR |
399 | * |
400 | * @return the opened resource if found, NULL i not | |
401 | * | |
08fe2e33 NR |
402 | * @throws IOException |
403 | * in case of I/O error | |
404 | */ | |
405 | private InputStream load(URL url, boolean allowOld, boolean stable) | |
406 | throws IOException { | |
407 | File cached = getCached(url); | |
408 | if (cached.exists() && !isOld(cached, stable)) { | |
409 | return new MarkableFileInputStream(new FileInputStream(cached)); | |
410 | } | |
411 | ||
412 | return null; | |
413 | } | |
414 | ||
415 | /** | |
416 | * Save the given resource to the cache. | |
417 | * | |
418 | * @param url | |
419 | * the resource | |
420 | * @param support | |
421 | * the {@link BasicSupport} used to download it | |
422 | * @param originalUrl | |
423 | * the original {@link URL} used to locate the cached resource | |
424 | * | |
425 | * @throws IOException | |
426 | * in case of I/O error | |
08fe2e33 NR |
427 | */ |
428 | private void save(URL url, BasicSupport support, URL originalUrl) | |
429 | throws IOException { | |
6e06d2cc | 430 | InputStream in = openNoCache(url, support, originalUrl, null); |
08fe2e33 NR |
431 | try { |
432 | File cached = getCached(originalUrl); | |
433 | BufferedOutputStream out = new BufferedOutputStream( | |
434 | new FileOutputStream(cached)); | |
435 | try { | |
436 | byte[] buf = new byte[4096]; | |
437 | int len; | |
438 | while ((len = in.read(buf)) > 0) { | |
439 | out.write(buf, 0, len); | |
440 | } | |
441 | } finally { | |
442 | out.close(); | |
443 | } | |
444 | } finally { | |
445 | in.close(); | |
446 | } | |
447 | } | |
448 | ||
6e06d2cc NR |
449 | /** |
450 | * Open a connection on the given {@link URL}, and manage the cookies that | |
451 | * come with it. | |
452 | * | |
453 | * @param url | |
454 | * the {@link URL} to open | |
455 | * @param support | |
456 | * the {@link BasicSupport} to use for cookie generation | |
457 | * | |
458 | * @return the connection | |
459 | * | |
460 | * @throws IOException | |
461 | * in case of I/O error | |
462 | */ | |
463 | private URLConnection openConnectionWithCookies(URL url, | |
464 | BasicSupport support) throws IOException { | |
465 | URLConnection conn = url.openConnection(); | |
466 | ||
467 | conn.setRequestProperty("User-Agent", UA); | |
468 | conn.setRequestProperty("Cookie", generateCookies(support)); | |
469 | conn.setRequestProperty("Accept-Encoding", "gzip"); | |
470 | if (support != null && support.getCurrentReferer() != null) { | |
471 | conn.setRequestProperty("Referer", support.getCurrentReferer() | |
472 | .toString()); | |
473 | conn.setRequestProperty("Host", support.getCurrentReferer() | |
474 | .getHost()); | |
475 | } | |
476 | ||
477 | return conn; | |
478 | } | |
479 | ||
08fe2e33 NR |
480 | /** |
481 | * Check if the {@link File} is too old according to | |
482 | * {@link Cache#tooOldChanging}. | |
483 | * | |
484 | * @param file | |
485 | * the file to check | |
486 | * @param stable | |
487 | * TRUE to denote files that are not supposed to change too often | |
488 | * | |
489 | * @return TRUE if it is | |
490 | */ | |
491 | private boolean isOld(File file, boolean stable) { | |
492 | long max = tooOldChanging; | |
493 | if (stable) { | |
494 | max = tooOldStable; | |
495 | } | |
496 | ||
497 | if (max < 0) { | |
498 | return false; | |
499 | } | |
500 | ||
501 | long time = new Date().getTime() - file.lastModified(); | |
502 | if (time < 0) { | |
503 | System.err.println("Timestamp in the future for file: " | |
504 | + file.getAbsolutePath()); | |
505 | } | |
506 | ||
507 | return time < 0 || time > max; | |
508 | } | |
509 | ||
510 | /** | |
511 | * Get the cache resource from the cache if it is present for this | |
512 | * {@link URL}. | |
513 | * | |
514 | * @param url | |
515 | * the url | |
0efd25e3 | 516 | * |
08fe2e33 NR |
517 | * @return the cached version if present, NULL if not |
518 | */ | |
519 | private File getCached(URL url) { | |
520 | String name = url.getHost(); | |
57f02339 | 521 | if (name == null || name.isEmpty()) { |
08fe2e33 NR |
522 | name = url.getFile(); |
523 | } else { | |
524 | name = url.toString(); | |
525 | } | |
526 | ||
57f02339 NR |
527 | return getCached(name); |
528 | } | |
529 | ||
530 | /** | |
531 | * Get the cache resource from the cache if it is present for this unique | |
532 | * ID. | |
533 | * | |
0efd25e3 NR |
534 | * @param uniqueID |
535 | * the id | |
536 | * | |
57f02339 NR |
537 | * @return the cached version if present, NULL if not |
538 | */ | |
539 | private File getCached(String uniqueID) { | |
540 | uniqueID = uniqueID.replace('/', '_').replace(':', '_') | |
541 | .replace("\\", "_"); | |
08fe2e33 | 542 | |
57f02339 | 543 | return new File(dir, uniqueID); |
08fe2e33 NR |
544 | } |
545 | ||
546 | /** | |
547 | * Generate the cookie {@link String} from the local {@link CookieStore} so | |
548 | * it is ready to be passed. | |
549 | * | |
550 | * @return the cookie | |
551 | */ | |
552 | private String generateCookies(BasicSupport support) { | |
553 | StringBuilder builder = new StringBuilder(); | |
554 | for (HttpCookie cookie : cookies.getCookieStore().getCookies()) { | |
555 | if (builder.length() > 0) { | |
556 | builder.append(';'); | |
557 | } | |
558 | ||
559 | // TODO: check if format is ok | |
560 | builder.append(cookie.toString()); | |
561 | } | |
562 | ||
563 | if (support != null) { | |
6e06d2cc NR |
564 | try { |
565 | for (Map.Entry<String, String> set : support.getCookies() | |
566 | .entrySet()) { | |
567 | if (builder.length() > 0) { | |
568 | builder.append(';'); | |
569 | } | |
570 | builder.append(set.getKey()); | |
571 | builder.append('='); | |
572 | builder.append(set.getValue()); | |
08fe2e33 | 573 | } |
6e06d2cc NR |
574 | } catch (IOException e) { |
575 | Instance.syserr(e); | |
08fe2e33 NR |
576 | } |
577 | } | |
578 | ||
579 | return builder.toString(); | |
580 | } | |
581 | } |