From c5c8525b49a666449717ef8efaac47fb7e2b2213 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 7 Mar 2016 12:30:09 +0000 Subject: [PATCH] Move CachedResource2 --- source/net/filebot/Cache.java | 20 ++++--- .../filebot/{web => }/CachedResource2.java | 24 ++++----- source/net/filebot/{web => }/Resource.java | 2 +- .../net/filebot/web/CachedJsonResource.java | 52 ------------------- source/net/filebot/web/TVMazeClient.java | 1 + source/net/filebot/web/TheTVDBClient.java | 2 +- 6 files changed, 28 insertions(+), 73 deletions(-) rename source/net/filebot/{web => }/CachedResource2.java (90%) rename source/net/filebot/{web => }/Resource.java (77%) delete mode 100644 source/net/filebot/web/CachedJsonResource.java diff --git a/source/net/filebot/Cache.java b/source/net/filebot/Cache.java index d230a7dc..11576af9 100644 --- a/source/net/filebot/Cache.java +++ b/source/net/filebot/Cache.java @@ -1,19 +1,20 @@ package net.filebot; import static java.nio.charset.StandardCharsets.*; +import static net.filebot.CachedResource2.*; import static net.filebot.Logging.*; -import static net.filebot.web.CachedResource2.*; import java.io.Serializable; import java.net.URL; +import java.nio.ByteBuffer; import java.time.Duration; import java.util.Arrays; import java.util.function.Predicate; -import net.filebot.web.CachedResource2; -import net.filebot.web.CachedResource2.Source; +import net.filebot.CachedResource2.Fetch; +import net.filebot.CachedResource2.Source; +import net.filebot.CachedResource2.Transform; import net.filebot.web.FloodLimit; -import net.filebot.web.Resource; import net.sf.ehcache.Element; import org.w3c.dom.Document; @@ -21,20 +22,25 @@ import org.w3c.dom.Document; public class Cache { public static final Duration ONE_DAY = Duration.ofDays(1); + public static final Duration ONE_WEEK = Duration.ofDays(7); public static Cache getCache(String name, CacheType type) { return CacheManager.getInstance().getCache(name.toLowerCase(), type); } - public Resource text(String url, Duration expirationTime, FloodLimit limit) { + public CachedResource2 resource(String key, Source source, Fetch fetch, Transform parse, Transform cast, Duration expirationTime) { + return new CachedResource2(key, source, fetch, parse, cast, expirationTime, this); + } + + public CachedResource2 text(String url, Duration expirationTime, FloodLimit limit) { return new CachedResource2(url, URL::new, withPermit(fetchIfModified(), r -> limit.acquirePermit() != null), getText(UTF_8), String.class::cast, expirationTime, this); } - public Resource xml(String key, Source source, Duration expirationTime) { + public CachedResource2 xml(String key, Source source, Duration expirationTime) { return new CachedResource2(key, source, fetchIfModified(), validateXml(getText(UTF_8)), getXml(String.class::cast), expirationTime, this); } - public Resource json(String key, Source source, Duration expirationTime) { + public CachedResource2 json(String key, Source source, Duration expirationTime) { return new CachedResource2(key, source, fetchIfModified(), validateJson(getText(UTF_8)), getJson(String.class::cast), expirationTime, this); } diff --git a/source/net/filebot/web/CachedResource2.java b/source/net/filebot/CachedResource2.java similarity index 90% rename from source/net/filebot/web/CachedResource2.java rename to source/net/filebot/CachedResource2.java index f6076e6f..70ff36ad 100644 --- a/source/net/filebot/web/CachedResource2.java +++ b/source/net/filebot/CachedResource2.java @@ -1,4 +1,4 @@ -package net.filebot.web; +package net.filebot; import static net.filebot.Logging.*; @@ -10,8 +10,8 @@ import java.nio.charset.Charset; import java.time.Duration; import java.util.concurrent.Callable; -import net.filebot.Cache; import net.filebot.util.JsonUtilities; +import net.filebot.web.WebRequest; import org.w3c.dom.Document; @@ -20,19 +20,19 @@ public class CachedResource2 implements Resource { public static final int DEFAULT_RETRY_LIMIT = 2; public static final Duration DEFAULT_RETRY_DELAY = Duration.ofSeconds(2); - protected final K key; + private K key; - protected final Source source; - protected final Fetch fetch; - protected final Transform parse; - protected final Transform cast; + private Source source; + private Fetch fetch; + private Transform parse; + private Transform cast; - protected final Duration expirationTime; + private Duration expirationTime; - protected final int retryCountLimit; - protected final long retryWaitTime; + private int retryCountLimit; + private long retryWaitTime; - protected final Cache cache; + private final Cache cache; public CachedResource2(K key, Source source, Fetch fetch, Transform parse, Transform cast, Duration expirationTime, Cache cache) { this(key, source, fetch, parse, cast, DEFAULT_RETRY_LIMIT, DEFAULT_RETRY_DELAY, expirationTime, cache); @@ -59,7 +59,7 @@ public class CachedResource2 implements Resource { debug.fine(format("Fetch %s (If-Modified-Since: %tc)", resource, lastModified)); try { - ByteBuffer data = retry(() -> fetch.fetch(resource, lastModified), retryCountLimit, lastModified); + ByteBuffer data = retry(() -> fetch.fetch(resource, lastModified), retryCountLimit, retryWaitTime); // 304 Not Modified if (data == null && element != null && element.getObjectValue() != null) { diff --git a/source/net/filebot/web/Resource.java b/source/net/filebot/Resource.java similarity index 77% rename from source/net/filebot/web/Resource.java rename to source/net/filebot/Resource.java index 6e0bbcad..6e7fe158 100644 --- a/source/net/filebot/web/Resource.java +++ b/source/net/filebot/Resource.java @@ -1,4 +1,4 @@ -package net.filebot.web; +package net.filebot; @FunctionalInterface public interface Resource { diff --git a/source/net/filebot/web/CachedJsonResource.java b/source/net/filebot/web/CachedJsonResource.java deleted file mode 100644 index 0cf40dd0..00000000 --- a/source/net/filebot/web/CachedJsonResource.java +++ /dev/null @@ -1,52 +0,0 @@ -package net.filebot.web; - -import static net.filebot.util.JsonUtilities.*; - -import java.io.IOException; -import java.net.URL; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; - -import net.sf.ehcache.Cache; -import net.sf.ehcache.CacheManager; - -public class CachedJsonResource extends AbstractCachedResource { - - public CachedJsonResource(String resource) { - super(resource, String.class, ONE_DAY, 2, 1000); - } - - @Override - protected Cache getCache() { - return CacheManager.getInstance().getCache("web-datasource-lv3"); - } - - public Object getJsonObject() throws IOException { - try { - return readJson(get()); - } catch (Exception e) { - throw new IOException(String.format("Error while loading JSON resource: %s (%s)", getResourceLocation(resource), e.getMessage())); - } - } - - @Override - public String process(String data) throws IOException { - try { - readJson(get()); // make sure JSON is valid - return data; - } catch (Exception e) { - throw new IOException(String.format("Malformed JSON: %s (%s)", getResourceLocation(resource), e.getMessage())); - } - } - - @Override - protected String fetchData(URL url, long lastModified) throws IOException { - ByteBuffer data = WebRequest.fetchIfModified(url, lastModified); - - if (data == null) - return null; // not modified - - return StandardCharsets.UTF_8.decode(data).toString(); - } - -} diff --git a/source/net/filebot/web/TVMazeClient.java b/source/net/filebot/web/TVMazeClient.java index 1c9392af..532eeabe 100644 --- a/source/net/filebot/web/TVMazeClient.java +++ b/source/net/filebot/web/TVMazeClient.java @@ -15,6 +15,7 @@ import javax.swing.Icon; import net.filebot.Cache; import net.filebot.CacheType; +import net.filebot.Resource; import net.filebot.ResourceManager; public class TVMazeClient extends AbstractEpisodeListProvider { diff --git a/source/net/filebot/web/TheTVDBClient.java b/source/net/filebot/web/TheTVDBClient.java index a3c1dd5e..82ea0d7c 100644 --- a/source/net/filebot/web/TheTVDBClient.java +++ b/source/net/filebot/web/TheTVDBClient.java @@ -12,7 +12,6 @@ import java.io.Serializable; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import java.time.Duration; import java.util.ArrayList; import java.util.EnumMap; import java.util.EnumSet; @@ -29,6 +28,7 @@ import javax.swing.Icon; import net.filebot.Cache; import net.filebot.CacheType; +import net.filebot.Resource; import net.filebot.ResourceManager; import net.filebot.util.FileUtilities; import net.filebot.web.TheTVDBClient.BannerDescriptor.BannerProperty;