X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2FCache.java;h=7f603c1ba15f1c7e9d812d76941ce616e9d419b3;hb=57f02339393c9997391b76ffcb22ae72fd0a45cb;hp=8e8392aa12d61795b53ce03d320e591f11b20712;hpb=6e06d2cc9bf068a8ec4ad105aaef955a6eb509a1;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/Cache.java b/src/be/nikiroo/fanfix/Cache.java index 8e8392a..7f603c1 100644 --- a/src/be/nikiroo/fanfix/Cache.java +++ b/src/be/nikiroo/fanfix/Cache.java @@ -3,6 +3,7 @@ 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; @@ -222,7 +223,7 @@ public class Cache { throws IOException { URLConnection conn = openConnectionWithCookies(url, support); - if (postParams != null) { + if (postParams != null && conn instanceof HttpURLConnection) { StringBuilder postData = new StringBuilder(); for (Map.Entry param : postParams.entrySet()) { if (postData.length() != 0) @@ -234,6 +235,10 @@ public class Cache { } 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()); @@ -341,11 +346,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). * @@ -496,15 +522,28 @@ public class Cache { */ 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); + } + + /** + * Get the cache resource from the cache if it is present for this unique + * ID. + * + * @param url + * the url + * @return the cached version if present, NULL if not + */ + private File getCached(String uniqueID) { + uniqueID = uniqueID.replace('/', '_').replace(':', '_') + .replace("\\", "_"); - return new File(dir, name); + return new File(dir, uniqueID); } /**