diff options
author | guangxingwang <gw1218@att.com> | 2017-09-11 12:57:58 -0500 |
---|---|---|
committer | guangxingwang <gw1218@att.com> | 2017-09-11 12:58:03 -0500 |
commit | 717c5711cbd6ceb10356280156c288ea5ce96f50 (patch) | |
tree | 6d34e9f40b348ead9bae16074ec258253aa33f1c /POLICY-SDK-APP | |
parent | 1bfd18f999cda39a7b27bef8972864aafe59aecb (diff) |
Enhanced MS JSON Generation
This enhancement excludes empty attributes and objects from creating MS
Json file.
Issure-Id: POLICY-193
Change-Id: I139012456fdc44b0ef6d58c208f1ee5db6bcf910
Signed-off-by: guangxingwang <gw1218@att.com>
Diffstat (limited to 'POLICY-SDK-APP')
-rw-r--r-- | POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java | 121 |
1 files changed, 116 insertions, 5 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java index 815b88f70..6405e6f03 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/CreateDcaeMicroServiceController.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.io.StringReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -47,9 +48,16 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; +import javax.json.JsonValue; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -88,6 +96,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.gson.Gson; @@ -159,8 +168,17 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { String jsonContent = null; try{ - jsonContent = decodeContent(root.get("policyJSON")).toString(); - constructJson(policyData, jsonContent); + 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(); + 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); } @@ -173,7 +191,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { return groupList; } - private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent) { + private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) { ObjectWriter om = new ObjectMapper().writer(); String json=""; DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject(); @@ -228,9 +246,13 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } catch (JsonProcessingException e) { LOGGER.error("Error writing out the object", e); } - LOGGER.info(json); + LOGGER.info("input json: " + json); + LOGGER.info("input jsonContent: " + jsonContent); String cleanJson = cleanUPJson(json); - cleanJson = removeNullAttributes(cleanJson); + //--- 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; } @@ -243,10 +265,20 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { 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(); + JsonObject jsonContent = Json.createReader(new StringReader(contentStr)).readObject(); + removed = removeNull(jsonContent); + if(!jsonContent.toString().equals(removed.toString())){ + contentChanged = true; + } + } if (value==null || value.isNull()){ ((ObjectNode) returnNode).remove(key); remove = true; @@ -255,12 +287,91 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { 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; } + public static 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 static 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()) + builder.add(key, object); + break; + case STRING: + String s = obj.getString(key); + if (s != null && !s.isEmpty()) + 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(); + } + // Second index of dot should be returned. public int stringBetweenDots(String str){ String stringToSearch=str; |