diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/onap/aai/datarouter/util/RouterServiceUtil.java | 118 |
1 files changed, 64 insertions, 54 deletions
diff --git a/src/main/java/org/onap/aai/datarouter/util/RouterServiceUtil.java b/src/main/java/org/onap/aai/datarouter/util/RouterServiceUtil.java index d24a153..f2f5801 100644 --- a/src/main/java/org/onap/aai/datarouter/util/RouterServiceUtil.java +++ b/src/main/java/org/onap/aai/datarouter/util/RouterServiceUtil.java @@ -29,7 +29,6 @@ import org.json.JSONObject; import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.restclient.client.Headers; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -138,24 +137,8 @@ public class RouterServiceUtil { public static String recursivelyLookupJsonPayload(JsonNode node, String key) { String value = null; - if (node.isObject()) { - Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields(); - - while (nodeIterator.hasNext()) { - Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next(); - if (!entry.getValue().isValueNode()) { - value = recursivelyLookupJsonPayload(entry.getValue(), key); - if (value != null) { - return value; - } - } - String name = entry.getKey(); - if (name.equalsIgnoreCase(key)) { - return entry.getValue().asText(); - } - } - } else if (node.isArray()) { + if (node.isArray()) { Iterator<JsonNode> arrayItemsIterator = node.elements(); while (arrayItemsIterator.hasNext()) { value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key); @@ -163,43 +146,74 @@ public class RouterServiceUtil { return value; } } + + return value; + } + + if (!node.isObject()) { + return value; + } + + Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields(); + + while (nodeIterator.hasNext()) { + Map.Entry<String, JsonNode> entry = nodeIterator.next(); + if (!entry.getValue().isValueNode()) { + value = recursivelyLookupJsonPayload(entry.getValue(), key); + if (value != null) { + return value; + } + } + + String name = entry.getKey(); + if (name.equalsIgnoreCase(key)) { + return entry.getValue().asText(); + } } + return value; } public static void extractObjectsByKey(JsonNode node, String searchKey, Collection<JsonNode> foundObjects) { - if (node.isObject()) { - Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields(); + if (node.isArray()) { + Iterator<JsonNode> arrayItemsIterator = node.elements(); + while (arrayItemsIterator.hasNext()) { + extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects); + } - while (nodeIterator.hasNext()) { - Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next(); - if (!entry.getValue().isValueNode()) { - extractObjectsByKey(entry.getValue(), searchKey, foundObjects); - } + return; + } + + if (!node.isObject()) { + return; + } - String name = entry.getKey(); - if (name.equalsIgnoreCase(searchKey)) { + Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields(); - JsonNode entryValue = entry.getValue(); + while (nodeIterator.hasNext()) { + Map.Entry<String, JsonNode> entry = nodeIterator.next(); + if (!entry.getValue().isValueNode()) { + extractObjectsByKey(entry.getValue(), searchKey, foundObjects); + } - if (entryValue.isArray()) { + String name = entry.getKey(); + if (!name.equalsIgnoreCase(searchKey)) { + continue; + } - Iterator<JsonNode> arrayItemsIterator = entryValue.elements(); - while (arrayItemsIterator.hasNext()) { - foundObjects.add(arrayItemsIterator.next()); - } + JsonNode entryValue = entry.getValue(); - } else { - foundObjects.add(entry.getValue()); - } + if (entryValue.isArray()) { + + Iterator<JsonNode> arrayItemsIterator = entryValue.elements(); + while (arrayItemsIterator.hasNext()) { + foundObjects.add(arrayItemsIterator.next()); } - } - } else if (node.isArray()) { - Iterator<JsonNode> arrayItemsIterator = node.elements(); - while (arrayItemsIterator.hasNext()) { - extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects); + + } else { + foundObjects.add(entry.getValue()); } } } @@ -219,29 +233,25 @@ public class RouterServiceUtil { public static void extractFieldValuesFromObject(JsonNode node, Collection<String> attributesToExtract, Collection<String> fieldValues) { - if (node.isObject()) { - - JsonNode valueNode; + if (node==null || !node.isObject()) { + return; + } - for (String attrToExtract : attributesToExtract) { + JsonNode valueNode; - valueNode = node.get(attrToExtract); + for (String attrToExtract : attributesToExtract) { - if (valueNode != null) { + valueNode = node.get(attrToExtract); - if (valueNode.isValueNode()) { - fieldValues.add(valueNode.asText()); - } - } + if (valueNode != null && valueNode.isValueNode()) { + fieldValues.add(valueNode.asText()); } } } - public static String objToJson(Object obj) { JSONObject jsonObject = new JSONObject(obj); - String json = jsonObject.toString(); - return json; + return jsonObject.toString(); } /** |