Search in sources :

Example 6 with AttributeKey

use of org.keycloak.client.registration.cli.common.AttributeKey in project keycloak by keycloak.

the class AttrsCmd method execute.

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    try {
        processGlobalOptions();
        if (printHelp()) {
            return CommandResult.SUCCESS;
        }
        EndpointType regType = EndpointType.DEFAULT;
        PrintStream out = commandInvocation.getShell().out();
        if (endpoint != null) {
            regType = EndpointType.of(endpoint);
        }
        if (args != null) {
            if (args.size() > 1) {
                throw new IllegalArgumentException("Invalid option: " + args.get(1));
            }
            attr = args.get(0);
        }
        Class type = regType == EndpointType.DEFAULT ? ClientRepresentation.class : (regType == EndpointType.OIDC ? OIDCClientRepresentation.class : null);
        if (type == null) {
            throw new IllegalArgumentException("Endpoint not supported: " + regType);
        }
        AttributeKey key = attr == null ? new AttributeKey() : new AttributeKey(attr);
        Field f = ReflectionUtil.resolveField(type, key);
        String ts = f != null ? ReflectionUtil.getTypeString(null, f) : null;
        if (f == null) {
            out.printf("Attributes for %s format:\n", regType.getEndpoint());
            LinkedHashMap<String, String> items = getAttributeListWithJSonTypes(type, key);
            for (Map.Entry<String, String> item : items.entrySet()) {
                out.printf("  %-40s %s\n", item.getKey(), item.getValue());
            }
        } else {
            out.printf("%-40s %s", attr, ts);
            boolean eol = false;
            Type t = f.getGenericType();
            if (isListType(f.getType()) && t instanceof ParameterizedType) {
                t = ((ParameterizedType) t).getActualTypeArguments()[0];
                if (!isBasicType(t) && t instanceof Class) {
                    eol = true;
                    System.out.printf(", where value is:\n", ts);
                    LinkedHashMap<String, String> items = ReflectionUtil.getAttributeListWithJSonTypes((Class) t, null);
                    for (Map.Entry<String, String> item : items.entrySet()) {
                        out.printf("    %-36s %s\n", item.getKey(), item.getValue());
                    }
                }
            } else if (isMapType(f.getType()) && t instanceof ParameterizedType) {
                t = ((ParameterizedType) t).getActualTypeArguments()[1];
                if (!isBasicType(t) && t instanceof Class) {
                    eol = true;
                    out.printf(", where value is:\n", ts);
                    LinkedHashMap<String, String> items = ReflectionUtil.getAttributeListWithJSonTypes((Class) t, null);
                    for (Map.Entry<String, String> item : items.entrySet()) {
                        out.printf("    %-36s %s\n", item.getKey(), item.getValue());
                    }
                }
            }
            if (!eol) {
                // add end of line
                out.println();
            }
        }
        return CommandResult.SUCCESS;
    } finally {
        commandInvocation.stop();
    }
}
Also used : PrintStream(java.io.PrintStream) LinkedHashMap(java.util.LinkedHashMap) AttributeKey(org.keycloak.client.registration.cli.common.AttributeKey) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ReflectionUtil.isListType(org.keycloak.client.registration.cli.util.ReflectionUtil.isListType) ReflectionUtil.isMapType(org.keycloak.client.registration.cli.util.ReflectionUtil.isMapType) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) ReflectionUtil.isBasicType(org.keycloak.client.registration.cli.util.ReflectionUtil.isBasicType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

AttributeKey (org.keycloak.client.registration.cli.common.AttributeKey)6 Field (java.lang.reflect.Field)5 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Test (org.junit.Test)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1 AttributeOperation (org.keycloak.client.registration.cli.common.AttributeOperation)1 EndpointType (org.keycloak.client.registration.cli.common.EndpointType)1 ReflectionUtil.isBasicType (org.keycloak.client.registration.cli.util.ReflectionUtil.isBasicType)1 ReflectionUtil.isListType (org.keycloak.client.registration.cli.util.ReflectionUtil.isListType)1 ReflectionUtil.isMapType (org.keycloak.client.registration.cli.util.ReflectionUtil.isMapType)1