INT-1604 removed OutboundTweetMessageMapper and AbstractOutboundTwitterEndpointSupport, renamed TimelineSendingMessageHandler to StatusUpdatingMessageHandler

This commit is contained in:
Mark Fisher
2010-11-11 18:54:20 -05:00
parent e02582f079
commit ae619d95bf
8 changed files with 41 additions and 137 deletions

View File

@@ -46,7 +46,7 @@ public class TwitterOutboundChannelAdapterParser extends AbstractOutboundChannel
String className = null;
String elementName = element.getLocalName().trim();
if ("outbound-channel-adapter".equals(elementName)) {
className = BASE_PACKAGE + ".outbound.TimelineSendingMessageHandler";
className = BASE_PACKAGE + ".outbound.StatusUpdatingMessageHandler";
}
else if ("dm-outbound-channel-adapter".equals(elementName)) {
className = BASE_PACKAGE + ".outbound.DirectMessageSendingMessageHandler";

View File

@@ -125,7 +125,8 @@ abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint implemen
public Message<?> receive() {
Tweet tweet = this.tweets.poll();
if (tweet != null) {
this.setLastProcessedId(tweet.getId());
this.lastProcessedId = tweet.getId();
this.metadataStore.put(this.metadataKey, String.valueOf(this.lastProcessedId));
return MessageBuilder.withPayload(tweet).build();
}
return null;
@@ -149,11 +150,6 @@ abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint implemen
}
}
private void setLastProcessedId(long statusId) {
this.lastProcessedId = statusId;
this.metadataStore.put(this.metadataKey, String.valueOf(statusId));
}
/**
* Subclasses must implement this to return tweets.

View File

@@ -1,37 +0,0 @@
/*
* 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.outbound;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
/**
* Base adapter class for all outbound Twitter adapters
*
* @author Josh Long
* @since 2.0
*/
public abstract class AbstractOutboundTwitterEndpointSupport extends AbstractMessageHandler {
protected final TwitterOperations twitter;
protected final OutboundTweetMessageMapper outboundMaper = new OutboundTweetMessageMapper();
public AbstractOutboundTwitterEndpointSupport(TwitterOperations twitter){
Assert.notNull(twitter, "'twitter' must not be null");
this.twitter = twitter;
}
}

View File

@@ -17,28 +17,31 @@
package org.springframework.integration.twitter.outbound;
import org.springframework.integration.Message;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
/**
* Simple adapter to support sending outbound direct messages ("DM"s) using twitter
* Simple adapter to support sending outbound direct messages ("DM"s) using Twitter.
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0
*/
public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterEndpointSupport {
public DirectMessageSendingMessageHandler(TwitterOperations twitter){
super(twitter);
public class DirectMessageSendingMessageHandler extends AbstractMessageHandler {
private final TwitterOperations twitterOperations;
public DirectMessageSendingMessageHandler(TwitterOperations twitterOperations) {
Assert.notNull(twitterOperations, "twitterOperations must not be null");
this.twitterOperations = twitterOperations;
}
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
if (this.twitter == null) {
this.afterPropertiesSet();
}
Assert.isInstanceOf(String.class, message.getPayload(), "Only payload of type String is supported. If your payload " +
"is not of type String consider adding a transformer to the message flow in front of this adapter.");
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.DM_TARGET_USER_ID),
@@ -49,10 +52,10 @@ public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterE
"' 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, payload);
this.twitterOperations.sendDirectMessage((Integer) toUser, payload);
}
else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, payload);
this.twitterOperations.sendDirectMessage((String) toUser, payload);
}
}

View File

@@ -1,67 +0,0 @@
/*
* 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.
* 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.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 twitter4j.StatusUpdate;
/**
* 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
*/
public class OutboundTweetMessageMapper implements OutboundMessageMapper<Tweet> {
/**
* {@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
*/
public Tweet fromMessage(Message<?> message) {
Object payload = message.getPayload();
Tweet tweet = null;
if (payload instanceof String) {
tweet = new Tweet();
tweet.setText((String) payload);
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)) {
tweet.setToUserId(replyId);
}
}
}
else if (payload instanceof Tweet) {
tweet = (Tweet) payload;
}
else {
throw new MessageHandlingException(message,
"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 tweet;
}
}

View File

@@ -13,41 +13,48 @@
* 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;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.twitter.core.Tweet;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
/**
* This class is useful for both sending regular status updates as well as 'replies' or 'mentions'
* MessageHandler for sending regular status updates as well as 'replies' or 'mentions'.
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0
*/
public class TimelineSendingMessageHandler extends AbstractOutboundTwitterEndpointSupport {
public TimelineSendingMessageHandler(TwitterOperations twitter){
super(twitter);
public class StatusUpdatingMessageHandler extends AbstractMessageHandler {
private final TwitterOperations twitterOperations;
public StatusUpdatingMessageHandler(TwitterOperations twitterOperations) {
Assert.notNull(twitterOperations, "twitterOperations must not be null");
this.twitterOperations = twitterOperations;
}
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
Object payload = message.getPayload();
String statusText = null;
if (payload instanceof Tweet){
statusText = ((Tweet)payload).getText();
if (payload instanceof Tweet) {
statusText = ((Tweet) payload).getText();
}
else if (payload instanceof String){
else if (payload instanceof String) {
statusText = (String) payload;
}
else {
throw new MessageHandlingException(message, "Unsupported payload type '" + payload.getClass().getName() + "'");
}
this.twitter.updateStatus(statusText);
this.twitterOperations.updateStatus(statusText);
}
}

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.Test;
@@ -20,13 +21,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Oleg Zhurakousky
*
* @since 2.0
*/
public class TestSendingMessageHandlerParserTests {
@Test
public void testSendingMessageHandlerSuccessfullBootstrap(){
public void testSendingMessageHandlerSuccessfulBootstrap(){
new ClassPathXmlApplicationContext("TestSendingMessageHandlerParser-context.xml", this.getClass());
// the fact that no exception was thrown satisfies this test
}
}

View File

@@ -58,7 +58,7 @@ public class TimelineUpdateSendingMessageHandlerTests {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStatusUpdate() throws Exception{
TimelineSendingMessageHandler handler = new TimelineSendingMessageHandler(twitterOperations);
StatusUpdatingMessageHandler handler = new StatusUpdatingMessageHandler(twitterOperations);
Tweet tweet = new Tweet();
tweet.setText("writing twitter tests");
handler.handleMessage(new GenericMessage(tweet));
@@ -67,7 +67,7 @@ public class TimelineUpdateSendingMessageHandlerTests {
}
@Test
public void testSendingStatusUpdateWithHeaders() throws Exception{
TimelineSendingMessageHandler handler = new TimelineSendingMessageHandler(twitterOperations);
StatusUpdatingMessageHandler handler = new StatusUpdatingMessageHandler(twitterOperations);
Message<?> message = MessageBuilder.withPayload("writing twitter tests")
.setHeader(TwitterHeaders.IN_REPLY_TO_STATUS_ID, new Long(123))
.setHeader(TwitterHeaders.PLACE_ID, "123")