diff --git a/src/main/java/twitter4j/DirectMessage.java b/src/main/java/twitter4j/DirectMessage.java index 80e927bce..e6ad88ef0 100644 --- a/src/main/java/twitter4j/DirectMessage.java +++ b/src/main/java/twitter4j/DirectMessage.java @@ -29,6 +29,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import twitter4j.http.Response; import java.util.ArrayList; import java.util.Date; @@ -48,12 +49,19 @@ public class DirectMessage extends TwitterResponse implements java.io.Serializab private String recipient_screen_name; private static final long serialVersionUID = -3253021825891789737L; - /*package*/DirectMessage(Element elem, Twitter twitter) throws TwitterException { - super(); + /*package*/DirectMessage(Response res, Twitter twitter) throws TwitterException { + super(res); + init(res, res.asDocument().getDocumentElement(), twitter); + } + /*package*/DirectMessage(Response res, Element elem, Twitter twitter) throws TwitterException { + super(res); + init(res, elem, twitter); + } + private void init(Response res, Element elem, Twitter twitter) throws TwitterException{ ensureRootNodeNameIs("direct_message", elem); - sender = new User((Element) elem.getElementsByTagName("sender").item(0), + sender = new User(res, (Element) elem.getElementsByTagName("sender").item(0), twitter); - recipient = new User((Element) elem.getElementsByTagName("recipient").item(0), + recipient = new User(res, (Element) elem.getElementsByTagName("recipient").item(0), twitter); id = getChildInt("id", elem); text = getChildText("text", elem); @@ -109,8 +117,9 @@ public User getRecipient() { } /*package*/ - static List constructDirectMessages(Document doc, + static List constructDirectMessages(Response res, Twitter twitter) throws TwitterException { + Document doc = res.asDocument(); if (isRootNodeNilClasses(doc)) { return new ArrayList(0); } else { @@ -122,7 +131,7 @@ static List constructDirectMessages(Document doc, List messages = new ArrayList(size); for (int i = 0; i < size; i++) { Element status = (Element) list.item(i); - messages.add(new DirectMessage(status, twitter)); + messages.add(new DirectMessage(res, status, twitter)); } return messages; } catch (TwitterException te) { @@ -135,38 +144,6 @@ static List constructDirectMessages(Document doc, } } - /* - - - 3611242 - test - 4933401 - 6459452 - Thu Jun 07 06:36:21 +0000 2007 - yusukey - fast_ts - - 4933401 - Yusuke Yamamoto - yusukey - Tokyo - http://yusuke.homeip.net/diary/ - http://assets3.twitter.com/system/user/profile_image/4933401/normal/1023824_2048059614.jpg?1176769649 - http://yusuke.homeip.net/diary/ - false - - - 6459452 - fast_ts - fast_ts - - - http://assets1.twitter.com/system/user/profile_image/6459452/normal/_____-1.gif?1180974738 - - true - - - */ @Override public int hashCode() { return id; diff --git a/src/main/java/twitter4j/ExtendedUser.java b/src/main/java/twitter4j/ExtendedUser.java index 405ba3d38..8769b1663 100644 --- a/src/main/java/twitter4j/ExtendedUser.java +++ b/src/main/java/twitter4j/ExtendedUser.java @@ -27,6 +27,7 @@ package twitter4j; import org.w3c.dom.Element; +import twitter4j.http.Response; import java.util.Date; @@ -81,8 +82,9 @@ public class ExtendedUser extends UserWithStatus { private int statusesCount; private static final long serialVersionUID = -8486230870587454252L; - public ExtendedUser(Element elem, Twitter twitter) throws TwitterException { - super(elem, twitter); + public ExtendedUser(Response res, Twitter twitter) throws TwitterException { + super(res, twitter); + Element elem = res.asDocument().getDocumentElement(); profileBackgroundColor = getChildText("profile_background_color", elem); profileTextColor = getChildText("profile_text_color", elem); profileLinkColor = getChildText("profile_link_color", elem); diff --git a/src/main/java/twitter4j/IDs.java b/src/main/java/twitter4j/IDs.java index e13f9c6af..780d21460 100644 --- a/src/main/java/twitter4j/IDs.java +++ b/src/main/java/twitter4j/IDs.java @@ -26,8 +26,9 @@ */ package twitter4j; -import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import org.w3c.dom.Element; +import twitter4j.http.Response; import java.util.Arrays; @@ -40,8 +41,9 @@ public class IDs extends TwitterResponse { private int[] ids; private static final long serialVersionUID = -6585026560164704953L; - /*package*/ IDs(Element elem) throws TwitterException { - super(); + /*package*/ IDs(Response res) throws TwitterException { + super(res); + Element elem = res.asDocument().getDocumentElement(); ensureRootNodeNameIs("ids", elem); NodeList idlist = elem.getElementsByTagName("id"); ids = new int[idlist.getLength()]; diff --git a/src/main/java/twitter4j/RateLimitStatus.java b/src/main/java/twitter4j/RateLimitStatus.java index 4d22cecd0..762df7cc4 100644 --- a/src/main/java/twitter4j/RateLimitStatus.java +++ b/src/main/java/twitter4j/RateLimitStatus.java @@ -27,6 +27,7 @@ package twitter4j; import org.w3c.dom.Element; +import twitter4j.http.Response; import java.util.Date; @@ -41,8 +42,9 @@ public class RateLimitStatus extends TwitterResponse { private Date dateTime; private static final long serialVersionUID = -2326597301406666858L; - /* package */ RateLimitStatus(Element elem) throws TwitterException { - super(); + /* package */ RateLimitStatus(Response res) throws TwitterException { + super(res); + Element elem = res.asDocument().getDocumentElement(); remainingHits = getChildInt("remaining-hits", elem); hourlyLimit = getChildInt("hourly-limit", elem); resetTimeInSeconds = getChildInt("reset-time-in-seconds", elem); diff --git a/src/main/java/twitter4j/Status.java b/src/main/java/twitter4j/Status.java index 0f550177d..3ceb7d60c 100644 --- a/src/main/java/twitter4j/Status.java +++ b/src/main/java/twitter4j/Status.java @@ -29,6 +29,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import twitter4j.http.Response; import java.util.ArrayList; import java.util.Date; @@ -71,11 +72,23 @@ public class Status extends TwitterResponse implements java.io.Serializable { private boolean isFavorited; private static final long serialVersionUID = 1608000492860584608L; - /*package*/Status(Element elem, Twitter twitter) throws TwitterException { - super(); + /*package*/Status(Response res, Twitter twitter) throws TwitterException { + super(res); + Element elem = res.asDocument().getDocumentElement(); + init(res, elem, twitter); + } + + /*package*/Status(Response res, Element elem, Twitter twitter) throws + TwitterException { + super(res); + init(res, elem, twitter); + } + + private void init(Response res, Element elem, Twitter twitter) throws + TwitterException { ensureRootNodeNameIs("status", elem); - user = new User((Element) elem.getElementsByTagName("user").item(0), - twitter); + user = new User(res, (Element) elem.getElementsByTagName("user").item(0) + , twitter); id = getChildLong("id", elem); text = getChildText("text", elem); source = getChildText("source", elem); @@ -179,8 +192,9 @@ public User getUser() { } /*package*/ - static List constructStatuses(Document doc, + static List constructStatuses(Response res, Twitter twitter) throws TwitterException { + Document doc = res.asDocument(); if (isRootNodeNilClasses(doc)) { return new ArrayList(0); } else { @@ -192,7 +206,7 @@ static List constructStatuses(Document doc, List statuses = new ArrayList(size); for (int i = 0; i < size; i++) { Element status = (Element) list.item(i); - statuses.add(new Status(status, twitter)); + statuses.add(new Status(res, status, twitter)); } return statuses; } catch (TwitterException te) { diff --git a/src/main/java/twitter4j/Twitter.java b/src/main/java/twitter4j/Twitter.java index 4d0a46a1c..ec0a7776a 100644 --- a/src/main/java/twitter4j/Twitter.java +++ b/src/main/java/twitter4j/Twitter.java @@ -530,8 +530,7 @@ public QueryResult search(Query query) throws TwitterException { public List getPublicTimeline() throws TwitterException { return Status.constructStatuses(get(baseURL + - "statuses/public_timeline.xml", false). - asDocument(), this); + "statuses/public_timeline.xml", false), this); } /** @@ -546,8 +545,8 @@ public List getPublicTimeline() throws public List getPublicTimeline(int sinceID) throws TwitterException { return Status.constructStatuses(get(baseURL + - "statuses/public_timeline.xml", null, new Paging((long)sinceID), false). - asDocument(), this); + "statuses/public_timeline.xml", null, new Paging((long) sinceID) + , false), this); } /** @@ -561,7 +560,7 @@ public List getPublicTimeline(int sinceID) throws */ public List getFriendsTimeline() throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml", true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml", true), this); } /** @@ -576,7 +575,7 @@ public List getFriendsTimeline() throws */ public List getFriendsTimelineByPage(int page) throws TwitterException { - return getFriendsTimeline(page); + return getFriendsTimeline(new Paging(page)); } /** @@ -623,7 +622,7 @@ public List getFriendsTimeline(long sinceId, int page) throws */ public List getFriendsTimeline(String id) throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/friends_timeline/" + id + ".xml", true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/friends_timeline/" + id + ".xml", true), this); } /** @@ -634,7 +633,6 @@ public List getFriendsTimeline(String id) throws * @param page the number of page * @return list of the Friends Timeline * @throws TwitterException when Twitter service or network is unavailable - * @deprecated Use getFriendsTimeline(String id, int page) instead * @see Twitter API Wiki / Twitter REST API Method: statuses friends_timeline * @deprecated Use getFriendsTimeline(String id, Paging paging) instead */ @@ -690,7 +688,7 @@ public List getFriendsTimeline(long sinceId, String id, int page) throws */ public List getFriendsTimeline(Paging paging) throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml",null, paging, true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml",null, paging, true), this); } /** @@ -706,7 +704,7 @@ public List getFriendsTimeline(Paging paging) throws */ public List getFriendsTimeline(String id, Paging paging) throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/friends_timeline/" + id + ".xml",null, paging, true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/friends_timeline/" + id + ".xml",null, paging, true), this); } @@ -723,7 +721,7 @@ public List getFriendsTimeline(String id, Paging paging) throws public List getFriendsTimeline(Date since) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml", - "since", format.format(since), true).asDocument(), this); + "since", format.format(since), true), this); } /** @@ -740,7 +738,7 @@ public List getFriendsTimeline(Date since) throws public List getFriendsTimeline(long sinceId) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/friends_timeline.xml", - "since_id", String.valueOf(sinceId), true).asDocument(), this); + "since_id", String.valueOf(sinceId), true), this); } /** @@ -757,7 +755,7 @@ public List getFriendsTimeline(long sinceId) throws public List getFriendsTimeline(String id, Date since) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/friends_timeline/" + id + ".xml", - "since", format.format(since), true).asDocument(), this); + "since", format.format(since), true), this); } /** @@ -794,7 +792,7 @@ public List getUserTimeline(String id, int count throw new IllegalArgumentException("count may not be greater than " + MAX_COUNT + " for performance purposes."); } return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", - "since", format.format(since), "count", String.valueOf(count), true).asDocument(), this); + "since", format.format(since), "count", String.valueOf(count), true), this); } /** @@ -828,7 +826,7 @@ public List getUserTimeline(String id, int count, public List getUserTimeline(String id, Paging paging) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", - null, paging, true).asDocument(), this); + null, paging, true), this); } /** @@ -844,7 +842,7 @@ public List getUserTimeline(String id, Paging paging) */ public List getUserTimeline(String id, Date since) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", - "since", format.format(since), true).asDocument(), this); + "since", format.format(since), true), this); } /** @@ -864,7 +862,7 @@ public List getUserTimeline(String id, int count) throws throw new IllegalArgumentException("count may not be greater than " + MAX_COUNT + " for performance purposes."); } return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", - "count", String.valueOf(count), true).asDocument(), this); + "count", String.valueOf(count), true), this); } /** @@ -883,7 +881,7 @@ public List getUserTimeline(int count, Date since) throws TwitterExcepti throw new IllegalArgumentException("count may not be greater than " + MAX_COUNT + " for performance purposes."); } return Status.constructStatuses(get(baseURL + "statuses/user_timeline.xml", - "since", format.format(since), "count", String.valueOf(count), true).asDocument(), this); + "since", format.format(since), "count", String.valueOf(count), true), this); } /** @@ -912,7 +910,7 @@ public List getUserTimeline(int count, long sinceId) throws TwitterExcep * @see Twitter API Wiki / Twitter REST API Method: statuses friends_timeline */ public List getUserTimeline(String id) throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/user_timeline/" + id + ".xml", true), this); } /** @@ -942,7 +940,7 @@ public List getUserTimeline(String id, long sinceId) throws TwitterExcep public List getUserTimeline() throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/user_timeline.xml" - , true).asDocument(), this); + , true), this); } /** @@ -958,7 +956,7 @@ public List getUserTimeline() throws public List getUserTimeline(Paging paging) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/user_timeline.xml" - , null, paging, true).asDocument(), this); + , null, paging, true), this); } /** @@ -987,7 +985,7 @@ public List getUserTimeline(long sinceId) throws * @see Twitter API Wiki / Twitter REST API Method: statuses mentions */ public List getReplies() throws TwitterException { - return Status.constructStatuses(get(baseURL + "statuses/replies.xml", true).asDocument(), this); + return Status.constructStatuses(get(baseURL + "statuses/replies.xml", true), this); } /** @@ -1003,7 +1001,7 @@ public List getReplies() throws TwitterException { */ public List getReplies(long sinceId) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/replies.xml", - "since_id", String.valueOf(sinceId), true).asDocument(), this); + "since_id", String.valueOf(sinceId), true), this); } /** @@ -1021,7 +1019,7 @@ public List getRepliesByPage(int page) throws TwitterException { throw new IllegalArgumentException("page should be positive integer. passed:" + page); } return Status.constructStatuses(get(baseURL + "statuses/replies.xml", - "page", String.valueOf(page), true).asDocument(), this); + "page", String.valueOf(page), true), this); } /** @@ -1040,7 +1038,7 @@ public List getReplies(int page) throws TwitterException { throw new IllegalArgumentException("page should be positive integer. passed:" + page); } return Status.constructStatuses(get(baseURL + "statuses/replies.xml", - "page", String.valueOf(page), true).asDocument(), this); + "page", String.valueOf(page), true), this); } /** @@ -1061,7 +1059,7 @@ public List getReplies(long sinceId, int page) throws TwitterException { } return Status.constructStatuses(get(baseURL + "statuses/replies.xml", "since_id", String.valueOf(sinceId), - "page", String.valueOf(page), true).asDocument(), this); + "page", String.valueOf(page), true), this); } /** @@ -1075,7 +1073,7 @@ public List getReplies(long sinceId, int page) throws TwitterException { */ public List getMentions() throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/mentions.xml", - null, true).asDocument(), this); + null, true), this); } /** @@ -1090,7 +1088,7 @@ public List getMentions() throws TwitterException { */ public List getMentions(Paging paging) throws TwitterException { return Status.constructStatuses(get(baseURL + "statuses/mentions.xml", - null, paging, true).asDocument(), this); + null, paging, true), this); } /** @@ -1121,7 +1119,7 @@ public Status show(int id) throws TwitterException { */ public Status show(long id) throws TwitterException { - return new Status(get(baseURL + "statuses/show/" + id + ".xml", false).asDocument().getDocumentElement(), this); + return new Status(get(baseURL + "statuses/show/" + id + ".xml", false), this); } /** @@ -1135,7 +1133,7 @@ public Status show(long id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: statuses show */ public Status showStatus(long id) throws TwitterException { - return new Status(get(baseURL + "statuses/show/" + id + ".xml", false).asDocument().getDocumentElement(), this); + return new Status(get(baseURL + "statuses/show/" + id + ".xml", false), this); } /** @@ -1166,7 +1164,7 @@ public Status update(String status) throws TwitterException { */ public Status updateStatus(String status) throws TwitterException { return new Status(http.post(baseURL + "statuses/update.xml", - new PostParameter[]{new PostParameter("status", status), new PostParameter("source", source)}, true).asDocument().getDocumentElement(), this); + new PostParameter[]{new PostParameter("status", status), new PostParameter("source", source)}, true), this); } /** @@ -1199,7 +1197,7 @@ public Status update(String status, long inReplyToStatusId) throws TwitterExcept */ public Status updateStatus(String status, long inReplyToStatusId) throws TwitterException { return new Status(http.post(baseURL + "statuses/update.xml", - new PostParameter[]{new PostParameter("status", status), new PostParameter("in_reply_to_status_id", String.valueOf(inReplyToStatusId)), new PostParameter("source", source)}, true).asDocument().getDocumentElement(), this); + new PostParameter[]{new PostParameter("status", status), new PostParameter("in_reply_to_status_id", String.valueOf(inReplyToStatusId)), new PostParameter("source", source)}, true), this); } /** @@ -1214,7 +1212,7 @@ public Status updateStatus(String status, long inReplyToStatusId) throws Twitter */ public Status destroyStatus(long statusId) throws TwitterException { return new Status(http.post(baseURL + "statuses/destroy/" + statusId + ".xml", - new PostParameter[0], true).asDocument().getDocumentElement(), this); + new PostParameter[0], true), this); } /** @@ -1227,7 +1225,7 @@ public Status destroyStatus(long statusId) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: users show */ public ExtendedUser getUserDetail(String id) throws TwitterException { - return new ExtendedUser(get(baseURL + "users/show/" + id + ".xml", true).asDocument().getDocumentElement(), this); + return new ExtendedUser(get(baseURL + "users/show/" + id + ".xml", true), this); } /* User Methods */ @@ -1241,7 +1239,7 @@ public ExtendedUser getUserDetail(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: statuses friends */ public List getFriends() throws TwitterException { - return User.constructUsers(get(baseURL + "statuses/friends.xml", true).asDocument(), this); + return User.constructUsers(get(baseURL + "statuses/friends.xml", true), this); } /** @@ -1256,7 +1254,7 @@ public List getFriends() throws TwitterException { */ public List getFriends(Paging paging) throws TwitterException { return User.constructUsers(get(baseURL + "statuses/friends.xml", null, - paging, true).asDocument(), this); + paging, true), this); } /** @@ -1284,7 +1282,7 @@ public List getFriends(int page) throws TwitterException { */ public List getFriends(String id) throws TwitterException { return User.constructUsers(get(baseURL + "statuses/friends/" + id + ".xml" - , true).asDocument(), this); + , true), this); } /** @@ -1300,7 +1298,7 @@ public List getFriends(String id) throws TwitterException { */ public List getFriends(String id, Paging paging) throws TwitterException { return User.constructUsers(get(baseURL + "statuses/friends/" + id + ".xml" - , null, paging, true).asDocument(), this); + , null, paging, true), this); } /** @@ -1327,7 +1325,7 @@ public List getFriends(String id, int page) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: statuses followers */ public List getFollowers() throws TwitterException { - return User.constructUsers(get(baseURL + "statuses/followers.xml", true).asDocument(), this); + return User.constructUsers(get(baseURL + "statuses/followers.xml", true), this); } /** @@ -1342,7 +1340,7 @@ public List getFollowers() throws TwitterException { */ public List getFollowers(Paging paging) throws TwitterException { return User.constructUsers(get(baseURL + "statuses/followers.xml", null - , paging, true).asDocument(), this); + , paging, true), this); } /** @@ -1371,7 +1369,7 @@ public List getFollowers(int page) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: statuses followers */ public List getFollowers(String id) throws TwitterException { - return User.constructUsers(get(baseURL + "statuses/followers/" + id + ".xml", true).asDocument(), this); + return User.constructUsers(get(baseURL + "statuses/followers/" + id + ".xml", true), this); } /** @@ -1387,7 +1385,7 @@ public List getFollowers(String id) throws TwitterException { */ public List getFollowers(String id, Paging paging) throws TwitterException { return User.constructUsers(get(baseURL + "statuses/followers/" + id + - ".xml", null, paging, true).asDocument(), this); + ".xml", null, paging, true), this); } /** @@ -1413,7 +1411,7 @@ public List getFollowers(String id, int page) throws TwitterException { * @throws TwitterException when Twitter service or network is unavailable */ public List getFeatured() throws TwitterException { - return User.constructUsers(get(baseURL + "statuses/featured.xml", true).asDocument(), this); + return User.constructUsers(get(baseURL + "statuses/featured.xml", true), this); } /** @@ -1425,7 +1423,7 @@ public List getFeatured() throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: direct_messages */ public List getDirectMessages() throws TwitterException { - return DirectMessage.constructDirectMessages(get(baseURL + "direct_messages.xml", true).asDocument(), this); + return DirectMessage.constructDirectMessages(get(baseURL + "direct_messages.xml", true), this); } /** @@ -1439,7 +1437,7 @@ public List getDirectMessages() throws TwitterException { */ public List getDirectMessages(Paging paging) throws TwitterException { return DirectMessage.constructDirectMessages(get(baseURL - + "direct_messages.xml", null, paging, true).asDocument(), this); + + "direct_messages.xml", null, paging, true), this); } /** @@ -1500,7 +1498,7 @@ public List getDirectMessages(int sinceId) throws TwitterExceptio public List getDirectMessages(Date since) throws TwitterException { return DirectMessage.constructDirectMessages(get(baseURL + - "direct_messages.xml", "since", format.format(since), true).asDocument(), this); + "direct_messages.xml", "since", format.format(since), true), this); } /** @@ -1514,7 +1512,7 @@ public List getDirectMessages(Date since) throws public List getSentDirectMessages() throws TwitterException { return DirectMessage.constructDirectMessages(get(baseURL + - "direct_messages/sent.xml", new PostParameter[0], true).asDocument(), this); + "direct_messages/sent.xml", new PostParameter[0], true), this); } /** @@ -1530,7 +1528,7 @@ public List getSentDirectMessages() throws public List getSentDirectMessages(Paging paging) throws TwitterException { return DirectMessage.constructDirectMessages(get(baseURL + - "direct_messages/sent.xml", new PostParameter[0],paging, true).asDocument(), this); + "direct_messages/sent.xml", new PostParameter[0],paging, true), this); } /** @@ -1546,7 +1544,7 @@ public List getSentDirectMessages(Paging paging) throws public List getSentDirectMessages(Date since) throws TwitterException { return DirectMessage.constructDirectMessages(get(baseURL + - "direct_messages/sent.xml", "since", format.format(since), true).asDocument(), this); + "direct_messages/sent.xml", "since", format.format(since), true), this); } /** @@ -1596,8 +1594,7 @@ public DirectMessage sendDirectMessage(String id, String text) throws TwitterException { return new DirectMessage(http.post(baseURL + "direct_messages/new.xml", new PostParameter[]{new PostParameter("user", id), - new PostParameter("text", text)}, true). - asDocument().getDocumentElement(), this); + new PostParameter("text", text)}, true), this); } @@ -1614,7 +1611,7 @@ public DirectMessage sendDirectMessage(String id, public DirectMessage deleteDirectMessage(int id) throws TwitterException { return new DirectMessage(http.post(baseURL + - "direct_messages/destroy/" + id + ".xml", new PostParameter[0], true).asDocument().getDocumentElement(), this); + "direct_messages/destroy/" + id + ".xml", new PostParameter[0], true), this); } /** @@ -1630,7 +1627,7 @@ public DirectMessage deleteDirectMessage(int id) throws public DirectMessage destroyDirectMessage(int id) throws TwitterException { return new DirectMessage(http.post(baseURL + - "direct_messages/destroy/" + id + ".xml", new PostParameter[0], true).asDocument().getDocumentElement(), this); + "direct_messages/destroy/" + id + ".xml", new PostParameter[0], true), this); } /** @@ -1657,8 +1654,7 @@ public User create(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: friendships create */ public User createFriendship(String id) throws TwitterException { - return new User(http.post(baseURL + "friendships/create/" + id + ".xml", new PostParameter[0], true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "friendships/create/" + id + ".xml", new PostParameter[0], true), this); } /** @@ -1674,8 +1670,8 @@ public User createFriendship(String id) throws TwitterException { public User createFriendship(String id, boolean follow) throws TwitterException { return new User(http.post(baseURL + "friendships/create/" + id + ".xml" , new PostParameter[]{new PostParameter("follow" - , String.valueOf(follow))}, true).asDocument() - .getDocumentElement(), this); + , String.valueOf(follow))}, true) + , this); } /** @@ -1701,8 +1697,7 @@ public User destroy(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: friendships destroy */ public User destroyFriendship(String id) throws TwitterException { - return new User(http.post(baseURL + "friendships/destroy/" + id + ".xml", new PostParameter[0], true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "friendships/destroy/" + id + ".xml", new PostParameter[0], true), this); } /** @@ -1741,7 +1736,7 @@ public boolean existsFriendship(String userA, String userB) throws TwitterExcept * @see Twitter API Wiki / Twitter REST API Method: friends ids */ public IDs getFriendsIDs() throws TwitterException { - return new IDs(get(baseURL + "friends/ids.xml", true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "friends/ids.xml", true)); } /** @@ -1753,7 +1748,7 @@ public IDs getFriendsIDs() throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: friends ids */ public IDs getFriendsIDs(Paging paging) throws TwitterException { - return new IDs(get(baseURL + "friends/ids.xml", null, paging, true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "friends/ids.xml", null, paging, true)); } /** @@ -1765,7 +1760,7 @@ public IDs getFriendsIDs(Paging paging) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: friends ids */ public IDs getFriendsIDs(int userId) throws TwitterException { - return new IDs(get(baseURL + "friends/ids.xml?user_id=" + userId, true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "friends/ids.xml?user_id=" + userId, true)); } /** @@ -1779,7 +1774,7 @@ public IDs getFriendsIDs(int userId) throws TwitterException { */ public IDs getFriendsIDs(int userId, Paging paging) throws TwitterException { return new IDs(get(baseURL + "friends/ids.xml?user_id=" + userId, null - , paging, true).asDocument().getDocumentElement()); + , paging, true)); } /** @@ -1791,7 +1786,7 @@ public IDs getFriendsIDs(int userId, Paging paging) throws TwitterException { * @see Twitter API Wiki / REST API Documentation - Social Graph Methods - friends/ids */ public IDs getFriendsIDs(String screenName) throws TwitterException { - return new IDs(get(baseURL + "friends/ids.xml?screen_name=" + screenName, true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "friends/ids.xml?screen_name=" + screenName, true)); } /** @@ -1805,7 +1800,7 @@ public IDs getFriendsIDs(String screenName) throws TwitterException { */ public IDs getFriendsIDs(String screenName, Paging paging) throws TwitterException { return new IDs(get(baseURL + "friends/ids.xml?screen_name=" + screenName - , null, paging, true).asDocument().getDocumentElement()); + , null, paging, true)); } /** @@ -1816,7 +1811,7 @@ public IDs getFriendsIDs(String screenName, Paging paging) throws TwitterExcepti * @see Twitter API Wiki / Twitter REST API Method: followers ids */ public IDs getFollowersIDs() throws TwitterException { - return new IDs(get(baseURL + "followers/ids.xml", true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "followers/ids.xml", true)); } /** @@ -1829,7 +1824,7 @@ public IDs getFollowersIDs() throws TwitterException { */ public IDs getFollowersIDs(Paging paging) throws TwitterException { return new IDs(get(baseURL + "followers/ids.xml", null, paging - , true).asDocument().getDocumentElement()); + , true)); } /** @@ -1841,7 +1836,7 @@ public IDs getFollowersIDs(Paging paging) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: followers ids */ public IDs getFollowersIDs(int userId) throws TwitterException { - return new IDs(get(baseURL + "followers/ids.xml?user_id=" + userId, true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "followers/ids.xml?user_id=" + userId, true)); } /** @@ -1855,7 +1850,7 @@ public IDs getFollowersIDs(int userId) throws TwitterException { */ public IDs getFollowersIDs(int userId, Paging paging) throws TwitterException { return new IDs(get(baseURL + "followers/ids.xml?user_id=" + userId, null - , paging, true).asDocument().getDocumentElement()); + , paging, true)); } /** @@ -1867,7 +1862,7 @@ public IDs getFollowersIDs(int userId, Paging paging) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: followers ids */ public IDs getFollowersIDs(String screenName) throws TwitterException { - return new IDs(get(baseURL + "followers/ids.xml?screen_name=" + screenName, true).asDocument().getDocumentElement()); + return new IDs(get(baseURL + "followers/ids.xml?screen_name=" + screenName, true)); } /** @@ -1881,7 +1876,7 @@ public IDs getFollowersIDs(String screenName) throws TwitterException { */ public IDs getFollowersIDs(String screenName, Paging paging) throws TwitterException { return new IDs(get(baseURL + "followers/ids.xml?screen_name=" - + screenName, null, paging, true).asDocument().getDocumentElement()); + + screenName, null, paging, true)); } /** @@ -1889,11 +1884,12 @@ public IDs getFollowersIDs(String screenName, Paging paging) throws TwitterExcep * * @return extended user * @since Twitter4J 2.0.0 + * @throws TwitterException when Twitter service or network is unavailable * @see Twitter API Wiki / Twitter REST API Method: account verify_credentials */ public ExtendedUser verifyCredentials() throws TwitterException { return new ExtendedUser(get(baseURL + "account/verify_credentials.xml" - , true).asDocument().getDocumentElement(), this); + , true), this); } /** @@ -1907,8 +1903,7 @@ public ExtendedUser verifyCredentials() throws TwitterException { * @deprecated Use updateProfile(String name, String email, String url, String location, String description) instead */ public User updateLocation(String location) throws TwitterException { - return new User(http.post(baseURL + "account/update_location.xml", new PostParameter[]{new PostParameter("location", location)}, true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "account/update_location.xml", new PostParameter[]{new PostParameter("location", location)}, true), this); } /** @@ -1933,8 +1928,7 @@ public ExtendedUser updateProfile(String name, String email, String url addParameterToList(profile, "location", location); addParameterToList(profile, "description", description); return new ExtendedUser(http.post(baseURL + "account/update_profile.xml" - , profile.toArray(new PostParameter[profile.size()]), true). - asDocument().getDocumentElement(), this); + , profile.toArray(new PostParameter[profile.size()]), true), this); } /** @@ -1946,8 +1940,7 @@ public ExtendedUser updateProfile(String name, String email, String url * @see Twitter API Wiki / Twitter REST API Method: account rate_limit_status */ public RateLimitStatus rateLimitStatus() throws TwitterException { - return new RateLimitStatus(http.get(baseURL + "account/rate_limit_status.xml", null != getUserId() && null != getPassword()). - asDocument().getDocumentElement()); + return new RateLimitStatus(http.get(baseURL + "account/rate_limit_status.xml", null != getUserId() && null != getPassword())); } public final static Device IM = new Device("im"); @@ -1972,8 +1965,7 @@ public Device(String device) { * @see Twitter API Wiki / Twitter REST API Method: account update_delivery_device */ public User updateDeliverlyDevice(Device device) throws TwitterException { - return new User(http.post(baseURL + "account/update_delivery_device.xml", new PostParameter[]{new PostParameter("device", device.DEVICE)}, true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "account/update_delivery_device.xml", new PostParameter[]{new PostParameter("device", device.DEVICE)}, true), this); } @@ -2009,8 +2001,7 @@ public ExtendedUser updateProfileColors( , profileSidebarBorderColor); return new ExtendedUser(http.post(baseURL + "account/update_profile_colors.xml", - colors.toArray(new PostParameter[colors.size()]), true). - asDocument().getDocumentElement(), this); + colors.toArray(new PostParameter[colors.size()]), true), this); } private void addParameterToList(List colors, @@ -2041,8 +2032,7 @@ public List favorites() throws TwitterException { * @since Twitter4J 2.0.1 */ public List getFavorites() throws TwitterException { - return Status.constructStatuses(get(baseURL + "favorites.xml", new PostParameter[0], true). - asDocument(), this); + return Status.constructStatuses(get(baseURL + "favorites.xml", new PostParameter[0], true), this); } /** @@ -2068,8 +2058,7 @@ public List favorites(int page) throws TwitterException { * @since Twitter4J 2.0.1 */ public List getFavorites(int page) throws TwitterException { - return Status.constructStatuses(get(baseURL + "favorites.xml", "page", String.valueOf(page), true). - asDocument(), this); + return Status.constructStatuses(get(baseURL + "favorites.xml", "page", String.valueOf(page), true), this); } /** @@ -2095,8 +2084,7 @@ public List favorites(String id) throws TwitterException { * @since Twitter4J 2.0.1 */ public List getFavorites(String id) throws TwitterException { - return Status.constructStatuses(get(baseURL + "favorites/" + id + ".xml", new PostParameter[0], true). - asDocument(), this); + return Status.constructStatuses(get(baseURL + "favorites/" + id + ".xml", new PostParameter[0], true), this); } /** @@ -2124,8 +2112,7 @@ public List favorites(String id, int page) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: favorites */ public List getFavorites(String id, int page) throws TwitterException { - return Status.constructStatuses(get(baseURL + "favorites/" + id + ".xml", "page", String.valueOf(page), true). - asDocument(), this); + return Status.constructStatuses(get(baseURL + "favorites/" + id + ".xml", "page", String.valueOf(page), true), this); } /** @@ -2137,8 +2124,7 @@ public List getFavorites(String id, int page) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: favorites create */ public Status createFavorite(long id) throws TwitterException { - return new Status(http.post(baseURL + "favorites/create/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new Status(http.post(baseURL + "favorites/create/" + id + ".xml", true), this); } /** @@ -2150,8 +2136,7 @@ public Status createFavorite(long id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: favorites destroy */ public Status destroyFavorite(long id) throws TwitterException { - return new Status(http.post(baseURL + "favorites/destroy/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new Status(http.post(baseURL + "favorites/destroy/" + id + ".xml", true), this); } /** @@ -2177,8 +2162,7 @@ public User follow(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: notifications follow */ public User enableNotification(String id) throws TwitterException { - return new User(http.post(baseURL + "notifications/follow/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "notifications/follow/" + id + ".xml", true), this); } /** @@ -2204,8 +2188,7 @@ public User leave(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: notifications leave */ public User disableNotification(String id) throws TwitterException { - return new User(http.post(baseURL + "notifications/leave/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "notifications/leave/" + id + ".xml", true), this); } /* Block Methods */ @@ -2221,8 +2204,7 @@ public User disableNotification(String id) throws TwitterException { * @deprecated Use createBlock(String id) instead */ public User block(String id) throws TwitterException { - return new User(http.post(baseURL + "blocks/create/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "blocks/create/" + id + ".xml", true), this); } /** @@ -2235,8 +2217,7 @@ public User block(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: blocks create */ public User createBlock(String id) throws TwitterException { - return new User(http.post(baseURL + "blocks/create/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "blocks/create/" + id + ".xml", true), this); } @@ -2251,8 +2232,7 @@ public User createBlock(String id) throws TwitterException { * @deprecated Use destroyBlock(String id) instead */ public User unblock(String id) throws TwitterException { - return new User(http.post(baseURL + "blocks/destroy/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "blocks/destroy/" + id + ".xml", true), this); } /** @@ -2265,8 +2245,7 @@ public User unblock(String id) throws TwitterException { * @see Twitter API Wiki / Twitter REST API Method: blocks destroy */ public User destroyBlock(String id) throws TwitterException { - return new User(http.post(baseURL + "blocks/destroy/" + id + ".xml", true). - asDocument().getDocumentElement(), this); + return new User(http.post(baseURL + "blocks/destroy/" + id + ".xml", true), this); } /* Help Methods */ @@ -2294,7 +2273,7 @@ public boolean test() throws TwitterException { * @deprecated Use verifyCredentials() instead */ public ExtendedUser getAuthenticatedUser() throws TwitterException { - return new ExtendedUser(get(baseURL + "account/verify_credentials.xml", true).asDocument().getDocumentElement(),this); + return new ExtendedUser(get(baseURL + "account/verify_credentials.xml", true),this); } /** diff --git a/src/main/java/twitter4j/TwitterResponse.java b/src/main/java/twitter4j/TwitterResponse.java index 1a7dc3864..8d8f9b439 100644 --- a/src/main/java/twitter4j/TwitterResponse.java +++ b/src/main/java/twitter4j/TwitterResponse.java @@ -29,8 +29,9 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import twitter4j.http.HTMLEntity; -import twitter4j.org.json.JSONObject; +import twitter4j.http.Response; import twitter4j.org.json.JSONException; +import twitter4j.org.json.JSONObject; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; @@ -38,6 +39,7 @@ import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -45,7 +47,6 @@ import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import java.net.URLDecoder; /** * Super class of Twitter Response objects. @@ -59,10 +60,28 @@ public class TwitterResponse implements java.io.Serializable { private Map formatMap = new HashMap(); private static final long serialVersionUID = 3519962197957449562L; + private transient int rateLimitLimit = -1; + private transient int rateLimitRemaining = -1; + private transient long rateLimitReset = -1; public TwitterResponse() { } + public TwitterResponse(Response res) { + String limit = res.getResponseHeader("X-RateLimit-Limit"); + if(null != limit){ + rateLimitLimit = Integer.parseInt(limit); + } + String remaining = res.getResponseHeader("X-RateLimit-Remaining"); + if(null != remaining){ + rateLimitRemaining = Integer.parseInt(remaining); + } + String reset = res.getResponseHeader("X-RateLimit-Reset"); + if(null != reset){ + rateLimitReset = Long.parseLong(reset); + } + } + protected void ensureRootNodeNameIs(String rootName, Element elem) throws TwitterException { if (!rootName.equals(elem.getNodeName())) { throw new TwitterException("Unexpected root node name:" + elem.getNodeName() + ". Expected:" + rootName + ". Check Twitter service availability.\n" + toString(elem)); @@ -168,4 +187,15 @@ protected Date encodeDate(String str, String format) throws TwitterException{ } } + public int getRateLimitLimit() { + return rateLimitLimit; + } + + public int getRateLimitRemaining() { + return rateLimitRemaining; + } + + public long getRateLimitReset() { + return rateLimitReset; + } } diff --git a/src/main/java/twitter4j/User.java b/src/main/java/twitter4j/User.java index 5082fe5c1..d59f040c8 100644 --- a/src/main/java/twitter4j/User.java +++ b/src/main/java/twitter4j/User.java @@ -29,12 +29,13 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import twitter4j.http.Response; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; -import java.util.List; import java.util.Date; +import java.util.List; /** * A data class representing Basic user information element @@ -89,8 +90,18 @@ public class User extends TwitterResponse implements java.io.Serializable { private static final long serialVersionUID = 3037057798600246529L; - /*package*/User(Element elem, Twitter twitter) throws TwitterException { - super(); + /*package*/User(Response res, Twitter twitter) throws TwitterException { + super(res); + Element elem = res.asDocument().getDocumentElement(); + init(elem, twitter); + } + + /*package*/User(Response res, Element elem, Twitter twitter) throws TwitterException { + super(res); + init(elem, twitter); + } + + private void init(Element elem, Twitter twitter) throws TwitterException { this.twitter = twitter; ensureRootNodeNameIs(POSSIBLE_ROOT_NAMES, elem); id = getChildInt("id", elem); @@ -213,7 +224,8 @@ public DirectMessage sendDirectMessage(String text) throws TwitterException { return twitter.sendDirectMessage(this.getName(), text); } - public static List constructUsers(Document doc, Twitter twitter) throws TwitterException { + public static List constructUsers(Response res, Twitter twitter) throws TwitterException { + Document doc = res.asDocument(); if (isRootNodeNilClasses(doc)) { return new ArrayList(0); } else { @@ -224,7 +236,7 @@ public static List constructUsers(Document doc, Twitter twitter) throws Tw int size = list.getLength(); List users = new ArrayList(size); for (int i = 0; i < size; i++) { - users.add(new User((Element) list.item(i), twitter)); + users.add(new User(res, (Element) list.item(i), twitter)); } return users; } catch (TwitterException te) { diff --git a/src/main/java/twitter4j/UserWithStatus.java b/src/main/java/twitter4j/UserWithStatus.java index 6de2b8a61..509c3ccb9 100644 --- a/src/main/java/twitter4j/UserWithStatus.java +++ b/src/main/java/twitter4j/UserWithStatus.java @@ -26,7 +26,7 @@ */ package twitter4j; -import org.w3c.dom.Element; +import twitter4j.http.Response; import java.util.Date; @@ -39,8 +39,8 @@ */ public abstract class UserWithStatus extends User { - public UserWithStatus(Element elem, Twitter twitter) throws TwitterException { - super(elem, twitter); + public UserWithStatus(Response res, Twitter twitter) throws TwitterException { + super(res, twitter); } public abstract String getProfileBackgroundColor(); diff --git a/src/main/java/twitter4j/http/HttpClient.java b/src/main/java/twitter4j/http/HttpClient.java index d2af3ac84..2ffacf842 100644 --- a/src/main/java/twitter4j/http/HttpClient.java +++ b/src/main/java/twitter4j/http/HttpClient.java @@ -40,8 +40,8 @@ import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.zip.GZIPInputStream; /** * A utility class to handle HTTP request/response. @@ -394,7 +394,6 @@ protected Response httpRequest(String url, PostParameter[] postParams, con.setDoInput(true); setHeaders(url, postParams, con, authenticated); if (null != postParams) { - log("POST ", url); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -410,25 +409,29 @@ protected Response httpRequest(String url, PostParameter[] postParams, osw.flush(); osw.close(); } else { - log("GET " + url); con.setRequestMethod("GET"); } + res = new Response(con); responseCode = con.getResponseCode(); - log("Response code: ", String.valueOf(responseCode)); - if (responseCode == OK) { - is = con.getInputStream(); - } else { - is = con.getErrorStream(); - } - if("gzip".equals(con.getContentEncoding())){ - // the response is gzipped - is = new GZIPInputStream(is); + if(DEBUG){ + log("Response: "); + Map> responseHeaders = con.getHeaderFields(); + for (String key : responseHeaders.keySet()) { + List values = responseHeaders.get(key); + for (String value : values) { + if(null != key){ + log(key + ": " + value); + }else{ + log(value); + } + } + } } - res = new Response(con.getResponseCode(), is); - log("Response: ", res.toString()); + log(res.toString()); if (responseCode != OK) { throw new TwitterException(getCause(responseCode) + "\n" + res.toString(), responseCode); } + con.disconnect(); break; } finally { try { @@ -482,7 +485,12 @@ public static String encodeParameters(PostParameter[] postParams) { * @param authenticated boolean */ private void setHeaders(String url, PostParameter[] params, HttpURLConnection connection, boolean authenticated) { - log("Request Headers: "); + log("Request: "); + if (null != params) { + log("POST ", url); + }else{ + log("GET ", url); + } if (authenticated) { if (basic == null && oauth == null) { diff --git a/src/main/java/twitter4j/http/Response.java b/src/main/java/twitter4j/http/Response.java index 698343a1e..67d36574e 100644 --- a/src/main/java/twitter4j/http/Response.java +++ b/src/main/java/twitter4j/http/Response.java @@ -29,8 +29,8 @@ import org.w3c.dom.Document; import org.xml.sax.SAXException; import twitter4j.TwitterException; -import twitter4j.org.json.JSONObject; import twitter4j.org.json.JSONException; +import twitter4j.org.json.JSONObject; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -40,6 +40,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.util.zip.GZIPInputStream; /** * A data class representing HTTP Response @@ -48,7 +50,6 @@ public class Response implements java.io.Serializable { static DocumentBuilder builder = null; static { - try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException ex) { @@ -56,16 +57,31 @@ public class Response implements java.io.Serializable { } } + private int statusCode; private Document response = null; private String responseString = null; private InputStream is; - private static final long serialVersionUID = 6190279542077827227L; private SAXException saxe = null; + private HttpURLConnection con; + private static final long serialVersionUID = -8868373803067492270L; - public Response(int statusCode, InputStream is) throws IOException { - this.statusCode = statusCode; + public Response(HttpURLConnection con) throws IOException { + this.statusCode = con.getResponseCode(); BufferedReader br = null; + InputStream is = null; + if (statusCode == 200) { + is = con.getInputStream(); + } else { + is = con.getErrorStream(); + } + if("gzip".equals(con.getContentEncoding())){ + // the response is gzipped + is = new GZIPInputStream(is); + } + + this.con = con; + try { br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer buf = new StringBuffer(); @@ -84,6 +100,10 @@ public int getStatusCode() { return statusCode; } + public String getResponseHeader(String name) { + return con.getHeaderField(name); + } + public String asString() { return responseString; } @@ -130,4 +150,5 @@ public InputStreamReader asReader() { public String toString(){ return responseString; } + } diff --git a/src/test/java/twitter4j/ResponseTest.java b/src/test/java/twitter4j/ResponseTest.java deleted file mode 100644 index 8cdcd1355..000000000 --- a/src/test/java/twitter4j/ResponseTest.java +++ /dev/null @@ -1,157 +0,0 @@ -/* -Copyright (c) 2007-2009, Yusuke Yamamoto -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of the Yusuke Yamamoto nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -package twitter4j; - -import junit.framework.TestCase; -import org.w3c.dom.Document; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.List; - -/** - *

Title: Twitter4J

- * - *

Description:

- * - * @version 0.9 - * @author Yusuke Yamamoto - yusuke at mac.com - */ -public class ResponseTest extends TestCase { - - public ResponseTest(String name) { - super(name); - } - - protected void setUp() throws Exception { - super.setUp(); - } - - protected void tearDown() throws Exception { - super.tearDown(); - } - - public int count; - static DocumentBuilder builder = null; - static { - - try { - builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - } catch (ParserConfigurationException ex) { - } - } - - public void testUser() throws Exception { - String responseString = "" - + " " - + " 6377362" - + " twit4j2" - + " twit4j2" - + " " - + " " - + " http://assets1.twitter.com/images/default_image.gif?1180256973" - + " " - + " false" - + " 9ae4e8" - + " 000000" - + " 0000ff" - + " e0ff92" - + " 87bc44" - + " 1" - + " 1" - + " 0" - + " 0" - + " "; - InputStream is; - is = new ByteArrayInputStream(responseString.getBytes("UTF-8")); - Document doc = builder.parse(new ByteArrayInputStream(responseString.getBytes("UTF-8"))); - User user = new User(doc.getDocumentElement(),null); - assertEquals(6377362, user.getId()); - } - - public void testStatus() throws Exception { - String responseString = "" - + "" - + " " - + " Sat Jun 02 14:56:38 +0000 2007" - + " 88182692" - + " 疲れた。ブログは明日にする。 *Tw*" - + " " - + " 5608852" - + " s_d" - + " s_d" - + " 日本" - + " もうCOBOLしない。Macは好きだよ。HR/HMを良く聴く。Jazzも好き。金融系SEしてる。メンヘラでもある。" - + " http://assets1.twitter.com/system/user/profile_image/5608852/normal/Cornell1.jpg?1177784770" - + " http://blog.so-net.ne.jp/s_d/" - + " false" - + " " - + " " - + " " - + " Sat Jun 02 14:56:25 +0000 2007" - + " 88182532" - + " Well, praise the Lord that McKenna is ok. Went through the same thing many years ago. Not much is scarier." - + " " - + " 5385192" - + " Linda Tyler" - + " ginnylin" - + " " - + " " - + " http://assets2.twitter.com/system/user/profile_image/5385192/normal/J.B.BOW3pts.jpg?1177184596" - + " " - + " false" - + " " - + " " - + " " - + " Sat Jun 02 14:56:25 +0000 2007" - + " 88182522" - + " @misspeter Downloading :)" - + " " - + " 4990471" - + " Guy David" - + " guy_david" - + " Somewhere in Israel" - + " I'm Guy David, a musician, a podcaster and a digital artist. I live in Israel with my wife and my son." - + " http://assets1.twitter.com/system/user/profile_image/4990471/normal/gd_290x290.jpg?1178210980" - + " http://www.guydavid.com" - + " false" - + " " - + " " - + ""; - - Document doc = builder.parse(new ByteArrayInputStream(responseString.getBytes("UTF-8"))); - List statuses = Status.constructStatuses(doc,null); - assertEquals(88182692, statuses.get(0).getId()); - assertEquals(88182532, statuses.get(1).getId()); - assertEquals(88182522, statuses.get(2).getId()); - assertEquals("Guy David", statuses.get(2).getUser().getName()); - } - -} diff --git a/src/test/java/twitter4j/SizeofStatusChecker.java b/src/test/java/twitter4j/SizeofStatusChecker.java index d47520f93..2b979d235 100644 --- a/src/test/java/twitter4j/SizeofStatusChecker.java +++ b/src/test/java/twitter4j/SizeofStatusChecker.java @@ -48,7 +48,7 @@ public static void main(String[] args) throws Exception { for (int i = 0; i < count; i++) { Document doc = builder.parse(new ByteArrayInputStream(statusXML.getBytes())); Element elem = doc.getDocumentElement(); - statuses[i] = new Status(elem, null); +// statuses[i] = new Status(elem, null); } System.out.println((before - Runtime.getRuntime().freeMemory()) / count); } diff --git a/src/test/java/twitter4j/TwitterTestUnit.java b/src/test/java/twitter4j/TwitterTestUnit.java index a8db723dd..de2fa1800 100644 --- a/src/test/java/twitter4j/TwitterTestUnit.java +++ b/src/test/java/twitter4j/TwitterTestUnit.java @@ -231,6 +231,9 @@ public void testShow() throws Exception{ assertEquals(52,status.getUser().getId()); Status status2 = unauthenticated.showStatus(1000l); assertEquals(52,status2.getUser().getId()); + assertTrue(50 < status.getRateLimitLimit()); + assertTrue(1 < status.getRateLimitRemaining()); + assertTrue(1 < status.getRateLimitReset()); status2 = unauthenticated.showStatus(999383469l); assertEquals("01010100 01110010 01101001 01110101 01101101 01110000 01101000 <3",status2.getText()); @@ -241,6 +244,7 @@ public void testShow() throws Exception{ public void testStatusMethods() throws Exception{ String date = new java.util.Date().toString()+"test"; Status status = twitterAPI1.updateStatus(date); + assertEquals(date, status.getText()); Status status2 = twitterAPI2.updateStatus("@" + id1 + " " + date, status.getId()); assertEquals("@" + id1 + " " + date, status2.getText());