Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.git] / src / be / nikiroo / fanfix / Cache.java
index b290756d1bdcb632d22cc66caa7e812cfa26bd9e..40ce15efec98860c559177dc0734c3b68377f797 100644 (file)
@@ -3,18 +3,20 @@ package be.nikiroo.fanfix;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStreamWriter;
 import java.net.CookieHandler;
 import java.net.CookieManager;
 import java.net.CookiePolicy;
 import java.net.CookieStore;
 import java.net.HttpCookie;
 import java.net.HttpURLConnection;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLEncoder;
 import java.util.Date;
 import java.util.Map;
 import java.util.zip.GZIPInputStream;
@@ -24,6 +26,7 @@ import javax.imageio.ImageIO;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.ImageUtils;
 import be.nikiroo.utils.MarkableFileInputStream;
 
 /**
@@ -84,6 +87,13 @@ public class Cache {
                CookieHandler.setDefault(cookies);
        }
 
+       /**
+        * Clear all the cookies currently in the jar.
+        */
+       public void clearCookies() {
+               cookies.getCookieStore().removeAll();
+       }
+
        /**
         * Open a resource (will load it from the cache if possible, or save it into
         * the cache after downloading if not).
@@ -150,6 +160,112 @@ public class Cache {
                }
        }
 
+       /**
+        * Open the given {@link URL} without using the cache, but still using and
+        * updating the cookies.
+        * 
+        * @param url
+        *            the {@link URL} to open
+        * @param support
+        *            the {@link BasicSupport} used for the cookies
+        * 
+        * @return the {@link InputStream} of the opened page
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public InputStream openNoCache(URL url, BasicSupport support)
+                       throws IOException {
+               return openNoCache(url, support, url, null);
+       }
+
+       /**
+        * Open the given {@link URL} without using the cache, but still using and
+        * updating the cookies.
+        * 
+        * @param url
+        *            the {@link URL} to open
+        * @param support
+        *            the {@link BasicSupport} used for the cookies
+        * @param postParams
+        *            the POST parameters
+        * 
+        * @return the {@link InputStream} of the opened page
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public InputStream openNoCache(URL url, BasicSupport support,
+                       Map<String, String> postParams) throws IOException {
+               return openNoCache(url, support, url, postParams);
+       }
+
+       /**
+        * Open the given {@link URL} without using the cache, but still using and
+        * updating the cookies.
+        * 
+        * @param url
+        *            the {@link URL} to open
+        * @param support
+        *            the {@link BasicSupport} used for the cookies
+        * @param originalUrl
+        *            the original {@link URL} before any redirection occurs
+        * @param postParams
+        *            the POST parameters
+        * 
+        * @return the {@link InputStream} of the opened page
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       private InputStream openNoCache(URL url, BasicSupport support,
+                       final URL originalUrl, Map<String, String> postParams)
+                       throws IOException {
+
+               URLConnection conn = openConnectionWithCookies(url, support);
+               if (postParams != null && conn instanceof HttpURLConnection) {
+                       StringBuilder postData = new StringBuilder();
+                       for (Map.Entry<String, String> param : postParams.entrySet()) {
+                               if (postData.length() != 0)
+                                       postData.append('&');
+                               postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
+                               postData.append('=');
+                               postData.append(URLEncoder.encode(
+                                               String.valueOf(param.getValue()), "UTF-8"));
+                       }
+
+                       conn.setDoOutput(true);
+                       ((HttpURLConnection) conn).setRequestMethod("POST");
+                       conn.setRequestProperty("Content-Type",
+                                       "application/x-www-form-urlencoded");
+                       conn.setRequestProperty("charset", "utf-8");
+
+                       OutputStreamWriter writer = new OutputStreamWriter(
+                                       conn.getOutputStream());
+
+                       writer.write(postData.toString());
+                       writer.flush();
+                       writer.close();
+               }
+
+               conn.connect();
+
+               // Check if redirect
+               if (conn instanceof HttpURLConnection
+                               && ((HttpURLConnection) conn).getResponseCode() / 100 == 3) {
+                       String newUrl = conn.getHeaderField("Location");
+                       return openNoCache(new URL(newUrl), support, originalUrl,
+                                       postParams);
+               }
+
+               InputStream in = conn.getInputStream();
+               if ("gzip".equals(conn.getContentEncoding())) {
+                       in = new GZIPInputStream(in);
+               }
+
+               return in;
+       }
+
        /**
         * Refresh the resource into cache if needed.
         * 
@@ -187,17 +303,13 @@ public class Cache {
        }
 
        /**
-        * Open a resource (will load it from the cache if possible, or save it into
-        * the cache after downloading if not) as an Image, then save it where
-        * requested.
-        * <p>
-        * This version will not always work properly if the original file was not
-        * downloaded before.
+        * Save the given resource as an image on disk using the default image
+        * format for content.
         * 
         * @param url
-        *            the resource to open
-        * 
-        * @return the opened resource image
+        *            the resource
+        * @param target
+        *            the target file
         * 
         * @throws IOException
         *             in case of I/O error
@@ -208,7 +320,7 @@ public class Cache {
 
                if (!cached.exists() || isOld(cached, true)) {
                        InputStream imageIn = open(url, null, true);
-                       ImageIO.write(IOUtils.toImage(imageIn), Instance.getConfig()
+                       ImageIO.write(ImageUtils.fromStream(imageIn), Instance.getConfig()
                                        .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(),
                                        cached);
                }
@@ -230,11 +342,32 @@ public class Cache {
         *             in case of I/O error
         */
        public File addToCache(InputStream in, String uniqueID) throws IOException {
-               File file = getCached(new File(uniqueID).toURI().toURL());
+               File file = getCached(uniqueID);
                IOUtils.write(in, file);
                return file;
        }
 
+       /**
+        * Return the {@link InputStream} corresponding to the given unique ID, or
+        * NULL if none found.
+        * 
+        * @param uniqueID
+        *            the unique ID
+        * 
+        * @return the content or NULL
+        */
+       public InputStream getFromCache(String uniqueID) {
+               File file = getCached(uniqueID);
+               if (file.exists()) {
+                       try {
+                               return new MarkableFileInputStream(new FileInputStream(file));
+                       } catch (FileNotFoundException e) {
+                       }
+               }
+
+               return null;
+       }
+
        /**
         * Clean the cache (delete the cached items).
         * 
@@ -263,16 +396,21 @@ public class Cache {
         * 
         * @param url
         *            the resource to open
+        * @param allowTooOld
+        *            allow files even if they are considered too old
+        * @param stable
+        *            a stable file (that dones't change too often) -- parameter
+        *            used to check if the file is too old to keep or not
         * 
         * @return the opened resource if found, NULL i not
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       private InputStream load(URL url, boolean allowOld, boolean stable)
+       private InputStream load(URL url, boolean allowTooOld, boolean stable)
                        throws IOException {
                File cached = getCached(url);
-               if (cached.exists() && !isOld(cached, stable)) {
+               if (cached.exists() && (allowTooOld || !isOld(cached, stable))) {
                        return new MarkableFileInputStream(new FileInputStream(cached));
                }
 
@@ -291,37 +429,10 @@ public class Cache {
         * 
         * @throws IOException
         *             in case of I/O error
-        * @throws URISyntaxException
         */
        private void save(URL url, BasicSupport support, URL originalUrl)
                        throws IOException {
-               URLConnection conn = url.openConnection();
-
-               conn.setRequestProperty("User-Agent", UA);
-               conn.setRequestProperty("Cookie", generateCookies(support));
-               conn.setRequestProperty("Accept-Encoding", "gzip");
-               if (support != null && support.getCurrentReferer() != null) {
-                       conn.setRequestProperty("Referer", support.getCurrentReferer()
-                                       .toString());
-                       conn.setRequestProperty("Host", support.getCurrentReferer()
-                                       .getHost());
-               }
-
-               conn.connect();
-
-               // Check if redirect
-               if (conn instanceof HttpURLConnection
-                               && ((HttpURLConnection) conn).getResponseCode() / 100 == 3) {
-                       String newUrl = conn.getHeaderField("Location");
-                       save(new URL(newUrl), support, originalUrl);
-                       return;
-               }
-
-               InputStream in = conn.getInputStream();
-               if ("gzip".equals(conn.getContentEncoding())) {
-                       in = new GZIPInputStream(in);
-               }
-
+               InputStream in = openNoCache(url, support, originalUrl, null);
                try {
                        File cached = getCached(originalUrl);
                        BufferedOutputStream out = new BufferedOutputStream(
@@ -340,6 +451,37 @@ public class Cache {
                }
        }
 
+       /**
+        * Open a connection on the given {@link URL}, and manage the cookies that
+        * come with it.
+        * 
+        * @param url
+        *            the {@link URL} to open
+        * @param support
+        *            the {@link BasicSupport} to use for cookie generation
+        * 
+        * @return the connection
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       private URLConnection openConnectionWithCookies(URL url,
+                       BasicSupport support) throws IOException {
+               URLConnection conn = url.openConnection();
+
+               conn.setRequestProperty("User-Agent", UA);
+               conn.setRequestProperty("Cookie", generateCookies(support));
+               conn.setRequestProperty("Accept-Encoding", "gzip");
+               if (support != null && support.getCurrentReferer() != null) {
+                       conn.setRequestProperty("Referer", support.getCurrentReferer()
+                                       .toString());
+                       conn.setRequestProperty("Host", support.getCurrentReferer()
+                                       .getHost());
+               }
+
+               return conn;
+       }
+
        /**
         * Check if the {@link File} is too old according to
         * {@link Cache#tooOldChanging}.
@@ -376,19 +518,34 @@ public class Cache {
         * 
         * @param url
         *            the url
+        * 
         * @return the cached version if present, NULL if not
         */
        private File getCached(URL url) {
                String name = url.getHost();
-               if (name == null || name.length() == 0) {
+               if (name == null || name.isEmpty()) {
                        name = url.getFile();
                } else {
                        name = url.toString();
                }
 
-               name = name.replace('/', '_').replace(':', '_');
+               return getCached(name);
+       }
 
-               return new File(dir, name);
+       /**
+        * Get the cache resource from the cache if it is present for this unique
+        * ID.
+        * 
+        * @param uniqueID
+        *            the id
+        * 
+        * @return the cached version if present, NULL if not
+        */
+       private File getCached(String uniqueID) {
+               uniqueID = uniqueID.replace('/', '_').replace(':', '_')
+                               .replace("\\", "_");
+
+               return new File(dir, uniqueID);
        }
 
        /**
@@ -409,14 +566,18 @@ public class Cache {
                }
 
                if (support != null) {
-                       for (Map.Entry<String, String> set : support.getCookies()
-                                       .entrySet()) {
-                               if (builder.length() > 0) {
-                                       builder.append(';');
+                       try {
+                               for (Map.Entry<String, String> set : support.getCookies()
+                                               .entrySet()) {
+                                       if (builder.length() > 0) {
+                                               builder.append(';');
+                                       }
+                                       builder.append(set.getKey());
+                                       builder.append('=');
+                                       builder.append(set.getValue());
                                }
-                               builder.append(set.getKey());
-                               builder.append('=');
-                               builder.append(set.getValue());
+                       } catch (IOException e) {
+                               Instance.syserr(e);
                        }
                }