Reduce complexity of switch statement in VulnerbilityAPI
@@ -77,41 +77,25 @@ public class VulnerabilityApi {
}
@PostMapping(value = "/vulnerabilities/coordinates", produces = MediaType.APPLICATION_JSON_VALUE)
- ResponseEntity<String> getCoordinatesVulnerabilities(@RequestBody List<String> mavenCoordinates,
+ ResponseEntity<String> getCoordinatesVulnerabilities(@RequestBody List<String> coordinates,
@RequestParam(required = false, defaultValue = "") List<String> attributes) {
JSONArray jsonArray = new JSONArray();
logger.info("Received a list of coordinates");
+ coordinates.stream().map(c -> {
+ String package_name, package_version;
switch (KnowledgeBaseConnector.forge) {
case "mvn": {
- mavenCoordinates.stream().map(c -> {
- var package_name = c.split(Constants.mvnCoordinateSeparator)[0]
+ package_name = c.split(Constants.mvnCoordinateSeparator)[0]
+ Constants.mvnCoordinateSeparator
+ c.split(Constants.mvnCoordinateSeparator)[1];
- var package_version = c.split(Constants.mvnCoordinateSeparator)[2];
- var vulnerabilities = "";
- if (!KnowledgeBaseConnector.kbDao.assertPackageExistence(package_name, package_version)) {
- try {
- LazyIngestionProvider.ingestArtifactWithDependencies(package_name, package_version);
- logger.info("Coordinate " + c + " not found, but should be processed soon. Try again later");
- } catch (Exception e) {
- logger.info("Cannot find coordinate " + c + ": " +e.getMessage());
- }
- } else {
- var statements = KnowledgeBaseConnector.kbDao
- .getPackageVersionVulnerabilities(package_name, package_version, false);
- JSONArray vul = new JSONArray(statements);
- vulnerabilities = getStatementsAttributes(vul, attributes).toString();
- }
- var json = new JSONObject();
- json.put(c, vulnerabilities);
- return json;
- }).forEach(jsonArray::put);
+ package_version = c.split(Constants.mvnCoordinateSeparator)[2];
break;
}
default: {
- mavenCoordinates.stream().map(c -> {
- var package_name = c.split(Constants.mvnCoordinateSeparator)[0];
- var package_version = c.split(Constants.mvnCoordinateSeparator)[1];
+ package_name = c.split(Constants.mvnCoordinateSeparator)[0];
+ package_version = c.split(Constants.mvnCoordinateSeparator)[1];
+ }
+ }
var vulnerabilities = "";
if (!KnowledgeBaseConnector.kbDao.assertPackageExistence(package_name, package_version)) {
try {
@@ -130,9 +114,6 @@ public class VulnerabilityApi {
json.put(c, vulnerabilities);
return json;
}).forEach(jsonArray::put);
- break;
- }
- }
var result = jsonArray.toString();
result = result.replace("\\/", "/");
return new ResponseEntity<>(result, HttpStatus.OK);
