@Test public void testSanitizeJsonNode() { String anonId = "eyJrbGciOiJFQ0RILUVTK0EyNTZLVyIsImVuYyI6IkEyNTZHQ00iLCJlc0EyNTZLVyIsImVuYyI6I"; ObjectNode json = Json.asObjectNode( ImmutableMap.of("payload", ImmutableMap.of("myList", ImmutableList.of("foo", anonId)))); ObjectNode expected = Json.asObjectNode(ImmutableMap.of("payload", ImmutableMap.of("myList", ImmutableList.of("foo", "eyJr<73 characters redacted>")))); SanitizeJsonPayload.sanitizeJsonNode(json); assertEquals(Json.asString(expected), Json.asString(json)); }
@VisibleForTesting static void sanitizeJsonNode(JsonNode node) { if (node.isObject()) { List<Entry<String, JsonNode>> fields = Lists.newArrayList(node.fields()); for (Map.Entry<String, JsonNode> entry : fields) { String fieldName = entry.getKey(); JsonNode value = entry.getValue(); ((ObjectNode) node).set(fieldName, redactedNode(value)); } } if (node.isArray()) { List<JsonNode> elements = Lists.newArrayList(node.elements()); for (int i = 0; i < elements.size(); i++) { JsonNode value = elements.get(i); if (value.isTextual() && value.asText().length() > 32) { ((ArrayNode) node).set(i, redactedNode(value)); } } } if (node.isContainerNode()) { node.elements().forEachRemaining(SanitizeJsonPayload::sanitizeJsonNode); } }
SanitizeJsonPayload { @VisibleForTesting static void sanitizeJsonNode(JsonNode node) { if (node.isObject()) { List<Entry<String, JsonNode>> fields = Lists.newArrayList(node.fields()); for (Map.Entry<String, JsonNode> entry : fields) { String fieldName = entry.getKey(); JsonNode value = entry.getValue(); ((ObjectNode) node).set(fieldName, redactedNode(value)); } } if (node.isArray()) { List<JsonNode> elements = Lists.newArrayList(node.elements()); for (int i = 0; i < elements.size(); i++) { JsonNode value = elements.get(i); if (value.isTextual() && value.asText().length() > 32) { ((ArrayNode) node).set(i, redactedNode(value)); } } } if (node.isContainerNode()) { node.elements().forEachRemaining(SanitizeJsonPayload::sanitizeJsonNode); } } }
SanitizeJsonPayload { @VisibleForTesting static void sanitizeJsonNode(JsonNode node) { if (node.isObject()) { List<Entry<String, JsonNode>> fields = Lists.newArrayList(node.fields()); for (Map.Entry<String, JsonNode> entry : fields) { String fieldName = entry.getKey(); JsonNode value = entry.getValue(); ((ObjectNode) node).set(fieldName, redactedNode(value)); } } if (node.isArray()) { List<JsonNode> elements = Lists.newArrayList(node.elements()); for (int i = 0; i < elements.size(); i++) { JsonNode value = elements.get(i); if (value.isTextual() && value.asText().length() > 32) { ((ArrayNode) node).set(i, redactedNode(value)); } } } if (node.isContainerNode()) { node.elements().forEachRemaining(SanitizeJsonPayload::sanitizeJsonNode); } }  }
SanitizeJsonPayload { @VisibleForTesting static void sanitizeJsonNode(JsonNode node) { if (node.isObject()) { List<Entry<String, JsonNode>> fields = Lists.newArrayList(node.fields()); for (Map.Entry<String, JsonNode> entry : fields) { String fieldName = entry.getKey(); JsonNode value = entry.getValue(); ((ObjectNode) node).set(fieldName, redactedNode(value)); } } if (node.isArray()) { List<JsonNode> elements = Lists.newArrayList(node.elements()); for (int i = 0; i < elements.size(); i++) { JsonNode value = elements.get(i); if (value.isTextual() && value.asText().length() > 32) { ((ArrayNode) node).set(i, redactedNode(value)); } } } if (node.isContainerNode()) { node.elements().forEachRemaining(SanitizeJsonPayload::sanitizeJsonNode); } }  static MapElements<PubsubMessage, PubsubMessage> of(); }
SanitizeJsonPayload { @VisibleForTesting static void sanitizeJsonNode(JsonNode node) { if (node.isObject()) { List<Entry<String, JsonNode>> fields = Lists.newArrayList(node.fields()); for (Map.Entry<String, JsonNode> entry : fields) { String fieldName = entry.getKey(); JsonNode value = entry.getValue(); ((ObjectNode) node).set(fieldName, redactedNode(value)); } } if (node.isArray()) { List<JsonNode> elements = Lists.newArrayList(node.elements()); for (int i = 0; i < elements.size(); i++) { JsonNode value = elements.get(i); if (value.isTextual() && value.asText().length() > 32) { ((ArrayNode) node).set(i, redactedNode(value)); } } } if (node.isContainerNode()) { node.elements().forEachRemaining(SanitizeJsonPayload::sanitizeJsonNode); } }  static MapElements<PubsubMessage, PubsubMessage> of();  }