// Read access token from DB and display error message if it's not available
  protected AccessToken loadAccessTokenOrRedirectToObtainIt(
      String username, OAuthProvider oauthProvider, RenderRequest request, RenderResponse response)
      throws IOException, PortletException {
    // Try requestContext first. Otherwise obtain OAuthProvider via API
    AccessToken accessToken = requestContext.getAccessToken(oauthProviderKey);
    if (accessToken == null) {
      accessToken = oauthProvider.loadAccessToken(username);
    }

    if (accessToken == null) {
      // Will be processed by method AbstractSocialPortlet.actionRedirectToOAuthFlow
      PortletURL actionURL = response.createActionURL();
      actionURL.setParameter(
          ActionRequest.ACTION_NAME, AbstractSocialPortlet.ACTION_OAUTH_REDIRECT);

      request.setAttribute(
          ATTRIBUTE_ERROR_MESSAGE,
          oauthProvider.getFriendlyName() + " access token not available for you.");
      request.setAttribute(ATTRIBUTE_OAUTH_PROVIDER, oauthProvider);
      PortletRequestDispatcher prd =
          filterConfig.getPortletContext().getRequestDispatcher("/jsp/error/token.jsp");
      prd.include(request, response);
    }

    return accessToken;
  }
  @Override
  public void doFilter(ActionRequest request, ActionResponse response, FilterChain chain)
      throws IOException, PortletException {
    String username = request.getRemoteUser();
    OAuthProvider oauthProvider = getOAuthProvider();
    AccessToken accessToken;
    if (username != null && oauthProvider != null) {
      accessToken = oauthProvider.loadAccessToken(username);
    } else {
      accessToken = null;
    }

    if (oauthProvider != null) {
      requestContext.saveOAuthInfo(oauthProvider, accessToken);
    }

    chain.doFilter(request, response);
  }
  // Validate obtained access token with usage of concrete OAuthProvider and save it to session if
  // it's valid
  protected AccessToken validateAccessToken(
      PortletRequest request,
      PortletResponse response,
      OAuthProvider oauthProvider,
      AccessToken accessToken)
      throws PortletException, IOException {
    AccessToken previousAccessToken =
        (AccessToken) request.getPortletSession().getAttribute(ATTRIBUTE_ACCESS_TOKEN);

    if (isValidationNeeded(accessToken, previousAccessToken)) {
      // Validate accessToken
      try {
        accessToken = getOAuthProvider().validateTokenAndUpdateScopes(accessToken);
      } catch (OAuthApiException oe) {
        String jspPage;
        if (oe.getExceptionCode() == OAuthApiExceptionCode.ACCESS_TOKEN_ERROR) {
          request.setAttribute(
              ATTRIBUTE_ERROR_MESSAGE,
              oauthProvider.getFriendlyName() + " access token is invalid.");
          request.setAttribute(ATTRIBUTE_OAUTH_PROVIDER, oauthProvider);
          jspPage = "/jsp/error/token.jsp";
        } else if (oe.getExceptionCode() == OAuthApiExceptionCode.IO_ERROR) {
          oe.printStackTrace();
          request.setAttribute(
              ATTRIBUTE_ERROR_MESSAGE, "I/O error happened. See server.log for more details");
          jspPage = "/jsp/error/error.jsp";
        } else {
          // Some unexpected error
          throw new PortletException(oe);
        }

        PortletRequestDispatcher prd =
            filterConfig.getPortletContext().getRequestDispatcher(jspPage);
        prd.include(request, response);
        return null;
      }

      if (!accessToken.equals(previousAccessToken)) {
        saveAccessToken(request, response, accessToken);
      }
    }

    return accessToken;
  }