From 90cc137faf2f1a73fec8a99b60e54cdcf808f283 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 27 Oct 2010 22:54:04 -0400 Subject: [PATCH] INT-1471 removed the TWITTER_ prefix on the header constant definitions, and switched to camelCase for consistency --- .../twitter/core/TwitterHeaders.java | 18 +++--- .../OutboundDirectMessageMessageHandler.java | 30 +++++----- .../OutboundStatusUpdateMessageMapper.java | 57 +++++++------------ .../config/TestSendingDMsUsingNamespace.java | 10 ++-- .../TestSendingUpdatesUsingNamespace.java | 5 +- ...boundDirectMessageMessageHandlerTests.java | 25 ++++---- 6 files changed, 67 insertions(+), 78 deletions(-) diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterHeaders.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterHeaders.java index 4e4a430dd3..18fdbca3cd 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterHeaders.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/core/TwitterHeaders.java @@ -17,23 +17,23 @@ package org.springframework.integration.twitter.core; /** - * An enum to allow users to express interest in particular kinds of tweets. - *

- * 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"; } diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandler.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandler.java index 18639115d4..4e853970e6 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandler.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandler.java @@ -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); } } + } diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundStatusUpdateMessageMapper.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundStatusUpdateMessageMapper.java index 25a10520c7..f6a97f29b1 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundStatusUpdateMessageMapper.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/OutboundStatusUpdateMessageMapper.java @@ -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 { + /** * {@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; } + } diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingDMsUsingNamespace.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingDMsUsingNamespace.java index 610382f934..7779966d4d 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingDMsUsingNamespace.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingDMsUsingNamespace.java @@ -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 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()); } + } diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingUpdatesUsingNamespace.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingUpdatesUsingNamespace.java index 9377e0b2e1..1496eb92ec 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingUpdatesUsingNamespace.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/config/TestSendingUpdatesUsingNamespace.java @@ -41,10 +41,11 @@ public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContex @Ignore public void testSendingATweet() throws Throwable { MessageBuilder 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 m = mb.build(); this.messagingTemplate.send(this.channel, m); } + } diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandlerTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandlerTests.java index 1121a3155b..4cba993ac4 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandlerTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/outbound/OutboundDirectMessageMessageHandlerTests.java @@ -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 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; } + }