package be.nikiroo.fanfix; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Map; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.utils.Cache; import be.nikiroo.utils.CacheMemory; import be.nikiroo.utils.Downloader; import be.nikiroo.utils.Image; import be.nikiroo.utils.ImageUtils; import be.nikiroo.utils.TraceHandler; /** * This cache will manage Internet (and local) downloads, as well as put the * downloaded files into a cache. *
* As long the cached resource is not too old, it will use it instead of * retrieving the file again. * * @author niki */ public class DataLoader { private Downloader downloader; private Downloader downloaderNoCache; private Cache cache; private boolean offline; /** * Create a new {@link DataLoader} object. * * @param dir * the directory to use as cache * @param UA * the User-Agent to use to download the resources * @param hoursChanging * the number of hours after which a cached file that is thought * to change ~often is considered too old (or -1 for * "never too old") * @param hoursStable * the number of hours after which a LARGE cached file that is * thought to change rarely is considered too old (or -1 for * "never too old") * * @throws IOException * in case of I/O error */ public DataLoader(File dir, String UA, int hoursChanging, int hoursStable) throws IOException { downloader = new Downloader(UA, new Cache(dir, hoursChanging, hoursStable)); downloaderNoCache = new Downloader(UA); cache = downloader.getCache(); } /** * Create a new {@link DataLoader} object without disk cache (will keep a * memory cache for manual cache operations). * * @param UA * the User-Agent to use to download the resources */ public DataLoader(String UA) { downloader = new Downloader(UA); downloaderNoCache = downloader; cache = new CacheMemory(); } /** * This {@link Downloader} is forbidden to try and connect to the network. *
* If TRUE, it will only check the cache (even in no-cache mode!). *
* Default is FALSE. * * @return TRUE if offline */ public boolean isOffline() { return offline; } /** * This {@link Downloader} is forbidden to try and connect to the network. *
* If TRUE, it will only check the cache (even in no-cache mode!). *
* Default is FALSE. * * @param offline TRUE for offline, FALSE for online */ public void setOffline(boolean offline) { this.offline = offline; downloader.setOffline(offline); downloaderNoCache.setOffline(offline); // If we don't, we cannot support no-cache using code in OFFLINE mode if (offline) { downloaderNoCache.setCache(cache); } else { downloaderNoCache.setCache(null); } } /** * The traces handler for this {@link Cache}. * * @param tracer * the new traces handler */ public void setTraceHandler(TraceHandler tracer) { downloader.setTraceHandler(tracer); downloaderNoCache.setTraceHandler(tracer); cache.setTraceHandler(tracer); if (downloader.getCache() != null) { downloader.getCache().setTraceHandler(tracer); } } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). *
* The cached resource will be assimilated to the given original {@link URL} * * @param url * the resource to open * @param support * the support to use to download the resource (can be NULL) * @param stable * TRUE for more stable resources, FALSE when they often change * * @return the opened resource, NOT NULL * * @throws IOException * in case of I/O error */ public InputStream open(URL url, BasicSupport support, boolean stable) throws IOException { return open(url, url, support, stable, null, null, null); } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). *
* The cached resource will be assimilated to the given original {@link URL} * * @param url * the resource to open * @param originalUrl * the original {@link URL} before any redirection occurs, which * is also used for the cache ID if needed (so we can retrieve * the content with this URL if needed) * @param support * the support to use to download the resource * @param stable * TRUE for more stable resources, FALSE when they often change * * @return the opened resource, NOT NULL * * @throws IOException * in case of I/O error */ public InputStream open(URL url, URL originalUrl, BasicSupport support, boolean stable) throws IOException { return open(url, originalUrl, support, stable, null, null, null); } /** * Open a resource (will load it from the cache if possible, or save it into * the cache after downloading if not). *
* The cached resource will be assimilated to the given original {@link URL}
*
* @param url
* the resource to open
* @param originalUrl
* the original {@link URL} before any redirection occurs, which
* is also used for the cache ID if needed (so we can retrieve
* the content with this URL if needed)
* @param support
* the support to use to download the resource (can be NULL)
* @param stable
* TRUE for more stable resources, FALSE when they often change
* @param postParams
* the POST parameters
* @param getParams
* the GET parameters (priority over POST)
* @param oauth
* OAuth authorization (aka, "bearer XXXXXXX")
*
* @return the opened resource, NOT NULL
*
* @throws IOException
* in case of I/O error
*/
public InputStream open(URL url, URL originalUrl, BasicSupport support,
boolean stable, Map