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  
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  public class JsonArrayCveItemSource implements CveItemSource<DefCveItem> {
30  
31      /**
32       * The object mapper.
33       */
34      private final ObjectMapper mapper;
35      /**
36       * The input stream.
37       */
38      private final InputStream inputStream;
39      /**
40       * The JSON parser.
41       */
42      private final JsonParser jsonParser;
43      /**
44       * The current item.
45       */
46      private DefCveItem currentItem;
47      /**
48       * The next item.
49       */
50      private DefCveItem nextItem;
51  
52      /**
53       * Constructs a new Item Source.
54       *
55       * @param inputStream the input stream to read from
56       * @throws IOException thrown if there is a problem reading from the input
57       * stream
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                  //ignore
77              }
78          }
79          if (inputStream != null) {
80              try {
81                  inputStream.close();
82              } catch (IOException ex) {
83                  //ignore
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 }