1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.update.nvd.api;
19
20 import java.io.File;
21 import java.net.URL;
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Future;
25 import javax.annotation.concurrent.ThreadSafe;
26 import org.apache.commons.lang3.StringUtils;
27 import org.owasp.dependencycheck.data.nvdcve.CveDB;
28 import org.owasp.dependencycheck.utils.Downloader;
29 import org.owasp.dependencycheck.utils.Settings;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 @ThreadSafe
40 public class DownloadTask implements Callable<Future<NvdApiProcessor>> {
41
42
43
44
45 private static final Logger LOGGER = LoggerFactory.getLogger(DownloadTask.class);
46
47
48
49 private final CveDB cveDB;
50
51
52
53 private final ExecutorService processorService;
54
55
56
57 private final String url;
58
59
60
61 private final Settings settings;
62
63
64
65
66
67
68
69
70
71
72
73 public DownloadTask(String url, ExecutorService processor, CveDB cveDB, Settings settings) {
74 this.url = url;
75 this.processorService = processor;
76 this.cveDB = cveDB;
77 this.settings = settings;
78 }
79
80 @SuppressWarnings("BusyWait")
81 @Override
82 public Future<NvdApiProcessor> call() throws Exception {
83 try {
84 final URL u = new URL(url);
85 LOGGER.info("Download Started for NVD Cache - {}", url);
86 final long startDownload = System.currentTimeMillis();
87 final File outputFile = settings.getTempFile("nvd-datafeed-", "json.gz");
88 Downloader.getInstance().fetchFile(u, outputFile, true, Settings.KEYS.NVD_API_DATAFEED_USER, Settings.KEYS.NVD_API_DATAFEED_PASSWORD);
89 if (this.processorService == null) {
90 return null;
91 }
92 final NvdApiProcessor task = new NvdApiProcessor(cveDB, outputFile, startDownload);
93 final Future<NvdApiProcessor> val = this.processorService.submit(task);
94 return val;
95 } catch (Throwable ex) {
96 LOGGER.error("Error downloading NVD CVE - {} Reason: {}", url, ex.getMessage());
97 throw ex;
98 } finally {
99 settings.cleanup(false);
100 }
101 }
102
103
104
105
106
107
108
109
110 public boolean isModified() {
111 return StringUtils.containsIgnoreCase(url, "modified");
112 }
113 }