Esempio n. 1
0
 /**
  * Create an instance of Credential class.
  *
  * @param usernameOrEmail Username or e-mail.
  * @param password Password.
  * @param consumerKey Consumer key.
  * @param consumerSecret Consumer secret.
  * @throws IllegalArgumentException If any parameter is empty or null.
  */
 public Credential(
     String usernameOrEmail, String password, String consumerKey, String consumerSecret) {
   if (StringUtil.isEmpty(usernameOrEmail)) {
     throw new IllegalArgumentException("Username or e-mail must not be empty/null");
   }
   if (StringUtil.isEmpty(password)) {
     throw new IllegalArgumentException("Password must not be empty/null");
   }
   if (StringUtil.isEmpty(consumerKey)) {
     throw new IllegalArgumentException("Consumer Key must not be empty/null");
   }
   if (StringUtil.isEmpty(consumerSecret)) {
     throw new IllegalArgumentException("Consumer Secret must not be empty/null");
   }
   //
   Hashtable credtls = new Hashtable(4);
   //
   if (usernameOrEmail.indexOf('@') != -1) { // is e-mail?
     credtls.put(MetadataSet.CREDENTIAL_EMAIL, usernameOrEmail);
   } else {
     credtls.put(MetadataSet.CREDENTIAL_USERNAME, usernameOrEmail);
   }
   credtls.put(MetadataSet.CREDENTIAL_PASSWORD, password);
   credtls.put(MetadataSet.CREDENTIAL_CONSUMER_KEY, consumerKey);
   credtls.put(MetadataSet.CREDENTIAL_CONSUMER_SECRET, consumerSecret);
   //
   setData(credtls);
 }
Esempio n. 2
0
 /**
  * Populate the given hash according to the tags and their values
  *
  * @param data Hash to be populated.
  * @param path XML path.
  * @param text Tag's text.
  */
 public void populate(Hashtable data, String path, String text) {
   if (path.endsWith("/created_at")) {
     data.put(MetadataSet.TWEET_PUBLISH_DATE, "" + StringUtil.convertTweetDateToLong(text));
   } else if (path.endsWith("/id")) {
     data.put(MetadataSet.TWEET_ID, text);
   } else if (path.endsWith("/text")) {
     data.put(MetadataSet.TWEET_CONTENT, text);
   } else if (path.endsWith("/source")) {
     data.put(MetadataSet.TWEET_SOURCE, StringUtil.removeTags(text));
   } else if (path.endsWith("/favorited")) {
     data.put(MetadataSet.TWEET_FAVOURITE, text);
   } else if (path.endsWith("/in_reply_to_status_id")) {
     data.put(MetadataSet.TWEET_IN_REPLY_TO_TWEET_ID, text);
   }
 }
 /**
  * Perform an analyze of a given HttpResponse object's response-code in order to interpret whether
  * the requests to Twitter API went well. Otherwise, an exception is thrown describing the
  * problem.
  *
  * @param response HttpResponse object to be interpreted.
  * @throws IOException If an I/O or service error occurs.
  * @throws LimitExceededException If a request limit exceeded error occurs.
  * @throws InvalidQueryException If an invalid query error occurs.
  * @throws SecurityException If a security error occurs.
  * @throws IllegalArgumentException If response is null.
  */
 public static void perform(HttpResponse response) throws IOException, LimitExceededException {
   if (response == null) {
     throw new IllegalArgumentException("Response must not be null.");
   }
   //
   final int respCode = response.getCode();
   //
   if (respCode != HttpConnection.HTTP_OK && respCode != HttpConnection.HTTP_NOT_MODIFIED) {
     if (isInvalidQueryError(respCode)) {
       throw new InvalidQueryException(getErrorMessage(response));
     } else if (isLimitExceededError(respCode)) {
       String emgs = getErrorMessage(response);
       String raft = response.getResponseField("X-Rate-Limit-Reset");
       //
       if (!StringUtil.isEmpty(raft)) {
         emgs += " / Retry after " + raft + " secs.";
       }
       //
       throw new LimitExceededException(emgs);
     } else if (isSecurityError(respCode)) {
       throw new SecurityException(getErrorMessage(response));
     } else {
       throw new IOException(getErrorMessage(response));
     }
   }
 }
 /**
  * Replace the key of a given property.
  *
  * @param hashtable Hastable.
  * @param searchKey Key to be replaced.
  * @param replacementKey Replacement key.
  */
 protected void replaceProperty(Hashtable hashtable, String searchKey, String replacementKey) {
   Object value = hashtable.get(searchKey);
   //
   if (value != null) {
     hashtable.remove(searchKey);
     //
     if (value instanceof String) {
       String str = value.toString();
       //
       if (StringUtil.isEmpty(str) || "null".equals(str)) {
         return;
       }
     }
     //
     hashtable.put(replacementKey, value);
   }
 }
 /**
  * Test method for {@link
  * com.twitterapime.rest.handler.UserAccountHandler#populate(java.util.Hashtable, String,
  * String)}.
  */
 public void testPopulate() {
   Hashtable sampleRef = new Hashtable();
   sampleRef.put(
       MetadataSet.USERACCOUNT_CREATE_DATE,
       Long.toString(StringUtil.convertTweetDateToLong("Sat Nov 07 21:30:03 +0000 2009")));
   sampleRef.put(
       MetadataSet.USERACCOUNT_DESCRIPTION,
       "API for accessing Twitter's Service from mobile devices.");
   sampleRef.put(MetadataSet.USERACCOUNT_FAVOURITES_COUNT, "0");
   sampleRef.put(MetadataSet.USERACCOUNT_FOLLOWERS_COUNT, "1");
   sampleRef.put(MetadataSet.USERACCOUNT_FRIENDS_COUNT, "1");
   sampleRef.put(MetadataSet.USERACCOUNT_ID, "88275918");
   sampleRef.put(MetadataSet.USERACCOUNT_LOCATION, "Brazil");
   sampleRef.put(MetadataSet.USERACCOUNT_NAME, "Twiter API ME");
   sampleRef.put(MetadataSet.USERACCOUNT_NOTIFICATIONS, "false");
   sampleRef.put(
       MetadataSet.USERACCOUNT_PICTURE_URI,
       "http://s.twimg.com/a/1261519751/images/default_profile_3_normal.png");
   sampleRef.put(MetadataSet.USERACCOUNT_PROFILE_BACKGROUND_COLOR, "9ae4e8");
   sampleRef.put(
       MetadataSet.USERACCOUNT_PROFILE_BACKGROUND_IMAGE_URI,
       "http://s.twimg.com/a/1261519751/images/themes/theme1/bg.png");
   sampleRef.put(MetadataSet.USERACCOUNT_PROFILE_LINK_COLOR, "0000ff");
   sampleRef.put(MetadataSet.USERACCOUNT_PROFILE_TEXT_COLOR, "000000");
   sampleRef.put(MetadataSet.USERACCOUNT_PROTECTED, "false");
   sampleRef.put(MetadataSet.USERACCOUNT_TIME_ZONE, "Santiago");
   sampleRef.put(MetadataSet.USERACCOUNT_TWEETS_COUNT, "17");
   sampleRef.put(MetadataSet.USERACCOUNT_URL, "http://www.twitterapime.com");
   sampleRef.put(MetadataSet.USERACCOUNT_USER_NAME, "twiterapime");
   sampleRef.put(MetadataSet.USERACCOUNT_UTC_OFFSET, "-14400");
   sampleRef.put(MetadataSet.USERACCOUNT_VERIFIED, "false");
   //
   UserAccountHandler h = new UserAccountHandler();
   //
   Hashtable sampleTest = new Hashtable();
   h.populate(sampleTest, "/created_at", "Sat Nov 07 21:30:03 +0000 2009");
   h.populate(
       sampleTest, "/description", "API for accessing Twitter's Service from mobile devices.");
   h.populate(sampleTest, "/favourites_count", "0");
   h.populate(sampleTest, "/followers_count", "1");
   h.populate(sampleTest, "/friends_count", "1");
   h.populate(sampleTest, "/id", "88275918");
   h.populate(sampleTest, "/location", "Brazil");
   h.populate(sampleTest, "/name", "Twiter API ME");
   h.populate(sampleTest, "/notifications", "false");
   h.populate(
       sampleTest,
       "/profile_image_url",
       "http://s.twimg.com/a/1261519751/images/default_profile_3_normal.png");
   h.populate(sampleTest, "/profile_background_color", "9ae4e8");
   h.populate(
       sampleTest,
       "/profile_background_image_url",
       "http://s.twimg.com/a/1261519751/images/themes/theme1/bg.png");
   h.populate(sampleTest, "/profile_link_color", "0000ff");
   h.populate(sampleTest, "/profile_text_color", "000000");
   h.populate(sampleTest, "/protected", "false");
   h.populate(sampleTest, "/time_zone", "Santiago");
   h.populate(sampleTest, "/statuses_count", "17");
   h.populate(sampleTest, "/url", "http://www.twitterapime.com");
   h.populate(sampleTest, "/screen_name", "twiterapime");
   h.populate(sampleTest, "/utc_offset", "-14400");
   h.populate(sampleTest, "/verified", "false");
   //
   assertTrue(new UserAccount(sampleRef).equals(new UserAccount(sampleTest)));
 }
 /**
  * Verify whether the given string is null, empty or "null".
  *
  * @param str The string.
  * @return true null/empty/"null".
  */
 protected boolean isEmpty(String str) {
   return StringUtil.isEmpty(str) || "null".equals(str.trim());
 }
Esempio n. 7
0
 /**
  * Checks if the value associated to the is null or empty.
  *
  * @param key Key.
  * @return Empty (true).
  */
 public boolean isEmpty(String key) {
   Object v = data.get(key);
   //
   return v == null || StringUtil.isEmpty(v.toString());
 }