From 403e929eb61f1e3476d417e53749a5a1ffb39137 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 16 Dec 2010 16:50:57 -0500 Subject: [PATCH] polishing --- docs/src/reference/docbook/twitter.xml | 20 ++++--- .../DirectMessageSendingMessageHandler.java | 57 ++++++++++--------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/docs/src/reference/docbook/twitter.xml b/docs/src/reference/docbook/twitter.xml index 4460b25d62..62d6294834 100644 --- a/docs/src/reference/docbook/twitter.xml +++ b/docs/src/reference/docbook/twitter.xml @@ -258,24 +258,26 @@ received. The only extra configuration that is required for this adapter is the twitter-template reference. - When it comes to Twitter Direct Message you must specify who you sending this message to - target userid. - Twitter Outbound Direct Message Channel Adapter provides several ways you can specify target user. - By default it will look for target userid in the Message headers under the name twitter_dmTargetUserId which - is also identified by the following constant - TwitterHeaders.DM_TARGET_USER_ID. - So when creating a Message all you need to do is add this header: + When it comes to Twitter Direct Messages, you must specify who you are sending the message to - the target userid. + The Twitter Outbound Direct Message Channel Adapter provides two ways you can specify that target user. + + + The first (and default) way is that it will look for a target userid in the Message headers under the name twitter_dmTargetUserId which + is also identified by the following constant: TwitterHeaders.DM_TARGET_USER_ID. + So when creating a Message all you need to do is add a value for that header. - The above approach works well if you know the target userid in advance. However there are times when such - value must be determined dynamically. For those cases you can utilize SpEL support by providing valid SpEL - expression via target-user-expression attribute. + The above approach works well if you know the target userid in advance. However there are times when that + value must be determined dynamically. For those cases you can utilize the second approach: SpEL support. + Simply provide a valid SpEL expression via the target-user-expression attribute. ]]> + target-user-expression="payload.user.twitterId"/>]]> diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/DirectMessageSendingMessageHandler.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/DirectMessageSendingMessageHandler.java index 4d5cb4caf7..9915210574 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/DirectMessageSendingMessageHandler.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/DirectMessageSendingMessageHandler.java @@ -35,15 +35,22 @@ import org.springframework.util.Assert; * * @author Josh Long * @author Oleg Zhurakousky + * @author Mark Fisher * @since 2.0 */ public class DirectMessageSendingMessageHandler extends AbstractMessageHandler { - private final TwitterOperations twitterOperations; - private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); private static final ExpressionParser PARSER = new SpelExpressionParser(); - - private volatile Expression targetUserExpression; + + private static final Expression DEFAULT_TARGET_USER_EXPRESSION = PARSER.parseExpression( + "headers[T(org.springframework.integration.twitter.core.TwitterHeaders).DM_TARGET_USER_ID]"); + + + private final TwitterOperations twitterOperations; + + private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); + + private volatile Expression targetUserExpression = DEFAULT_TARGET_USER_EXPRESSION; public DirectMessageSendingMessageHandler(TwitterOperations twitterOperations) { @@ -51,18 +58,29 @@ public class DirectMessageSendingMessageHandler extends AbstractMessageHandler { this.twitterOperations = twitterOperations; } + public void setTargetUserExpression(Expression targetUserExpression) { - Assert.notNull(targetUserExpression, "'targetUserExpression' must not be null"); - this.targetUserExpression = targetUserExpression; + this.targetUserExpression = (targetUserExpression != null) ? targetUserExpression : DEFAULT_TARGET_USER_EXPRESSION; + } + + @Override + public void onInit() throws Exception { + super.onInit(); + BeanFactory beanFactory = this.getBeanFactory(); + if (beanFactory != null) { + this.evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); + } + ConversionService conversionService = this.getConversionService(); + if (conversionService != null) { + this.evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService)); + } } @Override protected void handleMessageInternal(Message message) throws Exception { - 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."); - - Object toUser = targetUserExpression.getValue(this.evaluationContext, message); - + Assert.isTrue(message.getPayload() instanceof String, "Only payload of type String is supported. " + + "Consider adding a transformer to the message flow in front of this adapter."); + Object toUser = this.targetUserExpression.getValue(this.evaluationContext, message); 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)"); @@ -74,22 +92,5 @@ public class DirectMessageSendingMessageHandler extends AbstractMessageHandler { this.twitterOperations.sendDirectMessage((String) toUser, payload); } } - - @Override - public void onInit() throws Exception{ - super.onInit(); - BeanFactory beanFactory = this.getBeanFactory(); - if (beanFactory != null) { - this.evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); - } - ConversionService conversionService = this.getConversionService(); - if (conversionService != null) { - this.evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService)); - } - if (targetUserExpression == null){ - targetUserExpression = - PARSER.parseExpression("headers[T(org.springframework.integration.twitter.core.TwitterHeaders).DM_TARGET_USER_ID]"); - } - } }