/**
   * Services a Commandline Request.
   *
   * @param rc Request Context.
   * @throw CLIException if the request cannot serviced.
   */
  @Override
  public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);

    String realm = getStringOptionValue(IArgument.REALM_NAME);
    List<String> appNames = (List) rc.getOption(PARAM_APPL_NAMES);
    String[] param = {realm};
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_DELETE_APPLICATIONS", param);

    Subject adminSubject = getAdminSubject();
    try {
      for (String a : appNames) {
        ApplicationManager.deleteApplication(adminSubject, "/", a);
      }
      IOutput writer = getOutputWriter();
      writer.printlnMessage(
          MessageFormat.format(
              getResourceString("delete-applications-succeeded"), (Object[]) param));
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_DELETE_APPLICATIONS", param);
    } catch (EntitlementException e) {
      String[] params = {realm, e.getMessage()};
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_DELETE_APPLICATIONS", params);
      throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
  }
  /**
   * Services a Commandline Request.
   *
   * @param rc Request Context.
   * @throws CLIException if the request cannot serviced.
   */
  @Override
  public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    String name = getStringOptionValue(PARAM_NAME);
    String[] params = {realm, name};
    Set<SubjectImplementation> newSubjects = getSubjects(rc);
    boolean bAdd = isOptionSet(PARAM_ADD);

    Subject userSubject = SubjectUtils.createSubject(getAdminSSOToken());
    ApplicationPrivilegeManager apm = ApplicationPrivilegeManager.getInstance(realm, userSubject);
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_UPDATE_APPLICATION_PRIVILEGE", params);

    try {
      ApplicationPrivilege appPrivilege = apm.getPrivilege(name);
      Set<SubjectImplementation> origSubjects = appPrivilege.getSubjects();
      Set<SubjectImplementation> subjects =
          (bAdd) ? mergeSubjects(origSubjects, newSubjects) : newSubjects;
      appPrivilege.setSubject(subjects);
      apm.replacePrivilege(appPrivilege);

      Object[] msgParam = {name};
      getOutputWriter()
          .printlnMessage(
              MessageFormat.format(
                  getResourceString("update-application-privilege-succeeded"), msgParam));
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_UPDATE_APPLICATION_PRIVILEGE", params);
    } catch (EntitlementException ex) {
      String[] paramExs = {realm, name, ex.getMessage()};
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_UPDATE_APPLICATION_PRIVILEGE", paramExs);
      throw new CLIException(ex, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
  }
  /**
   * Services a Commandline Request.
   *
   * @param rc Request Context.
   * @throw CLIException if the request cannot serviced.
   */
  @Override
  public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    String name = getStringOptionValue(PARAM_NAME);
    String[] params = {realm, name};

    Subject userSubject = SubjectUtils.createSubject(getAdminSSOToken());
    ApplicationPrivilegeManager apm = ApplicationPrivilegeManager.getInstance(realm, userSubject);
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_SHOW_APPLICATION_PRIVILEGE", params);

    try {
      ApplicationPrivilege appPrivilege = apm.getPrivilege(name);
      outputInfo("show-application-privilege-output-name", name);
      String description = appPrivilege.getDescription();
      if (description == null) {
        description = "";
      }
      outputInfo("show-application-privilege-output-description", description);
      outputInfo("show-application-privilege-output-actions", getDisplayAction(appPrivilege));
      outputInfo("show-application-privilege-output-subjects", getSubjects(appPrivilege));
      outputInfo(
          "show-application-privilege-output-resources", getApplicationToResources(appPrivilege));
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_SHOW_APPLICATION_PRIVILEGE", params);
    } catch (EntitlementException ex) {
      String[] paramExs = {realm, name, ex.getMessage()};
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_SHOW_APPLICATION_PRIVILEGE", paramExs);
      throw new CLIException(ex, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
  }
  /**
   * Services a Commandline Request.
   *
   * @param rc Request Context.
   * @throws CLIException if the request cannot serviced.
   */
  @Override
  public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    String realm = getStringOptionValue(IArgument.REALM_NAME);
    String name = getStringOptionValue(PARAM_NAME);
    String[] params = {realm, name};

    String description = getStringOptionValue(PARAM_DESCRIPTION);
    boolean hasDescription = (description != null) && description.trim().length() > 0;
    String actions = getStringOptionValue(PARAM_ACTIONS);
    ApplicationPrivilege.PossibleAction action = (actions != null) ? getActions() : null;

    if (!hasDescription && (action == null)) {
      throw new CLIException(
          getResourceString("update-application-privilege-invalid"),
          ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }

    Subject userSubject = SubjectUtils.createSubject(getAdminSSOToken());
    ApplicationPrivilegeManager apm = ApplicationPrivilegeManager.getInstance(realm, userSubject);
    writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_UPDATE_APPLICATION_PRIVILEGE", params);
    try {
      ApplicationPrivilege appPrivilege = apm.getPrivilege(name);

      if (hasDescription) {
        appPrivilege.setDescription(description);
      }
      if (action != null) {
        appPrivilege.setActionValues(action);
      }

      apm.replacePrivilege(appPrivilege);

      Object[] msgParam = {name};
      getOutputWriter()
          .printlnMessage(
              MessageFormat.format(
                  getResourceString("update-application-privilege-succeeded"), msgParam));
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_UPDATE_APPLICATION_PRIVILEGE", params);
    } catch (EntitlementException ex) {
      String[] paramExs = {realm, name, ex.getMessage()};
      writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_UPDATE_APPLICATION_PRIVILEGE", paramExs);
      throw new CLIException(ex, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    }
  }