Search in sources :

Example 1 with APPLICATION_JSON

use of org.keycloak.client.registration.cli.util.HttpUtil.APPLICATION_JSON in project keycloak by keycloak.

the class GetCmd method execute.

@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    try {
        if (printHelp()) {
            return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
        }
        processGlobalOptions();
        if (args == null || args.isEmpty()) {
            throw new IllegalArgumentException("CLIENT not specified");
        }
        if (args.size() > 1) {
            throw new IllegalArgumentException("Invalid option: " + args.get(1));
        }
        String clientId = args.get(0);
        EndpointType regType = endpoint != null ? EndpointType.of(endpoint) : EndpointType.DEFAULT;
        if (clientId.startsWith("-")) {
            warnfErr(ParseUtil.CLIENT_OPTION_WARN, clientId);
        }
        ConfigData config = loadConfig();
        config = copyWithServerInfo(config);
        if (token == null) {
            // if registration access token is not set via -t, try use the one from configuration
            token = getRegistrationToken(config.sessionRealmConfigData(), clientId);
        }
        setupTruststore(config, commandInvocation);
        String auth = token;
        if (auth == null) {
            config = ensureAuthInfo(config, commandInvocation);
            config = copyWithServerInfo(config);
            if (credentialsAvailable(config)) {
                auth = ensureToken(config);
            }
        }
        auth = auth != null ? "Bearer " + auth : null;
        final String server = config.getServerUrl();
        final String realm = config.getRealm();
        InputStream response = doGet(server + "/realms/" + realm + "/clients-registrations/" + regType.getEndpoint() + "/" + urlencode(clientId), APPLICATION_JSON, auth);
        try {
            String json = readFully(response);
            Object result = null;
            switch(regType) {
                case DEFAULT:
                    {
                        ClientRepresentation client = JsonSerialization.readValue(json, ClientRepresentation.class);
                        result = client;
                        saveMergeConfig(cfg -> {
                            setRegistrationToken(cfg.ensureRealmConfigData(server, realm), client.getClientId(), client.getRegistrationAccessToken());
                        });
                        break;
                    }
                case OIDC:
                    {
                        OIDCClientRepresentation client = JsonSerialization.readValue(json, OIDCClientRepresentation.class);
                        result = client;
                        saveMergeConfig(cfg -> {
                            setRegistrationToken(cfg.ensureRealmConfigData(server, realm), client.getClientId(), client.getRegistrationAccessToken());
                        });
                        break;
                    }
                case INSTALL:
                    {
                        result = JsonSerialization.readValue(json, AdapterConfig.class);
                        break;
                    }
                case SAML2:
                    {
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("Unexpected type: " + regType);
                    }
            }
            if (!compressed && result != null) {
                json = JsonSerialization.writeValueAsPrettyString(result);
            }
            printOut(json);
        // } catch (UnrecognizedPropertyException e) {
        // throw new RuntimeException("Failed to parse returned JSON - " + e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException("Failed to process HTTP response", e);
        }
        return CommandResult.SUCCESS;
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(e.getMessage() + suggestHelp(), e);
    } finally {
        commandInvocation.stop();
    }
}
Also used : APPLICATION_JSON(org.keycloak.client.registration.cli.util.HttpUtil.APPLICATION_JSON) CMD(org.keycloak.client.registration.cli.util.OsUtil.CMD) CommandResult(org.jboss.aesh.console.command.CommandResult) CommandDefinition(org.jboss.aesh.cl.CommandDefinition) AuthUtil.ensureToken(org.keycloak.client.registration.cli.util.AuthUtil.ensureToken) ConfigUtil.setRegistrationToken(org.keycloak.client.registration.cli.util.ConfigUtil.setRegistrationToken) IoUtil.printOut(org.keycloak.client.registration.cli.util.IoUtil.printOut) ConfigUtil.saveMergeConfig(org.keycloak.client.registration.cli.util.ConfigUtil.saveMergeConfig) CommandInvocation(org.jboss.aesh.console.command.invocation.CommandInvocation) HttpUtil.doGet(org.keycloak.client.registration.cli.util.HttpUtil.doGet) ParseUtil(org.keycloak.client.registration.cli.util.ParseUtil) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) ConfigUtil.getRegistrationToken(org.keycloak.client.registration.cli.util.ConfigUtil.getRegistrationToken) IoUtil.warnfErr(org.keycloak.client.registration.cli.util.IoUtil.warnfErr) EOL(org.keycloak.client.registration.cli.util.OsUtil.EOL) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) PrintWriter(java.io.PrintWriter) HttpUtil.urlencode(org.keycloak.client.registration.cli.util.HttpUtil.urlencode) Arguments(org.jboss.aesh.cl.Arguments) StringWriter(java.io.StringWriter) DEFAULT_CONFIG_FILE_STRING(org.keycloak.client.registration.cli.util.ConfigUtil.DEFAULT_CONFIG_FILE_STRING) IOException(java.io.IOException) ConfigUtil.loadConfig(org.keycloak.client.registration.cli.util.ConfigUtil.loadConfig) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) JsonSerialization(org.keycloak.util.JsonSerialization) ConfigUtil.credentialsAvailable(org.keycloak.client.registration.cli.util.ConfigUtil.credentialsAvailable) PROMPT(org.keycloak.client.registration.cli.util.OsUtil.PROMPT) Option(org.jboss.aesh.cl.Option) List(java.util.List) CommandException(org.jboss.aesh.console.command.CommandException) IoUtil.readFully(org.keycloak.client.registration.cli.util.IoUtil.readFully) AdapterConfig(org.keycloak.representations.adapters.config.AdapterConfig) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) InputStream(java.io.InputStream) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) InputStream(java.io.InputStream) EndpointType(org.keycloak.client.registration.cli.common.EndpointType) IOException(java.io.IOException) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Arguments (org.jboss.aesh.cl.Arguments)1 CommandDefinition (org.jboss.aesh.cl.CommandDefinition)1 Option (org.jboss.aesh.cl.Option)1 CommandException (org.jboss.aesh.console.command.CommandException)1 CommandResult (org.jboss.aesh.console.command.CommandResult)1 CommandInvocation (org.jboss.aesh.console.command.invocation.CommandInvocation)1 EndpointType (org.keycloak.client.registration.cli.common.EndpointType)1 ConfigData (org.keycloak.client.registration.cli.config.ConfigData)1 AuthUtil.ensureToken (org.keycloak.client.registration.cli.util.AuthUtil.ensureToken)1 DEFAULT_CONFIG_FILE_STRING (org.keycloak.client.registration.cli.util.ConfigUtil.DEFAULT_CONFIG_FILE_STRING)1 ConfigUtil.credentialsAvailable (org.keycloak.client.registration.cli.util.ConfigUtil.credentialsAvailable)1 ConfigUtil.getRegistrationToken (org.keycloak.client.registration.cli.util.ConfigUtil.getRegistrationToken)1 ConfigUtil.loadConfig (org.keycloak.client.registration.cli.util.ConfigUtil.loadConfig)1 ConfigUtil.saveMergeConfig (org.keycloak.client.registration.cli.util.ConfigUtil.saveMergeConfig)1 ConfigUtil.setRegistrationToken (org.keycloak.client.registration.cli.util.ConfigUtil.setRegistrationToken)1