/**
   * Endpoint for getting SSO Sessions in JSON format.
   *
   * @param type the type
   * @return the sso sessions
   */
  @RequestMapping(value = "/getSsoSessions", method = RequestMethod.GET)
  @ResponseBody
  public Map<String, Object> getSsoSessions(@RequestParam(defaultValue = "ALL") final String type) {
    final Map<String, Object> sessionsMap = new HashMap<>(1);
    final SsoSessionReportOptions option = SsoSessionReportOptions.valueOf(type);

    final Collection<Map<String, Object>> activeSsoSessions = getActiveSsoSessions(option);
    sessionsMap.put("activeSsoSessions", activeSsoSessions);

    long totalTicketGrantingTickets = 0;
    long totalProxyGrantingTickets = 0;
    long totalUsageCount = 0;

    final Set<String> uniquePrincipals = new HashSet<>();

    for (final Map<String, Object> activeSsoSession : activeSsoSessions) {

      if (activeSsoSession.containsKey(SsoSessionAttributeKeys.IS_PROXIED.toString())) {
        final Boolean isProxied =
            Boolean.valueOf(
                activeSsoSession.get(SsoSessionAttributeKeys.IS_PROXIED.toString()).toString());
        if (isProxied) {
          totalProxyGrantingTickets++;
        } else {
          totalTicketGrantingTickets++;
          final String principal =
              activeSsoSession
                  .get(SsoSessionAttributeKeys.AUTHENTICATED_PRINCIPAL.toString())
                  .toString();
          uniquePrincipals.add(principal);
        }
      } else {
        totalTicketGrantingTickets++;
        final String principal =
            activeSsoSession
                .get(SsoSessionAttributeKeys.AUTHENTICATED_PRINCIPAL.toString())
                .toString();
        uniquePrincipals.add(principal);
      }
      totalUsageCount +=
          Long.parseLong(
              activeSsoSession.get(SsoSessionAttributeKeys.NUMBER_OF_USES.toString()).toString());
    }

    sessionsMap.put("totalProxyGrantingTickets", totalProxyGrantingTickets);
    sessionsMap.put("totalTicketGrantingTickets", totalTicketGrantingTickets);
    sessionsMap.put("totalTickets", totalTicketGrantingTickets + totalProxyGrantingTickets);
    sessionsMap.put("totalPrincipals", uniquePrincipals.size());
    sessionsMap.put("totalUsageCount", totalUsageCount);
    return sessionsMap;
  }
  /**
   * Gets sso sessions.
   *
   * @param option the option
   * @return the sso sessions
   */
  private Collection<Map<String, Object>> getActiveSsoSessions(
      final SsoSessionReportOptions option) {
    final Collection<Map<String, Object>> activeSessions = new ArrayList<>();
    final ISOStandardDateFormat dateFormat = new ISOStandardDateFormat();

    for (final Ticket ticket : getNonExpiredTicketGrantingTickets()) {
      final TicketGrantingTicket tgt = (TicketGrantingTicket) ticket;

      if (option == SsoSessionReportOptions.DIRECT && tgt.getProxiedBy() != null) {
        continue;
      }

      final Authentication authentication = tgt.getAuthentication();
      final Principal principal = authentication.getPrincipal();

      final Map<String, Object> sso = new HashMap<>(SsoSessionAttributeKeys.values().length);
      sso.put(SsoSessionAttributeKeys.AUTHENTICATED_PRINCIPAL.toString(), principal.getId());
      sso.put(
          SsoSessionAttributeKeys.AUTHENTICATION_DATE.toString(),
          authentication.getAuthenticationDate());
      sso.put(
          SsoSessionAttributeKeys.AUTHENTICATION_DATE_FORMATTED.toString(),
          dateFormat.format(authentication.getAuthenticationDate()));
      sso.put(SsoSessionAttributeKeys.NUMBER_OF_USES.toString(), tgt.getCountOfUses());
      sso.put(SsoSessionAttributeKeys.TICKET_GRANTING_TICKET.toString(), tgt.getId());
      sso.put(SsoSessionAttributeKeys.PRINCIPAL_ATTRIBUTES.toString(), principal.getAttributes());
      sso.put(
          SsoSessionAttributeKeys.AUTHENTICATION_ATTRIBUTES.toString(),
          authentication.getAttributes());

      if (option != SsoSessionReportOptions.DIRECT) {
        if (tgt.getProxiedBy() != null) {
          sso.put(SsoSessionAttributeKeys.IS_PROXIED.toString(), Boolean.TRUE);
          sso.put(SsoSessionAttributeKeys.PROXIED_BY.toString(), tgt.getProxiedBy().getId());
        } else {
          sso.put(SsoSessionAttributeKeys.IS_PROXIED.toString(), Boolean.FALSE);
        }
      }

      sso.put(SsoSessionAttributeKeys.AUTHENTICATED_SERVICES.toString(), tgt.getServices());

      activeSessions.add(sso);
    }
    return activeSessions;
  }