private boolean dealWithFacebookPrincipal(
      HttpServletRequest request, HttpServletResponse response, Principal principal)
      throws IOException {
    SocialRequestWrapper requestWrapper = (SocialRequestWrapper) request;
    requestWrapper.setUserPrincipal(principal);

    request.getSession().setAttribute("STATE", STATES.FINISH.name());

    return true;
  }
  private boolean dealWithOpenIDPrincipal(
      HttpServletRequest request, HttpServletResponse response, Principal principal)
      throws IOException {
    HttpSession httpSession = request.getSession();

    SocialRequestWrapper requestWrapper = (SocialRequestWrapper) request;
    requestWrapper.setUserPrincipal(principal);

    if (trace) log.trace("Logged in as:" + principal);
    httpSession.setAttribute("STATE", STATES.FINISH.name());
    return true;
  }
  protected boolean processOpenID(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    Principal userPrincipal = request.getUserPrincipal();
    if (userPrincipal != null) {
      if (trace) log.trace("Logged in as:" + userPrincipal);
      return true;
    }

    if (!openidProcessor.isInitialized()) {
      try {
        openidProcessor.initialize(roles);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    HttpSession httpSession = request.getSession();
    String state = (String) httpSession.getAttribute("STATE");
    if (trace) log.trace("state=" + state);

    if (STATES.FINISH.name().equals(state)) {
      // This is a replay. We need to resend a request back to the OpenID provider
      httpSession.setAttribute("STATE", STATES.AUTH.name());

      return prepareAndSendAuthRequest(request, response);
    }

    if (state == null || state.isEmpty()) {
      return prepareAndSendAuthRequest(request, response);
    }
    // We have sent an auth request
    if (state.equals(STATES.AUTH.name())) {
      Principal principal = processIncomingAuthResult(request, response);

      if (principal == null) {
        log.error(
            "Principal was null. Maybe login modules need to be configured properly. Or user chose no data");
        return false;
      }

      return dealWithOpenIDPrincipal(request, response, principal);
    }
    return false;
  }
  protected boolean processFacebook(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    HttpSession session = request.getSession();
    String state = (String) session.getAttribute("STATE");

    if (STATES.FINISH.name().equals(state)) {
      Principal principal = request.getUserPrincipal();
      if (principal == null) {
        principal = getFacebookPrincipal(request, response);
      }
      if (principal == null) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return false;
      }
      return dealWithFacebookPrincipal(request, response, principal);
    }

    if (state == null || state.isEmpty()) {
      return initialFacebookInteraction(request, response);
    }
    // We have sent an auth request
    if (state.equals(STATES.AUTH.name())) {
      return facebookProcessor.handleAuthStage(request, response);
    }

    // Principal facebookPrincipal = null;
    if (state.equals(STATES.AUTHZ.name())) {
      Principal principal = getFacebookPrincipal(request, response);

      if (principal == null) {
        log.error(
            "Principal was null. Maybe login modules need to be configured properly. Or user chose no data");
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return false;
      }

      return dealWithFacebookPrincipal(request, response, principal);
    }
    return false;
  }