diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterNamespaceHandler.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterNamespaceHandler.java index 0dc22df983..8765c3cab7 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterNamespaceHandler.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterNamespaceHandler.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.integration.twitter.config; import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler; diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Tweet.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Tweet.java index 6aa97d3999..ca8fd4095b 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Tweet.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Tweet.java @@ -1,17 +1,17 @@ /* - * Copyright 2010 the original author or authors. + * Copyright 2002-2010 the original author or authors * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.springframework.integration.twitter.core; diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Twitter4jTemplate.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Twitter4jTemplate.java index 1eb5641455..9e5f1014c3 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Twitter4jTemplate.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/Twitter4jTemplate.java @@ -1,5 +1,17 @@ -/** - * +/* + * Copyright 2002-2010 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.springframework.integration.twitter.core; @@ -59,7 +71,7 @@ public class Twitter4jTemplate implements TwitterOperations{ throw new TwitterOperationException("Failed to obtain profile id ", e); } } - @Override + //@Override public RateLimitStatus getRateLimitStatus() { try { return twitter.getRateLimitStatus(); @@ -167,6 +179,10 @@ public class Twitter4jTemplate implements TwitterOperations{ } } + public Twitter getUnderlyingTwitter(){ + return this.twitter; + } + private List buildTweetsFromTwitterResponses(List responses){ List tweets = new LinkedList(); if (responses != null){ diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterOperations.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterOperations.java index 39999d29e9..e6c5b0a075 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterOperations.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterOperations.java @@ -1,15 +1,24 @@ -/** - * +/* + * Copyright 2002-2010 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.springframework.integration.twitter.core; import java.util.List; -import twitter4j.DirectMessage; import twitter4j.Paging; -import twitter4j.RateLimitStatus; -import twitter4j.Status; -import twitter4j.StatusUpdate; +import twitter4j.Twitter; /** * @author Oleg Zhurakousky @@ -20,8 +29,6 @@ public interface TwitterOperations { String getProfileId(); - RateLimitStatus getRateLimitStatus(); - List getDirectMessages(); List getDirectMessages(Paging paging); @@ -39,4 +46,6 @@ public interface TwitterOperations { void sendDirectMessage(int userId, String text); void updateStatus(Tweet status); + + Twitter getUnderlyingTwitter(); } diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/AbstractTwitterMessageSource.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/AbstractTwitterMessageSource.java index 2245f95d5c..213ecd3528 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/AbstractTwitterMessageSource.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/AbstractTwitterMessageSource.java @@ -34,6 +34,7 @@ import org.springframework.integration.store.MetadataStore; import org.springframework.integration.store.SimpleMetadataStore; import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.twitter.core.Tweet; +import org.springframework.integration.twitter.core.Twitter4jTemplate; import org.springframework.integration.twitter.core.TwitterOperations; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -148,7 +149,10 @@ public abstract class AbstractTwitterMessageSource extends AbstractEndpoint protected void doStart(){ Assert.notNull(this.twitter, "'twitter' instance can't be null"); historyWritingPostProcessor.setTrackableComponent(this); - RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(this.twitter); + // temporarily injecting Twitter into a trigger so it can deal with Rate Limits. will be changed + // once we switch to Spring Social + RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(this.twitter.getUnderlyingTwitter()); + // Runnable apiCallback = this.getApiCallback(); twitterUpdatePollingTask = this.getTaskScheduler().schedule(apiCallback, trigger); } diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/RateLimitStatusTrigger.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/RateLimitStatusTrigger.java index 33bf3d5032..5abbebdcbd 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/RateLimitStatusTrigger.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/inbound/RateLimitStatusTrigger.java @@ -19,12 +19,14 @@ import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.integration.twitter.core.TwitterOperations; +import org.springframework.scheduling.SchedulingException; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.util.Assert; import twitter4j.RateLimitStatus; +import twitter4j.Twitter; +import twitter4j.TwitterException; /** * @author Oleg Zhurakousky @@ -32,9 +34,9 @@ import twitter4j.RateLimitStatus; */ class RateLimitStatusTrigger implements Trigger { protected final Log logger = LogFactory.getLog(getClass()); - private TwitterOperations twitter; + private Twitter twitter; - public RateLimitStatusTrigger(TwitterOperations twitter){ + public RateLimitStatusTrigger(Twitter twitter){ Assert.notNull(twitter, "'twitter' must not be null"); this.twitter = twitter; } @@ -46,7 +48,7 @@ class RateLimitStatusTrigger implements Trigger { if (triggerContext.lastCompletionTime() == null){ return new Date(System.currentTimeMillis()); } -// try { + try { RateLimitStatus rateLimitStatus = twitter.getRateLimitStatus(); int secondsUntilReset = rateLimitStatus.getSecondsUntilReset(); int remainingHits = rateLimitStatus.getRemainingHits(); @@ -67,8 +69,8 @@ class RateLimitStatusTrigger implements Trigger { " remaining pull this rate period. The period ends in " + secondsUntilReset); return new Date(System.currentTimeMillis() + msUntilWeCanPullAgain); -// } catch (TwitterException e) { -// throw new SchedulingException("Failed to schedule the next Twitter update", e); -// } + } catch (TwitterException e) { + throw new SchedulingException("Failed to schedule the next Twitter update", e); + } } } diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java index b84481c297..52b8fdccfd 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java @@ -38,6 +38,7 @@ import twitter4j.DirectMessage; import twitter4j.Paging; import twitter4j.RateLimitStatus; import twitter4j.ResponseList; +import twitter4j.Twitter; /** * @author Oleg Zhurakousky @@ -61,13 +62,15 @@ public class DirectMessageReceivingMessageSourceTests { when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L)); when(secondMessage.getId()).thenReturn((long) 2000); - when(twitter.getProfileId()).thenReturn("kermit"); + Twitter tw = mock(Twitter.class); + when(twitter.getUnderlyingTwitter()).thenReturn(tw); } @Test public void testSuccessfullInitialization() throws Exception{ + DirectMessageReceivingMessageSource source = new DirectMessageReceivingMessageSource(twitter); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); @@ -105,7 +108,9 @@ public class DirectMessageReceivingMessageSourceTests { @SuppressWarnings("unchecked") private void setUpMockScenarioForMessagePolling() throws Exception{ RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class); - when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatus); + Twitter tw = mock(Twitter.class); + when(twitter.getUnderlyingTwitter()).thenReturn(tw); + when(tw.getRateLimitStatus()).thenReturn(rateLimitStatus); when(rateLimitStatus.getSecondsUntilReset()).thenReturn(2464); when(rateLimitStatus.getRemainingHits()).thenReturn(250); diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/RateLimitStatusTriggerTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/RateLimitStatusTriggerTests.java index 4eeeace434..b78558ed98 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/RateLimitStatusTriggerTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/RateLimitStatusTriggerTests.java @@ -26,6 +26,7 @@ import org.springframework.integration.twitter.core.TwitterOperations; import org.springframework.scheduling.TriggerContext; import twitter4j.RateLimitStatus; +import twitter4j.Twitter; /** * @author Oleg Zhurakousky @@ -36,7 +37,9 @@ public class RateLimitStatusTriggerTests { @Test public void testTriggerImediateAndSubsequentExecutionTime() throws Exception{ TwitterOperations twitter = mock(TwitterOperations.class); - RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter); + Twitter tw = mock(Twitter.class); + when(twitter.getUnderlyingTwitter()).thenReturn(tw); + RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter.getUnderlyingTwitter()); TriggerContext context = mock(TriggerContext.class); Date currentDate = new Date(System.currentTimeMillis()); Date nextDate = trigger.nextExecutionTime(context); @@ -45,7 +48,7 @@ public class RateLimitStatusTriggerTests { RateLimitStatus rateLimitStatis = mock(RateLimitStatus.class); - when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatis); + when(tw.getRateLimitStatus()).thenReturn(rateLimitStatis); when(rateLimitStatis.getRemainingHits()).thenReturn(2000); when(rateLimitStatis.getSecondsUntilReset()).thenReturn(4000);