Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gzip cache output #79

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ public Collection<DefCveItem> next() {
this.indexesToRetrieve.remove(call.getStartIndex());
} catch (JsonProcessingException e) {
return next();
// throw new NvdApiException("Failed to parse JSON starting with: \"" + json.substring(0, 100) + "\"",
// e);
// throw new NvdApiException("Failed to parse JSON starting with: \"" + json.substring(0, 100) +
// "\"",
// e);
}
this.totalAvailable = current.getTotalResults();
lastUpdated = findLastUpdated(lastUpdated, current.getVulnerabilities());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Year;
Expand All @@ -50,6 +51,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import static com.diogonunes.jcolor.Ansi.colorize;

Expand Down Expand Up @@ -233,12 +236,14 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
final String prefix = properties.get("prefix", "nvdcve-");
// load existing cached files
for (int year = 2002; year <= Year.now().getValue(); year++) {
File file = new File(properties.getDirectory(), prefix + year + ".json");
File file = new File(properties.getDirectory(), prefix + year + ".json.gz");
cves.put(Integer.toString(year), new HashMap<>());
if (file.isFile()) {
CveApiJson20 data;
try (InputStream in = new FileInputStream(file)) {
data = objectMapper.readValue(in, CveApiJson20.class);
try (FileInputStream fileInputStream = new FileInputStream(file);
GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
) {
data = objectMapper.readValue(gzipInputStream, CveApiJson20.class);
} catch (IOException exception) {
throw new CacheException("Unable to read cached data: " + file, exception);
}
Expand Down Expand Up @@ -266,7 +271,7 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
final String version = "2.0";

for (Map.Entry<String, HashMap<String, DefCveItem>> entry : cves.entrySet()) {
File file = new File(properties.getDirectory(), prefix + entry.getKey() + ".json");
File file = new File(properties.getDirectory(), prefix + entry.getKey() + ".json.gz");
List<DefCveItem> vulnerabilities = new ArrayList(entry.getValue().values());
vulnerabilities.sort((v1, v2) -> {
return v1.getCve().getId().compareTo(v2.getCve().getId());
Expand All @@ -284,8 +289,9 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
properties.set("lastModifiedDate." + entry.getKey(), timestamp);
CveApiJson20 data = new CveApiJson20(vulnerabilities.size(), 0, vulnerabilities.size(), format, version,
timestamp, vulnerabilities);
try {
objectMapper.writeValue(file, data);
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);) {
objectMapper.writeValue(gzipOutputStream, data);
} catch (IOException ex) {
throw new CacheException("Unable to write cached data: " + file, ex);
}
Expand Down
Loading