diff options
author | Prudence Au <prudence.au@amdocs.com> | 2018-08-16 21:17:16 -0400 |
---|---|---|
committer | Prudence Au <prudence.au@amdocs.com> | 2018-08-16 21:50:48 -0400 |
commit | 8d9044a5e2232776243f641cab1a8b90f26ff9ff (patch) | |
tree | 13ce6be1e3810d0459862c82a0af4cfb9750edf0 /src/main/java/org/onap/pomba/contextbuilder/aai/reader | |
parent | 3bc6eb063c86b8269e0d70f04510783e8d1fe474 (diff) |
Initial seed code submission
Change-Id: I66baf23256c39475f5e0031e387be2e6b872bf7b
Issue-ID: LOG-521
Signed-off-by: Prudence Au <prudence.au@amdocs.com>
Diffstat (limited to 'src/main/java/org/onap/pomba/contextbuilder/aai/reader')
-rw-r--r-- | src/main/java/org/onap/pomba/contextbuilder/aai/reader/JsonReader.java | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/main/java/org/onap/pomba/contextbuilder/aai/reader/JsonReader.java b/src/main/java/org/onap/pomba/contextbuilder/aai/reader/JsonReader.java new file mode 100644 index 0000000..5866705 --- /dev/null +++ b/src/main/java/org/onap/pomba/contextbuilder/aai/reader/JsonReader.java @@ -0,0 +1,186 @@ +/* + * ============LICENSE_START=================================================== + * Copyright (c) 2018 Amdocs + * ============================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================================== + */ +package org.onap.pomba.contextbuilder.aai.reader; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; +import com.jayway.jsonpath.ReadContext; +import com.jayway.jsonpath.spi.json.GsonJsonProvider; +import com.jayway.jsonpath.spi.json.JsonProvider; +import com.jayway.jsonpath.spi.mapper.GsonMappingProvider; +import com.jayway.jsonpath.spi.mapper.MappingProvider; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Set; +import org.onap.pomba.contextbuilder.aai.exception.AuditError; +import org.onap.pomba.contextbuilder.aai.exception.AuditException; + +/** + * Reads JSON objects. Supported by the JayWay JsonPath library. + */ +public class JsonReader { + + private Configuration jsonPathConfig; + + /** + * Initialize the JSON reader. + */ + public JsonReader() { + setJsonProvider(); + this.jsonPathConfig = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); + } + + /** + * Parse the JSON. + * + * @param json + * the JSON object + * @return a {@link ReadContext} the parsed JSON. + * @throws ValidationServiceException + */ + public DocumentContext parse(String json) throws AuditException { + DocumentContext document = null; + try { + document = JsonPath.using(jsonPathConfig).parse(json); + } catch (Exception e) { + throw new AuditException(AuditError.JSON_READER_PARSE_ERROR, e); + } + return document; + } + + /** + * Gets values from JSON objects. + * + * @param json + * the JSON object + * @param path + * the path to property values. The format must comply with the JayWay JsonPath definition. + * @return a List of values found by evaluating the path. + * @throws ValidationServiceException + */ + public List<String> get(String json, String path) throws AuditException { + return getAsList(parse(json), path); + } + + /** + * Gets values from JSON objects. Used in combination with {@link JsonReader#parse(String)} it reduces the number of + * times the JSON document is parsed. + * + * @param document + * a {@link DocumentContext} object with the parsed JSON + * @param path + * the path to property values. The format must comply with the JayWay JsonPath definition. + * @return a List of values found by evaluating the path, or an empty list if no values were found + */ + public List<String> getAsList(DocumentContext document, String path) { + List<String> result = new ArrayList<>(); + JsonElement jsonElement = document.read(path); + if (jsonElement != null) { + if (jsonElement.isJsonPrimitive()) { + result.add(jsonElement.getAsString()); + } else if (jsonElement.isJsonObject()) { + result.add(jsonElement.getAsJsonObject().toString()); + } else if (jsonElement.isJsonArray()) { + for (JsonElement obj : jsonElement.getAsJsonArray()) { + Object object = jsonElementToObject(obj); + result.add(object == null ? null : object.toString()); + } + } + } + return result; + } + + /** + * Get the value(s) from the specified JSON document + * + * @param document + * a {@link DocumentContext} object with the parsed JSON + * @param path + * the path to property value(s). The format must comply with the JayWay JsonPath definition. + * @return either all the values found by evaluating the path (e.g. as an array), or a String object (only) where + * the path evaluates to a single primitive value + */ + public Object getObject(DocumentContext document, String path) { + return jsonElementToObject(document.read(path)); + } + + /** + * Convert the JSON element to a String or Array where possible, otherwise return the JSON object. + * + * @param jsonElement + * @return the jsonElement converted to a Java Object + */ + private Object jsonElementToObject(JsonElement jsonElement) { + if (jsonElement == null) { + return null; + } else if (jsonElement.isJsonPrimitive()) { + return jsonElement.getAsString(); + } else if (jsonElement.isJsonObject()) { + return jsonElement.getAsJsonObject(); + } else if (jsonElement.isJsonArray()) { + // Convert to a List for simplified handling within rules + return jsonArrayToList(jsonElement.getAsJsonArray()); + } else { + return jsonElement; + } + } + + private List<Object> jsonArrayToList(JsonArray jsonArray) { + List<Object> result = new ArrayList<>(); + for (JsonElement obj : jsonArray) { + result.add(jsonElementToObject(obj)); + } + return result; + } + + /** + * @param document + * @param path + * @return a JsonElement from the document + */ + public JsonElement getJsonElement(DocumentContext document, String path) { + return document.read(path); + } + + private void setJsonProvider() { + Configuration.setDefaults(new Configuration.Defaults() { + private final JsonProvider jsonProvider = new GsonJsonProvider(); + private final MappingProvider mappingProvider = new GsonMappingProvider(); + + @Override + public JsonProvider jsonProvider() { + return jsonProvider; + } + + @Override + public MappingProvider mappingProvider() { + return mappingProvider; + } + + @Override + public Set<Option> options() { + return EnumSet.noneOf(Option.class); + } + }); + } +} |