- private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
- private Map<String, String> matchableValues;
- private static CommonClassDao commonClassDao;
-
- public static CommonClassDao getCommonClassDao() {
- return commonClassDao;
- }
-
- public static void setCommonClassDao(CommonClassDao commonClassDao) {
- CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
- }
-
- private MicroServiceModels newModel;
- private String newFile;
- private String directory;
- private List<String> modelList = new ArrayList<>();
- private List<String> dirDependencyList = new ArrayList<>();
- private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
- //Tosca Model related Datastructure.
- String referenceAttributes;
- String attributeString;
- String listConstraints;
- String subAttributeString;
- HashMap<String, Object> retmap = new HashMap<>();
- Set<String> uniqueKeys= new HashSet<>();
- Set<String> uniqueDataKeys= new HashSet<>();
- StringBuilder dataListBuffer=new StringBuilder();
- List<String> dataConstraints= new ArrayList <>();
- Set<String> allManyTrueKeys= new HashSet <>();
-
- public static final String DATATYPE = "data_types.policy.data.";
- public static final String PROPERTIES=".properties.";
- public static final String TYPE=".type";
- public static final String STRING="string";
- public static final String INTEGER="integer";
- public static final String LIST="list";
- public static final String DEFAULT=".default";
- public static final String REQUIRED=".required";
- public static final String MANYFALSE=":MANY-false";
- public static final String MATCHABLE=".matchable";
-
- @Autowired
- private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
- CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
- }
-
- public CreateDcaeMicroServiceController(){
- // Empty Constructor
- }
-
- protected PolicyRestAdapter policyAdapter = null;
- private int priorityCount;
- private Map<String, String> attributesListRefMap = new HashMap<>();
- private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
-
- public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
-
- String jsonContent = null;
- try{
- LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
-
- String tempJson = root.get("policyJSON").toString();
-
- //---replace empty value with the value below before calling decodeContent method.
- String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
- LOGGER.info("dummyValue:" + dummyValue);
- tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
- ObjectMapper mapper = new ObjectMapper();
- JsonNode tempJsonNode = mapper.readTree(tempJson);
- jsonContent = decodeContent(tempJsonNode).toString();
- constructJson(policyData, jsonContent, dummyValue);
- }catch(Exception e){
- LOGGER.error("Error while decoding microservice content", e);
- }
-
- return policyData;
- }
-
- private GroupPolicyScopeList getPolicyObject(String policyScope) {
- return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
- }
-
- private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
- ObjectWriter om = new ObjectMapper().writer();
- String json="";
- DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
- MicroServiceModels returnModel = new MicroServiceModels();
- microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
- if(policyAdapter.getServiceType() !=null){
- microServiceObject.setService(policyAdapter.getServiceType());
- microServiceObject.setVersion(policyAdapter.getVersion());
- returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
- }
- if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
- if(policyAdapter.getUuid()!=null){
- microServiceObject.setUuid(policyAdapter.getUuid());
- }
- if(policyAdapter.getLocation()!=null){
- microServiceObject.setLocation(policyAdapter.getLocation());
- }
- if(policyAdapter.getConfigName()!=null){
- microServiceObject.setConfigName(policyAdapter.getConfigName());
- }
- GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
- if(policyScopeValue!=null){
- microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
- }
- }
-
- if(policyAdapter.getPolicyName()!=null){
- microServiceObject.setPolicyName(policyAdapter.getPolicyName());
- }
- if(policyAdapter.getPolicyDescription()!=null){
- microServiceObject.setDescription(policyAdapter.getPolicyDescription());
- }
- if (policyAdapter.getPriority()!=null){
- microServiceObject.setPriority(policyAdapter.getPriority());
- }else {
- microServiceObject.setPriority("9999");
- }
-
- if (policyAdapter.getRiskLevel()!=null){
- microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
- }
- if (policyAdapter.getRiskType()!=null){
- microServiceObject.setRiskType(policyAdapter.getRiskType());
- }
- if (policyAdapter.getGuard()!=null){
- microServiceObject.setGuard(policyAdapter.getGuard());
- }
- microServiceObject.setContent(jsonContent);
-
- try {
- json = om.writeValueAsString(microServiceObject);
- } catch (JsonProcessingException e) {
- LOGGER.error("Error writing out the object", e);
- }
- LOGGER.info("input json: " + json);
- LOGGER.info("input jsonContent: " + jsonContent);
- String cleanJson = cleanUPJson(json);
- //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
- String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""});
- LOGGER.info("tempJson: " + tempJson);
- cleanJson = removeNullAttributes(tempJson);
- policyAdapter.setJsonBody(cleanJson);
- return policyAdapter;
- }
-
- private String removeNullAttributes(String cleanJson) {
- ObjectMapper mapper = new ObjectMapper();
-
- try {
- JsonNode rootNode = mapper.readTree(cleanJson);
- JsonNode returnNode = mapper.readTree(cleanJson);
- Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
- boolean remove = false;
- JsonObject removed = null;
- boolean contentChanged = false;
- while (fieldsIterator.hasNext()) {
- Map.Entry<String, JsonNode> field = fieldsIterator.next();
- final String key = field.getKey();
- final JsonNode value = field.getValue();
- if("content".equalsIgnoreCase(key)){
- String contentStr = value.toString();
- try(JsonReader reader = Json.createReader(new StringReader(contentStr))){
- JsonObject jsonContent = reader.readObject();
- removed = removeNull(jsonContent);
- if(!jsonContent.toString().equals(removed.toString())){
- contentChanged = true;
- }
+ private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
+
+ private static CommonClassDao commonClassDao;
+
+ public static CommonClassDao getCommonClassDao() {
+ return commonClassDao;
+ }
+
+ public static void setCommonClassDao(CommonClassDao commonClassDao) {
+ CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
+ }
+
+ private MicroServiceModels newModel;
+ private String newFile;
+ private String directory;
+ private List<String> modelList = new ArrayList<>();
+ private List<String> dirDependencyList = new ArrayList<>();
+ private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
+ String referenceAttributes;
+ String attributeString;
+ Set<String> allManyTrueKeys = null;
+ private Map<String, String> sigRules = null;
+
+ public static final String DATATYPE = "data_types.policy.data.";
+ public static final String PROPERTIES = ".properties.";
+ public static final String TYPE = ".type";
+ public static final String STRING = "string";
+ public static final String INTEGER = "integer";
+ public static final String LIST = "list";
+ public static final String DEFAULT = ".default";
+ public static final String REQUIRED = ".required";
+ public static final String MATCHABLE = ".matchable";
+ public static final String MANYFALSE = ":MANY-false";
+ private static final Pattern PATTERN = Pattern.compile("[A][0-9]");
+ private static final String POLICYJSON = "policyJSON";
+
+ @Autowired
+ private CreateDcaeMicroServiceController(CommonClassDao commonClassDao) {
+ CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
+ }
+
+ public CreateDcaeMicroServiceController() {
+ // Empty Constructor
+ }
+
+ protected PolicyRestAdapter policyAdapter = null;
+ private int priorityCount;
+ private Map<String, String> attributesListRefMap = new HashMap<>();
+ private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
+ private Map<String, String> jsonStringValues = new HashMap<>();
+
+ public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
+
+ String jsonContent = null;
+ try {
+ LOGGER.info("policyJSON :" + (root.get(POLICYJSON)).toString());
+
+ String tempJson = root.get(POLICYJSON).toString();
+ JSONObject policyJSON = new JSONObject(root.get(POLICYJSON).toString());
+ if (policyJSON != null) {
+ tempJson = saveOriginalJsonObject(policyJSON, jsonStringValues).toString();
+ }
+ // ---replace empty value with the value below before calling decodeContent method.
+ String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
+ LOGGER.info("dummyValue:" + dummyValue);
+ tempJson =
+ StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode tempJsonNode = mapper.readTree(tempJson);
+ jsonContent = decodeContent(tempJsonNode).toString();
+ constructJson(policyData, jsonContent, dummyValue);
+ } catch (Exception e) {
+ LOGGER.error("Error while decoding microservice content", e);
+ }
+
+ // ----Here is the final step to reset the original value back.
+ if (policyData.getJsonBody() != null && jsonStringValues.size() > 0) {
+ String contentBody = policyData.getJsonBody();
+ JSONObject contentJson = new JSONObject(contentBody);
+ JSONObject content = contentJson.getJSONObject("content");
+ content = setOriginalJsonObject(content, jsonStringValues);
+ contentJson.put("content", content);
+ policyData.setJsonBody(contentJson.toString());
+ }
+
+ return policyData;
+ }
+
+ private JSONObject saveOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
+ for (Object key : jsonObj.keySet()) {
+ String keyStr = (String) key;
+ Object keyvalue = jsonObj.get(keyStr);
+ if (keyvalue.toString().contains("{\\\"") || keyvalue.toString().contains("\\\"")) {
+ jsonStringValues.put(keyStr, keyvalue.toString());
+ // --- set default value
+ jsonObj.put(keyStr, "JSON_STRING");
+ }
+
+ // for nested objects iteration if required
+ if (keyvalue instanceof JSONObject) {
+ saveOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
+ // --- set default value
+ jsonObj.put(keyStr, "JSON_STRING");
+ }
+
+ if (keyvalue instanceof JSONArray) {
+ for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
+ JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
+ saveOriginalJsonObject(temp, jsonStringValues);
+ }
+ }
+ }
+
+ return jsonObj;
+ }
+
+ private JSONObject setOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
+ for (Object key : jsonObj.keySet()) {
+ String keyStr = (String) key;
+ Object keyvalue = jsonObj.get(keyStr);
+ String originalValue = getOriginalValue(keyStr);
+ if (originalValue != null) {
+ jsonObj.put(keyStr, originalValue);
+ }
+
+ // for nested objects iteration if required
+ if (keyvalue instanceof JSONObject) {
+ setOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
+ jsonObj.put(keyStr, originalValue);
+ }
+
+ if (keyvalue instanceof JSONArray) {
+ for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
+ JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
+ setOriginalJsonObject(temp, jsonStringValues);
+ }
+ }
+ }
+
+ return jsonObj;
+ }
+
+ private GroupPolicyScopeList getPolicyObject(String policyScope) {
+ return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
+ }
+
+ private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue)
+ throws IOException {
+ ObjectWriter om = new ObjectMapper().writer();
+ String json = "";
+ DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
+ MicroServiceModels returnModel = new MicroServiceModels();
+ microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
+ if (policyAdapter.getServiceType() != null) {
+ microServiceObject.setService(policyAdapter.getServiceType());
+ microServiceObject.setVersion(policyAdapter.getVersion());
+ returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
+ }
+ if (returnModel.getAnnotation() == null || returnModel.getAnnotation().isEmpty()) {
+ if (policyAdapter.getUuid() != null) {
+ microServiceObject.setUuid(policyAdapter.getUuid());
+ }
+ if (policyAdapter.getLocation() != null) {
+ microServiceObject.setLocation(policyAdapter.getLocation());
+ }
+ if (policyAdapter.getConfigName() != null) {
+ microServiceObject.setConfigName(policyAdapter.getConfigName());
+ }
+ GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
+ if (policyScopeValue != null) {
+ microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
+ }
+ }
+
+ if (policyAdapter.getPolicyName() != null) {
+ microServiceObject.setPolicyName(policyAdapter.getPolicyName());
+ }
+ if (policyAdapter.getPolicyDescription() != null) {
+ microServiceObject.setDescription(policyAdapter.getPolicyDescription());
+ }
+ if (policyAdapter.getPriority() != null) {
+ microServiceObject.setPriority(policyAdapter.getPriority());
+ } else {
+ microServiceObject.setPriority("9999");
+ }
+
+ if (policyAdapter.getRiskLevel() != null) {
+ microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
+ }
+ if (policyAdapter.getRiskType() != null) {
+ microServiceObject.setRiskType(policyAdapter.getRiskType());
+ }
+ if (policyAdapter.getGuard() != null) {
+ microServiceObject.setGuard(policyAdapter.getGuard());
+ }
+ microServiceObject.setContent(jsonContent);
+ String modelName = policyAdapter.getServiceType();
+ String versionName = policyAdapter.getVersion();
+ List<Object> triggerData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version",
+ modelName + ":" + versionName);
+ MicroServiceModels model = null;
+ boolean ruleCheck = false;
+ boolean SymptomRuleCheck = false;
+ if (!triggerData.isEmpty()) {
+ model = (MicroServiceModels) triggerData.get(0);
+ if (model.getRuleFormation() != null) {
+ microServiceObject.setUiContent(jsonContent);
+ ruleCheck = true;
+ if (model.getRuleFormation().contains("@")) {
+ SymptomRuleCheck = true;
+ }
+ }
+ }
+ try {
+ json = om.writeValueAsString(microServiceObject);
+ } catch (JsonProcessingException e) {
+ LOGGER.error("Error writing out the object", e);
+ }
+ LOGGER.info("input json: " + json);
+ LOGGER.info("input jsonContent: " + jsonContent);
+ String cleanJson = cleanUPJson(json);
+ // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
+ String tempJson =
+ StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
+ LOGGER.info("tempJson: " + tempJson);
+ cleanJson = removeNullAttributes(tempJson);
+ if (cleanJson.contains("\\")) {
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\"}, new String[] {""});
+ }
+ policyAdapter.setJsonBody(cleanJson);
+ // for Triggers
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode tempJsonNode = mapper.readTree(cleanJson);
+ if (ruleCheck) {
+ // JsonNode tempJsonNode = mapper.readTree(cleanJson);
+ ObjectNode finalJson = (ObjectNode) tempJsonNode;
+ JsonNode object = tempJsonNode.get("content");
+ String primaryKey1 = model.getRuleFormation();
+ String[] primaryKeyForSignatures = primaryKey1.split("@");
+ for (String primaryKeyForSignature : primaryKeyForSignatures) {
+ String primarykeyAlarm = primaryKeyForSignature.substring(0, primaryKeyForSignature.indexOf('.'));
+ JsonNode triggerSig = object.get(primarykeyAlarm);
+ sigRules = new HashMap<>();
+ String parseKey = primaryKeyForSignature.substring(primaryKeyForSignature.indexOf('.') + 1);
+ StringBuilder sb = null;
+ if (triggerSig instanceof ArrayNode) {
+ for (int i = 0; i < triggerSig.size(); i++) {
+ sb = new StringBuilder();
+ parseData(triggerSig.get(i), parseKey);
+ sb.append("(");
+ List<?> keyList = new ArrayList<>(sigRules.keySet());
+ for (int j = keyList.size() - 1; j >= 0; j--) {
+ String key = (String) keyList.get(j);
+ String jsonNode = sigRules.get(key);
+ constructRule(sb, jsonNode, sigRules);
+ }
+ sb.append(")").toString();
+ putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
+ sigRules = new HashMap<>();
+ }
+ } else {
+ sb = new StringBuilder();
+ parseData(triggerSig, parseKey);
+ }
+ }
+ policyAdapter.setJsonBody(finalJson.toString());
+ }
+ return policyAdapter;
+ }
+
+ private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
+ String primaryKey) {
+ JsonNode tmp = tmpJsonNode;
+ ObjectNode objectNode = (ObjectNode) tmp;
+ JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ String[] tempSt = parseKey.split("\\.");
+ for (String value : tempSt) {
+ if (value.contains("[")) {
+ if (tempRuleJsonNode instanceof ArrayNode) {
+ JsonNode tempRuleNode = tempRuleJsonNode.get(item);
+ ((ArrayNode) tempRuleJsonNode).removeAll();
+ ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
+ objectNode = (ObjectNode) tempRuleJsonNode.get(item);
+ }
+ String key = value.substring(0, value.indexOf('['));
+ objectNode.remove(key);
+ objectNode.put(key, sb.toString());
+ return tmp;
+ } else {
+ jsonNode = jsonNode.get(value);
+ if (jsonNode instanceof ArrayNode) {
+ tempRuleJsonNode = jsonNode;
+ jsonNode = jsonNode.get(item);
+ }
+ }
+ }
+ return tmp;
+ }
+
+ public boolean checkPattern(String patternString) {
+ return PATTERN.matcher(patternString).find();
+ }
+
+ /**
+ * Construct rule.
+ *
+ * @param sb the sb
+ * @param jsonNode the json node
+ * @param sigRules2 the sig rules 2
+ */
+ public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
+ int count = 0;
+ String cleanJsonNode = jsonNode.replace("\"\"", " ");
+ cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
+ boolean flag = false;
+ if (cleanJsonNode.contains("OR")) {
+ sb.append("(");
+ flag = true;
+ }
+ for (String rowValue : cleanJsonNode.split(" ")) {
+ if (checkPattern(rowValue)) {
+ String value = sigRules2.get(rowValue);
+ LOGGER.info(" Value is:" + value);
+ constructRule(sb, value, sigRules2);
+ } else {
+ if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
+ sb.append("(");
+ }
+ count++;
+ LOGGER.info(" " + rowValue + " ");
+ sb.append(" " + rowValue + " ");
+ if (count % 3 == 0) {
+ sb.append(")");
+ count = 0;
+ }
+ }
+ }
+ if (flag) {
+ sb.append(")");
+ }
+ }
+
+ /**
+ * Parses the data.
+ *
+ * @param jsonNode the json node
+ * @param string the string
+ */
+ public void parseData(JsonNode jsonNode, String string) {
+ if (string.contains(".")) {
+ String firstIndex = string.substring(0, string.indexOf('.'));
+ JsonNode signtures = jsonNode.get(firstIndex);
+ String subIndex = string.substring(firstIndex.length() + 1);
+ if (signtures instanceof ArrayNode) {
+ for (int i = 0; i < signtures.size(); i++) {
+ parseData(signtures.get(i), subIndex);
+ }
+ } else {
+ parseData(signtures, subIndex);
+ }
+ } else {
+ if (string.contains("[")) {
+ String ruleIndex = string.substring(0, string.indexOf('['));
+ String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
+ String key = "A" + Integer.valueOf(sigRules.size() + 1);
+ JsonNode node = jsonNode.get(ruleIndex);
+ StringBuilder sb = new StringBuilder("(");
+ for (int i = 0; i < keys.length; i++) {
+ sb.append(node.get(keys[i].trim()));
+ }
+ sb.append(")");
+ sigRules.put(key, sb.toString());
+ }
+ }
+ }
+
+ public String removeNullAttributes(String cleanJson) {
+ ObjectMapper mapper = new ObjectMapper();
+
+ try {
+ JsonNode rootNode = mapper.readTree(cleanJson);
+ JsonNode returnNode = mapper.readTree(cleanJson);
+ Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
+ boolean remove = false;
+ JsonObject removed = null;
+ boolean contentChanged = false;
+ while (fieldsIterator.hasNext()) {
+ Map.Entry<String, JsonNode> field = fieldsIterator.next();
+ final String key = field.getKey();
+ final JsonNode value = field.getValue();
+ if ("content".equalsIgnoreCase(key)) {
+ String contentStr = value.toString();
+ try (JsonReader reader = Json.createReader(new StringReader(contentStr))) {
+ JsonObject jsonContent = reader.readObject();
+ removed = removeNull(jsonContent);
+ if (!jsonContent.toString().equals(removed.toString())) {
+ contentChanged = true;
+ }
+ }
+
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+
+ if (contentChanged) {
+ // set modified content to cleanJson
+ JSONObject jObject = new JSONObject(cleanJson);
+ jObject.put("content", removed.toString());
+ cleanJson = cleanUPJson(jObject.toString());
+ }
+
+ } catch (IOException e) {
+ LOGGER.error("Error writing out the JsonNode", e);
+ }
+ return cleanJson;
+ }
+
+ /**
+ * To verify if it is a JSON string. If it is, then return its original value.
+ *
+ * @param key holds the values
+ * @return
+ */
+ private String getOriginalValue(String key) {
+ for (String k : jsonStringValues.keySet()) {
+ if (k.contains("@")) {
+ String[] arrOfKeys = k.split("@");
+ for (int i = 0; i < arrOfKeys.length; i++) {
+ if (arrOfKeys[i].contains(".")) {
+ arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1);
+ if (arrOfKeys[i].equals(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""},
+ new String[] {"\\\""});
+ }
+ }
+ }
+ }
+ if (k.endsWith(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""});
+ }
+ }
+
+ return null;
+ }
+
+ public JsonArray removeNull(JsonArray array) {
+ JsonArrayBuilder builder = Json.createArrayBuilder();
+ int i = 0;
+ for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
+ JsonValue value = it.next();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray a = removeNull(array.getJsonArray(i));
+ if (!a.isEmpty())
+ builder.add(a);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(array.getJsonObject(i));
+ if (!object.isEmpty())
+ builder.add(object);
+ break;
+ case STRING:
+ String s = array.getString(i);
+ if (s != null && !s.isEmpty())
+ builder.add(s);
+ break;
+ case NUMBER:
+ builder.add(array.getJsonNumber(i));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(array.getBoolean(i));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public JsonObject removeNull(JsonObject obj) {
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
+ Entry<String, JsonValue> e = it.next();
+ String key = e.getKey();
+ JsonValue value = e.getValue();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray array = removeNull(obj.getJsonArray(key));
+ if (!array.isEmpty())
+ builder.add(key, array);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(obj.getJsonObject(key));
+ if (!object.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ builder.add(key, object.toString());
+ break;
+ }
+ }
+ builder.add(key, object);
+ }
+ break;
+ case STRING:
+ String s = obj.getString(key);
+ if (s != null && !s.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ s = getOriginalValue(key);
+ }
+ }
+ builder.add(key, s);
+ }
+ break;
+ case NUMBER:
+ builder.add(key, obj.getJsonNumber(key));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(key, obj.getBoolean(key));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public String cleanUPJson(String json) {
+ String cleanJson = StringUtils.replaceEach(json, new String[] {"\\\\", "\\\\\\", "\\\\\\\\"},
+ new String[] {"\\", "\\", "\\"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\"}, new String[] {"\\"});
+ cleanJson =
+ StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
+
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\"", "\\\"", "\"[{", "}]\""},
+ new String[] {"\"", "\"", "[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[{", "}]\""}, new String[] {"[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"{", "}\""}, new String[] {"{", "}"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\"\"", "\"\""}, new String[] {"\"", "\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\""}, new String[] {""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\\\\\\"}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ return cleanJson;
+ }
+
+ public JSONObject decodeContent(JsonNode jsonNode) {
+ Iterator<JsonNode> jsonElements = jsonNode.elements();
+ Iterator<String> jsonKeys = jsonNode.fieldNames();
+ Map<String, String> element = new TreeMap<>();
+ while (jsonElements.hasNext() && jsonKeys.hasNext()) {
+ element.put(jsonKeys.next(), jsonElements.next().toString());
+ }
+ JSONObject jsonResult = new JSONObject();
+ JSONArray jsonArray = null;
+ String oldValue = null;
+ String nodeKey = null;
+ String arryKey = null;
+ Boolean isArray = false;
+ JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
+ ObjectNode node = nodeFactory.objectNode();
+ String prevKey = null;
+ String presKey;
+ for (Entry<String, String> entry : element.entrySet()) {
+ String key = entry.getKey();
+ String value = entry.getValue();
+ if (key.contains(".")) {
+ presKey = key.substring(0, key.indexOf('.'));
+ } else if (key.contains("@")) {
+ presKey = key.substring(0, key.indexOf('@'));
+ } else {
+ presKey = key;
+ }
+ // first check if we are different from old.
+ LOGGER.info(key + "\n");
+ if (jsonArray != null && jsonArray.length() > 0 && key.contains("@") && !key.contains(".")
+ && oldValue != null) {
+ if (!oldValue.equals(key.substring(0, key.indexOf('@')))) {
+ jsonResult.put(oldValue, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
+ jsonResult.put(oldValue, jsonArray);
+ isArray = false;
+ jsonArray = new JSONArray();
+ }
+
+ prevKey = presKey;
+ //
+ if (key.contains(".")) {
+ if (nodeKey == null) {
+ nodeKey = key.substring(0, key.indexOf('.'));
+ }
+ if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
+ || !key.contains("@")) {
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ nodeKey = key.substring(0, key.indexOf('.'));
+ if (nodeKey.contains("@")) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ }
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ if (key.contains("@")) {
+ isArray = true;
+ if (key.endsWith("@0") || jsonArray == null) {
+ jsonArray = new JSONArray();