refactored the twitter package to use interface types for the domain model, which gives us wiggle room to move to Spring Social

This commit is contained in:
Josh Long
2010-10-22 19:34:12 -07:00
parent 3ddde2e450
commit 82ac95b7c6
11 changed files with 332 additions and 11 deletions

View File

@@ -62,9 +62,9 @@ public abstract class AbstractInboundTwitterEndpointSupport<T> extends AbstractE
abstract protected void markLastStatusId(T statusId);
abstract protected List<T> sort(List<T> rl);
abstract protected List<T> sort( List<T> rl);
protected void forwardAll(ResponseList<T> tResponses) {
protected void forwardAll( List<T> tResponses) {
List<T> stats = new ArrayList<T>();
for (T t : tResponses)

View File

@@ -15,7 +15,12 @@
*/
package org.springframework.integration.twitter;
import twitter4j.Status;
//import twitter4j.Status;
//import twitter4j.Status;
import org.springframework.integration.twitter.model.Status;
import org.springframework.integration.twitter.model.Twitter4jStatusImpl;
import java.util.ArrayList;
import java.util.Collections;
@@ -29,12 +34,19 @@ import java.util.List;
*
* @author Josh Long
*/
abstract public class AbstractInboundTwitterStatusEndpointSupport extends AbstractInboundTwitterEndpointSupport<Status> {
abstract public class AbstractInboundTwitterStatusEndpointSupport
extends AbstractInboundTwitterEndpointSupport<Status> {
private Comparator<Status> statusComparator = new Comparator<Status>() {
public int compare(Status status, Status status1) {
return status.getCreatedAt().compareTo(status1.getCreatedAt());
}
};
protected List<Status> fromTwitter4jStatus(List<twitter4j.Status> stats) {
List<Status> fwd = new ArrayList<Status>();
for (twitter4j.Status s : stats)
fwd.add(new Twitter4jStatusImpl(s));
return fwd;
}
@Override
protected void markLastStatusId(Status statusId) {

View File

@@ -15,8 +15,12 @@
*/
package org.springframework.integration.twitter;
import twitter4j.DirectMessage;
//import twitter4j.DirectMessage;
import org.springframework.integration.twitter.model.DirectMessage;
import org.springframework.integration.twitter.model.Twitter4jDirectMessageImpl;
import twitter4j.Paging;
import twitter4j.Twitter;
import java.util.ArrayList;
@@ -44,8 +48,11 @@ public class InboundDirectMessageStatusEndpoint extends AbstractInboundTwitterEn
@Override
protected List<DirectMessage> sort(List<DirectMessage> rl) {
List<DirectMessage> dms = new ArrayList<DirectMessage>();
dms.addAll(rl);
Collections.sort(dms, dmComparator);
return dms;
@@ -56,8 +63,18 @@ public class InboundDirectMessageStatusEndpoint extends AbstractInboundTwitterEn
this.runAsAPIRateLimitsPermit(new ApiCallback<InboundDirectMessageStatusEndpoint>() {
public void run(InboundDirectMessageStatusEndpoint t, Twitter twitter)
throws Exception {
forwardAll((!hasMarkedStatus()) ? t.twitter.getDirectMessages() : t.twitter.getDirectMessages(new Paging(t.getMarkerId())));
List<twitter4j.DirectMessage> dms = !hasMarkedStatus() ? t.twitter.getDirectMessages() : t.twitter.getDirectMessages(new Paging(t.getMarkerId()));
List<DirectMessage> dmsToFwd = new ArrayList<DirectMessage>();
for( twitter4j.DirectMessage dm : dms)
dmsToFwd.add( new Twitter4jDirectMessageImpl( dm));
forwardAll( dmsToFwd );
}
});
}
}

View File

@@ -18,19 +18,29 @@ package org.springframework.integration.twitter;
import twitter4j.Paging;
import twitter4j.Twitter;
import java.util.List;
/**
* Handles forwarding all new {@link twitter4j.Status} that are 'replies' or 'mentions' to some other tweet.
*
* @author Josh Long
*/
public class InboundMentionStatusEndpoint extends AbstractInboundTwitterStatusEndpointSupport {
public class InboundMentionStatusEndpoint
extends AbstractInboundTwitterStatusEndpointSupport {
@Override
protected void refresh() throws Exception {
this.runAsAPIRateLimitsPermit(new ApiCallback<InboundMentionStatusEndpoint>() {
public void run(InboundMentionStatusEndpoint ctx, Twitter twitter)
throws Exception {
forwardAll((!hasMarkedStatus()) ? twitter.getMentions() : twitter.getMentions(new Paging(ctx.getMarkerId())));
public void run(InboundMentionStatusEndpoint ctx,
Twitter twitter) throws Exception {
List<twitter4j.Status> stats = (!hasMarkedStatus())
? twitter.getMentions()
: twitter.getMentions(new Paging(ctx.getMarkerId()));
forwardAll( fromTwitter4jStatus( stats));
}
});
}

View File

@@ -32,7 +32,9 @@ public class InboundUpdatedStatusEndpoint extends AbstractInboundTwitterStatusEn
this.runAsAPIRateLimitsPermit(new ApiCallback<InboundUpdatedStatusEndpoint>() {
public void run(InboundUpdatedStatusEndpoint t, Twitter twitter)
throws Exception {
forwardAll((!t.hasMarkedStatus()) ? twitter.getFriendsTimeline() : twitter.getFriendsTimeline(new Paging(t.getMarkerId())));
forwardAll( fromTwitter4jStatus(!t.hasMarkedStatus()
? twitter.getFriendsTimeline() :
twitter.getFriendsTimeline(new Paging(t.getMarkerId()))));
}
});
}

View File

@@ -0,0 +1,21 @@
package org.springframework.integration.twitter.model;
/**
* Describes a direct-message in twitter. (Also known as a "DM").
* <p/>
* these are messages sent privately to a user.
*
* @author Josh Long
*/
public interface DirectMessage {
int getId();
java.lang.String getText();
java.util.Date getCreatedAt();
User getSender() ;
User getRecipient();
}

View File

@@ -0,0 +1,42 @@
package org.springframework.integration.twitter.model;
/**
* describes a simple status message on twitter. this could be a message
* that updates a timeline on behalf of a user, or it could be a message that contains a reply.
*
*
* @author Josh Long
*/
public interface Status {
java.util.Date getCreatedAt();
long getId();
java.lang.String getText();
java.lang.String getSource();
boolean isTruncated();
long getInReplyToStatusId();
int getInReplyToUserId();
java.lang.String getInReplyToScreenName();
//twitter4j.GeoLocation getGeoLocation();
//twitter4j.Place getPlace();
boolean isFavorited();
User getUser();
boolean isRetweet();
Status getRetweetedStatus();
java.lang.String[] getContributors();
}

View File

@@ -0,0 +1,57 @@
package org.springframework.integration.twitter.model;
import twitter4j.DirectMessage;
import java.util.Date;
/**
* @author Josh Long
*/
public class Twitter4jDirectMessageImpl implements org.springframework.integration.twitter.model.DirectMessage {
private DirectMessage directMessage;
public Twitter4jDirectMessageImpl(DirectMessage directMessage) {
this.directMessage = directMessage;
}
public DirectMessage getDirectMessage() {
return this.directMessage;
}
public int getId() {
return this.directMessage.getId();
}
public User getSender() {
return new Twitter4jUserImpl(this.directMessage.getSender());
}
public User getRecipient() {
return new Twitter4jUserImpl(this.directMessage.getRecipient());
}
public String getText() {
return this.directMessage.getText();
}
public int getSenderId() {
return this.directMessage.getSenderId();
}
public int getRecipientId() {
return this.directMessage.getRecipientId();
}
public Date getCreatedAt() {
return this.directMessage.getCreatedAt();
}
public String getSenderScreenName() {
return this.directMessage.getSenderScreenName();
}
public String getRecipientScreenName() {
return this.directMessage.getRecipientScreenName();
}
}

View File

@@ -0,0 +1,70 @@
package org.springframework.integration.twitter.model;
import java.util.Date;
/**
*
* An implemention o the {@link org.springframework.integration.twitter.model.Status} interface that
* forwards requests to a {@link twitter4j.Status} implementation
*
* @author Josh Long
*/
public class Twitter4jStatusImpl implements Status {
private twitter4j.Status status;
public Twitter4jStatusImpl(twitter4j.Status s) {
this.status = s;
}
public Date getCreatedAt() {
return this.status.getCreatedAt();
}
public long getId() {
return this.status.getId();
}
public String getText() {
return this.status.getText();
}
public String getSource() {
return this.status.getSource();
}
public boolean isTruncated() {
return this.status.isTruncated();
}
public long getInReplyToStatusId() {
return this.status.getInReplyToStatusId();
}
public int getInReplyToUserId() {
return this.status.getInReplyToUserId();
}
public String getInReplyToScreenName() {
return this.status.getInReplyToScreenName();
}
public boolean isFavorited() {
return this.status.isFavorited();
}
public User getUser() {
return new Twitter4jUserImpl(this.status.getUser());
}
public boolean isRetweet() {
return this.status.isRetweet();
}
public Status getRetweetedStatus() {
return new Twitter4jStatusImpl(this.status.getRetweetedStatus());
}
public String[] getContributors() {
return this.status.getContributors();
}
}

View File

@@ -0,0 +1,60 @@
package org.springframework.integration.twitter.model;
import twitter4j.User;
import java.net.URL;
/**
* implementation of the User interfce to represent Users in a Twitter application
*
* @author Josh Long
*/
public class Twitter4jUserImpl implements org.springframework.integration.twitter.model.User {
private twitter4j.User user;
public Twitter4jUserImpl(User u) {
this.user = u;
}
public int getId() {
return this.user.getId();
}
public String getName() {
return user.getName();
}
public String getScreenName() {
return this.user.getScreenName();
}
public String getLocation() {
return this.user.getLocation();
}
public String getDescription() {
return user.getDescription();
}
public boolean isContributorsEnabled() {
return user.isContributorsEnabled();
}
public URL getProfileImageURL() {
return user.getProfileImageURL();
}
public URL getURL() {
return this.user.getURL();
}
public boolean isProtected() {
return this.user.isProtected();
}
public int getFollowersCount() {
return this.user.getFollowersCount();
}
}

View File

@@ -0,0 +1,30 @@
package org.springframework.integration.twitter.model;
/**
* describes the user producing or receiving messages
*
* @author Josh Long
*/
public interface User {
int getId();
java.lang.String getName();
java.lang.String getScreenName();
java.lang.String getLocation();
java.lang.String getDescription();
boolean isContributorsEnabled();
java.net.URL getProfileImageURL();
java.net.URL getURL();
boolean isProtected();
int getFollowersCount();
}