@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);
  }
  @Override
  public void doFilter(RenderRequest request, RenderResponse response, FilterChain chain)
      throws IOException, PortletException {
    String username = request.getRemoteUser();

    if (username == null) {
      request.setAttribute(
          ATTRIBUTE_ERROR_MESSAGE,
          "No content available for anonymous user. You need to login first.");
      PortletRequestDispatcher prd =
          filterConfig.getPortletContext().getRequestDispatcher("/jsp/error/error.jsp");
      prd.include(request, response);
      return;
    }

    OAuthProvider oauthProvider = getOAuthProvider();

    if (oauthProvider == null) {
      String errorMessage = "OAuth provider '" + oauthProviderKey + "' not available";
      request.setAttribute(ATTRIBUTE_ERROR_MESSAGE, errorMessage);
      PortletRequestDispatcher prd =
          filterConfig.getPortletContext().getRequestDispatcher("/jsp/error/error.jsp");
      prd.include(request, response);
      return;
    }

    AccessToken accessToken =
        loadAccessTokenOrRedirectToObtainIt(username, oauthProvider, request, response);
    if (accessToken != null) {
      accessToken = validateAccessToken(request, response, oauthProvider, accessToken);
      if (accessToken != null) {
        requestContext.saveOAuthInfo(oauthProvider, accessToken);
        chain.doFilter(request, response);
      }
    }
  }