summaryrefslogtreecommitdiffstats
path: root/profiles/http
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 /profiles/http
parent4f1d279e42a3326cdc97e1eacaf0fdb58ee944e7 (diff)
Migrate to gson
Issue-ID: CLI-247 Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com> Change-Id: If66134db5503382fb45210ece883402fc130ac93
Diffstat (limited to 'profiles/http')
-rw-r--r--profiles/http/pom.xml11
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java9
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java17
-rw-r--r--profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java32
4 files changed, 41 insertions, 28 deletions
diff --git a/profiles/http/pom.xml b/profiles/http/pom.xml
index fe2cee86..058749d7 100644
--- a/profiles/http/pom.xml
+++ b/profiles/http/pom.xml
@@ -77,9 +77,9 @@ Excluded commons-codec vulnerable version and added invulnerable version
<version>2.2.0</version>
</dependency>
<dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.4</version>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.onap.cli</groupId>
@@ -110,6 +110,11 @@ Excluded commons-codec vulnerable version and added invulnerable version
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
</exclusion>
+ <!--JIRA: CLI-253-->
+ <exclusion>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java
index f0fe43cd..25cf14a3 100644
--- a/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/mock/MocoServer.java
@@ -32,8 +32,8 @@ import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.Yaml;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.github.dreamhead.moco.HttpServer;
import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.ResponseHandler;
@@ -43,6 +43,7 @@ public class MocoServer {
private Runner runner;
private Map<String, Object> mocoServerConfigs = new HashMap();
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
public MocoServer(String mockFile) throws OnapCommandException {
Resource resource = null;
@@ -71,8 +72,8 @@ public class MocoServer {
if(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON) != null) {
try {
mocoServerConfigs.put(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON,
- new ObjectMapper().writeValueAsString(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)));
- } catch (JsonProcessingException e) {
+ gson.toJson(response.get(OnapCommandHttpConstants.VERIFY_RESPONSE_JSON)));
+ } catch (Exception e) { // NOSONAR
throw new OnapCommandException("Invalid mocking file" + mockFile, e);
}
}
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java
index aaca5c87..c07a6f7f 100644
--- a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java
@@ -16,7 +16,6 @@
package org.onap.cli.fw.http.schema;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -42,20 +41,20 @@ import org.onap.cli.fw.registrar.OnapCommandRegistrar;
import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
import org.onap.cli.fw.utils.OnapCommandUtils;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import net.minidev.json.JSONObject;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
public class OnapCommandSchemaHttpLoader {
private static final String ATTRIBUTE = "Attribute '";
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
private OnapCommandSchemaHttpLoader() {
// to follow standards !
}
- public static List<String> loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault,
- boolean validateSchema) throws OnapCommandException {
+ public static List<String> loadHttpSchema(OnapHttpCommand cmd, String schemaName, boolean includeDefault, boolean validateSchema) throws OnapCommandException {
try {
List<String> errors = new ArrayList<>();
if (includeDefault) {
@@ -70,7 +69,7 @@ public class OnapCommandSchemaHttpLoader {
}
Map<String, List<Map<String, String>>> commandYamlMap =
- (Map<String, List<Map<String, String>>>)OnapCommandSchemaLoader.validateSchemaVersion(schemaName, cmd.getSchemaVersion());
+ (Map<String, List<Map<String, String>>>) OnapCommandSchemaLoader.validateSchemaVersion(schemaName, cmd.getSchemaVersion());
errors.addAll(parseHttpSchema(cmd, commandYamlMap, validateSchema));
@@ -417,8 +416,8 @@ public class OnapCommandSchemaHttpLoader {
errorList.add(OnapCommandHttpConstants.HTTP_BODY_JSON_EMPTY);
} else {
try {
- new ObjectMapper().readValue(body, JSONObject.class);
- } catch (IOException e1) { // NOSONAR
+ gson.fromJson(body, JsonElement.class);
+ } catch (Exception e1) { // NOSONAR
errorList.add(OnapCommandHttpConstants.HTTP_BODY_FAILED_PARSING);
}
}
diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
index d1f88df4..0eb06c96 100644
--- a/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
+++ b/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
@@ -40,8 +40,9 @@ import org.onap.cli.fw.utils.OnapCommandUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
@@ -50,6 +51,7 @@ import net.minidev.json.JSONArray;
public class OnapCommandHttpUtils {
static Logger LOG = LoggerFactory.getLogger(OnapCommandHttpUtils.class);
+ private static Gson gson = new GsonBuilder().serializeNulls().create();
/**
* Set argument to param value.
@@ -277,26 +279,32 @@ public class OnapCommandHttpUtils {
}
}
- public static void normalizeJson(JsonNode node) {
- Iterator<JsonNode> it = node.iterator();
+ public static void normalizeJson(JsonElement node) {
+ Iterator<Entry<String, JsonElement>> it = node.getAsJsonObject().entrySet().iterator();
+
while (it.hasNext()) {
- JsonNode child = it.next();
- if (child.isTextual() && child.asText().equals(""))
+ JsonElement child = it.next().getValue();
+ if (child.isJsonPrimitive() && child.getAsJsonPrimitive().isString() && child.getAsJsonPrimitive().getAsString().equals(""))
it.remove();
- else if (child.isNull())
+ else if (child.isJsonNull())
it.remove();
- else
+ else if (child.isJsonObject())
normalizeJson(child);
+ else if (child.isJsonArray()) {
+ for (JsonElement ele:child.getAsJsonArray()) {
+ if (ele.isJsonObject())
+ normalizeJson(ele);
+ }
+ }
}
}
public static String normalizeJson(String json) throws OnapCommandHttpInvalidRequestBody {
- ObjectMapper mapper = new ObjectMapper();
- JsonNode node;
+ JsonElement node;
try {
- node = mapper.readTree(json);
+ node = gson.fromJson(json,JsonElement.class);
normalizeJson(node);
- return mapper.writeValueAsString(node);
+ return gson.toJson(node);
} catch (Exception e) { //NOSONAR
throw new OnapCommandHttpInvalidRequestBody(e);
}