diff options
Diffstat (limited to 'prh-app-server/src/main')
-rw-r--r-- | prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapConsumerJsonParser.java | 18 |
1 files changed, 12 insertions, 6 deletions
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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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) { |