Skip to content

Commit

Permalink
Do not use var when the type is not obvious
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Mar 25, 2023
1 parent 2da223f commit e681cac
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import alpine.model.About;
import alpine.model.ConfigProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.dependencytrack.common.Jackson;
import org.dependencytrack.model.Finding;
import org.dependencytrack.model.Project;
Expand Down Expand Up @@ -73,7 +74,7 @@ private JsonNode initialize(final UUID projectUuid, final List<Finding> findings
This is useful for file-based parsing systems that needs to be able to
identify what type of file it is, and what type of system generated it.
*/
final var meta = Jackson.newObject();
final ObjectNode meta = Jackson.newObject();
meta.put(FIELD_APPLICATION, about.getApplication());
meta.put(FIELD_VERSION, about.getVersion());
meta.put(FIELD_TIMESTAMP, DateUtil.toISO8601(new Date()));
Expand All @@ -87,7 +88,7 @@ private JsonNode initialize(final UUID projectUuid, final List<Finding> findings
well as not have to perform additional queries back to Dependency-Track
to discover basic project information.
*/
final var projectJson = Jackson.newObject();
final ObjectNode projectJson = Jackson.newObject();
projectJson.put(FIELD_UUID, project.getUuid().toString());
projectJson.put(FIELD_NAME, project.getName());
if (project.getVersion() != null) {
Expand All @@ -107,7 +108,7 @@ private JsonNode initialize(final UUID projectUuid, final List<Finding> findings
Add the meta and project objects along with the findings array
to a root json object and return.
*/
final var root = Jackson.newObject();
final ObjectNode root = Jackson.newObject();
root.put(FIELD_VERSION, FPF_VERSION);
root.set(FIELD_META, meta);
root.set(FIELD_PROJECT, projectJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import alpine.common.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -53,7 +54,7 @@ public FortifySscClient(final FortifySscUploader uploader, final URL baseURL) {
public String generateOneTimeUploadToken(final String citoken) {
LOGGER.debug("Generating one-time upload token");
var request = new HttpPost(baseURL + "/api/v1/fileTokens");
final var payload = Jackson.newObject().put("fileTokenType", "UPLOAD");
final ObjectNode payload = Jackson.newObject().put("fileTokenType", "UPLOAD");
request.addHeader("Content-Type", "application/json");
request.addHeader("Authorization", "FortifyToken " + Base64.getEncoder().encodeToString(citoken.getBytes(StandardCharsets.UTF_8)));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ private ObjectNode generateKdiAsset(final Project project, final String external
// If the project has tags, add them to the KDI
final List<Tag> tags = project.getTags();
if (CollectionUtils.isNotEmpty(tags)) {
final var tagArray = Jackson.newArray();
final ArrayNode tagArray = Jackson.newArray();
for (final Tag tag: tags) {
tagArray.add(tag.getName());
}
asset.put("tags", tagArray);
asset.set("tags", tagArray);
}
return asset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.dependencytrack.parser.vulndb;

import com.fasterxml.jackson.databind.JsonNode;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
Expand Down Expand Up @@ -84,7 +85,7 @@ private Results getResults(String url, Class clazz, int size, int page) throws I
Results results;
if (response != null) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
results = vulnDbParser.parse(jsonObject, clazz);
return results;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import alpine.model.ConfigProperty;
import alpine.notification.Notification;
import alpine.notification.NotificationLevel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.github.packageurl.PackageURLBuilder;
Expand Down Expand Up @@ -134,7 +136,7 @@ private void retrieveAdvisories(final String advisoriesEndCursor) throws IOExcep
request.addHeader("Authorization", "bearer " + accessToken);
request.addHeader("content-type", "application/json");
request.addHeader("accept", "application/json");
var jsonBody = Jackson.newObject();
ObjectNode jsonBody = Jackson.newObject();
jsonBody.put("query", queryTemplate);
var stringEntity = new StringEntity(jsonBody.toString());
request.setEntity(stringEntity);
Expand All @@ -145,7 +147,7 @@ private void retrieveAdvisories(final String advisoriesEndCursor) throws IOExcep
mirroredWithoutErrors = false;
} else {
var parser = new GitHubSecurityAdvisoryParser();
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
final PageableList pageableList = parser.parse(jsonObject);
updateDatasource(pageableList.getAdvisories());
if (pageableList.isHasNextPage()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public MetaModel analyze(final Component component) {
final String url = String.format(baseUrl + API_URL, component.getPurl().getName());
try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
final JsonNode crate = Jackson.optNode(jsonObject, "crate");
if (crate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.dependencytrack.tasks.repositories;

import alpine.common.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.packageurl.PackageURL;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -68,7 +69,7 @@ public MetaModel analyze(final Component component) {
final String url = String.format(baseUrl + API_URL, component.getPurl().getName());
try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
final String latest = jsonObject.get("version").asText();
meta.setLatestVersion(latest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.dependencytrack.tasks.repositories;

import alpine.common.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.packageurl.PackageURL;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -67,7 +68,7 @@ public MetaModel analyze(final Component component) {

try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
final var jsonObject = Jackson.readHttpResponse(response);
final JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
meta.setLatestVersion(jsonObject.get("Version").asText());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public MetaModel analyze(final Component component) {
final String url = String.format(baseUrl + API_URL, packageName);
try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
final ArrayNode releasesArray = Jackson.asArray(jsonObject, "releases");
if (releasesArray.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.dependencytrack.tasks.repositories;

import alpine.common.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.packageurl.PackageURL;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -76,7 +77,7 @@ public MetaModel analyze(final Component component) {
final String url = String.format(baseUrl + API_URL, packageName);
try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
final String latest = Jackson.optString(jsonObject, "latest");
if (latest != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private boolean performVersionCheck(final MetaModel meta, final Component compon
final String url = String.format(versionQueryUrl, component.getPurl().getName().toLowerCase());
try (final CloseableHttpResponse response = processHttpRequest(url)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
var jsonObject = Jackson.readHttpResponse(response);
JsonNode jsonObject = Jackson.readHttpResponse(response);
if (jsonObject != null) {
final ArrayNode versions = Jackson.asArray(jsonObject, "versions");
final String latest = findLatestVersion(versions); // get the last version in the array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import alpine.model.ConfigProperty;
import alpine.security.crypto.DataEncryption;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import io.github.resilience4j.core.IntervalFunction;
Expand Down Expand Up @@ -197,11 +199,11 @@ public void analyze(final List<Component> components) {
List<Component> componentWithInvalidAnalysisFromCache = componentsPartitionByCacheValidity.get(false);
final Pageable<Component> paginatedComponents = new Pageable<>(Config.getInstance().getPropertyAsInt(ConfigKey.OSSINDEX_REQUEST_MAX_PURL), componentWithInvalidAnalysisFromCache);
while (!paginatedComponents.isPaginationComplete()) {
final var coordinates = Jackson.newArray();
final ArrayNode coordinates = Jackson.newArray();
final List<Component> paginatedList = paginatedComponents.getPaginatedList();
paginatedList.forEach(component -> coordinates.add(minimizePurl(component.getPurl())));
if (coordinates.size() > 0) {
final var json = Jackson.newObject();
final ObjectNode json = Jackson.newObject();
json.set("coordinates", coordinates);
try {
final List<ComponentReport> report = ossIndexRetryer.executeCheckedSupplier(() -> submit(json));
Expand Down

0 comments on commit e681cac

Please sign in to comment.