Modified stream reading to not alter file + fixed content-type + minor logic issues
This commit is contained in:
parent
8bf269ca5b
commit
ee35e70c5b
|
@ -23,6 +23,8 @@
|
||||||
|
|
||||||
package net.socialgamer.cah.customsets;
|
package net.socialgamer.cah.customsets;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import net.socialgamer.cah.data.GameOptions;
|
import net.socialgamer.cah.data.GameOptions;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
@ -32,10 +34,7 @@ import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.JSONValue;
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
@ -90,7 +89,10 @@ public class CustomCardsService {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String content = getUrlContent(url);
|
String content = getUrlContent(url);
|
||||||
if (content == null) return null;
|
if (content == null) {
|
||||||
|
putCache(null, INVALID_SET_CACHE_LIFETIME, url, null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return loadSetFromJson(content, url);
|
return loadSetFromJson(content, url);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -103,7 +105,7 @@ public class CustomCardsService {
|
||||||
|
|
||||||
public CustomDeck loadSetFromJson(String jsonStr, String url) {
|
public CustomDeck loadSetFromJson(String jsonStr, String url) {
|
||||||
String hash = DigestUtils.md5Hex(jsonStr);
|
String hash = DigestUtils.md5Hex(jsonStr);
|
||||||
CacheEntry entry = checkCacheHash(url);
|
CacheEntry entry = checkCacheHash(hash);
|
||||||
if (checkCacheValid(entry, "json", hash))
|
if (checkCacheValid(entry, "json", hash))
|
||||||
return entry.deck;
|
return entry.deck;
|
||||||
|
|
||||||
|
@ -250,25 +252,19 @@ public class CustomCardsService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final String contentType = conn.getContentType();
|
final String contentType = conn.getContentType();
|
||||||
if (!"application/json".equals(contentType)) {
|
if (contentType == null || !contentType.startsWith("application/json")) {
|
||||||
LOG.error(String.format("Got content-type %s for %s", contentType, urlStr));
|
LOG.error(String.format("Got content-type %s for %s", contentType, urlStr));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final InputStream is = conn.getInputStream();
|
try (InputStream is = conn.getInputStream()) {
|
||||||
final InputStreamReader isr = new InputStreamReader(is);
|
return new ByteSource() {
|
||||||
final BufferedReader reader = new BufferedReader(isr);
|
@Override
|
||||||
final StringBuilder builder = new StringBuilder(4096);
|
public InputStream openStream() {
|
||||||
String line;
|
return is;
|
||||||
while ((line = reader.readLine()) != null) {
|
}
|
||||||
builder.append(line);
|
}.asCharSource(Charsets.UTF_8).read();
|
||||||
builder.append('\n');
|
|
||||||
}
|
}
|
||||||
reader.close();
|
|
||||||
isr.close();
|
|
||||||
is.close();
|
|
||||||
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CacheEntry {
|
private static class CacheEntry {
|
||||||
|
|
Loading…
Reference in New Issue