INT-1471 removed the TWITTER_ prefix on the header constant definitions, and switched to camelCase for consistency

This commit is contained in:
Mark Fisher
2010-10-27 22:54:04 -04:00
committed by Chris Beams
parent 0be4c52287
commit 90cc137faf
6 changed files with 67 additions and 78 deletions

View File

@@ -17,23 +17,23 @@
package org.springframework.integration.twitter.core;
/**
* An enum to allow users to express interest in particular kinds of tweets.
* <p/>
* Contains header keys used by the various adapters.
* Header keys used by the various Twitter adapters.
*
* @author Josh Long
* @since 2.0
*/
public class TwitterHeaders {
public abstract class TwitterHeaders {
public static final String TWITTER_IN_REPLY_TO_STATUS_ID = "TWITTER_IN_REPLY_TO_STATUS_ID";
private static final String PREFIX = "twitter_";
public static final String TWITTER_PLACE_ID = "TWITTER_PLACE_ID";
public static final String IN_REPLY_TO_STATUS_ID = PREFIX + "inReplyToStatusId";
public static final String TWITTER_GEOLOCATION = "TWITTER_GEOLOCATION";
public static final String PLACE_ID = PREFIX + "placeId";
public static final String TWITTER_DISPLAY_COORDINATES = "TWITTER_DISPLAY_COORDINATES";
public static final String GEOLOCATION = PREFIX + "geolocation";
public static final String TWITTER_DM_TARGET_USER_ID = "TWITTER_DM_TARGET_USER_ID";
public static final String DISPLAY_COORDINATES = PREFIX + "displayCoordinates";
public static final String DM_TARGET_USER_ID = PREFIX + "dmTargetUserId";
}

View File

@@ -1,5 +1,5 @@
/*
* 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import org.springframework.integration.Message;
@@ -22,7 +23,6 @@ import org.springframework.util.Assert;
import twitter4j.TwitterException;
/**
* Simple adapter to support sending outbound direct messages ("DM"s) using twitter
*
@@ -34,29 +34,29 @@ public class OutboundDirectMessageMessageHandler extends AbstractOutboundTwitter
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
if (this.twitter == null){
if (this.twitter == null) {
this.afterPropertiesSet();
}
try {
Object payload = (String) message.getPayload();
Assert.isInstanceOf(String.class, payload, "Only payload of type String is supported. If your payload " +
Assert.isInstanceOf(String.class, message.getPayload(), "Only payload of type String is supported. If your payload " +
"is not of type String you may want to introduce transformer");
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.TWITTER_DM_TARGET_USER_ID),
"You must provide '" + TwitterHeaders.TWITTER_DM_TARGET_USER_ID + "' header");
Object toUser = message.getHeaders().get(TwitterHeaders.TWITTER_DM_TARGET_USER_ID);
Assert.state(toUser instanceof String || toUser instanceof Integer,
"the header '" + TwitterHeaders.TWITTER_DM_TARGET_USER_ID +
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.DM_TARGET_USER_ID),
"the '" + TwitterHeaders.DM_TARGET_USER_ID + "' header is required");
Object toUser = message.getHeaders().get(TwitterHeaders.DM_TARGET_USER_ID);
Assert.isTrue(toUser instanceof String || toUser instanceof Integer,
"the header '" + TwitterHeaders.DM_TARGET_USER_ID +
"' must be either a String (a screenname) or an int (a user ID)");
String payload = (String) message.getPayload();
if (toUser instanceof Integer) {
this.twitter.sendDirectMessage((Integer) toUser, (String) payload);
this.twitter.sendDirectMessage((Integer) toUser, payload);
}
else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, (String) payload);
this.twitter.sendDirectMessage((String) toUser, payload);
}
} catch (TwitterException e) {
}
catch (TwitterException e) {
throw new MessageHandlingException(message, e);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import org.springframework.integration.Message;
@@ -24,16 +25,17 @@ import org.springframework.util.StringUtils;
import twitter4j.GeoLocation;
import twitter4j.StatusUpdate;
/**
* Convenience class that can take a seemingly disparate jumble of headers and a payload and do a best-faith attempt at vending a {@link twitter4j.StatusUpdate} instance
* Convenience class that maps headers and a payload to a {@link twitter4j.StatusUpdate} instance.
*
* @author Josh Long
* @author Mark Fisher
* @since 2.0
* @see twitter4j.StatusUpdate
* @see org.springframework.integration.twitter.core.TwitterHeaders
* @since 2.0
*/
public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<StatusUpdate> {
/**
* {@link StatusUpdate} instances are used to drive status updates.
*
@@ -43,59 +45,42 @@ public class OutboundStatusUpdateMessageMapper implements OutboundMessageMapper<
public StatusUpdate fromMessage(Message<?> message) {
Object payload = message.getPayload();
StatusUpdate statusUpdate = null;
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.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.TWITTER_PLACE_ID)) {
String placeId = (String) message.getHeaders()
.get(TwitterHeaders.TWITTER_PLACE_ID);
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.TWITTER_GEOLOCATION)) {
GeoLocation geoLocation = (GeoLocation) message.getHeaders().get(TwitterHeaders.TWITTER_GEOLOCATION);
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.TWITTER_DISPLAY_COORDINATES)) {
Boolean displayCoords = (Boolean) message.getHeaders()
.get(TwitterHeaders.TWITTER_DISPLAY_COORDINATES);
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) {
}
else if (payload instanceof StatusUpdate) {
statusUpdate = (StatusUpdate) payload;
} else {
}
else {
throw new MessageHandlingException(message,
"Failed to create StatusUpdate from the payload of type: " + message.getPayload().getClass() +
" Only java.lang.String or twitter4j.StatusUpdate is currently supported");
"Failed to create StatusUpdate from payload of type '" + message.getPayload().getClass() +
"'. Only java.lang.String and twitter4j.StatusUpdate are currently supported.");
}
if (payload instanceof StatusUpdate) {
statusUpdate = (StatusUpdate) payload;
}
return statusUpdate;
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.config;
import org.junit.Ignore;
@@ -42,15 +43,14 @@ public class TestSendingDMsUsingNamespace extends AbstractJUnit4SpringContextTes
@Test
@Ignore
public void testSendigRealDirectMessage() throws Throwable {
String dmUsr = "z_oleg";
MessageBuilder<String> mb = MessageBuilder.withPayload("'Hello world!', from the Spring Integration outbound Twitter adapter " + System.currentTimeMillis())
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
.setHeader(TwitterHeaders.GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true);
if (StringUtils.hasText(dmUsr)) {
mb.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, dmUsr);
mb.setHeader(TwitterHeaders.DM_TARGET_USER_ID, dmUsr);
}
inputChannel.send(mb.build());
}
}

View File

@@ -41,10 +41,11 @@ public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContex
@Ignore
public void testSendingATweet() throws Throwable {
MessageBuilder<String> mb = MessageBuilder.withPayload("simple test demonstrating the ability to encode location information")
.setHeader(TwitterHeaders.TWITTER_IN_REPLY_TO_STATUS_ID, 21927437001L)
.setHeader(TwitterHeaders.IN_REPLY_TO_STATUS_ID, 21927437001L)
//.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new Twitter4jGeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true);
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true);
Message<String> m = mb.build();
this.messagingTemplate.send(this.channel, m);
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import static org.mockito.Mockito.mock;
@@ -30,19 +31,19 @@ import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*
*/
public class OutboundDirectMessageMessageHandlerTests {
private Twitter twitter;
@Test
public void validateSendDirectMessage() throws Exception{
MessageBuilder<String> mb = MessageBuilder.withPayload("hello")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, "foo");
.setHeader(TwitterHeaders.GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.DM_TARGET_USER_ID, "foo");
OutboundDirectMessageMessageHandler handler = new OutboundDirectMessageMessageHandler();
handler.setConfiguration(this.getTestConfiguration());
handler.afterPropertiesSet();
@@ -50,18 +51,20 @@ public class OutboundDirectMessageMessageHandlerTests {
verify(twitter, times(1)).sendDirectMessage("foo", "hello");
mb = MessageBuilder.withPayload("hello")
.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.TWITTER_DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.TWITTER_DM_TARGET_USER_ID, 123);
.setHeader(TwitterHeaders.GEOLOCATION, new GeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.DM_TARGET_USER_ID, 123);
handler.handleMessage(mb.build());
verify(twitter, times(1)).sendDirectMessage(123, "hello");
}
private OAuthConfiguration getTestConfiguration() throws Exception{
private OAuthConfiguration getTestConfiguration() throws Exception {
twitter = mock(Twitter.class);
OAuthConfiguration configuration = mock(OAuthConfiguration.class);
when(configuration.getTwitter()).thenReturn(twitter);
return configuration;
}
}