|
47 | 47 |
|
48 | 48 | import com.google.gson.Gson; |
49 | 49 | import com.google.gson.reflect.TypeToken; |
| 50 | +import org.slf4j.Logger; |
| 51 | +import org.slf4j.LoggerFactory; |
50 | 52 | import org.spdx.library.InvalidSPDXAnalysisException; |
51 | 53 | import org.spdx.library.SpdxConstants; |
52 | 54 |
|
|
57 | 59 | */ |
58 | 60 | public class SpdxListedLicenseWebStore extends SpdxListedLicenseModelStore { |
59 | 61 |
|
| 62 | + private static final Logger logger = LoggerFactory.getLogger(SpdxListedLicenseModelStore.class); |
| 63 | + |
60 | 64 | private static final int READ_TIMEOUT = 5000; |
61 | 65 | private static final int IO_BUFFER_SIZE = 8192; |
62 | 66 |
|
@@ -130,20 +134,27 @@ private InputStream getUrlInputStreamThroughCache(final URL url) throws IOExcept |
130 | 134 | final File cachedMetadataFile = new File(cacheDir, cacheKey + ".metadata.json"); |
131 | 135 |
|
132 | 136 | if (cachedFile.exists() && cachedMetadataFile.exists()) { |
133 | | - final HashMap<String,String> cachedMetadata = readMetadataFile(cachedMetadataFile); |
| 137 | + try { |
| 138 | + final HashMap<String,String> cachedMetadata = readMetadataFile(cachedMetadataFile); |
134 | 139 |
|
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); |
144 | 153 | } |
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. |
147 | 158 | } |
148 | 159 | } else { |
149 | 160 | cacheMiss(url); |
@@ -216,6 +227,8 @@ private void writeContentFile(final InputStream urlInputStream, final File cache |
216 | 227 | } |
217 | 228 |
|
218 | 229 | private void cacheMiss(URL url, HttpURLConnection connection) throws IOException { |
| 230 | + logger.debug("Cache miss for " + String.valueOf(url)); |
| 231 | + |
219 | 232 | final URL redirectUrl = processPossibleRedirect(connection); |
220 | 233 | if (redirectUrl != null) { |
221 | 234 | url = redirectUrl; |
|
0 commit comments