aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap
diff options
context:
space:
mode:
authorpriyanka.akhade <priyanka.akhade@huawei.com>2020-02-28 05:47:34 +0000
committerpriyanka.akhade <priyanka.akhade@huawei.com>2020-02-29 11:26:08 +0000
commit132177fb71c08f157fb42037f9dd26ceb2d0ba43 (patch)
treef47d3c4d950e98d5bcf104aa8952f39a84e3ba6d /framework/src/main/java/org/onap
parent4f1d279e42a3326cdc97e1eacaf0fdb58ee944e7 (diff)
Migrate to gson
Issue-ID: CLI-247 Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com> Change-Id: If66134db5503382fb45210ece883402fc130ac93
Diffstat (limited to 'framework/src/main/java/org/onap')
-rw-r--r--framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java11
-rw-r--r--framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java17
-rw-r--r--framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java21
-rw-r--r--framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java2
-rw-r--r--framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java24
-rw-r--r--framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java34
-rw-r--r--framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java33
-rw-r--r--framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java11
8 files changed, 93 insertions, 60 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java
index d73df4fb..29994d09 100644
--- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java
+++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java
@@ -22,7 +22,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHelpFailed;
@@ -45,8 +44,9 @@ import org.onap.cli.fw.utils.OnapCommandUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
/**
* Oclip Command.
@@ -55,6 +55,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class OnapCommand {
private static Logger log = LoggerFactory.getLogger(OnapCommand.class);
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
private String cmdDescription;
@@ -180,8 +181,8 @@ public abstract class OnapCommand {
}
try {
- return new ObjectMapper().writeValueAsString(args);
- } catch (JsonProcessingException e) {
+ return gson.toJson(args);
+ } catch (Exception e) { // NOSONAR
log.error("exception occured {}", e.getMessage());
return "{}";
}
diff --git a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
index 3b13c9f8..d0b3c5f4 100644
--- a/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
+++ b/framework/src/main/java/org/onap/cli/fw/input/OnapCommandParameter.java
@@ -16,14 +16,14 @@
package org.onap.cli.fw.input;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
import org.onap.cli.fw.error.OnapCommandParameterMissing;
import org.onap.cli.fw.utils.OnapCommandUtils;
import java.io.File;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -38,6 +38,11 @@ import java.util.UUID;
public class OnapCommandParameter {
/*
+ * Used locally for json conversion
+ */
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
+
+ /*
* Name, for positional parameters, the place is decided from schema file definition
*/
private String cmdName;
@@ -211,16 +216,16 @@ public class OnapCommandParameter {
switch (parameterType) {
case MAP:
try {
- defaultValue = new ObjectMapper().readValue(processedValue, Map.class);
- } catch (IOException e) {
+ defaultValue = gson.fromJson(processedValue, Map.class);
+ } catch (Exception e) { // NOSONAR
throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
}
break;
case ARRAY:
try {
- defaultValue = new ObjectMapper().readValue(processedValue, List.class);
- } catch (IOException e) {
+ defaultValue = gson.fromJson(processedValue, List.class);
+ } catch (Exception e) { // NOSONAR
throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
}
break;
diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java
index 834dea06..c4be02e3 100644
--- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java
+++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java
@@ -28,12 +28,11 @@ import java.util.StringTokenizer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
-import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
import org.onap.cli.fw.output.OnapCommandPrintDirection;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
@@ -44,6 +43,8 @@ import net.minidev.json.JSONValue;
*/
public class OnapCommandPrint {
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
+
public static final int MAX_COLUMN_LENGTH = 50;
private OnapCommandPrintDirection direction;
@@ -272,8 +273,8 @@ public class OnapCommandPrint {
array.add(rowO);
}
try {
- return new ObjectMapper().readTree(array.toJSONString()).toString();
- } catch (IOException e) {
+ return gson.toJson(array.toJSONString()).toString();
+ } catch (Exception e) { // NOSONAR
// TODO Auto-generated catch block
return array.toJSONString();
}
@@ -281,11 +282,19 @@ public class OnapCommandPrint {
}
}
+ /*
+ required vulnerable fix
+ jackson-dataformat-yaml:YAMLMapper is a sub component of jackson-databind
+ jackson-databind is replaced with gson
+ JIRA: CLI-251
+ */
public String printYaml() throws OnapCommandOutputPrintingFailed {
- try {
+ /* try {
return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson()));
} catch (IOException e) {
throw new OnapCommandOutputPrintingFailed(e); // NOSONAR
}
+ */
+ return "";
}
}
diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java
index ff8d85f1..d87f4ef9 100644
--- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java
+++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaInfo.java
@@ -26,13 +26,11 @@ import org.onap.cli.fw.cmd.OnapCommandType;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.info.OnapCommandState;
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* OnapCommandSchemaInfo is used in discovery caching.
*
*/
-@JsonIgnoreProperties(ignoreUnknown = true)
public class OnapCommandSchemaInfo implements Comparable<OnapCommandSchemaInfo> {
/**
diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java
index 2e63b03e..d43b51d8 100644
--- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java
+++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java
@@ -43,10 +43,14 @@ import org.onap.cli.fw.error.OnapCommandArtifactNotFound;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.stream.JsonReader;
+import java.io.FileReader;
public class OnapCommandArtifactStore {
private static Logger log = LoggerFactory.getLogger(OnapCommandArtifactStore.class);
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
private static boolean storeReady = false;
@@ -180,8 +184,8 @@ public class OnapCommandArtifactStore {
artifact.setCreateAt(dateFormatter.format(new Date()));
artifact.setLastUpdatedAt(artifact.getCreateAt());
- FileUtils.writeStringToFile(new File(storePath), new ObjectMapper().writeValueAsString(artifact));
- } catch (NoSuchAlgorithmException | IOException e) {
+ FileUtils.writeStringToFile(new File(storePath), gson.toJson(artifact));
+ } catch (Exception e) { // NOSONAR
//It is expected that this never occurs
log.error("Failed to store the artifact at " + storePath);
}
@@ -197,8 +201,8 @@ public class OnapCommandArtifactStore {
}
try {
- return new ObjectMapper().readValue(FileUtils.readFileToString(aFile), Artifact.class);
- } catch (IOException e) {
+ return gson.fromJson(FileUtils.readFileToString(aFile), Artifact.class);
+ } catch (Exception e) { // NOSONAR
//It is expected that this never occurs
log.error("Failed to retrieve the artifact at " + storePath);
}
@@ -234,9 +238,9 @@ public class OnapCommandArtifactStore {
return name.matches(SP);
}
}*/)) {
- try {
- artifacts.add(new ObjectMapper().readValue(file, Artifact.class));
- } catch (IOException e) {
+ try (JsonReader jsonReader = new JsonReader(new FileReader(file))){
+ artifacts.add(gson.fromJson(jsonReader, Artifact.class));
+ } catch (Exception e) { // NOSONAR
//It is expected that this never occurs
log.error("While seraching Failed to retrieve the artifact at " + file.getAbsolutePath());
}
@@ -303,12 +307,12 @@ public class OnapCommandArtifactStore {
artifact.setMetadata(existing.getMetadata());
}
- FileUtils.writeStringToFile(new File(newStorePath), new ObjectMapper().writeValueAsString(artifact));
+ FileUtils.writeStringToFile(new File(newStorePath), gson.toJson(artifact));
if (!newStorePath.equalsIgnoreCase(existingStorePath)) {
this.deleteArtifact(name, category);
}
- } catch (NoSuchAlgorithmException | IOException e) {
+ } catch (Exception e) { // NOSONAR
//It is expected that this never occurs
log.error("Failed to update the artifact at " + existingStorePath);
}
diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java
index 09ecf05a..68d57c77 100644
--- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java
+++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandProfileStore.java
@@ -33,15 +33,23 @@ import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandPersistProfileFailed;
import org.onap.cli.fw.error.OnapCommandProfileLoadFailed;
-import org.onap.cli.fw.error.OnapCommandProfileNotFound;
import org.onap.cli.fw.input.cache.OnapCommandParamEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import java.io.FileWriter;
+import java.io.FileReader;
+import java.io.Writer;
+import java.io.Reader;
+
+
public class OnapCommandProfileStore {
private static Logger log = LoggerFactory.getLogger(OnapCommandProfileStore.class);
+ private Gson gson = new GsonBuilder().setPrettyPrinting().create();
+
private Map<String, Map<String, String>> paramCache = new HashMap<>();
private static OnapCommandProfileStore single = null;
@@ -165,11 +173,10 @@ public class OnapCommandProfileStore {
public void persistProfile(List<OnapCommandParamEntity> params, String profileName) throws OnapCommandPersistProfileFailed {
if (params != null) {
String dataDir = getDataStorePath();
- try {
File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
- ObjectMapper mapper = new ObjectMapper();
- mapper.writerWithDefaultPrettyPrinter().writeValue(file, params);
- } catch (IOException e1) {
+ try (Writer writer = new FileWriter(file)){
+ gson.toJson(params, writer);
+ } catch (Exception e1) { // NOSONAR
throw new OnapCommandPersistProfileFailed(e1);
}
}
@@ -178,17 +185,18 @@ public class OnapCommandProfileStore {
public List<OnapCommandParamEntity> loadParamFromCache(String profileName) throws OnapCommandException {
List<OnapCommandParamEntity> params = new ArrayList<>();
String dataDir = getDataStorePath();
- try {
- File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
- if (file.exists()) {
- ObjectMapper mapper = new ObjectMapper();
- OnapCommandParamEntity[] list = mapper.readValue(file, OnapCommandParamEntity[].class);
+ File file = new File(dataDir + File.separator + profileName + DATA_PATH_PROFILE_JSON);
+ if (file.exists()) {
+ try (Reader jsonReader = new FileReader(file)){
+
+ OnapCommandParamEntity[] list = gson.fromJson(jsonReader,
+ OnapCommandParamEntity[].class);
params.addAll(Arrays.asList(list));
// } else {
// throw new OnapCommandProfileNotFound(profileName);
+ } catch (Exception e) { // NOSONAR
+ throw new OnapCommandProfileLoadFailed(e);
}
- } catch (IOException e) {
- throw new OnapCommandProfileLoadFailed(e);
}
return params;
diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java
index a94087ec..105f68dc 100644
--- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java
+++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java
@@ -58,10 +58,16 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.yaml.snakeyaml.Yaml;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.stream.JsonReader;
+import java.io.FileReader;
+import java.io.Writer;
+import java.io.FileWriter;
+
public class OnapCommandDiscoveryUtils {
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
/**
* Fetch a particular schema details.
@@ -90,10 +96,10 @@ public class OnapCommandDiscoveryUtils {
throw new OnapCommandNotFound(cmd, version);
return schemaInfo;
- }
+ }
/**
- * Load the previous discovered json file.
+ * Load the previous discovered json file.
*
* @return list
* @throws OnapCommandInvalidSchema
@@ -148,12 +154,11 @@ public class OnapCommandDiscoveryUtils {
if (!OnapCommandDiscoveryUtils.isAlreadyDiscovered()) return schemas;
String dataDir = OnapCommandDiscoveryUtils.getDataStorePath();
- try {
- File file = new File(dataDir + File.separator + DISCOVERY_FILE);
- ObjectMapper mapper = new ObjectMapper();
- OnapCommandSchemaInfo[] list = mapper.readValue(file, OnapCommandSchemaInfo[].class);
+ File file = new File(dataDir + File.separator + DISCOVERY_FILE);
+ try (JsonReader jsonReader = new JsonReader(new FileReader(file))){
+ OnapCommandSchemaInfo[] list = gson.fromJson(jsonReader, OnapCommandSchemaInfo[].class);
schemas.addAll(Arrays.asList(list));
- } catch (IOException e) {
+ } catch (Exception e) { // NOSONAR
throw new OnapCommandDiscoveryFailed(dataDir,
DISCOVERY_FILE, e);
}
@@ -189,10 +194,10 @@ public class OnapCommandDiscoveryUtils {
FileUtils.forceMkdir(new File(dataDir));
File file = new File(dataDir + File.separator + DISCOVERY_FILE);
- ObjectMapper mapper = new ObjectMapper();
- mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
- mapper.writerWithDefaultPrettyPrinter().writeValue(file, schemas);
- } catch (IOException e1) {
+ try(Writer writer = new FileWriter(file)){
+ gson.toJson(schemas,writer);
+ }
+ } catch (Exception e1) { // NOSONAR
throw new OnapCommandDiscoveryFailed(dataDir,
DISCOVERY_FILE, e1);
}
@@ -552,7 +557,7 @@ public class OnapCommandDiscoveryUtils {
* @throws OnapCommandInvalidSchema
* exception
*/
- public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema {
+ public static Map<String, ?> loadYaml(String filePath) throws OnapCommandInvalidSchema {
Map<String, ?> values = null;
try {
values = (Map<String, Object>) new Yaml().load(FileUtils.readFileToString(new File(filePath)));
diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
index 96f864e0..043ec8ed 100644
--- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
+++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
@@ -46,10 +46,11 @@ import org.onap.cli.fw.input.OnapCommandParameterType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.JsonPath;
+
/**
* Provides helper method to parse Yaml files and produce required objects.
*
@@ -57,6 +58,8 @@ import com.jayway.jsonpath.JsonPath;
public class OnapCommandUtils {
static Logger log = LoggerFactory.getLogger(OnapCommandUtils.class);
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
+
/**
* Private constructor.
*/
@@ -336,13 +339,13 @@ public class OnapCommandUtils {
currentIdx = idxE + 2;
} else if (OnapCommandParameterType.MAP.equals(param.getParameterType())) {
try {
- String value = new ObjectMapper().writeValueAsString(params.get(paramName).getValue());
+ String value = gson.toJson(params.get(paramName).getValue());
if ((idxS == 0) && (currentIdx == 0)) {
result = value;
} else {
result += line.substring(currentIdx, idxS - 1) + value;
}
- } catch (JsonProcessingException e) { // NOSONAR
+ } catch (Exception e) { // NOSONAR
//never occur as map is coverted to json string here
}