mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(core): deduplicate KMS and XML parsing (#3670)
This commit is contained in:
1 parent
7d58be1d4f
commit
8c61a01c8d
2 files changed
+32
-83
No files matched your search
@@ -298,13 +298,34 @@ public final class XmlParser {
|
||||
* tags inside a single {@code <QueueConfiguration>}).
|
||||
*/
|
||||
public static List<Map<String, List<String>>> extractGroupsMulti(String xml, String parentElement) {
|
||||
List<Map<String, List<String>>> result = new ArrayList<>();
|
||||
return extractLeafGroups(xml, parentElement,
|
||||
(group, name, text) -> group.computeIfAbsent(name, k -> new ArrayList<>()).add(text));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts every group of elements nested inside a repeating {@code parentElement},
|
||||
* returning each group as a {@code Map<localName, text>}.
|
||||
*
|
||||
* <p>Useful for notification-configuration blocks that contain multiple fields:
|
||||
* <pre>{@code
|
||||
* List<Map<String,String>> configs =
|
||||
* XmlParser.extractGroups(body, "QueueConfiguration");
|
||||
* // configs.get(0).get("QueueArn") → "arn:aws:sqs:..."
|
||||
* }</pre>
|
||||
*/
|
||||
public static List<Map<String, String>> extractGroups(String xml, String parentElement) {
|
||||
return extractLeafGroups(xml, parentElement, Map::put);
|
||||
}
|
||||
|
||||
private static <T> List<Map<String, T>> extractLeafGroups(
|
||||
String xml, String parentElement, LeafCollector<T> leafCollector) {
|
||||
List<Map<String, T>> result = new ArrayList<>();
|
||||
if (xml == null || xml.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
XMLStreamReader r = FACTORY.createXMLStreamReader(new StringReader(xml));
|
||||
Map<String, List<String>> current = null;
|
||||
Map<String, T> current = null;
|
||||
int depth = 0;
|
||||
while (r.hasNext()) {
|
||||
int event = r.next();
|
||||
@@ -316,7 +337,7 @@ public final class XmlParser {
|
||||
} else if (current != null && depth == 1) {
|
||||
String text = readLeafText(r);
|
||||
if (text != null) {
|
||||
current.computeIfAbsent(local, k -> new ArrayList<>()).add(text);
|
||||
leafCollector.add(current, local, text);
|
||||
}
|
||||
} else if (current != null) {
|
||||
depth++;
|
||||
@@ -338,56 +359,9 @@ public final class XmlParser {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts every group of elements nested inside a repeating {@code parentElement},
|
||||
* returning each group as a {@code Map<localName, text>}.
|
||||
*
|
||||
* <p>Useful for notification-configuration blocks that contain multiple fields:
|
||||
* <pre>{@code
|
||||
* List<Map<String,String>> configs =
|
||||
* XmlParser.extractGroups(body, "QueueConfiguration");
|
||||
* // configs.get(0).get("QueueArn") → "arn:aws:sqs:..."
|
||||
* }</pre>
|
||||
*/
|
||||
public static List<Map<String, String>> extractGroups(String xml, String parentElement) {
|
||||
List<Map<String, String>> result = new ArrayList<>();
|
||||
if (xml == null || xml.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
XMLStreamReader r = FACTORY.createXMLStreamReader(new StringReader(xml));
|
||||
Map<String, String> current = null;
|
||||
int depth = 0;
|
||||
while (r.hasNext()) {
|
||||
int event = r.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
String local = r.getLocalName();
|
||||
if (parentElement.equals(local)) {
|
||||
current = new LinkedHashMap<>();
|
||||
depth = 1;
|
||||
} else if (current != null && depth == 1) {
|
||||
String text = readLeafText(r);
|
||||
if (text != null) {
|
||||
current.put(local, text);
|
||||
}
|
||||
} else if (current != null) {
|
||||
depth++;
|
||||
}
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT) {
|
||||
if (current != null && parentElement.equals(r.getLocalName())) {
|
||||
result.add(current);
|
||||
current = null;
|
||||
depth = 0;
|
||||
} else if (current != null) {
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
}
|
||||
r.close();
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Ignoring malformed XML during parse: {0}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
@FunctionalInterface
|
||||
private interface LeafCollector<T> {
|
||||
void add(Map<String, T> group, String name, String text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -147,36 +147,7 @@ public class KmsJsonHandler {
|
||||
String granteePrincipal = request.path("GranteePrincipal").isMissingNode() ? null : request.path("GranteePrincipal").asText(null);
|
||||
|
||||
Map<String, Object> result = service.listGrants(keyId, region, marker, limit, grantId, granteePrincipal);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> grants = (List<Map<String, Object>>) result.get("Grants");
|
||||
ArrayNode array = response.putArray("Grants");
|
||||
for (Map<String, Object> grant : grants) {
|
||||
ObjectNode entry = array.addObject();
|
||||
entry.put("GrantId", (String) grant.get("GrantId"));
|
||||
entry.put("KeyId", (String) grant.get("KeyId"));
|
||||
entry.put("GranteePrincipal", (String) grant.get("GranteePrincipal"));
|
||||
entry.put("CreationDate", ((Number) grant.get("CreationDate")).longValue());
|
||||
if (grant.get("Name") != null) {
|
||||
entry.put("Name", (String) grant.get("Name"));
|
||||
}
|
||||
if (grant.get("Constraints") != null) {
|
||||
entry.set("Constraints", objectMapper.valueToTree(grant.get("Constraints")));
|
||||
}
|
||||
ArrayNode operations = entry.putArray("Operations");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> operationValues = (List<String>) grant.get("Operations");
|
||||
operationValues.forEach(operations::add);
|
||||
if (grant.get("RetiringPrincipal") != null) {
|
||||
entry.put("RetiringPrincipal", (String) grant.get("RetiringPrincipal"));
|
||||
}
|
||||
}
|
||||
response.put("Truncated", (boolean) result.get("Truncated"));
|
||||
if (Boolean.TRUE.equals(result.get("Truncated"))) {
|
||||
response.put("NextMarker", (String) result.get("NextMarker"));
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
return grantListResponse(result);
|
||||
}
|
||||
|
||||
private Response handleListRetirableGrants(JsonNode request, String region) {
|
||||
@@ -186,6 +157,10 @@ public class KmsJsonHandler {
|
||||
|
||||
Map<String, Object> result = service.listRetirableGrants(retiringPrincipal, region, marker, limit);
|
||||
|
||||
return grantListResponse(result);
|
||||
}
|
||||
|
||||
private Response grantListResponse(Map<String, Object> result) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> grants = (List<Map<String, Object>>) result.get("Grants");
|
||||
|
||||
Reference in new issue
Block a user