public void handleLogout(String sessionIndex) {

    HttpSession session = (HttpSession) sessionMap.get(sessionIndex);

    if (session == null) {
      // send cluster message
      sendSessionInvalidationClusterMessage(sessionIndex);
      return;
    }

    CarbonSSOSessionManager ssoSessionManager =
        SAML2SSOAuthFEDataHolder.getInstance().getCarbonSSOSessionManager();

    // mark this session as invalid.
    ssoSessionManager.makeSessionInvalid(sessionIndex);

    String username = (String) session.getAttribute(CarbonSecuredHttpContext.LOGGED_USER);
    log.info("Invalidating session for user " + username);

    // invalidating backend session
    try {
      CarbonUIAuthenticator authenticator =
          (CarbonUIAuthenticator)
              session.getAttribute(CarbonSecuredHttpContext.CARBON_AUTHNETICATOR);
      if (authenticator != null) {
        authenticator.unauthenticate(session);
        log.debug("Backend session invalidated");
      }
    } catch (Exception e) {
      log.error(e.getMessage());
    }

    // clearing front end session
    session.setAttribute("authenticated", false);
    session.removeAttribute(CarbonSecuredHttpContext.LOGGED_USER);
    session.getServletContext().removeAttribute(CarbonSecuredHttpContext.LOGGED_USER);

    removeSession(sessionIndex);

    try {
      session.invalidate();
    } catch (Exception ignored) {
      log.error(ignored.getMessage());
    }

    if (log.isDebugEnabled()) {
      log.debug("Cleared authenticated session " + session.getId());
    }
  }
  public void sendSessionInvalidationClusterMessage(String sessionIndex) {

    SessionClusterMessage clusterMessage = new SessionClusterMessage();
    clusterMessage.setMessageId(UUID.randomUUID());
    clusterMessage.setSessionIndex(sessionIndex);

    ClusteringAgent clusteringAgent =
        SAML2SSOAuthFEDataHolder.getInstance()
            .getConfigurationContextService()
            .getServerConfigContext()
            .getAxisConfiguration()
            .getClusteringAgent();

    if (clusteringAgent != null) {
      int numberOfRetries = 0;

      while (numberOfRetries < 60) {
        try {
          clusteringAgent.sendMessage(clusterMessage, true);
          log.info("Sent [" + clusterMessage + "]");
          break;
        } catch (ClusteringFault e) {
          numberOfRetries++;

          if (numberOfRetries < 60) {
            log.warn(
                "Could not send SSOSessionInvalidationClusterMessage. Retry will be attempted in 2s. Request: "
                    + clusterMessage,
                e);
          } else {
            log.error(
                "Could not send SSOSessionInvalidationClusterMessage. Several retries failed. Request:"
                    + clusterMessage,
                e);
          }

          try {
            Thread.sleep(2000);
          } catch (InterruptedException ignored) {
          }
        }
      }
    }
  }