From e8136cbac605226361693e42add472b32e8236b9 Mon Sep 17 00:00:00 2001 From: Alessandro Bahgat Date: Sun, 10 Jul 2011 13:46:00 +0200 Subject: [PATCH] TFJ-547 Timeline methods return empty collections on 404 Modified the internal factories to allow the creation of empty response lists with include rate limitation information (extracted from the exception) --- .../src/main/java/twitter4j/TwitterImpl.java | 339 ++++++++++++++---- .../internal/json/ResponseListImpl.java | 6 + .../json/zzzz_T4J_INTERNAL_Factory.java | 2 + .../zzzz_T4J_INTERNAL_JSONImplFactory.java | 5 + .../json/zzzz_T4J_INTERNAL_LazyFactory.java | 10 + 5 files changed, 285 insertions(+), 77 deletions(-) diff --git a/twitter4j-core/src/main/java/twitter4j/TwitterImpl.java b/twitter4j-core/src/main/java/twitter4j/TwitterImpl.java index 02d81362a..55c356003 100644 --- a/twitter4j-core/src/main/java/twitter4j/TwitterImpl.java +++ b/twitter4j-core/src/main/java/twitter4j/TwitterImpl.java @@ -174,8 +174,16 @@ public ResponseList getWeeklyTrends(Date date, boolean excludeHashTags) */ public ResponseList getPublicTimeline() throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/public_timeline.json?include_entities=" + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/public_timeline.json?include_entities=" + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -184,7 +192,15 @@ public ResponseList getPublicTimeline() throws public ResponseList getHomeTimeline() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() + "statuses/home_timeline.json?include_entities=" + conf.isIncludeEntitiesEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + "statuses/home_timeline.json?include_entities=" + conf.isIncludeEntitiesEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -193,8 +209,16 @@ public ResponseList getHomeTimeline() throws public ResponseList getHomeTimeline(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/home_timeline.json", mergeParameters(paging.asPostParameterArray(), INCLUDE_ENTITIES))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/home_timeline.json", mergeParameters(paging.asPostParameterArray(), INCLUDE_ENTITIES))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -203,9 +227,17 @@ public ResponseList getHomeTimeline(Paging paging) throws public ResponseList getFriendsTimeline() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/friends_timeline.json?include_entities=" - + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/friends_timeline.json?include_entities=" + + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -215,10 +247,18 @@ public ResponseList getFriendsTimeline() throws public ResponseList getFriendsTimeline(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/friends_timeline.json", - mergeParameters(new HttpParameter[]{INCLUDE_RTS, INCLUDE_ENTITIES} - , paging.asPostParameterArray()))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/friends_timeline.json", + mergeParameters(new HttpParameter[]{INCLUDE_RTS, INCLUDE_ENTITIES} + , paging.asPostParameterArray()))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } @@ -228,12 +268,20 @@ public ResponseList getFriendsTimeline(Paging paging) throws */ public ResponseList getUserTimeline(String screenName, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/user_timeline.json", - mergeParameters(new HttpParameter[]{new HttpParameter("screen_name", screenName) - , INCLUDE_RTS - , INCLUDE_ENTITIES} - , paging.asPostParameterArray()))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/user_timeline.json", + mergeParameters(new HttpParameter[]{new HttpParameter("screen_name", screenName) + , INCLUDE_RTS + , INCLUDE_ENTITIES} + , paging.asPostParameterArray()))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -241,26 +289,50 @@ public ResponseList getUserTimeline(String screenName, Paging paging) */ public ResponseList getUserTimeline(long userId, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/user_timeline.json", - mergeParameters(new HttpParameter[]{new HttpParameter("user_id", userId) - , INCLUDE_RTS - , INCLUDE_ENTITIES} - , paging.asPostParameterArray()))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/user_timeline.json", + mergeParameters(new HttpParameter[]{new HttpParameter("user_id", userId) + , INCLUDE_RTS + , INCLUDE_ENTITIES} + , paging.asPostParameterArray()))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getUserTimeline(String screenName) throws TwitterException { - return getUserTimeline(screenName, new Paging()); + try { + return getUserTimeline(screenName, new Paging()); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getUserTimeline(long userId) throws TwitterException { - return getUserTimeline(userId, new Paging()); + try { + return getUserTimeline(userId, new Paging()); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -268,7 +340,15 @@ public ResponseList getUserTimeline(long userId) throws TwitterException */ public ResponseList getUserTimeline() throws TwitterException { - return getUserTimeline(new Paging()); + try { + return getUserTimeline(new Paging()); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -277,11 +357,20 @@ public ResponseList getUserTimeline() throws public ResponseList getUserTimeline(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/user_timeline.json", - mergeParameters(new HttpParameter[]{INCLUDE_RTS - , INCLUDE_ENTITIES} - , paging.asPostParameterArray()))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/user_timeline.json", + mergeParameters(new HttpParameter[]{INCLUDE_RTS + , INCLUDE_ENTITIES} + , paging.asPostParameterArray()))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } + } /** @@ -289,9 +378,17 @@ public ResponseList getUserTimeline(Paging paging) throws */ public ResponseList getMentions() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/mentions.json?include_entities=" - + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/mentions.json?include_entities=" + + conf.isIncludeEntitiesEnabled() + "&include_rts=" + conf.isIncludeRTsEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -299,11 +396,19 @@ public ResponseList getMentions() throws TwitterException { */ public ResponseList getMentions(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/mentions.json", - mergeParameters(new HttpParameter[]{INCLUDE_RTS - , INCLUDE_ENTITIES} - , paging.asPostParameterArray()))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/mentions.json", + mergeParameters(new HttpParameter[]{INCLUDE_RTS + , INCLUDE_ENTITIES} + , paging.asPostParameterArray()))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -311,8 +416,16 @@ public ResponseList getMentions(Paging paging) throws TwitterException { */ public ResponseList getRetweetedByMe() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/retweeted_by_me.json?include_entities=" + conf.isIncludeEntitiesEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_by_me.json?include_entities=" + conf.isIncludeEntitiesEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -320,9 +433,17 @@ public ResponseList getRetweetedByMe() throws TwitterException { */ public ResponseList getRetweetedByMe(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/retweeted_by_me.json", mergeParameters(paging.asPostParameterArray() - , INCLUDE_ENTITIES))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_by_me.json", mergeParameters(paging.asPostParameterArray() + , INCLUDE_ENTITIES))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -330,9 +451,17 @@ public ResponseList getRetweetedByMe(Paging paging) throws TwitterExcept */ public ResponseList getRetweetedToMe() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/retweeted_to_me.json?include_entities=" - + conf.isIncludeEntitiesEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_to_me.json?include_entities=" + + conf.isIncludeEntitiesEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -340,9 +469,17 @@ public ResponseList getRetweetedToMe() throws TwitterException { */ public ResponseList getRetweetedToMe(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/retweeted_to_me.json", mergeParameters(paging.asPostParameterArray() - , INCLUDE_ENTITIES))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_to_me.json", mergeParameters(paging.asPostParameterArray() + , INCLUDE_ENTITIES))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -350,9 +487,17 @@ public ResponseList getRetweetedToMe(Paging paging) throws TwitterExcept */ public ResponseList getRetweetsOfMe() throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/retweets_of_me.json?include_entities=" - + conf.isIncludeEntitiesEnabled())); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweets_of_me.json?include_entities=" + + conf.isIncludeEntitiesEnabled())); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** @@ -360,53 +505,93 @@ public ResponseList getRetweetsOfMe() throws TwitterException { */ public ResponseList getRetweetsOfMe(Paging paging) throws TwitterException { ensureAuthorizationEnabled(); - return factory.createStatusList(get(conf.getRestBaseURL() - + "statuses/retweets_of_me.json", mergeParameters(paging.asPostParameterArray() - , INCLUDE_ENTITIES))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweets_of_me.json", mergeParameters(paging.asPostParameterArray() + , INCLUDE_ENTITIES))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getRetweetedToUser(String screenName, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/retweeted_to_user.json", mergeParameters(paging.asPostParameterArray() - , new HttpParameter[]{ - new HttpParameter("screen_name", screenName) - , INCLUDE_ENTITIES}))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_to_user.json", mergeParameters(paging.asPostParameterArray() + , new HttpParameter[]{ + new HttpParameter("screen_name", screenName) + , INCLUDE_ENTITIES}))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getRetweetedToUser(long userId, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/retweeted_to_user.json", mergeParameters(paging.asPostParameterArray() - , new HttpParameter[]{ - new HttpParameter("user_id", userId) - , INCLUDE_ENTITIES}))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_to_user.json", mergeParameters(paging.asPostParameterArray() + , new HttpParameter[]{ + new HttpParameter("user_id", userId) + , INCLUDE_ENTITIES}))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getRetweetedByUser(String screenName, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/retweeted_by_user.json", mergeParameters(paging.asPostParameterArray() - , new HttpParameter[]{ - new HttpParameter("screen_name", screenName) - , INCLUDE_ENTITIES}))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_by_user.json", mergeParameters(paging.asPostParameterArray() + , new HttpParameter[]{ + new HttpParameter("screen_name", screenName) + , INCLUDE_ENTITIES}))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** * {@inheritDoc} */ public ResponseList getRetweetedByUser(long userId, Paging paging) throws TwitterException { - return factory.createStatusList(get(conf.getRestBaseURL() + - "statuses/retweeted_by_user.json", mergeParameters(paging.asPostParameterArray() - , new HttpParameter[]{ - new HttpParameter("user_id", userId) - , INCLUDE_ENTITIES}))); + try { + return factory.createStatusList(get(conf.getRestBaseURL() + + "statuses/retweeted_by_user.json", mergeParameters(paging.asPostParameterArray() + , new HttpParameter[]{ + new HttpParameter("user_id", userId) + , INCLUDE_ENTITIES}))); + } catch (TwitterException te) { + if (404 == te.getStatusCode()) { + return factory.createEmptyStatusList(te); + } else { + throw te; + } + } } /** diff --git a/twitter4j-core/src/main/java/twitter4j/internal/json/ResponseListImpl.java b/twitter4j-core/src/main/java/twitter4j/internal/json/ResponseListImpl.java index a9d7413cd..eef9e0540 100644 --- a/twitter4j-core/src/main/java/twitter4j/internal/json/ResponseListImpl.java +++ b/twitter4j-core/src/main/java/twitter4j/internal/json/ResponseListImpl.java @@ -42,6 +42,12 @@ class ResponseListImpl extends ArrayList implements ResponseList { super(size); init(res); } + ResponseListImpl(RateLimitStatus rateLimitStatus, RateLimitStatus featureSpecificRateLimitStatus, int accessLevel) { + super(); + this.rateLimitStatus = rateLimitStatus; + this.featureSpecificRateLimitStatus = featureSpecificRateLimitStatus; + this.accessLevel = accessLevel; + } private void init(HttpResponse res){ this.rateLimitStatus = RateLimitStatusJSONImpl.createFromResponseHeader(res); this.featureSpecificRateLimitStatus = RateLimitStatusJSONImpl.createFeatureSpecificRateLimitStatusFromResponseHeader(res); diff --git a/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_Factory.java b/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_Factory.java index 7d7cee4a4..96e93add6 100644 --- a/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_Factory.java +++ b/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_Factory.java @@ -27,6 +27,8 @@ public interface zzzz_T4J_INTERNAL_Factory extends java.io.Serializable { ResponseList createStatusList(HttpResponse res) throws TwitterException; + ResponseList createEmptyStatusList(TwitterException te); + Trends createTrends(HttpResponse res) throws TwitterException; ResponseList createTrendsList(HttpResponse res) throws TwitterException; diff --git a/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_JSONImplFactory.java b/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_JSONImplFactory.java index b157c68c3..060aa999d 100644 --- a/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_JSONImplFactory.java +++ b/twitter4j-core/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_JSONImplFactory.java @@ -63,6 +63,11 @@ public ResponseList createStatusList(HttpResponse res) throws TwitterExc return StatusJSONImpl.createStatusList(res, conf); } + public ResponseList createEmptyStatusList(TwitterException te) { + return new ResponseListImpl(te.getRateLimitStatus(), + te.getFeatureSpecificRateLimitStatus(), te.getAccessLevel()); + } + /** * returns a GeoLocation instance if a "geo" element is found. * diff --git a/twitter4j-gae/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_LazyFactory.java b/twitter4j-gae/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_LazyFactory.java index 4af7ff1dd..56342a4b5 100644 --- a/twitter4j-gae/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_LazyFactory.java +++ b/twitter4j-gae/src/main/java/twitter4j/internal/json/zzzz_T4J_INTERNAL_LazyFactory.java @@ -53,6 +53,16 @@ protected ResponseList createActualResponseList() throws TwitterExceptio }; } + public ResponseList createEmptyStatusList(final TwitterException te) { + return new LazyResponseList() { + @Override + protected ResponseList createActualResponseList() throws TwitterException { + return new ResponseListImpl(te.getRateLimitStatus(), + te.getFeatureSpecificRateLimitStatus(), te.getAccessLevel()); + } + }; + } + public Trends createTrends(HttpResponse res) throws TwitterException { return new LazyTrends(res, factory); }