public void testUserAsJSON() throws Exception {
    // single User
    HttpClientWrapper http = new HttpClientWrapper();
    JSONObject json = getJSONObjectFromClassPath("/dao/user.json");
    User user = new UserJSONImpl(json);
    Assert.assertTrue(user.isGeoEnabled());
    Assert.assertFalse(user.isVerified());
    Assert.assertEquals(id1.screenName, user.getName());
    Assert.assertEquals(id1.screenName, user.getScreenName());
    Assert.assertNotNull(user.getLocation());
    Assert.assertNotNull(user.getDescription());
    Assert.assertNotNull(user.getProfileImageURL());
    Assert.assertNotNull(user.getURL());
    Assert.assertFalse(user.isProtected());

    Assert.assertTrue(0 <= user.getFavouritesCount());
    Assert.assertTrue(0 <= user.getFollowersCount());
    Assert.assertTrue(0 <= user.getFriendsCount());
    Assert.assertNotNull(user.getCreatedAt());
    Assert.assertNotNull(user.getTimeZone());
    Assert.assertNotNull(user.getProfileBackgroundImageURL());

    Assert.assertTrue(0 <= user.getStatusesCount());
    Assert.assertNotNull(user.getProfileBackgroundColor());
    Assert.assertNotNull(user.getProfileTextColor());
    Assert.assertNotNull(user.getProfileLinkColor());
    Assert.assertNotNull(user.getProfileSidebarBorderColor());
    Assert.assertNotNull(user.getProfileSidebarFillColor());
    Assert.assertNotNull(user.getProfileTextColor());

    Assert.assertTrue(1 < user.getFollowersCount());
    Assert.assertNotNull(user.getStatus().getCreatedAt());
    Assert.assertNotNull(user.getStatus().getText());
    Assert.assertNotNull(user.getStatus().getSource());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertEquals(-1, user.getStatus().getInReplyToStatusId());
    Assert.assertEquals(-1, user.getStatus().getInReplyToUserId());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertNull(user.getStatus().getInReplyToScreenName());
    assertDeserializedFormIsEqual(user);
    Assert.assertTrue(0 <= user.getListedCount());
    List<User> users;

    // User list
    users =
        UserJSONImpl.createUserList(
            http.get("http://twitter4j.org/en/testcases/statuses/followers/T4J_hudson.json"), conf);
    Assert.assertTrue(users.size() > 0);
    assertDeserializedFormIsEqual(users);
  }
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    if (!super.equals(o)) return false;

    TwitterStreamImpl that = (TwitterStreamImpl) o;

    if (handler != null ? !handler.equals(that.handler) : that.handler != null) return false;
    if (http != null ? !http.equals(that.http) : that.http != null) return false;
    if (lifeCycleListeners != null
        ? !lifeCycleListeners.equals(that.lifeCycleListeners)
        : that.lifeCycleListeners != null) return false;
    if (rawStreamListeners != null
        ? !rawStreamListeners.equals(that.rawStreamListeners)
        : that.rawStreamListeners != null) return false;
    if (siteStreamsListeners != null
        ? !siteStreamsListeners.equals(that.siteStreamsListeners)
        : that.siteStreamsListeners != null) return false;
    if (stallWarningsGetParam != null
        ? !stallWarningsGetParam.equals(that.stallWarningsGetParam)
        : that.stallWarningsGetParam != null) return false;
    if (stallWarningsParam != null
        ? !stallWarningsParam.equals(that.stallWarningsParam)
        : that.stallWarningsParam != null) return false;
    if (statusListeners != null
        ? !statusListeners.equals(that.statusListeners)
        : that.statusListeners != null) return false;
    if (userStreamListeners != null
        ? !userStreamListeners.equals(that.userStreamListeners)
        : that.userStreamListeners != null) return false;

    return true;
  }
 /** {@inheritDoc} */
 public AccessToken getOAuthAccessToken() throws TwitterException {
   ensureTokenIsAvailable();
   if (oauthToken instanceof AccessToken) {
     return (AccessToken) oauthToken;
   }
   oauthToken = new AccessToken(http.post(conf.getOAuthAccessTokenURL(), this));
   return (AccessToken) oauthToken;
 }
 /** {@inheritDoc} */
 public RequestToken getOAuthRequestToken(String callbackURL) throws TwitterException {
   HttpParameter[] params =
       null != callbackURL
           ? new HttpParameter[] {new HttpParameter("oauth_callback", callbackURL)}
           : new HttpParameter[0];
   oauthToken = new RequestToken(http.post(conf.getOAuthRequestTokenURL(), params, this), this);
   return (RequestToken) oauthToken;
 }
 @Override
 public int hashCode() {
   int result = super.hashCode();
   result = 31 * result + (http != null ? http.hashCode() : 0);
   result = 31 * result + (streamListeners != null ? Arrays.hashCode(streamListeners) : 0);
   result = 31 * result + (lifeCycleListeners != null ? lifeCycleListeners.hashCode() : 0);
   result = 31 * result + (handler != null ? handler.hashCode() : 0);
   return result;
 }
 /** {@inheritDoc} */
 public StatusStream getSampleStream() throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     return new StatusStreamImpl(
         getDispatcher(), http.get(conf.getStreamBaseURL() + "statuses/sample.json", auth), conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
 /** {@inheritDoc} */
 public AccessToken getOAuthAccessToken(String oauthVerifier) throws TwitterException {
   ensureTokenIsAvailable();
   oauthToken =
       new AccessToken(
           http.post(
               conf.getOAuthAccessTokenURL(),
               new HttpParameter[] {new HttpParameter("oauth_verifier", oauthVerifier)},
               this));
   return (AccessToken) oauthToken;
 }
 InputStream getSiteStream(boolean withFollowings, long[] follow) throws TwitterException {
   ensureOAuthEnabled();
   return http.post(
           conf.getSiteStreamBaseURL() + "site.json",
           new HttpParameter[] {
             new HttpParameter("with", withFollowings ? "followings" : "user"),
             new HttpParameter("follow", T4JInternalStringUtil.join(follow))
           },
           auth)
       .asStream();
 }
 /** {@inheritDoc} */
 public StatusStream getFilterStream(FilterQuery query) throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     return new StatusStreamImpl(
         getDispatcher(),
         http.post(
             conf.getStreamBaseURL() + "statuses/filter.json", query.asHttpParameterArray(), auth),
         conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
Beispiel #10
0
 /** {@inheritDoc} */
 public StatusStream getRetweetStream() throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     return new StatusStreamImpl(
         getDispatcher(),
         http.post(
             conf.getStreamBaseURL() + "statuses/retweet.json", new HttpParameter[] {}, auth),
         conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
 @Override
 public int hashCode() {
   int result = super.hashCode();
   result = 31 * result + (http != null ? http.hashCode() : 0);
   result = 31 * result + (lifeCycleListeners != null ? lifeCycleListeners.hashCode() : 0);
   result = 31 * result + (handler != null ? handler.hashCode() : 0);
   result = 31 * result + (stallWarningsGetParam != null ? stallWarningsGetParam.hashCode() : 0);
   result = 31 * result + (stallWarningsParam != null ? stallWarningsParam.hashCode() : 0);
   result = 31 * result + (userStreamListeners != null ? userStreamListeners.hashCode() : 0);
   result = 31 * result + (statusListeners != null ? statusListeners.hashCode() : 0);
   result = 31 * result + (siteStreamsListeners != null ? siteStreamsListeners.hashCode() : 0);
   result = 31 * result + (rawStreamListeners != null ? rawStreamListeners.hashCode() : 0);
   return result;
 }
Beispiel #12
0
 private StatusStream getCountStream(String relativeUrl, int count) throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     return new StatusStreamImpl(
         getDispatcher(),
         http.post(
             conf.getStreamBaseURL() + relativeUrl,
             new HttpParameter[] {new HttpParameter("count", String.valueOf(count))},
             auth),
         conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
  public String upload() throws TwitterException {
    preUpload();
    if (this.postParameter == null) {
      throw new AssertionError("Incomplete implementation. postParameter is not set.");
    }
    if (this.uploadUrl == null) {
      throw new AssertionError("Incomplete implementation. uploadUrl is not set.");
    }

    httpResponse = client.post(uploadUrl, postParameter, headers);

    String mediaUrl = postUpload();
    logger.debug("uploaded url [" + mediaUrl + "]");

    return mediaUrl;
  }
Beispiel #14
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    if (!super.equals(o)) return false;

    TwitterStreamImpl that = (TwitterStreamImpl) o;

    if (handler != null ? !handler.equals(that.handler) : that.handler != null) return false;
    if (http != null ? !http.equals(that.http) : that.http != null) return false;
    if (lifeCycleListeners != null
        ? !lifeCycleListeners.equals(that.lifeCycleListeners)
        : that.lifeCycleListeners != null) return false;
    if (!Arrays.equals(streamListeners, that.streamListeners)) return false;

    return true;
  }
 /**
  * Retrieves an access token associated with the supplied screen name and password using xAuth.
  * <br>
  * In order to get access acquire AccessToken using xAuth, you must apply by sending an email to
  * [email protected] — all other applications will receive an HTTP 401 error. Web-based applications
  * will not be granted access, except on a temporary basis for when they are converting from
  * basic-authentication support to full OAuth support.<br>
  * Storage of Twitter usernames and passwords is forbidden. By using xAuth, you are required to
  * store only access tokens and access token secrets. If the access token expires or is expunged
  * by a user, you must ask for their login and password again before exchanging the credentials
  * for an access token.
  *
  * @param screenName the screen name
  * @param password the password
  * @return access token associated with the supplied request token.
  * @throws TwitterException when Twitter service or network is unavailable, or the user has not
  *     authorized
  * @see <a href="http://dev.twitter.com/pages/oauth_faq">OAuth FAQ | dev.twitter.com - How long
  *     does an access token last?</a>
  * @see <a
  *     href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-oauth-access_token-for-xAuth">Twitter
  *     REST API Method: oauth access_token for xAuth</a>
  * @since Twitter 2.1.1
  */
 public AccessToken getOAuthAccessToken(String screenName, String password)
     throws TwitterException {
   try {
     oauthToken =
         new AccessToken(
             http.post(
                 conf.getOAuthAccessTokenURL(),
                 new HttpParameter[] {
                   new HttpParameter("x_auth_username", screenName),
                   new HttpParameter("x_auth_password", password),
                   new HttpParameter("x_auth_mode", "client_auth")
                 },
                 this));
     return (AccessToken) oauthToken;
   } catch (TwitterException te) {
     throw new TwitterException(
         "The screen name / password combination seems to be invalid.", te, te.getStatusCode());
   }
 }
Beispiel #16
0
 /** {@inheritDoc} */
 public UserStream getUserStream(String[] track) throws TwitterException {
   ensureAuthorizationEnabled();
   try {
     List<HttpParameter> params = new ArrayList<HttpParameter>();
     if (conf.isUserStreamRepliesAllEnabled()) {
       params.add(new HttpParameter("replies", "all"));
     }
     if (null != track) {
       params.add(new HttpParameter("track", T4JInternalStringUtil.join(track)));
     }
     return new UserStreamImpl(
         getDispatcher(),
         http.post(
             conf.getUserStreamBaseURL() + "user.json",
             params.toArray(new HttpParameter[params.size()]),
             auth),
         conf);
   } catch (IOException e) {
     throw new TwitterException(e);
   }
 }
 private static JSONObject getJSONObjectFromPostURL(String url, Configuration conf)
     throws Exception {
   HttpClientWrapper http = new HttpClientWrapper(conf);
   return http.post(url).asJSONObject();
 }
 private JSONArray getJSONArrayFromGetURL(String url, Configuration conf) throws Exception {
   HttpClientWrapper http = new HttpClientWrapper(conf);
   return http.get(url, getOAuthOuthorization(conf)).asJSONArray();
 }