From ae8ca2eeb3f5f0c992386a9a49fece0b195f95fd Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 11 Mar 2016 21:03:30 +0000 Subject: [PATCH] Refactor ETag handling --- source/net/filebot/CachedResource.java | 9 ++++----- source/net/filebot/web/WebRequest.java | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/source/net/filebot/CachedResource.java b/source/net/filebot/CachedResource.java index 969b508a..b9f834de 100644 --- a/source/net/filebot/CachedResource.java +++ b/source/net/filebot/CachedResource.java @@ -195,11 +195,10 @@ public class CachedResource implements Resource { } catch (FileNotFoundException e) { return fileNotFound(url, e); } finally { - List value = responseHeaders.get("ETag"); - if (value != null && value.size() > 0 && !value.contains(etagValue)) { - debug.finest(format("Store ETag: %s", value)); - etagStorage.put(etagKey, value.get(0)); - } + WebRequest.getETag(responseHeaders).ifPresent(etag -> { + debug.finest(format("Store ETag: %s", etag)); + etagStorage.put(etagKey, etag); + }); } }; } diff --git a/source/net/filebot/web/WebRequest.java b/source/net/filebot/web/WebRequest.java index d1b17df5..2499aba3 100644 --- a/source/net/filebot/web/WebRequest.java +++ b/source/net/filebot/web/WebRequest.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,14 +46,14 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import net.filebot.util.ByteBufferOutputStream; - import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; +import net.filebot.util.ByteBufferOutputStream; + public final class WebRequest { public static Reader getReader(URLConnection connection) throws IOException { @@ -266,6 +267,15 @@ public final class WebRequest { } } + public static Optional getETag(Map> responseHeaders) { + List header = responseHeaders.get("ETag"); + if (header != null && header.size() > 0) { + // e.g. W/"ca0072135d8a475a716e6595f577ae8b" + return Optional.of(header.get(0)); + } + return Optional.empty(); + } + private static Charset getCharset(String contentType) { if (contentType != null) { // e.g. Content-Type: text/html; charset=iso-8859-1