polishing
This commit is contained in:
@@ -258,24 +258,26 @@ received.
|
||||
The only extra configuration that is required for this adapter is the <code>twitter-template</code> reference.
|
||||
</para>
|
||||
<para>
|
||||
When it comes to Twitter Direct Message you must specify who you sending this message to - <emphasis>target userid</emphasis>.
|
||||
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 <code>twitter_dmTargetUserId</code> which
|
||||
is also identified by the following constant - <classname>TwitterHeaders.DM_TARGET_USER_ID</classname>.
|
||||
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 <emphasis>target userid</emphasis>.
|
||||
The Twitter Outbound Direct Message Channel Adapter provides two ways you can specify that target user.
|
||||
</para>
|
||||
<para>
|
||||
The first (and default) way is that it will look for a target userid in the Message headers under the name <code>twitter_dmTargetUserId</code> which
|
||||
is also identified by the following constant: <classname>TwitterHeaders.DM_TARGET_USER_ID</classname>.
|
||||
So when creating a Message all you need to do is add a value for that header.
|
||||
|
||||
<programlisting language="java"><![CDATA[Message message = MessageBuilder.withPayload("hello")
|
||||
.setHeader(TwitterHeaders.DM_TARGET_USER_ID, "z_oleg").build();]]></programlisting>
|
||||
</para>
|
||||
<para>
|
||||
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 <code>target-user-expression</code> 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 <code>target-user-expression</code> attribute.
|
||||
|
||||
<programlisting language="xml"><![CDATA[<twitter:dm-outbound-channel-adapter id="dmAdapter"
|
||||
twitter-template="twitter"
|
||||
channel="inputChannel"
|
||||
target-user-expression="'z' + '_oleg'"/>]]></programlisting>
|
||||
target-user-expression="payload.user.twitterId"/>]]></programlisting>
|
||||
</para>
|
||||
</section>
|
||||
<para>
|
||||
|
||||
@@ -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]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user