Replace custom GSON builder with the one from VulnerableCallChainJsonUtils
package eu.fasten.analyzer.restapiplugin.mvn.api.impl;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import eu.fasten.analyzer.restapiplugin.mvn.KnowledgeBaseConnector;
import eu.fasten.analyzer.restapiplugin.mvn.api.VulnerableCallChainsApiService;
import eu.fasten.core.data.FastenURI;
import eu.fasten.core.vulchains.VulnerableCallChain;
+import eu.fasten.core.vulchains.VulnerableCallChainJsonUtils;
import eu.fasten.core.vulchains.VulnerableCallChainRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,7 +31,6 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
-import java.nio.charset.StandardCharsets;
import java.util.Set;
@@ -54,36 +52,32 @@ public class VulnerableCallChainsApiServiceImpl implements VulnerableCallChainsA
}
/**
- * Helper method to convert set of vulnerable call chains to JSON formatted string response.
- * @param chains - a set of {@link VulnerableCallChain} objects to be serialized.
- * @return {@link String} formatted as JSON response.
+ * Helper function that prepares an HTTP response for the requested vulnerable call chains.
+ * @param chains - the set of vulnerable call chains to be included in the response.
+ * @return the response entity of 200 status and the call chains as JSON.
*/
- private String VulnerableCallChainsToJSON(Set<VulnerableCallChain> chains) {
- GsonBuilder builder = new GsonBuilder();
- Gson gson = builder.create();
- return gson.toJson(chains);
+ private ResponseEntity<String> prepareResponse(Set<VulnerableCallChain> chains) {
+ var result = VulnerableCallChainJsonUtils.toJson(chains);
+ return new ResponseEntity<>(result, HttpStatus.OK);
}
@Override
public ResponseEntity<String> getChainsForPackage(String forge, String packageName, String packageVersion) {
Set<VulnerableCallChain> chains = vulnerableCallChainRepository.getChainsForPackage(packageName, packageVersion);
- var result = VulnerableCallChainsToJSON(chains);
- return new ResponseEntity<>(result, HttpStatus.OK);
+ return prepareResponse(chains);
}
@Override
public ResponseEntity<String> getChainsForModule(String forge, String packageName, String packageVersion, String rawPath) {
FastenURI fastenUri = FastenURI.create(forge, packageName, packageVersion, rawPath);
Set<VulnerableCallChain> chains = vulnerableCallChainRepository.getChainsForModule(fastenUri);
- var result = VulnerableCallChainsToJSON(chains);
- return new ResponseEntity<>(result, HttpStatus.OK);
+ return prepareResponse(chains);
}
@Override
public ResponseEntity<String> getChainsForCallable(String forge, String packageName, String packageVersion, String rawPath) {
FastenURI fastenUri = FastenURI.create(forge, packageName, packageVersion, rawPath);
Set<VulnerableCallChain> chains = vulnerableCallChainRepository.getChainsForCallable(fastenUri);
- var result = VulnerableCallChainsToJSON(chains);
- return new ResponseEntity<>(result, HttpStatus.OK);
+ return prepareResponse(chains);
}
}
