Use Gson instead of Jackson in the DB code. Some more null checks.
package eu.fasten.analyzer.vulnerabilitystatementsprocessor.db;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
import eu.fasten.analyzer.vulnerabilitystatementsprocessor.utils.Cache;
import eu.fasten.core.data.Constants;
import eu.fasten.core.data.metadatadb.codegen.tables.Callables;
@@ -43,6 +42,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Objects;
import java.util.stream.Collectors;
import static eu.fasten.core.utils.FastenUriUtils.generateFullFastenUri;
@@ -50,7 +50,7 @@ import static eu.fasten.core.utils.FastenUriUtils.generateFullFastenUri;
public class MetadataUtility {
- private final ObjectMapper objectMapper = new ObjectMapper();
+ private final Gson gson = new Gson();
private final Cache cache = new Cache();
private final Logger logger = LoggerFactory.getLogger(MetadataUtility.class.getName());
@@ -67,7 +67,7 @@ public class MetadataUtility {
public HashMap<String, Long> getPackageIds(DSLContext context, Vulnerability v) {
var pkgIds = new HashMap<String, Long>();
- var uniqueBasePurls = v.getPurls().stream().map(p -> Purl.of(p).getBasePurl())
+ var uniqueBasePurls = v.getPurls().stream().map(p -> Objects.requireNonNull(Purl.of(p)).getBasePurl())
.distinct().collect(Collectors.toList());
for (String basePurl : uniqueBasePurls) {
@@ -100,7 +100,7 @@ public class MetadataUtility {
var pkgVersionIds = new ArrayList<Long>();
var verifiedPurls = new LinkedHashSet<String>();
for (var purlString : purls) {
- var purl = Purl.of(purlString);
+ var purl = new Purl(purlString);
var pkgId = pkgIds.get(purl.getBasePurl());
var pkgVersionId = getPackageVersionId(purl, context, pkgId);
if (pkgVersionId > 0L) {
@@ -297,12 +297,8 @@ public class MetadataUtility {
metadata.put("vulnerabilities", new JSONObject());
// Step 3: Get the JSON of the vulnerability and put it in the metadata
- try {
- var vulnJson = objectMapper.writeValueAsString(v);
+ var vulnJson = gson.toJson(v);
((JSONObject) metadata.get("vulnerabilities")).put(v.getId(), new JSONObject(vulnJson));
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
// Step 4: Update the value of the metadata in the DB
context.update(PackageVersions.PACKAGE_VERSIONS)
@@ -335,12 +331,8 @@ public class MetadataUtility {
metadata.put("vulnerabilities", new JSONObject());
// Step 3: Get the JSON of the vulnerability and put it in the metadata
- try {
- var vulnJson = objectMapper.writeValueAsString(v);
+ var vulnJson = gson.toJson(v);
((JSONObject) metadata.get("vulnerabilities")).put(v.getId(), new JSONObject(vulnJson));
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
// Step 4: Update the value of the metadata in the DB
return context.update(Callables.CALLABLES)
