diff options
author | YuanHu <yuan.hu1@zte.com.cn> | 2016-10-12 14:57:20 +0800 |
---|---|---|
committer | huangjian <huang.jian12@zte.com.cn> | 2016-10-13 14:13:12 +0800 |
commit | ad79f406675cd448eb3c661dd771d3c594824327 (patch) | |
tree | e9882e2c215a7329a5e4dad8dbca4c542bbdd26b /catalog-core/catalog-mgr | |
parent | c3f0d4c37d759e86f45d1f77a5a17ea71a711ea5 (diff) |
Protected of NullPointerException while convert json string to java Object.
When call zte parser api.
Issue-id: TOSCA-108
Change-Id: I430a018f787b8c413b625dbd4a07b79f87ab0ef7
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
Diffstat (limited to 'catalog-core/catalog-mgr')
2 files changed, 29 insertions, 13 deletions
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ToolUtil.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ToolUtil.java index dae083a0..576cade3 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ToolUtil.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ToolUtil.java @@ -16,6 +16,7 @@ package org.openo.commontosca.catalog.common; import com.google.gson.Gson; +import com.google.gson.JsonElement; import org.openo.commontosca.catalog.model.common.EnumToscaNodeTypeDefinition; import org.slf4j.Logger; @@ -387,4 +388,16 @@ public class ToolUtil { Gson gson = new Gson(); return gson.toJson(template); } + + /** + * @param value + * @return + */ + public static String getAsString(JsonElement value) { + if (value.isJsonPrimitive()) { + return value.getAsString(); + } + + return value.toString(); + } } diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java index 62692b24..1089dc3d 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java @@ -15,6 +15,8 @@ */ package org.openo.commontosca.catalog.model.parser.yaml.zte.entity; +import org.openo.commontosca.catalog.common.ToolUtil; + import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -28,7 +30,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - public class ParseYamlResult { private String toscaDefinitionsVersion; private String description; @@ -132,6 +133,9 @@ public class ParseYamlResult { } public Map<String, String> getMetadata() { + if (this.metadata == null) { + return new HashMap<>(); + } return metadata; } @@ -333,7 +337,7 @@ public class ParseYamlResult { Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, JsonElement> next = iterator.next(); - ret.put(next.getKey(), next.getValue().getAsString()); + ret.put(next.getKey(), ToolUtil.getAsString(next.getValue())); } return ret; } @@ -380,12 +384,12 @@ public class ParseYamlResult { } NodeTemplateScalable scalable = new NodeTemplateScalable(); - scalable - .setMin_instances(propertyJson.getAsJsonObject().get("min_instances").getAsString()); - scalable - .setMax_instances(propertyJson.getAsJsonObject().get("max_instances").getAsString()); + scalable.setMin_instances( + ToolUtil.getAsString(propertyJson.getAsJsonObject().get("min_instances"))); + scalable.setMax_instances( + ToolUtil.getAsString(propertyJson.getAsJsonObject().get("max_instances"))); scalable.setDefault_instances( - propertyJson.getAsJsonObject().get("default_instances").getAsString()); + ToolUtil.getAsString(propertyJson.getAsJsonObject().get("default_instances"))); return scalable; } @@ -483,7 +487,7 @@ public class ParseYamlResult { while (iterator.hasNext()) { Entry<String, JsonElement> next = iterator.next(); if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { - ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); + ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())}); continue; } @@ -499,7 +503,7 @@ public class ParseYamlResult { private String[] parseListValue(JsonArray jsonArray) { String[] value = new String[jsonArray.size()]; for (int i = 0, size = jsonArray.size(); i < size; i++) { - value[i] = jsonArray.get(i).getAsString(); + value[i] = ToolUtil.getAsString(jsonArray.get(i)); } return value; } @@ -522,9 +526,8 @@ public class ParseYamlResult { Iterator<Entry<String, JsonElement>> iterator = capabilities.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, JsonElement> next = iterator.next(); - if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { - ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); + ret.put(next.getKey(), new String[] {ToolUtil.getAsString(next.getValue())}); continue; } @@ -554,7 +557,7 @@ public class ParseYamlResult { Iterator<Entry<String, JsonElement>> iterator = properties.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, JsonElement> next = iterator.next(); - ret.put(next.getKey(), next.getValue().getAsString()); + ret.put(next.getKey(), ToolUtil.getAsString(next.getValue())); } return ret; } @@ -686,7 +689,7 @@ public class ParseYamlResult { return ""; } - return defaultValue.getAsString(); + return ToolUtil.getAsString(defaultValue); } public JsonObject getValue() { |