Skip to content

Commit 39fa709

Browse files
committed
🆕 Don't fail if cached file exists and IO exception on ETag request + logging
1 parent ce59abb commit 39fa709

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseWebStore.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
import com.google.gson.Gson;
4949
import com.google.gson.reflect.TypeToken;
50+
import org.slf4j.Logger;
51+
import org.slf4j.LoggerFactory;
5052
import org.spdx.library.InvalidSPDXAnalysisException;
5153
import org.spdx.library.SpdxConstants;
5254

@@ -57,6 +59,8 @@
5759
*/
5860
public class SpdxListedLicenseWebStore extends SpdxListedLicenseModelStore {
5961

62+
private static final Logger logger = LoggerFactory.getLogger(SpdxListedLicenseModelStore.class);
63+
6064
private static final int READ_TIMEOUT = 5000;
6165
private static final int IO_BUFFER_SIZE = 8192;
6266

@@ -130,20 +134,27 @@ private InputStream getUrlInputStreamThroughCache(final URL url) throws IOExcept
130134
final File cachedMetadataFile = new File(cacheDir, cacheKey + ".metadata.json");
131135

132136
if (cachedFile.exists() && cachedMetadataFile.exists()) {
133-
final HashMap<String,String> cachedMetadata = readMetadataFile(cachedMetadataFile);
137+
try {
138+
final HashMap<String,String> cachedMetadata = readMetadataFile(cachedMetadataFile);
134139

135-
if (cachedMetadata != null)
136-
{
137-
final String eTag = cachedMetadata.get("eTag");
138-
final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
139-
connection.setReadTimeout(READ_TIMEOUT);
140-
connection.setRequestProperty("If-None-Match", eTag);
141-
final int status = connection.getResponseCode();
142-
if (status != HttpURLConnection.HTTP_NOT_MODIFIED) {
143-
cacheMiss(url, connection);
140+
if (cachedMetadata != null) {
141+
final String eTag = cachedMetadata.get("eTag");
142+
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
143+
connection.setReadTimeout(READ_TIMEOUT);
144+
connection.setRequestProperty("If-None-Match", eTag);
145+
final int status = connection.getResponseCode();
146+
if (status != HttpURLConnection.HTTP_NOT_MODIFIED) {
147+
cacheMiss(url, connection);
148+
} else {
149+
logger.debug("Cache hit for " + String.valueOf(url));
150+
}
151+
} else {
152+
cacheMiss(url);
144153
}
145-
} else {
146-
cacheMiss(url);
154+
} catch (IOException ioe) {
155+
// We know we have a locally cached file here, so if we happen to get an error while making the ETag
156+
// request to check if it's up-to-date, we can safely ignore it and fall back on the (possibly stale)
157+
// cached content file. This makes the code more robust when the cache has previously been populated.
147158
}
148159
} else {
149160
cacheMiss(url);
@@ -216,6 +227,8 @@ private void writeContentFile(final InputStream urlInputStream, final File cache
216227
}
217228

218229
private void cacheMiss(URL url, HttpURLConnection connection) throws IOException {
230+
logger.debug("Cache miss for " + String.valueOf(url));
231+
219232
final URL redirectUrl = processPossibleRedirect(connection);
220233
if (redirectUrl != null) {
221234
url = redirectUrl;

src/test/resources/log4j2-test.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="warn">
3+
<Appenders>
4+
<Console name="console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Logger name="org.spdx.storage.listedlicense.SpdxListedLicenseWebStore" additivity="false" level="debug">
10+
<AppenderRef ref="console" />
11+
</Logger>
12+
<Root level="info" additivity="false">
13+
<AppenderRef ref="console" />
14+
</Root>
15+
</Loggers>
16+
</Configuration>

0 commit comments

Comments
 (0)