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 com.fasterxml.jackson.core.JsonParser;
21 import com.fasterxml.jackson.core.JsonToken;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
24 import io.github.jeremylong.openvulnerability.client.nvd.DefCveItem;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28
29 public class JsonArrayCveItemSource implements CveItemSource<DefCveItem> {
30
31
32
33
34 private final ObjectMapper mapper;
35
36
37
38 private final InputStream inputStream;
39
40
41
42 private final JsonParser jsonParser;
43
44
45
46 private DefCveItem currentItem;
47
48
49
50 private DefCveItem nextItem;
51
52
53
54
55
56
57
58
59 public JsonArrayCveItemSource(InputStream inputStream) throws IOException {
60 mapper = new ObjectMapper();
61 mapper.registerModule(new JavaTimeModule());
62 this.inputStream = inputStream;
63 jsonParser = mapper.getFactory().createParser(inputStream);
64
65 if (jsonParser.nextToken() == JsonToken.START_ARRAY) {
66 nextItem = readItem(jsonParser);
67 }
68 }
69
70 @Override
71 public void close() throws Exception {
72 if (jsonParser != null) {
73 try {
74 jsonParser.close();
75 } catch (IOException ex) {
76
77 }
78 }
79 if (inputStream != null) {
80 try {
81 inputStream.close();
82 } catch (IOException ex) {
83
84 }
85 }
86 }
87
88 @Override
89 public boolean hasNext() {
90 return nextItem != null;
91 }
92
93 @Override
94 public DefCveItem next() throws IOException {
95 currentItem = nextItem;
96 nextItem = readItem(jsonParser);
97 return currentItem;
98 }
99
100 private DefCveItem readItem(JsonParser jsonParser) throws IOException {
101 if (jsonParser.nextToken() == JsonToken.START_OBJECT) {
102 return mapper.readValue(jsonParser, DefCveItem.class);
103 }
104 return null;
105 }
106 }