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:55:22 -07:00
parent 82ac95b7c6
commit 39f9e2b9e4
5 changed files with 74 additions and 58 deletions

View File

@@ -16,8 +16,11 @@
package org.springframework.integration.twitter;
import org.springframework.integration.Message;
import org.springframework.integration.twitter.model.GeoLocation;
import org.springframework.integration.twitter.model.Twitter4jGeoLocationImpl;
import org.springframework.util.StringUtils;
import twitter4j.GeoLocation;
import twitter4j.StatusUpdate;
@@ -30,58 +33,71 @@ import twitter4j.StatusUpdate;
* @since 2.0
*/
public class StatusUpdateSupport {
/**
* {@link StatusUpdate} instances are used to drive status updates.
*
* @param message the inbound messages
* @return a {@link StatusUpdate} that's been materialized from the inbound message
* @throws Throwable thrown if something goes wrong
*/
public StatusUpdate fromMessage(Message<?> message)
throws Throwable {
Object payload = message.getPayload();
StatusUpdate statusUpdate = null;
/**
* {@link StatusUpdate} instances are used to drive status updates.
*
* @param message the inbound messages
* @return a {@link StatusUpdate} that's been materialized from the inbound message
* @throws Throwable thrown if something goes wrong
*/
public StatusUpdate fromMessage(Message<?> message)
throws Throwable {
Object payload = message.getPayload();
StatusUpdate statusUpdate = null;
if (payload instanceof String) {
statusUpdate = new StatusUpdate((String) payload);
if (payload instanceof String) {
statusUpdate = new StatusUpdate((String) payload);
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID)) {
Long replyId = (Long) message.getHeaders().get(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID);
if (message.getHeaders()
.containsKey(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID)) {
Long replyId = (Long) message.getHeaders()
.get(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID);
if ((replyId != null) && (replyId > 0)) {
statusUpdate.inReplyToStatusId(replyId);
}
}
if ((replyId != null) && (replyId > 0)) {
statusUpdate.inReplyToStatusId(replyId);
}
}
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_PLACE_ID)) {
String placeId = (String) message.getHeaders().get(TwitterHeaders.TWITTER_PLACE_ID);
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_PLACE_ID)) {
String placeId = (String) message.getHeaders()
.get(TwitterHeaders.TWITTER_PLACE_ID);
if (StringUtils.hasText(placeId)) {
statusUpdate.placeId(placeId);
}
}
if (StringUtils.hasText(placeId)) {
statusUpdate.placeId(placeId);
}
}
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_GEOLOCATION)) {
GeoLocation geoLocation = (GeoLocation) message.getHeaders().get(TwitterHeaders.TWITTER_GEOLOCATION);
if (message.getHeaders()
.containsKey(TwitterHeaders.TWITTER_GEOLOCATION)) {
GeoLocation geoLocation = (GeoLocation) message.getHeaders()
.get(TwitterHeaders.TWITTER_GEOLOCATION);
twitter4j.GeoLocation gl = null;
if (null != geoLocation) {
statusUpdate.location(geoLocation);
}
}
if (geoLocation instanceof Twitter4jGeoLocationImpl) {
gl = ((Twitter4jGeoLocationImpl) geoLocation).getGeoLocation();
}
if (message.getHeaders().containsKey(TwitterHeaders.TWITTER_DISPLAY_COORDINATES)) {
Boolean displayCoords = (Boolean) message.getHeaders().get(TwitterHeaders.TWITTER_DISPLAY_COORDINATES);
if (null != gl) {
statusUpdate.location(gl);
}
}
if (displayCoords != null) {
statusUpdate.displayCoordinates(displayCoords);
}
}
}
if (message.getHeaders()
.containsKey(TwitterHeaders.TWITTER_DISPLAY_COORDINATES)) {
Boolean displayCoords = (Boolean) message.getHeaders()
.get(TwitterHeaders.TWITTER_DISPLAY_COORDINATES);
if (payload instanceof StatusUpdate) {
statusUpdate = (StatusUpdate) payload;
}
if ((displayCoords != null) &&
displayCoords.equals(Boolean.TRUE)) {
statusUpdate.displayCoordinates(displayCoords);
}
}
}
return statusUpdate;
}
if (payload instanceof StatusUpdate) {
statusUpdate = (StatusUpdate) payload;
}
return statusUpdate;
}
}

View File

@@ -25,10 +25,6 @@ public interface Status {
java.lang.String getInReplyToScreenName();
//twitter4j.GeoLocation getGeoLocation();
//twitter4j.Place getPlace();
boolean isFavorited();
User getUser();

View File

@@ -37,14 +37,17 @@ import twitter4j.GeoLocation;
"/org/springframework/integration/twitter/sending_dms_using_ns.xml"}
)
public class TestSendingDMsUsingNamespace extends AbstractJUnit4SpringContextTests {
private volatile MessagingTemplate messagingTemplate = new MessagingTemplate();
@Value("#{out}")
private MessageChannel channel;
@Value("#{out}") private MessageChannel channel;
@Test
@Ignore
public void testSendingATweet() throws Throwable {
String dmUsr = System.getProperties().getProperty("twitter.dm.user");
MessageBuilder<String> mb = MessageBuilder.withPayload("'Hello world!', from the Spring Integration outbound Twitter adapter")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
@@ -53,8 +56,6 @@ public class TestSendingDMsUsingNamespace extends AbstractJUnit4SpringContextTes
mb.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, dmUsr);
}
Message<String> m = mb.build();
this.messagingTemplate.send(this.channel, m);
this.messagingTemplate.send(this.channel, mb.build());
}
}

View File

@@ -36,9 +36,10 @@ import twitter4j.GeoLocation;
"/org/springframework/integration/twitter/sending_updates_using_ns.xml"}
)
public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContextTests {
private MessagingTemplate messagingTemplate = new MessagingTemplate();
@Value("#{out}")
private MessageChannel channel;
@Value("#{out}") private MessageChannel channel;
@Test
@Ignore

View File

@@ -1,14 +1,16 @@
package org.springframework.integration.twitter;
import org.springframework.integration.twitter.model.DirectMessage;
import org.springframework.integration.twitter.model.Status;
import org.springframework.stereotype.Component;
import twitter4j.DirectMessage;
import twitter4j.Status;
@Component
public class TwitterAnnouncer {
public void dm(DirectMessage directMessage) {
System.out.println("A direct message has been received from " + directMessage.getSenderScreenName() + " with text " + directMessage.getText());
System.out.println("A direct message has been received from " +
directMessage.getSender().getScreenName() + " with text " + directMessage.getText());
}
public void mention(Status s) {