KEYCLOAK-5842 Fix NPE in admin-cli ParseUtil.mergeAttributes

This commit is contained in:
Marko Strukelj 2017-11-20 16:06:27 +01:00
parent 8e53ccf5ab
commit 8f82440103

View file

@ -74,7 +74,7 @@ public class ParseUtil {
} }
public static <T> CmdStdinContext<JsonNode> mergeAttributes(CmdStdinContext<JsonNode> ctx, ObjectNode newObject, List<AttributeOperation> attrs) { public static <T> CmdStdinContext<JsonNode> mergeAttributes(CmdStdinContext<JsonNode> ctx, ObjectNode newObject, List<AttributeOperation> attrs) {
String content = ctx.getContent();
JsonNode node = ctx.getResult(); JsonNode node = ctx.getResult();
if (node != null && !node.isObject()) { if (node != null && !node.isObject()) {
throw new RuntimeException("Not a JSON object: " + node); throw new RuntimeException("Not a JSON object: " + node);
@ -83,28 +83,24 @@ public class ParseUtil {
try { try {
if (result == null) { if (result == null) {
try { result = newObject;
result = newObject;
} catch (Throwable e) {
throw new RuntimeException("Failed to instantiate object: " + e.getMessage(), e);
}
} }
if (result != null) { if (result == null) {
try { throw new RuntimeException("Failed to set attribute(s) - no target object");
setAttributes(result, attrs);
} catch (AttributeException e) {
throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + result.getClass().getName() + "'", e);
}
content = JsonSerialization.writeValueAsString(result);
} else {
throw new RuntimeException("Setting attributes is not supported for type: " + result.getClass().getName());
} }
try {
setAttributes(result, attrs);
} catch (AttributeException e) {
throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + result.getClass().getName() + "'", e);
}
ctx.setContent(JsonSerialization.writeValueAsString(result));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to merge set attributes with configuration from file", e); throw new RuntimeException("Failed to merge attributes with configuration from file", e);
} }
ctx.setContent(content);
ctx.setResult(result); ctx.setResult(result);
return ctx; return ctx;
} }