Refactor ETag handling

This commit is contained in:
Reinhard Pointner 2016-03-11 21:03:30 +00:00
parent 0a8234fdf8
commit ae8ca2eeb3
2 changed files with 16 additions and 7 deletions

View File

@ -195,11 +195,10 @@ public class CachedResource<K, R> implements Resource<R> {
} catch (FileNotFoundException e) {
return fileNotFound(url, e);
} finally {
List<String> 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);
});
}
};
}

View File

@ -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<String> getETag(Map<String, List<String>> responseHeaders) {
List<String> 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