Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

fixes #39 by downloading to a temporary directory #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 0 additions & 3 deletions src/main/java/us/springett/nistdatamirror/MetaProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/us/springett/nistdatamirror/NistDataMirror.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -51,12 +54,13 @@ public class NistDataMirror {
private static final int START_YEAR = 2002;
private static final int END_YEAR = Calendar.getInstance().get(Calendar.YEAR);
private File outputDir;
private Path downloadDir;
private boolean downloadFailed = false;
private boolean json = true;
private boolean xml = true;
private final Proxy proxy;

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
// Ensure at least one argument was specified
if (args.length == 0 || args.length > 2) {
System.out.println("Usage: java NistDataMirror outputDir [xml|json]");
Expand All @@ -73,11 +77,12 @@ public static void main(String[] args) {
}
}

public NistDataMirror(String outputDirPath, String type) {
public NistDataMirror(String outputDirPath, String type) throws IOException {
outputDir = new File(outputDirPath);
if (!outputDir.exists()) {
outputDir.mkdirs();
}
downloadDir = Files.createTempDirectory("nist-data-mirror-");
if (type != null) {
if (type.equals("xml")) {
json = false;
Expand Down Expand Up @@ -148,12 +153,24 @@ public void mirror() {
String cveJsonBaseUrl = CVE_JSON_10_BASE_URL.replace("%d", String.valueOf(i));
doDownload(cveJsonBaseUrl);
}
// TODO Move CVE_BASE_META to outputDir
}
}
} catch (MirrorException ex) {
downloadFailed = true;
System.err.println("Error mirroring the NVD CVE data");
ex.printStackTrace(System.err);
} finally {
if (downloadDir != null && downloadDir.toFile().exists()) {
for (File file : downloadDir.toFile().listFiles()) {
if (downloadFailed) {
file.delete();
} else {
file.renameTo(Paths.get(outputDir.getAbsolutePath(), file.getName()).toFile());
}
}
downloadDir.toFile().delete();
}
}
}

Expand All @@ -167,7 +184,7 @@ private MetaProperties readLocalMetaForURL(String metaUrl) throws MirrorExceptio
MetaProperties meta = null;
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf('/') + 1);
File file = new File(outputDir, filename).getAbsoluteFile();
File file = new File(downloadDir.toFile(), filename).getAbsoluteFile();
if (file.isFile()) {
meta = new MetaProperties(file);
}
Expand All @@ -188,12 +205,12 @@ private void doDownload(String nvdUrl) throws MirrorException {
try {
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf('/') + 1);
file = new File(outputDir, filename).getAbsoluteFile();
file = new File(downloadDir.toFile(), filename).getAbsoluteFile();

URLConnection connection = url.openConnection(proxy);
System.out.println("Downloading " + url.toExternalForm());
bis = new BufferedInputStream(connection.getInputStream());
file = new File(outputDir, filename);
file = new File(downloadDir.toFile(), filename);
bos = new BufferedOutputStream(new FileOutputStream(file));

int i;
Expand Down