View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
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  import org.apache.commons.io.IOUtils;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  
30  public class JsonArrayCveItemSource implements CveItemSource<DefCveItem> {
31  
32      private final ObjectMapper mapper;
33      private final InputStream inputStream;
34      private final JsonParser jsonParser;
35      private DefCveItem currentItem;
36      private DefCveItem nextItem;
37  
38      public JsonArrayCveItemSource(InputStream inputStream) throws IOException {
39          mapper = new ObjectMapper();
40          mapper.registerModule(new JavaTimeModule());
41          this.inputStream = inputStream;
42          jsonParser = mapper.getFactory().createParser(inputStream);
43  
44          if (jsonParser.nextToken() == JsonToken.START_ARRAY) {
45              nextItem = readItem(jsonParser);
46          }
47      }
48  
49      @Override
50      public void close() throws Exception {
51          if (jsonParser != null) {
52              try {
53                  jsonParser.close();
54              } catch (IOException ex) {
55                  //ignore
56              }
57          }
58          if (inputStream != null) {
59              try {
60                  inputStream.close();
61              } catch (IOException ex) {
62                  //ignore
63              }
64          }
65      }
66  
67      @Override
68      public boolean hasNext() {
69          return nextItem != null;
70      }
71  
72      @Override
73      public DefCveItem next() throws IOException {
74          currentItem = nextItem;
75          nextItem = readItem(jsonParser);
76          return currentItem;
77      }
78  
79      private DefCveItem readItem(JsonParser jsonParser) throws IOException {
80          if (jsonParser.nextToken() == JsonToken.START_OBJECT) {
81              return mapper.readValue(jsonParser, DefCveItem.class);
82          }
83          return null;
84      }
85  }