\r
package org.openecomp.mso.bpmn.core.json;\r
\r
+import java.io.IOException;\r
import java.util.ArrayList;\r
+import java.util.HashMap;\r
import java.util.Iterator;\r
import java.util.List;\r
import java.util.Map;\r
-import java.util.HashMap;\r
import java.util.StringTokenizer;\r
\r
+import org.camunda.bpm.engine.delegate.DelegateExecution;\r
import org.camunda.bpm.engine.runtime.Execution;\r
import org.json.JSONArray;\r
import org.json.JSONException;\r
import org.json.JSONObject;\r
import org.json.XML;\r
-\r
+import org.openecomp.mso.apihandler.common.ValidationException;\r
//import org.openecomp.mso.bpmn.core.BPMNLogger;\r
import org.openecomp.mso.bpmn.core.xml.XmlTool;\r
import org.openecomp.mso.logger.MsoLogger;\r
\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.github.fge.jackson.JsonLoader;\r
+import com.github.fge.jsonschema.core.exceptions.ProcessingException;\r
+import com.github.fge.jsonschema.core.report.ProcessingReport;\r
+import com.github.fge.jsonschema.main.JsonSchemaFactory;\r
+import com.github.fge.jsonschema.main.JsonValidator;\r
+\r
/**\r
* Utility class for JSON processing\r
* \r
* @version 1.0\r
+ * \r
+ * Note: It was observed, that depending on the JSON implementation, an org.json.JSONException or a\r
+ * java.util.NoSuchElementException will be thrown in the event of the key value being "not found"\r
+ * in a JSON document. A general check has been added to the applicable catch blocks for this\r
+ * this type of behavior to reduce the amount of logging. As a key value not being found is\r
+ * expect behavior, it makes no sense to log the stack trace associated with this type of failure.\r
*/\r
\r
public class JsonUtils {\r
* to convert a JSONObject to an XML Doc. The intent of this is to\r
* correctly generate XML from JSON including TAGs for JSONArrays\r
*\r
- * @param jsonObj org.json.JSON object to be converted to XML\r
+ * @param obj org.json.JSON object to be converted to XML\r
* @param tagName optional XML tagname supplied primarily during recursive calls\r
* @return String containing the XML translation\r
*/\r
return null;\r
} else {\r
if (rawValue instanceof String) {\r
- msoLogger.debug("getJsonValue(): the raw value is a String Object=" + ((String) rawValue));\r
+ msoLogger.debug("getJsonValue(): the raw value is a String Object=" + rawValue);\r
return (String) rawValue;\r
} else {\r
msoLogger.debug("getJsonValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
return null;\r
} else {\r
if (rawValue instanceof String) {\r
- msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + ((String) rawValue));\r
+ msoLogger.debug("getJsonNodeValue(): the raw value is a String Object=" + rawValue);\r
return (String) rawValue;\r
} else {\r
msoLogger.debug("getJsonNodeValue(): the raw value is NOT a String Object=" + rawValue.toString());\r
return 0;\r
} else {\r
if (rawValue instanceof Integer) {\r
- msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + ((String) rawValue).toString());\r
+ msoLogger.debug("getJsonValue(): the raw value is an Integer Object=" + rawValue);\r
return (Integer) rawValue;\r
} else {\r
- msoLogger.debug("getJsonValue(): the raw value is NOT an Integer Object=" + rawValue.toString());\r
+ msoLogger.debug("getJsonIntValue(): the raw value is NOT an Integer Object=" + rawValue.toString());\r
return 0;\r
}\r
}\r
} catch (Exception e) {\r
- msoLogger.debug("getJsonValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(), e);\r
+ msoLogger.debug("getJsonIntValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(), e);\r
}\r
return 0;\r
}\r
return false;\r
} else {\r
if (rawValue instanceof Boolean) {\r
- msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + ((String) rawValue));\r
+ msoLogger.debug("getJsonBooleanValue(): the raw value is a Boolean Object=" + rawValue);\r
return (Boolean) rawValue;\r
} else {\r
- msoLogger.debug("getJsonValue(): the raw value is NOT an Boolean Object=" + rawValue.toString());\r
+ msoLogger.debug("getJsonBooleanValue(): the raw value is NOT an Boolean Object=" + rawValue.toString());\r
return false;\r
}\r
}\r
} catch (Exception e) {\r
- msoLogger.debug("getJsonValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(),e);\r
+ msoLogger.debug("getJsonBooleanValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(),e);\r
}\r
return false;\r
}\r
return null;\r
} else {\r
if (rawValue instanceof JSONArray) {\r
- msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + ((JSONArray) rawValue).toString());\r
+ msoLogger.debug("getJsonParamValue(): keys=" + keys + " points to JSONArray: " + rawValue.toString());\r
int arrayLen = ((JSONArray) rawValue).length();\r
if (index < 0 || arrayLen < index+1) {\r
msoLogger.debug("getJsonParamValue(): index: " + index + " is out of bounds for array size of " + arrayLen);\r
for (int i = 0; i < arrayLen; i++) {\r
msoLogger.debug("getJsonParamValue(): index: " + i + ", value: " + ((JSONArray) rawValue).get(i).toString());\r
if (((JSONArray) rawValue).get(i) instanceof JSONObject) {\r
- msoLogger.debug("getJsonParamValue(): index: " + i + " is a JSONObject");\r
+// msoLogger.debug("getJsonParamValue(): index: " + i + " is a JSONObject");\r
JSONObject jsonObj = (JSONObject)((JSONArray) rawValue).get(i);\r
String parmValue = jsonObj.get(name).toString();\r
if (parmValue != null) {\r
return null;\r
}\r
}\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("getJsonParamValue(): caught JSONException attempting to retrieve param value for keys:" + keys + ", name=" + name, je);\r
} catch (Exception e) {\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("getJsonParamValue(): failed to retrieve param value for keys:" + keys + ", name=" + name + ": " + e.getMessage()); \r
+ } else {\r
msoLogger.debug("getJsonParamValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(), e);\r
+ }\r
}\r
return null;\r
}\r
String keyValue = null;\r
try {\r
if (jsonObj.has(key)) {\r
- msoLogger.debug("getJsonValueForKey(): found value for key=" + key);\r
Object value = jsonObj.get(key);\r
- if (value == null)\r
+ msoLogger.debug("getJsonValueForKey(): found value=" + (String) value + ", for key=" + key);\r
+ if (value == null) {\r
return null;\r
- else\r
+ } else {\r
return ((String) value);\r
+ }\r
} else {\r
- msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
+// msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
- msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
+// msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call on: " +\r
+// ((JSONObject) obj).toString(MSOJsonIndentFactor));\r
keyValue = getJsonValueForKey((JSONObject) obj, key);\r
if (keyValue != null) {\r
- msoLogger.debug("getJsonValueForKey(): found value=" + keyValue + ", for key=" + key);\r
+// msoLogger.debug("getJsonValueForKey(): found value=" + keyValue + ", for key=" + key);\r
break;\r
}\r
} else {\r
}\r
}\r
}\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("getJsonValueForKey(): caught JSONException attempting to retrieve value for key=" + key, je);\r
- keyValue = null;\r
} catch (Exception e) {\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("getJsonValueForKey(): failed to retrieve param value for key=" + key + ": " + e.getMessage()); \r
+ } else {\r
msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(), e);\r
+ }\r
+ keyValue = null;\r
}\r
return keyValue;\r
}\r
*/\r
public static Integer getJsonIntValueForKey(JSONObject jsonObj, String key) {\r
// String isDebugLogEnabled = "true";\r
- Integer keyValue = 0;\r
+ Integer keyValue = null;\r
try {\r
if (jsonObj.has(key)) {\r
- msoLogger.debug("getJsonValueForKey(): found value for key=" + key);\r
- return (Integer) jsonObj.get(key);\r
+ Integer value = (Integer) jsonObj.get(key);\r
+ msoLogger.debug("getJsonIntValueForKey(): found value=" + value + ", for key=" + key);\r
+ return value;\r
} else {\r
- msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
+// msoLogger.debug("getJsonIntValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
- msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
+// msoLogger.debug("getJsonIntValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
keyValue = getJsonIntValueForKey((JSONObject) obj, key);\r
if (keyValue != null) {\r
- msoLogger.debug("getJsonValueForKey(): found value=" + keyValue + ", for key=" + key);\r
+// msoLogger.debug("getJsonIntValueForKey(): found value=" + keyValue + ", for key=" + key);\r
break;\r
}\r
} else {\r
- msoLogger.debug("getJsonValueForKey(): key=" + nextKey + ", does not point to a JSONObject, next key");\r
+ msoLogger.debug("getJsonIntValueForKey(): key=" + nextKey + ", does not point to a JSONObject, next key");\r
}\r
}\r
}\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("getJsonValueForKey(): caught JSONException attempting to retrieve value for key=" + key, je);\r
- keyValue = null;\r
} catch (Exception e) {\r
- msoLogger.debug("getJsonValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(),e);\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("getJsonIntValueForKey(): failed to retrieve param value for key=" + key + ": " + e.getMessage()); \r
+ } else {\r
+ msoLogger.debug("getJsonIntValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(),e);\r
+ }\r
+ keyValue = null;\r
}\r
return keyValue;\r
}\r
* @return String field value associated with key\r
*/\r
public static Boolean getJsonBooleanValueForKey(JSONObject jsonObj, String key) {\r
- Boolean keyValue = false;\r
+ Boolean keyValue = null;\r
try {\r
if (jsonObj.has(key)) {\r
- msoLogger.debug("getJsonBooleanValueForKey(): found value for key=" + key);\r
- return (Boolean) jsonObj.get(key);\r
+ Boolean value = (Boolean) jsonObj.get(key);\r
+ msoLogger.debug("getJsonBooleanValueForKey(): found value=" + value + ", for key=" + key); \r
+ return value;\r
} else {\r
- msoLogger.debug("getJsonBooleanValueForKey(): iterating over the keys");\r
+// msoLogger.debug("getJsonBooleanValueForKey(): iterating over the keys");\r
Iterator <String> itr = jsonObj.keys();\r
while (itr.hasNext()) {\r
- String nextKey = (String) itr.next();\r
+ String nextKey = itr.next();\r
Object obj = jsonObj.get(nextKey);\r
if (obj instanceof JSONObject) {\r
- msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
+// msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
keyValue = getJsonBooleanValueForKey((JSONObject) obj, key);\r
if (keyValue != null) {\r
- msoLogger.debug("getJsonBooleanValueForKey(): found value=" + keyValue + ", for key=" + key);\r
+// msoLogger.debug("getJsonBooleanValueForKey(): found value=" + keyValue + ", for key=" + key);\r
break;\r
}\r
} else {\r
}\r
}\r
}\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("getJsonBooleanValueForKey(): caught JSONException attempting to retrieve value for key=" + key,je);\r
- keyValue = null;\r
} catch (Exception e) {\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("getJsonBooleanValueForKey(): failed to retrieve param value for key=" + key + ": " + e.getMessage()); \r
+ } else {\r
msoLogger.debug("getJsonBooleanValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString(),e);\r
+ }\r
+ keyValue = null;\r
}\r
return keyValue;\r
}\r
if (!jsonValueExists(jsonStr, keys)) {\r
return putJsonValue(jsonStr, keys, value);\r
} else {\r
- msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + (String) value + " already exists");\r
+ msoLogger.debug("addJsonValue(): JSON add failed, key=" + keys + "/value=" + value + " already exists");\r
return jsonStr;\r
}\r
}\r
keyStr = keyTokens.nextToken();\r
Object keyValue = jsonObj.get(keyStr);\r
if (keyValue instanceof JSONObject) {\r
- msoLogger.debug("getJsonRawValue(): key=" + keyStr + " points to json object");\r
+// msoLogger.debug("getJsonRawValue(): key=" + keyStr + " points to json object");\r
jsonObj = (JSONObject) keyValue;\r
} else {\r
if (keyTokens.hasMoreElements()) {\r
return jsonObj.toString();\r
}\r
\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("getJsonRawValue(): caught JSONException attempting to retrieve raw value for key=" + keyStr,je);\r
} catch (Exception e) {\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("getJsonRawValue(): failed to retrieve param value for key=" + keyStr + ": " + e.getMessage()); \r
+ } else {\r
msoLogger.debug("getJsonRawValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString(),e);\r
+ }\r
}\r
return null;\r
}\r
if (keyTokens.hasMoreElements()) {\r
Object keyValue = jsonObj.get(keyStr);\r
if (keyValue instanceof JSONObject) {\r
- msoLogger.debug("putJsonValue(): key=" + keyStr + " points to json object");\r
+// msoLogger.debug("putJsonValue(): key=" + keyStr + " points to json object");\r
jsonObj = (JSONObject) keyValue;\r
} else {\r
- msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + (String) keyValue);\r
+ msoLogger.debug("putJsonValue(): key=" + keyStr + " not the last key but points to non-json object: " + keyValue);\r
return null;\r
}\r
} else { // at the last/new key value\r
// should not hit this point if the key points to a valid key value\r
return null;\r
\r
- } catch (JSONException je) {\r
- // JSONObject::get() throws this exception if one of the specified keys is not found\r
- msoLogger.debug("putJsonValue(): caught JSONException attempting to retrieve value for key=" + keyStr,je);\r
- return null;\r
} catch (Exception e) {\r
+ // JSONObject::get() throws a "not found" exception if one of the specified keys is not found\r
+ if (e.getMessage().contains("not found")) {\r
+ msoLogger.debug("putJsonValue(): failed to put param value for key=" + keyStr + ": " + e.getMessage()); \r
+ } else {\r
msoLogger.debug("putJsonValue(): unable to parse json to put value for key=" + keys + ". Exception was: " + e.toString(),e);\r
+ }\r
}\r
return null;\r
}\r
*\r
* @return Map - a Map containing the entries\r
*/\r
- public Map<String, String> entryArrayToMap(Execution execution, String entryArray) {\r
- msoLogger.debug("Started Entry Array To Map Util Method");\r
+ public Map<String, String> jsonStringToMap(DelegateExecution execution, String entry) {\r
+ msoLogger.debug("Started Json String To Map Method");\r
\r
Map<String, String> map = new HashMap<>();\r
\r
//Populate Map\r
- String entryListJson = "{ \"entry\":" + entryArray + "}";\r
- JSONObject obj = new JSONObject(entryListJson);\r
- JSONArray arr = obj.getJSONArray("entry");\r
- for (int i = 0; i < arr.length(); i++){\r
- JSONObject jo = arr.getJSONObject(i);\r
- String key = jo.getString("key");\r
- String value =jo.getString("value");\r
- map.put(key, value);\r
+ JSONObject obj = new JSONObject(entry);\r
+ \r
+ /* Wildfly is pushing a version of org.json which does not\r
+ * auto cast to string. Leaving it as an object prevents\r
+ * a method not found exception at runtime.\r
+ */\r
+ final Iterator<String> keys = obj.keys();\r
+ while (keys.hasNext()) {\r
+ final String key = keys.next();\r
+ map.put(key, obj.getString(key));\r
}\r
msoLogger.debug("Outgoing Map is: " + map);\r
- msoLogger.debug("Completed Entry Array To Map Util Method");\r
+ msoLogger.debug("Completed Json String To Map Method");\r
return map;\r
}\r
\r
* @return Map - a Map containing the entries\r
*\r
*/\r
- public Map<String, String> entryArrayToMap(Execution execution, String entryArray, String keyNode, String valueNode) {\r
+ public Map<String, String> entryArrayToMap(DelegateExecution execution, String entryArray, String keyNode, String valueNode) {\r
msoLogger.debug("Started Entry Array To Map Util Method");\r
\r
Map<String, String> map = new HashMap<>();\r
//Populate Map\r
- String entryListJson = "{ \"entry\":" + entryArray + "}";\r
+ String entryListJson = "{ \"wrapper\":" + entryArray + "}";\r
JSONObject obj = new JSONObject(entryListJson);\r
- JSONArray arr = obj.getJSONArray("entry");\r
+ JSONArray arr = obj.getJSONArray("wrapper");\r
for (int i = 0; i < arr.length(); i++){\r
JSONObject jo = arr.getJSONObject(i);\r
String key = jo.getString(keyNode);\r
* and puts it in a Java List<String>.\r
*\r
* @param execution\r
- * @param stringArray - the getJsonValue of a json array of strings\r
+ * @param jsonArrayOfStrings - the getJsonValue of a json array of strings\r
*\r
* @return List - a java list containing the strings\r
*\r
return true;\r
}\r
\r
-}
\ No newline at end of file
+ /**\r
+ *\r
+ * Validates the JSON document against a schema file.\r
+ *\r
+ * @param jsonStr String containing the JSON doc\r
+ * @param jsonSchemaPath full path to a valid JSON schema file\r
+ * @return String the validation results/report\r
+ *\r
+ *\r
+ */\r
+ public static String jsonSchemaValidation(String jsonStr, String jsonSchemaPath) throws ValidationException {\r
+ try {\r
+ msoLogger.debug("JSON document to be validated: " + jsonStr);\r
+ JsonNode document = JsonLoader.fromString(jsonStr);\r
+// JsonNode document = JsonLoader.fromFile(jsonDoc);\r
+ JsonNode schema = JsonLoader.fromPath(jsonSchemaPath);\r
+\r
+ JsonSchemaFactory factory = JsonSchemaFactory.byDefault();\r
+ JsonValidator validator = factory.getValidator();\r
+\r
+ ProcessingReport report = validator.validate(schema, document);\r
+ msoLogger.debug("JSON schema validation report: " + report.toString());\r
+ return report.toString();\r
+ } catch (IOException e) {\r
+ msoLogger.debug("IOException performing JSON schema validation on document: " + e.toString());\r
+ throw new ValidationException(e.getMessage());\r
+ } catch (ProcessingException e) {\r
+ msoLogger.debug("ProcessingException performing JSON schema validation on document: " + e.toString());\r
+ throw new ValidationException(e.getMessage());\r
+ }\r
+ }\r
+}\r