Skip to content

Commit

Permalink
fix: prevent NPE
Browse files Browse the repository at this point in the history
resolves #112
  • Loading branch information
jeremylong committed Dec 15, 2023
1 parent c2fb945 commit eb4f1c4
Showing 1 changed file with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,37 +319,43 @@ public Collection<DefCveItem> next() {
RateLimitedCall call;
try {
call = getCompletedFuture();
SimpleHttpResponse response = call.getResponse();
if (response.getCode() == 200) {
LOG.debug("Content-Type Received: {}", response.getContentType());
json = response.getBodyText();
// resolve issue #20
if (json == null && response.getBody().isBytes()) {
json = new String(response.getBodyBytes(), StandardCharsets.UTF_8);
}

CveApiJson20 current;
try {
current = objectMapper.readValue(json, CveApiJson20.class);
this.indexesToRetrieve.remove(call.getStartIndex());
} catch (JsonProcessingException e) {
if (call == null) {
if (hasNext()) {
return next();
}
this.totalAvailable = current.getTotalResults();
lastUpdated = findLastUpdated(lastUpdated, current.getVulnerabilities());
if (firstCall) {
firstCall = false;
queueCalls();
}
if (futures.isEmpty() && !indexesToRetrieve.isEmpty()) {
queueUnsuccessful();
}
return current.getVulnerabilities();
} else {
lastStatusCode = response.getCode();
LOG.debug("Status Code: {}", lastStatusCode);
LOG.debug("Response: {}", response.getBodyText());
throw new NvdApiException("NVD Returned Status Code: " + lastStatusCode);
SimpleHttpResponse response = call.getResponse();
if (response.getCode() == 200) {
LOG.debug("Content-Type Received: {}", response.getContentType());
json = response.getBodyText();
// resolve issue #20
if (json == null && response.getBody().isBytes()) {
json = new String(response.getBodyBytes(), StandardCharsets.UTF_8);
}

CveApiJson20 current;
try {
current = objectMapper.readValue(json, CveApiJson20.class);
this.indexesToRetrieve.remove(call.getStartIndex());
} catch (JsonProcessingException e) {
return next();
}
this.totalAvailable = current.getTotalResults();
lastUpdated = findLastUpdated(lastUpdated, current.getVulnerabilities());
if (firstCall) {
firstCall = false;
queueCalls();
}
if (futures.isEmpty() && !indexesToRetrieve.isEmpty()) {
queueUnsuccessful();
}
return current.getVulnerabilities();
} else {
lastStatusCode = response.getCode();
LOG.debug("Status Code: {}", lastStatusCode);
LOG.debug("Response: {}", response.getBodyText());
throw new NvdApiException("NVD Returned Status Code: " + lastStatusCode);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand All @@ -362,8 +368,8 @@ public Collection<DefCveItem> next() {
return next();
}
close();
return null;
}
return null;
}

/**
Expand All @@ -389,17 +395,17 @@ public ZonedDateTime getLastUpdated() {
}

private RateLimitedCall getCompletedFuture() throws InterruptedException, ExecutionException {
boolean notFound = futures.size() > 0;
Future<RateLimitedCall> result = null;
while (notFound) {
while (result == null && !futures.isEmpty()) {
for (Future<RateLimitedCall> future : futures) {
if (future.isDone()) {
result = future;
notFound = false;
break;
}
}
Thread.sleep(500);
if (result == null) {
Thread.sleep(500);
}
}
if (result != null) {
futures.remove(result);
Expand Down

0 comments on commit eb4f1c4

Please sign in to comment.