Esempio n. 1
0
  public boolean connect() throws InterruptedException {
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(TwitterApp.CONSUMER_KEY, TwitterApp.CONSUMER_SECRET);
    Session session = HibernateUtil.openSession();
    while (true) {
      try {
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = getAccessToken(session, TwitterApp.TWITTER_USER);
        while (null == accessToken) {
          Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
          String pin =
              JOptionPane.showInputDialog(
                  null, "Insert OAuth Twitter PIN", "Twitter", JOptionPane.ERROR_MESSAGE);
          accessToken = twitter.getOAuthAccessToken(requestToken, pin);
          if (accessToken != null)
            saveAccessToken(session, accessToken.getToken(), accessToken.getTokenSecret());
        }

        twitter =
            new TwitterFactory()
                .getOAuthAuthorizedInstance(
                    TwitterApp.CONSUMER_KEY, TwitterApp.CONSUMER_SECRET, accessToken);
      } catch (Exception e) {
        System.err.println(e.getMessage());
        session.close();
        Thread.sleep(60000);
        return false;
      }
      session.close();
      return true;
    }
  }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String action = req.getParameter(Pages.PARAM_ACTION);

    if (action == null) {
      resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
      return;
    }

    Twitter twitter = new Twitter();
    twitter.setOAuthConsumer(
        ApplicationProperty.read(ApplicationProperty.CONSUMER_KEY),
        ApplicationProperty.read(ApplicationProperty.CONSUMER_SECRET));
    log.warning(
        "action="
            + action
            + ", Using consumer key "
            + ApplicationProperty.read(ApplicationProperty.CONSUMER_KEY)
            + ", isTesting()="
            + ApplicationProperty.isTesting());
    try {
      User user = AuthFilter.getCurrentUser(req).getOpenId();

      if (ApplicationProperty.isTesting() && action.equalsIgnoreCase(Pages.PARAM_ACTION_ADD)) {
        saveToken("token", "tokenSecret", "papaya_" + (new Date().getTime()), user);
        resp.sendRedirect(Pages.PAGE_MAIN);
      } else if (action.equalsIgnoreCase(Pages.PARAM_ACTION_ADD)) {
        RequestToken requestToken = twitter.getOAuthRequestToken();
        log.warning(
            "requestToken="
                + requestToken
                + ", token="
                + requestToken.getToken()
                + ", secret="
                + requestToken.getTokenSecret()
                + ", url="
                + requestToken.getAuthorizationURL());
        resp.addCookie(new Cookie(COOKIE_TOKEN, requestToken.getToken()));
        resp.addCookie(new Cookie(COOKIE_TOKEN_SECRET, requestToken.getTokenSecret()));
        resp.sendRedirect(requestToken.getAuthorizationURL());
      } else if (action.equalsIgnoreCase(Pages.PARAM_OAUTH)) {
        String token = null, tokenSecret = null;
        Cookie[] cookies = req.getCookies();
        log.warning("back from twitter, req=" + req.getQueryString());
        for (Cookie cookie : cookies) {
          if (cookie.getName().equals(COOKIE_TOKEN)) {
            token = cookie.getValue();
          }
          if (cookie.getName().equals(COOKIE_TOKEN_SECRET)) {
            tokenSecret = cookie.getValue();
          }
        }
        AccessToken accessToken = twitter.getOAuthAccessToken(token, tokenSecret);
        if (accessToken == null) {
          log.warning(
              "twitter token cann't be retrieved for token=" + token + ", secret=" + tokenSecret);
        }

        this.saveToken(accessToken, user);
        resp.sendRedirect(Pages.PAGE_MAIN);
      } else if (action.equalsIgnoreCase(Pages.PARAM_ACTION_DELETE)) {
        this.deleteToken(req.getParameter(Pages.PARAM_SCREENNAME), user);
        resp.getWriter().write("Delete Successful");
      } else {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
      }
    } catch (TwitterException e) {
      e.printStackTrace(resp.getWriter());
    } catch (Exception e) {
      e.printStackTrace(resp.getWriter());
    }
  }