diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2020-07-18 17:47:37 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2020-07-18 17:54:40 +0800 |
commit | ce0b806a18d8f370e1245c480836d18a04afa243 (patch) | |
tree | 99f670a4e9ef8681e432b5c0a098f5bbc2a72449 /holmes-actions/src/main/java/org/onap | |
parent | 490fc3c1fafe50d5fb0e23db5cfd10730be96866 (diff) |
Added Some Tools to GsonUtil
Change-Id: I66cd546e6cb9af5313e235cde93a7e914ff00b1f
Issue-ID: HOLMES-331
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/main/java/org/onap')
-rw-r--r-- | holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java | 34 | ||||
-rw-r--r-- | holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java | 80 |
2 files changed, 68 insertions, 46 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java index c1eede7..377debb 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java @@ -30,6 +30,8 @@ import java.util.Map.Entry; import java.util.Set;
import java.util.stream.Stream;
+import static org.onap.holmes.common.utils.GsonUtil.getAsString;
+
public class DcaeConfigurationParser {
private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";
@@ -85,15 +87,9 @@ public class DcaeConfigurationParser { private static SecurityInfo createSecurityInfo(String key, JsonObject entity) {
SecurityInfo securityInfo = new SecurityInfo();
- if (entity.has("type") && !entity.get("type").isJsonNull()) {
- securityInfo.setType(entity.get("type").getAsString());
- }
- if (entity.has("aaf_password") && !entity.get("aaf_password").isJsonNull()) {
- securityInfo.setAafPassword(entity.get("aaf_password").getAsString());
- }
- if (entity.has("aaf_username") && !entity.get("aaf_username").isJsonNull()) {
- securityInfo.setAafUsername(entity.get("aaf_username").getAsString());
- }
+ securityInfo.setType(getAsString(entity, "type"));
+ securityInfo.setAafPassword(getAsString(entity, "aaf_password"));
+ securityInfo.setAafUsername(getAsString(entity, "aaf_username"));
securityInfo.setSecureTopic(!key.endsWith("unsecure"));
fillInDmaapInfo(securityInfo, entity.get("dmaap_info").getAsJsonObject());
return securityInfo;
@@ -101,21 +97,11 @@ public class DcaeConfigurationParser { private static void fillInDmaapInfo(SecurityInfo securityInfo, JsonObject jsonDmaapInfo) {
SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();
- if (jsonDmaapInfo.has("location") && !jsonDmaapInfo.get("location").isJsonNull()){
- dmaapInfo.setLocation(jsonDmaapInfo.get("location").getAsString());
- }
- if (jsonDmaapInfo.has("topic_url") && !jsonDmaapInfo.get("topic_url").isJsonNull()) {
- dmaapInfo.setTopicUrl(jsonDmaapInfo.get("topic_url").getAsString());
- }
- if (jsonDmaapInfo.has("client_id") && !jsonDmaapInfo.get("client_id").isJsonNull()) {
- dmaapInfo.setClientId(jsonDmaapInfo.get("client_id").getAsString());
- }
- if (jsonDmaapInfo.has("client_role") && !jsonDmaapInfo.get("client_role").isJsonNull()) {
- dmaapInfo.setClientRole(jsonDmaapInfo.get("client_role").getAsString());
- }
- if (jsonDmaapInfo.has("type") && !jsonDmaapInfo.get("type").isJsonNull()) {
- dmaapInfo.setType(jsonDmaapInfo.get("type").getAsString());
- }
+ dmaapInfo.setLocation(getAsString(jsonDmaapInfo, "location"));
+ dmaapInfo.setTopicUrl(getAsString(jsonDmaapInfo, "topic_url"));
+ dmaapInfo.setClientId(getAsString(jsonDmaapInfo, "client_id"));
+ dmaapInfo.setClientRole(getAsString(jsonDmaapInfo, "client_role"));
+ dmaapInfo.setType(getAsString(jsonDmaapInfo, "type"));
}
private static void fillInRules(DcaeConfigurations ret, JsonObject jsonObject) {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java index 452aaf8..a5e6461 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/GsonUtil.java @@ -1,5 +1,5 @@ /** - * Copyright 2018 ZTE Corporation. + * Copyright 2018-2020 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,28 +17,25 @@ package org.onap.holmes.common.utils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; +import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; -import java.lang.reflect.Type; +import org.apache.commons.lang3.StringUtils; + import java.util.List; import java.util.Map; public class GsonUtil { private static Gson gson = null; + static { if (gson == null) { gson = new GsonBuilder() - .registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() { - @Override - public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - try { - return json.getAsInt(); - } catch (NumberFormatException e) { - return 0; - } + .registerTypeAdapter(Integer.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> { + try { + return json.getAsInt(); + } catch (NumberFormatException e) { + return 0; } }) .create(); @@ -65,31 +62,70 @@ public class GsonUtil { return t; } - public static <T> List<T> GsonToList(String gsonString, Class<T> cls) { + public static <T> List<T> jsonToList(String gsonString, Class<T> cls) { List<T> list = null; if (gson != null) { - list = gson.fromJson(gsonString, new TypeToken<List<T>>() { - }.getType()); + list = gson.fromJson(gsonString, TypeToken.getParameterized(List.class, cls).getType()); } return list; } - public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) { + public static <T> List<Map<String, T>> jsonToListMaps(String gsonString, Class<T> cls) { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(gsonString, - new TypeToken<List<Map<String, T>>>() { - }.getType()); + TypeToken.getParameterized(List.class, + TypeToken.getParameterized(Map.class, String.class, cls).getType() + ).getType() + ); } return list; } - public static <T> Map<String, T> GsonToMaps(String gsonString) { + public static <T> Map<String, T> jsonToMap(String gsonString, Class<T> cls) { Map<String, T> map = null; if (gson != null) { - map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { - }.getType()); + map = gson.fromJson(gsonString, TypeToken.getParameterized(Map.class, String.class, cls).getType()); } return map; } + + public static String getAsString(JsonObject o, String field) { + String ret = null; + + if (field == null) { + field = StringUtils.EMPTY; + } + + if (o.has(field) && !o.get(field).isJsonNull()) { + ret = o.get(field).getAsString(); + } + return ret; + } + + public static long getAsLong(JsonObject o, String field) { + long ret = 0; + + if (field == null) { + field = StringUtils.EMPTY; + } + + if (o.has(field) && !o.get(field).isJsonNull()) { + ret = o.get(field).getAsLong(); + } + return ret; + } + + public static int getAsInt(JsonObject o, String field) { + int ret = 0; + + if (field == null) { + field = StringUtils.EMPTY; + } + + if (o.has(field) && !o.get(field).isJsonNull()) { + ret = o.get(field).getAsInt(); + } + return ret; + } } |