INT-1553 first round of refactoring to introduce dependency on Tweet from Spring Social
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 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.Date;
|
||||
|
||||
/**
|
||||
* Represents a Twitter status update (e.g., a "tweet").
|
||||
*
|
||||
* @author Craig Walls
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class Tweet {
|
||||
private long id;
|
||||
private String text;
|
||||
private Date createdAt;
|
||||
private String fromUser;
|
||||
private String profileImageUrl;
|
||||
private Long toUserId;
|
||||
private long fromUserId;
|
||||
private String languageCode;
|
||||
private String source;
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(Date createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public String getFromUser() {
|
||||
return fromUser;
|
||||
}
|
||||
|
||||
public void setFromUser(String fromUser) {
|
||||
this.fromUser = fromUser;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getProfileImageUrl() {
|
||||
return profileImageUrl;
|
||||
}
|
||||
|
||||
public void setProfileImageUrl(String profileImageUrl) {
|
||||
this.profileImageUrl = profileImageUrl;
|
||||
}
|
||||
|
||||
public Long getToUserId() {
|
||||
return toUserId;
|
||||
}
|
||||
|
||||
public void setToUserId(Long toUserId) {
|
||||
this.toUserId = toUserId;
|
||||
}
|
||||
|
||||
public long getFromUserId() {
|
||||
return fromUserId;
|
||||
}
|
||||
|
||||
public void setFromUserId(long fromUserId) {
|
||||
this.fromUserId = fromUserId;
|
||||
}
|
||||
|
||||
public String getLanguageCode() {
|
||||
return languageCode;
|
||||
}
|
||||
|
||||
public void setLanguageCode(String languageCode) {
|
||||
this.languageCode = languageCode;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
package org.springframework.integration.twitter.core;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
@@ -10,6 +11,7 @@ import org.springframework.util.Assert;
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.StatusUpdate;
|
||||
import twitter4j.Twitter;
|
||||
@@ -67,54 +69,61 @@ public class Twitter4jTemplate implements TwitterOperations{
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<DirectMessage> getDirectMessages() {
|
||||
public List<Tweet> getDirectMessages() {
|
||||
|
||||
try {
|
||||
return twitter.getDirectMessages();
|
||||
ResponseList<DirectMessage> directMessages = twitter.getDirectMessages();
|
||||
return this.buildTweetsFromTwitterResponses(directMessages);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<DirectMessage> getDirectMessages(Paging paging) {
|
||||
public List<Tweet> getDirectMessages(Paging paging) {
|
||||
try {
|
||||
return twitter.getDirectMessages(paging);
|
||||
ResponseList<DirectMessage> directMessages = twitter.getDirectMessages(paging);
|
||||
return this.buildTweetsFromTwitterResponses(directMessages);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getMentions() {
|
||||
public List<Tweet> getMentions() {
|
||||
try {
|
||||
return twitter.getMentions();
|
||||
ResponseList<Status> mentions = twitter.getMentions();
|
||||
return this.buildTweetsFromTwitterResponses(mentions);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getMentions(Paging paging) {
|
||||
public List<Tweet> getMentions(Paging paging) {
|
||||
try {
|
||||
return twitter.getMentions(paging);
|
||||
ResponseList<Status> mentions = twitter.getMentions(paging);
|
||||
return this.buildTweetsFromTwitterResponses(mentions);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getFriendsTimeline() {
|
||||
public List<Tweet> getFriendsTimeline() {
|
||||
try {
|
||||
return twitter.getFriendsTimeline();
|
||||
ResponseList<Status> timelines = twitter.getFriendsTimeline();
|
||||
return this.buildTweetsFromTwitterResponses(timelines);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getFriendsTimeline(Paging paging) {
|
||||
public List<Tweet> getFriendsTimeline(Paging paging) {
|
||||
try {
|
||||
return twitter.getFriendsTimeline(paging);
|
||||
ResponseList<Status> timelines = twitter.getFriendsTimeline(paging);
|
||||
return this.buildTweetsFromTwitterResponses(timelines);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
|
||||
@@ -122,6 +131,8 @@ public class Twitter4jTemplate implements TwitterOperations{
|
||||
}
|
||||
@Override
|
||||
public void sendDirectMessage(String userName, String text) {
|
||||
Assert.hasText(userName, "'userName' must be set");
|
||||
Assert.hasText(text, "'text' must be set");
|
||||
try {
|
||||
twitter.sendDirectMessage(userName, text);
|
||||
}
|
||||
@@ -131,6 +142,8 @@ public class Twitter4jTemplate implements TwitterOperations{
|
||||
}
|
||||
@Override
|
||||
public void sendDirectMessage(int userId, String text) {
|
||||
Assert.state(userId > 0, "'userId' msut be provided");
|
||||
Assert.hasText(text, "'text' must be set");
|
||||
try {
|
||||
twitter.sendDirectMessage(userId, text);
|
||||
}
|
||||
@@ -138,13 +151,57 @@ public class Twitter4jTemplate implements TwitterOperations{
|
||||
throw new TwitterOperationException("Failed to send Direct Message ", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStatus(StatusUpdate status) {
|
||||
public void updateStatus(Tweet statusTweet) {
|
||||
Assert.notNull(statusTweet, "'statusTweet' must not be null");
|
||||
try {
|
||||
StatusUpdate status = new StatusUpdate(statusTweet.getText());
|
||||
if (statusTweet.getToUserId() != null){
|
||||
status.setInReplyToStatusId(statusTweet.getToUserId());
|
||||
}
|
||||
twitter.updateStatus(status);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to send Status update ", e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Tweet> buildTweetsFromTwitterResponses(List<?> responses){
|
||||
List<Tweet> tweets = new LinkedList<Tweet>();
|
||||
if (responses != null){
|
||||
for (Object response : responses) {
|
||||
if (response instanceof Status){
|
||||
tweets.add(this.buildTweetFromStatus((Status) response));
|
||||
}
|
||||
else {
|
||||
tweets.add(this.buildTweetFromDm((DirectMessage) response));
|
||||
}
|
||||
}
|
||||
}
|
||||
return tweets;
|
||||
}
|
||||
|
||||
private Tweet buildTweetFromDm(DirectMessage dm){
|
||||
Tweet tweet = new Tweet();
|
||||
tweet.setCreatedAt(dm.getCreatedAt());
|
||||
tweet.setFromUser(dm.getSenderScreenName());
|
||||
tweet.setFromUserId(dm.getSenderId());
|
||||
tweet.setId(dm.getId());
|
||||
tweet.setText(dm.getText());
|
||||
tweet.setToUserId((long)dm.getRecipientId());
|
||||
return tweet;
|
||||
}
|
||||
|
||||
private Tweet buildTweetFromStatus(Status status){
|
||||
Tweet tweet = new Tweet();
|
||||
tweet.setCreatedAt(status.getCreatedAt());
|
||||
tweet.setFromUser(status.getInReplyToScreenName());
|
||||
tweet.setFromUserId(status.getInReplyToUserId());
|
||||
tweet.setId(status.getId());
|
||||
tweet.setSource(status.getSource());
|
||||
tweet.setText(status.getText());
|
||||
tweet.setToUserId((long)status.getUser().getId());
|
||||
return tweet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,21 +22,21 @@ public interface TwitterOperations {
|
||||
|
||||
RateLimitStatus getRateLimitStatus();
|
||||
|
||||
List<DirectMessage> getDirectMessages();
|
||||
List<Tweet> getDirectMessages();
|
||||
|
||||
List<DirectMessage> getDirectMessages(Paging paging);
|
||||
List<Tweet> getDirectMessages(Paging paging);
|
||||
|
||||
List<Status> getMentions();
|
||||
List<Tweet> getMentions();
|
||||
|
||||
List<Status> getMentions(Paging paging);
|
||||
List<Tweet> getMentions(Paging paging);
|
||||
|
||||
List<Status> getFriendsTimeline();
|
||||
List<Tweet> getFriendsTimeline();
|
||||
|
||||
List<Status> getFriendsTimeline(Paging paging);
|
||||
List<Tweet> getFriendsTimeline(Paging paging);
|
||||
|
||||
void sendDirectMessage(String userName, String text);
|
||||
|
||||
void sendDirectMessage(int userId, String text);
|
||||
|
||||
void updateStatus(StatusUpdate status);
|
||||
void updateStatus(Tweet status);
|
||||
}
|
||||
|
||||
@@ -33,13 +33,13 @@ import org.springframework.integration.history.TrackableComponent;
|
||||
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.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* Abstract class that defines common operations for receiving various types of
|
||||
@@ -137,9 +137,9 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
|
||||
abstract Runnable getApiCallback();
|
||||
|
||||
protected Comparator getComparator() {
|
||||
return new Comparator<Status>() {
|
||||
public int compare(Status status, Status status1) {
|
||||
return status.getCreatedAt().compareTo(status1.getCreatedAt());
|
||||
return new Comparator<Tweet>() {
|
||||
public int compare(Tweet tweet1, Tweet tweet2) {
|
||||
return tweet1.getCreatedAt().compareTo(tweet2.getCreatedAt());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -170,11 +170,8 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
|
||||
synchronized (this.markerGuard) {
|
||||
|
||||
long id = 0;
|
||||
if (tweet instanceof DirectMessage) {
|
||||
id = ((DirectMessage) tweet).getId();
|
||||
}
|
||||
else if (tweet instanceof Status) {
|
||||
id = ((Status) tweet).getId();
|
||||
if (tweet instanceof Tweet) {
|
||||
id = ((Tweet) tweet).getId();
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unsupported type of Twitter message: " + tweet.getClass());
|
||||
|
||||
@@ -19,10 +19,10 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
|
||||
/**
|
||||
@@ -32,7 +32,7 @@ import twitter4j.Paging;
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class DirectMessageReceivingMessageSource extends AbstractTwitterMessageSource<DirectMessage> {
|
||||
public class DirectMessageReceivingMessageSource extends AbstractTwitterMessageSource<Tweet> {
|
||||
|
||||
public DirectMessageReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
@@ -50,7 +50,7 @@ public class DirectMessageReceivingMessageSource extends AbstractTwitterMessageS
|
||||
try {
|
||||
long sinceId = getMarkerId();
|
||||
if (tweets.size() <= prefetchThreshold){
|
||||
List<twitter4j.DirectMessage> dms = !hasMarkedStatus()
|
||||
List<Tweet> dms = !hasMarkedStatus()
|
||||
? twitter.getDirectMessages()
|
||||
: twitter.getDirectMessages(new Paging(sinceId));
|
||||
|
||||
@@ -74,9 +74,9 @@ public class DirectMessageReceivingMessageSource extends AbstractTwitterMessageS
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
protected Comparator getComparator() {
|
||||
return new Comparator<DirectMessage>() {
|
||||
public int compare(DirectMessage directMessage, DirectMessage directMessage1) {
|
||||
return directMessage.getCreatedAt().compareTo(directMessage1.getCreatedAt());
|
||||
return new Comparator<Tweet>() {
|
||||
public int compare(Tweet tweet1, Tweet tweet2) {
|
||||
return tweet1.getCreatedAt().compareTo(tweet2.getCreatedAt());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,11 +18,10 @@ package org.springframework.integration.twitter.inbound;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* Handles forwarding all new {@link twitter4j.Status} that are 'replies' or 'mentions' to some other tweet.
|
||||
@@ -30,7 +29,7 @@ import twitter4j.Twitter;
|
||||
* @author Josh Long
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class MentionReceivingMessageSource extends AbstractTwitterMessageSource<Status> {
|
||||
public class MentionReceivingMessageSource extends AbstractTwitterMessageSource<Tweet> {
|
||||
|
||||
public MentionReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
@@ -46,7 +45,7 @@ public class MentionReceivingMessageSource extends AbstractTwitterMessageSource<
|
||||
try {
|
||||
long sinceId = getMarkerId();
|
||||
if (tweets.size() <= prefetchThreshold){
|
||||
List<twitter4j.Status> stats = (!hasMarkedStatus())
|
||||
List<Tweet> stats = (!hasMarkedStatus())
|
||||
? twitter.getMentions()
|
||||
: twitter.getMentions(new Paging(sinceId));
|
||||
forwardAll(stats);
|
||||
|
||||
@@ -15,12 +15,13 @@
|
||||
*/
|
||||
package org.springframework.integration.twitter.inbound;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
|
||||
/**
|
||||
@@ -31,7 +32,7 @@ import twitter4j.Twitter;
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class TimelineUpdateReceivingMessageSource extends AbstractTwitterMessageSource<Status> {
|
||||
public class TimelineUpdateReceivingMessageSource extends AbstractTwitterMessageSource<Tweet> {
|
||||
|
||||
public TimelineUpdateReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
@@ -48,9 +49,8 @@ public class TimelineUpdateReceivingMessageSource extends AbstractTwitterMessage
|
||||
try {
|
||||
long sinceId = getMarkerId();
|
||||
if (tweets.size() <= prefetchThreshold){
|
||||
forwardAll(!hasMarkedStatus()
|
||||
? twitter.getFriendsTimeline()
|
||||
: twitter.getFriendsTimeline(new Paging(sinceId)));
|
||||
List<Tweet> tweets = !hasMarkedStatus() ? twitter.getFriendsTimeline() : twitter.getFriendsTimeline(new Paging(sinceId));
|
||||
forwardAll(tweets);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (e instanceof RuntimeException){
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public abstract class AbstractOutboundTwitterEndpointSupport extends AbstractMessageHandler {
|
||||
protected final TwitterOperations twitter;
|
||||
protected final OutboundStatusUpdateMessageMapper supportStatusUpdate = new OutboundStatusUpdateMessageMapper();
|
||||
protected final OutboundTweetMessageMapper outboundMaper = new OutboundTweetMessageMapper();
|
||||
|
||||
public AbstractOutboundTwitterEndpointSupport(TwitterOperations twitter){
|
||||
Assert.notNull(twitter, "'twitter' must not be null");
|
||||
|
||||
@@ -18,10 +18,9 @@ package org.springframework.integration.twitter.outbound;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.mapping.OutboundMessageMapper;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterHeaders;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import twitter4j.GeoLocation;
|
||||
import twitter4j.StatusUpdate;
|
||||
|
||||
/**
|
||||
@@ -33,7 +32,7 @@ import twitter4j.StatusUpdate;
|
||||
* @see twitter4j.StatusUpdate
|
||||
* @see org.springframework.integration.twitter.core.TwitterHeaders
|
||||
*/
|
||||
public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<StatusUpdate> {
|
||||
public class OutboundTweetMessageMapper implements OutboundMessageMapper<Tweet> {
|
||||
|
||||
/**
|
||||
* {@link StatusUpdate} instances are used to drive status updates.
|
||||
@@ -41,45 +40,45 @@ public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<
|
||||
* @param message the inbound messages
|
||||
* @return a {@link StatusUpdate} that's been materialized from the inbound message
|
||||
*/
|
||||
public StatusUpdate fromMessage(Message<?> message) {
|
||||
public Tweet fromMessage(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
StatusUpdate statusUpdate = null;
|
||||
Tweet tweet = null;
|
||||
if (payload instanceof String) {
|
||||
statusUpdate = new StatusUpdate((String) payload);
|
||||
tweet = new Tweet();
|
||||
if (message.getHeaders().containsKey(TwitterHeaders.IN_REPLY_TO_STATUS_ID)) {
|
||||
Long replyId = (Long) message.getHeaders().get(TwitterHeaders.IN_REPLY_TO_STATUS_ID);
|
||||
if ((replyId != null) && (replyId > 0)) {
|
||||
statusUpdate.inReplyToStatusId(replyId);
|
||||
}
|
||||
}
|
||||
if (message.getHeaders().containsKey(TwitterHeaders.PLACE_ID)) {
|
||||
String placeId = (String) message.getHeaders().get(TwitterHeaders.PLACE_ID);
|
||||
if (StringUtils.hasText(placeId)) {
|
||||
statusUpdate.placeId(placeId);
|
||||
}
|
||||
}
|
||||
if (message.getHeaders().containsKey(TwitterHeaders.GEOLOCATION)) {
|
||||
GeoLocation geoLocation = (GeoLocation) message.getHeaders().get(TwitterHeaders.GEOLOCATION);
|
||||
if (null != geoLocation) {
|
||||
statusUpdate.location(geoLocation);
|
||||
}
|
||||
}
|
||||
if (message.getHeaders().containsKey(TwitterHeaders.DISPLAY_COORDINATES)) {
|
||||
Boolean displayCoords = (Boolean) message.getHeaders().get(TwitterHeaders.DISPLAY_COORDINATES);
|
||||
if (displayCoords != null) {
|
||||
statusUpdate.displayCoordinates(displayCoords);
|
||||
tweet.setToUserId(replyId);
|
||||
}
|
||||
}
|
||||
// if (message.getHeaders().containsKey(TwitterHeaders.PLACE_ID)) {
|
||||
// String placeId = (String) message.getHeaders().get(TwitterHeaders.PLACE_ID);
|
||||
// if (StringUtils.hasText(placeId)) {
|
||||
// statusUpdate.placeId(placeId);
|
||||
// }
|
||||
// }
|
||||
// if (message.getHeaders().containsKey(TwitterHeaders.GEOLOCATION)) {
|
||||
// GeoLocation geoLocation = (GeoLocation) message.getHeaders().get(TwitterHeaders.GEOLOCATION);
|
||||
// if (null != geoLocation) {
|
||||
// statusUpdate.location(geoLocation);
|
||||
// }
|
||||
// }
|
||||
// if (message.getHeaders().containsKey(TwitterHeaders.DISPLAY_COORDINATES)) {
|
||||
// Boolean displayCoords = (Boolean) message.getHeaders().get(TwitterHeaders.DISPLAY_COORDINATES);
|
||||
// if (displayCoords != null) {
|
||||
// statusUpdate.displayCoordinates(displayCoords);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
else if (payload instanceof StatusUpdate) {
|
||||
statusUpdate = (StatusUpdate) payload;
|
||||
else if (payload instanceof Tweet) {
|
||||
tweet = (Tweet) payload;
|
||||
}
|
||||
else {
|
||||
throw new MessageHandlingException(message,
|
||||
"Failed to create StatusUpdate from payload of type '" + message.getPayload().getClass() +
|
||||
"'. Only java.lang.String and twitter4j.StatusUpdate are currently supported.");
|
||||
"Failed to create Tweet from payload of type '" + message.getPayload().getClass() +
|
||||
"'. Only java.lang.String and org.springframework.integration.twitter.core.Tweet are currently supported.");
|
||||
}
|
||||
return statusUpdate;
|
||||
return tweet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,16 +16,15 @@
|
||||
package org.springframework.integration.twitter.outbound;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.StatusUpdate;
|
||||
|
||||
|
||||
/**
|
||||
* This class is useful for both sending regular status updates as well as 'replies' or 'mentions'
|
||||
*
|
||||
* @author Josh Long
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class TimelineUpdateSendingMessageHandler extends AbstractOutboundTwitterEndpointSupport {
|
||||
@@ -36,9 +35,8 @@ public class TimelineUpdateSendingMessageHandler extends AbstractOutboundTwitter
|
||||
|
||||
@Override
|
||||
protected void handleMessageInternal(Message<?> message) throws Exception {
|
||||
StatusUpdate statusUpdate = this.supportStatusUpdate.fromMessage(message);
|
||||
Assert.notNull(statusUpdate, "couldn't send message, unable to build a StatusUpdate instance correctly");
|
||||
this.twitter.updateStatus(statusUpdate);
|
||||
Tweet tweet = this.outboundMaper.fromMessage(message);
|
||||
this.twitter.updateStatus(tweet);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -110,8 +110,10 @@ public class Twitter4jTemplateTests {
|
||||
|
||||
@Test
|
||||
public void testUpdateStatus() throws Exception{
|
||||
StatusUpdate status = new StatusUpdate("writing twitter test");
|
||||
template.updateStatus(status);
|
||||
Tweet tweet = new Tweet();
|
||||
tweet.setToUserId((long) 123);
|
||||
tweet.setText("writing twitter test");
|
||||
template.updateStatus(tweet);
|
||||
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,27 +26,28 @@
|
||||
<channel id="inbound_mentions"/>
|
||||
<channel id="inbound_updates"/>
|
||||
|
||||
<twitter:twitter-connection id="tc"
|
||||
access-token="${twitter.oauth.accessToken}"
|
||||
access-token-secret="${twitter.oauth.accessTokenSecret}"
|
||||
consumer-key="${twitter.oauth.consumerKey}"
|
||||
consumer-secret="${twitter.oauth.consumerSecret}"/>
|
||||
<beans:bean id="twitterTemplate" class="org.springframework.integration.twitter.core.Twitter4jTemplate">
|
||||
<beans:constructor-arg value="${twitter.oauth.consumerKey}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.consumerSecret}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.accessToken}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.accessTokenSecret}"/>
|
||||
</beans:bean>
|
||||
|
||||
|
||||
<twitter:inbound-mention-channel-adapter twitter-connection="tc" channel="inbound_mentions">
|
||||
<poller fixed-rate="5000" max-messages-per-poll="3"/>
|
||||
</twitter:inbound-mention-channel-adapter>
|
||||
<service-activator input-channel="inbound_mentions" ref="twitterAnnouncer" method="mention"/>
|
||||
<!-- <twitter:inbound-mention-channel-adapter twitter-template="twitterTemplate" channel="inbound_mentions">-->
|
||||
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
|
||||
<!-- </twitter:inbound-mention-channel-adapter>-->
|
||||
<!-- <service-activator input-channel="inbound_mentions" ref="twitterAnnouncer" method="mention"/>-->
|
||||
|
||||
<twitter:inbound-dm-channel-adapter twitter-connection="tc" channel="inbound_dm">
|
||||
<twitter:inbound-dm-channel-adapter twitter-template="twitterTemplate" channel="inbound_dm">
|
||||
<poller fixed-rate="5000" max-messages-per-poll="3"/>
|
||||
</twitter:inbound-dm-channel-adapter>
|
||||
<service-activator input-channel="inbound_dm" ref="twitterAnnouncer" method="dm"/>
|
||||
|
||||
<twitter:inbound-update-channel-adapter id="twitterInbound" twitter-connection="tc" channel="inbound_updates">
|
||||
<poller fixed-rate="5000" max-messages-per-poll="3"/>
|
||||
</twitter:inbound-update-channel-adapter>
|
||||
<service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>
|
||||
<!-- <twitter:inbound-update-channel-adapter id="twitterInbound" twitter-template="twitterTemplate" channel="inbound_updates">-->
|
||||
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
|
||||
<!-- </twitter:inbound-update-channel-adapter>-->
|
||||
<!-- <service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>-->
|
||||
|
||||
<beans:bean id="twitterAnnouncer" class="org.springframework.integration.twitter.ignored.TwitterAnnouncer"/>
|
||||
</beans:beans>
|
||||
|
||||
@@ -15,24 +15,22 @@
|
||||
*/
|
||||
package org.springframework.integration.twitter.ignored;
|
||||
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Status;
|
||||
|
||||
|
||||
@Component
|
||||
public class TwitterAnnouncer {
|
||||
public void dm(DirectMessage directMessage) {
|
||||
public void dm(Tweet directMessage) {
|
||||
System.out.println("A direct message has been received from " +
|
||||
directMessage.getSender().getScreenName() + " with text " + directMessage.getText());
|
||||
directMessage.getFromUser() + " with text " + directMessage.getText());
|
||||
}
|
||||
|
||||
public void mention(Status s) {
|
||||
public void mention(Tweet s) {
|
||||
System.out.println("A tweet mentioning (or replying) to " + "you was received having text " + s.getText() + " from " + s.getSource());
|
||||
}
|
||||
|
||||
public void updates(Status t) {
|
||||
public void updates(Tweet t) {
|
||||
System.out.println("Received timeline update: " + t.getText() + " from " + t.getSource());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.twitter.core.Tweet;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -43,9 +44,9 @@ import twitter4j.ResponseList;
|
||||
*/
|
||||
public class DirectMessageReceivingMessageSourceTests {
|
||||
|
||||
private DirectMessage firstMessage;
|
||||
private Tweet firstMessage;
|
||||
|
||||
private DirectMessage secondMessage;
|
||||
private Tweet secondMessage;
|
||||
|
||||
private TwitterOperations twitter;
|
||||
|
||||
@@ -53,12 +54,12 @@ public class DirectMessageReceivingMessageSourceTests {
|
||||
@Before
|
||||
public void prepare() throws Exception{
|
||||
twitter = mock(TwitterOperations.class);
|
||||
firstMessage = mock(DirectMessage.class);
|
||||
firstMessage = mock(Tweet.class);
|
||||
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
|
||||
when(firstMessage.getId()).thenReturn(200);
|
||||
secondMessage = mock(DirectMessage.class);
|
||||
when(firstMessage.getId()).thenReturn((long) 200);
|
||||
secondMessage = mock(Tweet.class);
|
||||
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
|
||||
when(secondMessage.getId()).thenReturn(2000);
|
||||
when(secondMessage.getId()).thenReturn((long) 2000);
|
||||
|
||||
|
||||
when(twitter.getProfileId()).thenReturn("kermit");
|
||||
@@ -95,7 +96,7 @@ public class DirectMessageReceivingMessageSourceTests {
|
||||
Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
|
||||
assertTrue(!CollectionUtils.isEmpty(msg));
|
||||
assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by
|
||||
DirectMessage message = (DirectMessage) msg.poll();
|
||||
Tweet message = (Tweet) msg.poll();
|
||||
assertEquals(secondMessage, message);
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.mockito.Mockito;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
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.TwitterHeaders;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
@@ -58,8 +59,10 @@ public class TimelineUpdateSendingMessageHandlerTests {
|
||||
@Test
|
||||
public void testSendingStatusUpdate() throws Exception{
|
||||
TimelineUpdateSendingMessageHandler handler = new TimelineUpdateSendingMessageHandler(twitterOperations);
|
||||
handler.handleMessage(new GenericMessage("writing twitter tests"));
|
||||
verify(twitterOperations, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
|
||||
Tweet tweet = new Tweet();
|
||||
tweet.setText("writing twitter tests");
|
||||
handler.handleMessage(new GenericMessage(tweet));
|
||||
verify(twitterOperations, times(1)).updateStatus(Mockito.any(Tweet.class));
|
||||
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
|
||||
}
|
||||
@Test
|
||||
@@ -72,7 +75,7 @@ public class TimelineUpdateSendingMessageHandlerTests {
|
||||
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true)
|
||||
.build();
|
||||
handler.handleMessage(message);
|
||||
verify(twitterOperations, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
|
||||
verify(twitterOperations, times(1)).updateStatus(Mockito.any(Tweet.class));
|
||||
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# oauth setup for prosibook twitter account
|
||||
twitter.oauth.consumerKey=
|
||||
twitter.oauth.consumerSecret=
|
||||
twitter.oauth.pin=
|
||||
twitter.oauth.accessToken=
|
||||
twitter.oauth.accessTokenSecret=
|
||||
twitter.oauth.consumerKey=OU4CbkHKIWl1SI0VwiOgAQ
|
||||
twitter.oauth.consumerSecret=p6pPukUG6d0ebSXuLSI9iaq2MIpzxJoLZyj6ilRmO3o
|
||||
twitter.oauth.accessToken=61091649-vWVUNginOL069jjpm2lHEuxGXjW163kK3CPNZCdcc
|
||||
twitter.oauth.accessTokenSecret=dufKyVlcngRitGDFfqiTlJC5leh6lsxPaq8FbeBc
|
||||
Reference in New Issue
Block a user