1 package be
.nikiroo
.fanfix
;
3 import java
.io
.BufferedOutputStream
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileNotFoundException
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.io
.OutputStreamWriter
;
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
;
18 import java
.net
.URLConnection
;
19 import java
.net
.URLEncoder
;
20 import java
.util
.Date
;
22 import java
.util
.zip
.GZIPInputStream
;
24 import javax
.imageio
.ImageIO
;
26 import be
.nikiroo
.fanfix
.bundles
.Config
;
27 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
28 import be
.nikiroo
.utils
.IOUtils
;
29 import be
.nikiroo
.utils
.ImageUtils
;
30 import be
.nikiroo
.utils
.MarkableFileInputStream
;
33 * This cache will manage Internet (and local) downloads, as well as put the
34 * downloaded files into a cache.
36 * As long the cached resource is not too old, it will use it instead of
37 * retrieving the file again.
44 private long tooOldChanging
;
45 private long tooOldStable
;
46 private CookieManager cookies
;
49 * Create a new {@link Cache} object.
52 * the directory to use as cache
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
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
65 * in case of I/O error
67 public Cache(File dir
, String UA
, int hoursChanging
, int hoursStable
)
71 this.tooOldChanging
= 1000 * 60 * 60 * hoursChanging
;
72 this.tooOldStable
= 1000 * 60 * 60 * hoursStable
;
80 if (dir
== null || !dir
.exists()) {
81 throw new IOException("Cannot create the cache directory: "
82 + (dir
== null ?
"null" : dir
.getAbsolutePath()));
85 cookies
= new CookieManager();
86 cookies
.setCookiePolicy(CookiePolicy
.ACCEPT_ALL
);
87 CookieHandler
.setDefault(cookies
);
91 * Clear all the cookies currently in the jar.
93 public void clearCookies() {
94 cookies
.getCookieStore().removeAll();
98 * Open a resource (will load it from the cache if possible, or save it into
99 * the cache after downloading if not).
102 * the resource to open
104 * the support to use to download the resource
106 * TRUE for more stable resources, FALSE when they often change
108 * @return the opened resource, NOT NULL
110 * @throws IOException
111 * in case of I/O error
113 public InputStream
open(URL url
, BasicSupport support
, boolean stable
)
115 // MUST NOT return null
116 return open(url
, support
, stable
, url
);
120 * Open a resource (will load it from the cache if possible, or save it into
121 * the cache after downloading if not).
123 * The cached resource will be assimilated to the given original {@link URL}
126 * the resource to open
128 * the support to use to download the resource
130 * TRUE for more stable resources, FALSE when they often change
132 * the original {@link URL} used to locate the cached resource
134 * @return the opened resource, NOT NULL
136 * @throws IOException
137 * in case of I/O error
139 public InputStream
open(URL url
, BasicSupport support
, boolean stable
,
140 URL originalUrl
) throws IOException
{
141 // MUST NOT return null
143 InputStream in
= load(originalUrl
, false, stable
);
144 Instance
.trace("Cache " + (in
!= null ?
"hit" : "miss") + ": "
150 save(url
, support
, originalUrl
);
151 } catch (IOException e
) {
152 throw new IOException("Cannot save the url: "
153 + (url
== null ?
"null" : url
.toString()), e
);
156 // Was just saved, can load old, so, will not be null
157 in
= load(originalUrl
, true, stable
);
161 } catch (IOException e
) {
162 throw new IOException("Cannot open the url: "
163 + (url
== null ?
"null" : url
.toString()), e
);
168 * Open the given {@link URL} without using the cache, but still using and
169 * updating the cookies.
172 * the {@link URL} to open
174 * the {@link BasicSupport} used for the cookies
176 * @return the {@link InputStream} of the opened page
178 * @throws IOException
179 * in case of I/O error
181 public InputStream
openNoCache(URL url
, BasicSupport support
)
183 return openNoCache(url
, support
, url
, null, null, null);
187 * Open the given {@link URL} without using the cache, but still using and
188 * updating the cookies.
191 * the {@link URL} to open
193 * the {@link BasicSupport} used for the cookies
195 * the POST parameters
197 * the GET parameters (priority over POST)
199 * OAuth authorization (aka, "bearer XXXXXXX")
201 * @return the {@link InputStream} of the opened page
203 * @throws IOException
204 * in case of I/O error
206 public InputStream
openNoCache(URL url
, BasicSupport support
,
207 Map
<String
, String
> postParams
, Map
<String
, String
> getParams
,
208 String oauth
) throws IOException
{
209 return openNoCache(url
, support
, url
, postParams
, getParams
, oauth
);
213 * Open the given {@link URL} without using the cache, but still using and
214 * updating the cookies.
217 * the {@link URL} to open
219 * the {@link BasicSupport} used for the cookies
221 * the original {@link URL} before any redirection occurs
223 * the POST parameters
225 * the GET parameters (priority over POST)
227 * OAuth authorisation (aka, "bearer XXXXXXX")
228 * @return the {@link InputStream} of the opened page
230 * @throws IOException
231 * in case of I/O error
233 private InputStream
openNoCache(URL url
, BasicSupport support
,
234 final URL originalUrl
, Map
<String
, String
> postParams
,
235 Map
<String
, String
> getParams
, String oauth
) throws IOException
{
237 Instance
.trace("Open no cache: " + url
);
239 URLConnection conn
= openConnectionWithCookies(url
, support
);
240 if (support
!= null) {
241 // priority: arguments
243 oauth
= support
.getOAuth();
247 // Priority: GET over POST
248 Map
<String
, String
> params
= getParams
;
249 if (getParams
== null) {
253 if ((params
!= null || oauth
!= null)
254 && conn
instanceof HttpURLConnection
) {
255 StringBuilder requestData
= null;
256 if (params
!= null) {
257 requestData
= new StringBuilder();
258 for (Map
.Entry
<String
, String
> param
: params
.entrySet()) {
259 if (requestData
.length() != 0)
260 requestData
.append('&');
261 requestData
.append(URLEncoder
.encode(param
.getKey(),
263 requestData
.append('=');
264 requestData
.append(URLEncoder
.encode(
265 String
.valueOf(param
.getValue()), "UTF-8"));
268 conn
.setDoOutput(true);
270 if (getParams
== null && postParams
!= null) {
271 ((HttpURLConnection
) conn
).setRequestMethod("POST");
274 conn
.setRequestProperty("Content-Type",
275 "application/x-www-form-urlencoded");
276 conn
.setRequestProperty("charset", "utf-8");
280 conn
.setRequestProperty("Authorization", oauth
);
283 if (requestData
!= null) {
284 OutputStreamWriter writer
= new OutputStreamWriter(
285 conn
.getOutputStream());
287 writer
.write(requestData
.toString());
296 if (conn
instanceof HttpURLConnection
297 && ((HttpURLConnection
) conn
).getResponseCode() / 100 == 3) {
298 String newUrl
= conn
.getHeaderField("Location");
299 return openNoCache(new URL(newUrl
), support
, originalUrl
,
300 postParams
, getParams
, oauth
);
303 InputStream in
= conn
.getInputStream();
304 if ("gzip".equals(conn
.getContentEncoding())) {
305 in
= new GZIPInputStream(in
);
312 * Refresh the resource into cache if needed.
315 * the resource to open
317 * the support to use to download the resource
319 * TRUE for more stable resources, FALSE when they often change
321 * @throws IOException
322 * in case of I/O error
324 public void refresh(URL url
, BasicSupport support
, boolean stable
)
326 File cached
= getCached(url
);
327 if (cached
.exists() && !isOld(cached
, stable
)) {
331 open(url
, support
, stable
).close();
335 * Check the resource to see if it is in the cache.
338 * the resource to check
340 * @return TRUE if it is
343 public boolean check(URL url
) {
344 return getCached(url
).exists();
348 * Save the given resource as an image on disk using the default image
349 * format for content.
356 * @throws IOException
357 * in case of I/O error
359 public void saveAsImage(URL url
, File target
) throws IOException
{
360 URL cachedUrl
= new URL(url
.toString());
361 File cached
= getCached(cachedUrl
);
363 if (!cached
.exists() || isOld(cached
, true)) {
364 InputStream imageIn
= open(url
, null, true);
365 ImageIO
.write(ImageUtils
.fromStream(imageIn
), Instance
.getConfig()
366 .getString(Config
.IMAGE_FORMAT_CONTENT
).toLowerCase(),
370 IOUtils
.write(new FileInputStream(cached
), target
);
374 * Manually add this item to the cache.
379 * a unique ID for this resource
381 * @return the resulting {@link File}
383 * @throws IOException
384 * in case of I/O error
386 public File
addToCache(InputStream in
, String uniqueID
) throws IOException
{
387 File file
= getCached(uniqueID
);
388 File subdir
= new File(file
.getParentFile(), "_");
389 file
= new File(subdir
, file
.getName());
391 IOUtils
.write(in
, file
);
396 * Return the {@link InputStream} corresponding to the given unique ID, or
397 * NULL if none found.
402 * @return the content or NULL
404 public InputStream
getFromCache(String uniqueID
) {
405 File file
= getCached(uniqueID
);
406 File subdir
= new File(file
.getParentFile(), "_");
407 file
= new File(subdir
, file
.getName());
410 return new MarkableFileInputStream(new FileInputStream(file
));
411 } catch (FileNotFoundException e
) {
419 * Clean the cache (delete the cached items).
422 * only clean the files that are considered too old
424 * @return the number of cleaned items
426 public int cleanCache(boolean onlyOld
) {
427 return cleanCache(onlyOld
, dir
);
431 * Clean the cache (delete the cached items) in the given cache directory.
434 * only clean the files that are considered too old
436 * the cache directory to clean
438 * @return the number of cleaned items
440 private int cleanCache(boolean onlyOld
, File cacheDir
) {
442 for (File file
: cacheDir
.listFiles()) {
443 if (file
.isDirectory()) {
444 num
+= cleanCache(onlyOld
, file
);
446 if (!onlyOld
|| isOld(file
, true)) {
450 System
.err
.println("Cannot delete temporary file: "
451 + file
.getAbsolutePath());
461 * Open a resource from the cache if it exists.
464 * the resource to open
466 * allow files even if they are considered too old
468 * a stable file (that dones't change too often) -- parameter
469 * used to check if the file is too old to keep or not
471 * @return the opened resource if found, NULL i not
473 * @throws IOException
474 * in case of I/O error
476 private InputStream
load(URL url
, boolean allowTooOld
, boolean stable
)
478 File cached
= getCached(url
);
479 if (cached
.exists() && (allowTooOld
|| !isOld(cached
, stable
))) {
480 return new MarkableFileInputStream(new FileInputStream(cached
));
487 * Save the given resource to the cache.
492 * the {@link BasicSupport} used to download it
494 * the original {@link URL} used to locate the cached resource
496 * @throws IOException
497 * in case of I/O error
499 private void save(URL url
, BasicSupport support
, URL originalUrl
)
501 InputStream in
= openNoCache(url
, support
, originalUrl
, null, null,
504 File cached
= getCached(originalUrl
);
505 BufferedOutputStream out
= new BufferedOutputStream(
506 new FileOutputStream(cached
));
508 byte[] buf
= new byte[4096];
510 while ((len
= in
.read(buf
)) > 0) {
511 out
.write(buf
, 0, len
);
522 * Open a connection on the given {@link URL}, and manage the cookies that
526 * the {@link URL} to open
528 * the {@link BasicSupport} to use for cookie generation
530 * @return the connection
532 * @throws IOException
533 * in case of I/O error
535 private URLConnection
openConnectionWithCookies(URL url
,
536 BasicSupport support
) throws IOException
{
537 URLConnection conn
= url
.openConnection();
539 conn
.setRequestProperty("User-Agent", UA
);
540 conn
.setRequestProperty("Cookie", generateCookies(support
));
541 conn
.setRequestProperty("Accept-Encoding", "gzip");
542 if (support
!= null && support
.getCurrentReferer() != null) {
543 conn
.setRequestProperty("Referer", support
.getCurrentReferer()
545 conn
.setRequestProperty("Host", support
.getCurrentReferer()
553 * Check if the {@link File} is too old according to
554 * {@link Cache#tooOldChanging}.
559 * TRUE to denote files that are not supposed to change too often
561 * @return TRUE if it is
563 private boolean isOld(File file
, boolean stable
) {
564 long max
= tooOldChanging
;
573 long time
= new Date().getTime() - file
.lastModified();
575 System
.err
.println("Timestamp in the future for file: "
576 + file
.getAbsolutePath());
579 return time
< 0 || time
> max
;
583 * Return the associated cache {@link File} from this {@link URL}.
588 * @return the cached {@link File} version of this {@link URL}
590 private File
getCached(URL url
) {
593 String name
= url
.getHost();
594 if (name
== null || name
.isEmpty()) {
595 name
= url
.getFile();
597 File cacheDir
= getCached(".").getParentFile();
598 File subsubDir
= new File(cacheDir
, allowedChars(url
.getHost()));
599 subdir
= new File(subsubDir
, "_" + allowedChars(url
.getPath()));
600 name
= allowedChars("_" + url
.getQuery());
603 File cacheFile
= getCached(name
);
604 if (subdir
!= null) {
605 cacheFile
= new File(subdir
, cacheFile
.getName());
613 * Get the basic cache resource file corresponding to this unique ID.
615 * Note that you may need to add a sub-directory in some cases.
620 * @return the cached version if present, NULL if not
622 private File
getCached(String uniqueID
) {
623 return new File(dir
, allowedChars(uniqueID
));
627 * Replace not allowed chars (in a {@link File}) by "_".
630 * the raw {@link String}
632 * @return the sanitised {@link String}
634 private String
allowedChars(String raw
) {
635 return raw
.replace('/', '_').replace(':', '_').replace("\\", "_");
639 * Generate the cookie {@link String} from the local {@link CookieStore} so
640 * it is ready to be passed.
644 private String
generateCookies(BasicSupport support
) {
645 StringBuilder builder
= new StringBuilder();
646 for (HttpCookie cookie
: cookies
.getCookieStore().getCookies()) {
647 if (builder
.length() > 0) {
651 // TODO: check if format is ok
652 builder
.append(cookie
.toString());
655 if (support
!= null) {
656 for (Map
.Entry
<String
, String
> set
: support
.getCookies()
658 if (builder
.length() > 0) {
661 builder
.append(set
.getKey());
663 builder
.append(set
.getValue());
667 return builder
.toString();