use of org.keycloak.client.admin.cli.common.AttributeOperation in project keycloak by keycloak.
the class ReflectionUtil method setAttributes.
public static void setAttributes(JsonNode client, List<AttributeOperation> attrs) {
for (AttributeOperation item : attrs) {
AttributeKey attr = item.getKey();
JsonNode nested = client;
List<AttributeKey.Component> cs = attr.getComponents();
for (int i = 0; i < cs.size(); i++) {
AttributeKey.Component c = cs.get(i);
// if this is the last component of the name
if (i == cs.size() - 1) {
String val = item.getValue();
ObjectNode obj = (ObjectNode) nested;
if (SET == item.getType()) {
JsonNode valNode = valueToJsonNode(val);
if (c.isArray() || attr.isAppend()) {
JsonNode list = obj.get(c.getName());
// child expected to be an array
if (!(list instanceof ArrayNode)) {
// replace with new array
list = MAPPER.createArrayNode();
obj.set(c.getName(), list);
}
setArrayItem((ArrayNode) list, c.getIndex(), valNode);
} else {
((ObjectNode) nested).set(c.getName(), valNode);
}
} else {
// type == DELETE
if (c.isArray()) {
JsonNode list = obj.get(c.getName());
// child expected to be an array
if (list instanceof ArrayNode) {
removeArrayItem((ArrayNode) list, c.getIndex());
}
} else {
obj.remove(c.getName());
}
}
} else {
// get child and
// if exist set nested to child
// else create new empty object or array - depending on c.isArray()
JsonNode node = nested.get(c.getName());
if (node == null) {
if (c.isArray()) {
node = MAPPER.createArrayNode();
} else {
node = MAPPER.createObjectNode();
}
((ObjectNode) nested).set(c.getName(), node);
}
nested = node;
}
}
}
}
use of org.keycloak.client.admin.cli.common.AttributeOperation in project keycloak by keycloak.
the class MergeAttributesTest method testMergeAttrs.
@Test
public void testMergeAttrs() throws Exception {
List<AttributeOperation> attrs = new LinkedList<>();
attrs.add(new AttributeOperation(SET, "realm", "nurealm"));
attrs.add(new AttributeOperation(SET, "enabled", "true"));
attrs.add(new AttributeOperation(SET, "revokeRefreshToken", "true"));
attrs.add(new AttributeOperation(SET, "accessTokenLifespan", "900"));
attrs.add(new AttributeOperation(SET, "smtpServer.host", "localhost"));
attrs.add(new AttributeOperation(SET, "extra.key1", "somevalue"));
attrs.add(new AttributeOperation(SET, "extra.key2", "[\"somevalue\"]"));
attrs.add(new AttributeOperation(SET, "extra.key3[1]", "second item"));
attrs.add(new AttributeOperation(SET, "extra.key4", "\"true\""));
attrs.add(new AttributeOperation(SET, "extra.key5", "\"1000\""));
attrs.add(new AttributeOperation(DELETE, "id"));
attrs.add(new AttributeOperation(DELETE, "attributes.\"_browser_header.xFrameOptions\""));
String localJSON = "{\n" + " \"id\" : \"24e5d572-756a-435b-8b2b-edbd0a7aa93d\",\n" + " \"realm\" : \"demorealm\",\n" + " \"notBefore\" : 0,\n" + " \"revokeRefreshToken\" : false,\n" + " \"accessTokenLifespan\" : 300,\n" + " \"defaultRoles\" : [ \"offline_access\", \"uma_authorization\" ],\n" + " \"smtpServer\" : { },\n" + " \"attributes\" : {\n" + " \"_browser_header.xFrameOptions\" : \"SAMEORIGIN\",\n" + " \"_browser_header.contentSecurityPolicy\" : \"frame-src 'self'\"\n" + " }\n" + "}";
ObjectNode localNode = MAPPER.readValue(localJSON.getBytes(StandardCharsets.UTF_8), ObjectNode.class);
CmdStdinContext<JsonNode> ctx = new CmdStdinContext<>();
ctx.setResult(localNode);
ctx = mergeAttributes(ctx, MAPPER.createObjectNode(), attrs);
System.out.println(ctx);
String remoteJSON = "{\n" + " \"id\" : \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\n" + " \"realm\" : \"demorealm\",\n" + " \"notBefore\" : 0,\n" + " \"revokeRefreshToken\" : false,\n" + " \"accessTokenLifespan\" : 300,\n" + " \"defaultRoles\" : [ \"uma_authorization\" ],\n" + " \"remote\" : \"value\",\n" + " \"attributes\" : {\n" + " \"_browser_header.xFrameOptions\" : \"SAMEORIGIN\",\n" + " \"_browser_header.x\" : \"ORIGIN\",\n" + " \"_browser_header.contentSecurityPolicy\" : \"frame-src 'self'\"\n" + " }\n" + "}";
ObjectNode remoteNode = MAPPER.readValue(remoteJSON.getBytes(StandardCharsets.UTF_8), ObjectNode.class);
CmdStdinContext<ObjectNode> ctxremote = new CmdStdinContext<>();
ctxremote.setResult(remoteNode);
ReflectionUtil.merge(ctx.getResult(), ctxremote.getResult());
System.out.println(ctx);
// ctx = mergeAttributes(ctx, MAPPER.createObjectNode(), attrs);
}
use of org.keycloak.client.admin.cli.common.AttributeOperation in project keycloak by keycloak.
the class NewObjectCmd method process.
public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
List<AttributeOperation> attrs = new LinkedList<>();
Iterator<String> it = args.iterator();
while (it.hasNext()) {
String option = it.next();
switch(option) {
case "-s":
case "--set":
{
if (!it.hasNext()) {
throw new IllegalArgumentException("Option " + option + " requires a value");
}
String[] keyVal = parseKeyVal(it.next());
attrs.add(new AttributeOperation(SET, keyVal[0], keyVal[1]));
break;
}
default:
{
throw new IllegalArgumentException("Invalid option: " + option);
}
}
}
InputStream body = null;
CmdStdinContext<JsonNode> ctx = new CmdStdinContext<>();
if (file != null) {
ctx = parseFileOrStdin(file);
}
if (attrs.size() > 0) {
ctx = mergeAttributes(ctx, MAPPER.createObjectNode(), attrs);
}
if (body == null && ctx.getContent() != null) {
body = new ByteArrayInputStream(ctx.getContent().getBytes(StandardCharsets.UTF_8));
}
AccessibleBufferOutputStream abos = new AccessibleBufferOutputStream(System.out);
if (!compressed) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
copyStream(body, buffer);
try {
JsonNode rootNode = MAPPER.readValue(buffer.toByteArray(), JsonNode.class);
// now pretty print it to output
MAPPER.writeValue(abos, rootNode);
} catch (Exception ignored) {
copyStream(new ByteArrayInputStream(buffer.toByteArray()), abos);
}
} else {
copyStream(body, System.out);
}
int lastByte = abos.getLastByte();
if (lastByte != -1 && lastByte != 13 && lastByte != 10) {
printErr("");
}
return CommandResult.SUCCESS;
}
Aggregations