From 04fdbd0e18a891241ed068044093a257ff506400 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Mon, 28 May 2018 14:20:51 +0200 Subject: Fix for Json parser Change-Id: I322b893356a41ee27dfb14d9ef9dfcdd129e8ced Issue-ID: DCAEGEN2-451 Signed-off-by: pwielebs --- .../services/prh/service/DmaapConsumerJsonParser.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'prh-app-server/src/main/java/org') diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java index 0ef7e4fe..04956f8d 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java @@ -26,6 +26,8 @@ import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; +import java.util.stream.StreamSupport; + /** * @author Przemysław Wąsala on 5/8/18 */ @@ -42,9 +44,16 @@ public class DmaapConsumerJsonParser { private DmaapConsumerJsonParser() { } + public static ConsumerDmaapModel getJsonObject(String message) throws DmaapNotFoundException { JsonElement jsonElement = new JsonParser().parse(message); - JsonObject jsonObject = jsonElement.getAsJsonObject(); + JsonObject jsonObject; + + jsonObject = jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : + StreamSupport.stream(jsonElement.getAsJsonArray().spliterator(), false).findFirst() + .orElseThrow(() -> new DmaapNotFoundException("Json object not found in json array")) + .getAsJsonObject(); + return create(jsonObject); } @@ -64,14 +73,11 @@ public class DmaapConsumerJsonParser { } throw new DmaapNotFoundException("IPV4 and IPV6 are empty"); } - throw new DmaapNotFoundException("Incorrect JsonObject"); + throw new DmaapNotFoundException("Incorrect JsonObject - missing header"); } private static String getValueFromJson(JsonObject jsonObject, String jsonKey) { - if (jsonObject.has(jsonKey)) { - return jsonObject.get(jsonKey).getAsString(); - } - return ""; + return jsonObject.has(jsonKey) ? jsonObject.get(jsonKey).getAsString() : ""; } private static boolean isVendorAndSerialNotEmpty(String pnfSerialNumber, String pnfVendorName) { -- cgit 1.2.3-korg