Example #1
0
  public static void main(String[] args) throws Exception {
    keycloak = new KeycloakInstalled();
    br = new BufferedReader(new InputStreamReader(System.in));

    printHelp();
    printDivider();

    System.out.print("$ ");
    for (String s = br.readLine(); s != null; s = br.readLine()) {
      printDivider();

      try {
        if (s.equals("login")) {
          keycloak.login(System.out, br);
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("logout")) {
          keycloak.logout();
          System.out.println("Logged out");
        } else if (s.equals("login-desktop")) {
          keycloak.loginDesktop();
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("login-manual")) {
          keycloak.loginManual(System.out, br);
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("profile")) {
          profile();
        } else if (s.equals("customers")) {
          customers();
        } else if (s.equals("token")) {
          System.out.println(mapper.writeValueAsString(keycloak.getToken()));
        } else if (s.equals("id-token")) {
          System.out.println(mapper.writeValueAsString(keycloak.getIdToken()));
        } else if (s.equals("refresh")) {
          keycloak.refreshToken();
          System.out.println(
              "Token refreshed: expires at " + Time.toDate(keycloak.getToken().getExpiration()));
        } else if (s.equals("exit")) {
          System.exit(0);
        } else {
          printHelp();
        }
      } catch (ServerRequest.HttpFailure t) {
        System.out.println(t.getError());
      } catch (Throwable t) {
        System.out.println(t.getMessage() != null ? t.getMessage() : t.getClass().toString());
      }
      printDivider();

      System.out.print("$ ");
    }
  }