diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterOutboundChannelAdapterParser.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterOutboundChannelAdapterParser.java index 6a07e212fe..2070be1467 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterOutboundChannelAdapterParser.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/config/TwitterOutboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors + * Copyright 2002-2014 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. @@ -21,15 +21,18 @@ import org.w3c.dom.Element; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.ExpressionFactoryBean; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; import org.springframework.integration.twitter.outbound.DirectMessageSendingMessageHandler; import org.springframework.integration.twitter.outbound.StatusUpdatingMessageHandler; +import org.springframework.util.StringUtils; /** * Parser for all outbound Twitter adapters - * + * * @author Josh Long * @author Oleg Zhurakousky + * @author Artem Bilan * @since 2.0 */ public class TwitterOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { @@ -39,6 +42,13 @@ public class TwitterOutboundChannelAdapterParser extends AbstractOutboundChannel Class> clazz = determineClass(element, parserContext); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(clazz); builder.addConstructorArgReference(element.getAttribute("twitter-template")); + String tweetDataExpression = element.getAttribute("tweet-data-expression"); + if (StringUtils.hasText(tweetDataExpression)) { + builder.addPropertyValue("tweetDataExpression", + BeanDefinitionBuilder.genericBeanDefinition(ExpressionFactoryBean.class) + .addConstructorArgValue(tweetDataExpression) + .getBeanDefinition()); + } return builder.getBeanDefinition(); } diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/StatusUpdatingMessageHandler.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/StatusUpdatingMessageHandler.java index ed5715e7f1..4be21e92e3 100644 --- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/StatusUpdatingMessageHandler.java +++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/StatusUpdatingMessageHandler.java @@ -16,10 +16,16 @@ package org.springframework.integration.twitter.outbound; +import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; +import org.springframework.expression.TypeLocator; +import org.springframework.expression.spel.support.StandardTypeLocator; +import org.springframework.integration.expression.IntegrationEvaluationContextAware; import org.springframework.integration.handler.AbstractMessageHandler; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHandlingException; import org.springframework.social.twitter.api.Tweet; +import org.springframework.social.twitter.api.TweetData; import org.springframework.social.twitter.api.Twitter; import org.springframework.util.Assert; @@ -28,37 +34,80 @@ import org.springframework.util.Assert; * * @author Josh Long * @author Oleg Zhurakousky + * @author Artem Bilan * @since 2.0 */ -public class StatusUpdatingMessageHandler extends AbstractMessageHandler { +public class StatusUpdatingMessageHandler extends AbstractMessageHandler + implements IntegrationEvaluationContextAware { private final Twitter twitter; + private volatile Expression tweetDataExpression; + + private EvaluationContext evaluationContext; public StatusUpdatingMessageHandler(Twitter twitter) { Assert.notNull(twitter, "twitter must not be null"); this.twitter = twitter; } + @Override + public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) { + TypeLocator typeLocator = evaluationContext.getTypeLocator(); + if (typeLocator instanceof StandardTypeLocator) { + /* + * Register the twitter api package so they don't need a FQCN for TweetData. + */ + ((StandardTypeLocator) typeLocator).registerImport("org.springframework.social.twitter.api"); + } + this.evaluationContext = evaluationContext; + } + @Override public String getComponentType() { return "twitter:outbound-channel-adapter"; } + /** + * An expression that is used to build the {@link TweetData}; must resolve to a + * {@link TweetData} object, or a {@link String}, or a {@link Tweet}. + *
When using a {@code TweetData} directly in the expression, it is not necessary
+ * to include the package:
+ * {@code "new TweetData("test").withMedia(headers.mediaResource).displayCoordinates(true)")}.
+ * @param tweetDataExpression The expression.
+ * @since 4.0
+ */
+ public void setTweetDataExpression(Expression tweetDataExpression) {
+ this.tweetDataExpression = tweetDataExpression;
+ }
+
@Override
protected void handleMessageInternal(Message> message) throws Exception {
- Object payload = message.getPayload();
- String statusText = null;
- if (payload instanceof Tweet) {
- statusText = ((Tweet) payload).getText();
- }
- else if (payload instanceof String) {
- statusText = (String) payload;
+ Object value;
+ if (this.tweetDataExpression != null) {
+ value = this.tweetDataExpression.getValue(this.evaluationContext, message);
}
else {
- throw new MessageHandlingException(message, "Unsupported payload type '" + payload.getClass().getName() + "'");
+ value = message.getPayload();
}
- this.twitter.timelineOperations().updateStatus(statusText);
+ Assert.notNull(value, "The tweetData cannot evaluate to 'null'.");
+
+ TweetData tweetData = null;
+
+ if (value instanceof TweetData) {
+ tweetData = (TweetData) value;
+ }
+ else if (value instanceof Tweet) {
+ tweetData = new TweetData(((Tweet) value).getText());
+ }
+ else if (value instanceof String) {
+ tweetData = new TweetData((String) value);
+ }
+ else {
+ throw new MessageHandlingException(message, "Unsupported tweetData: " + value);
+ }
+
+ this.twitter.timelineOperations().updateStatus(tweetData);
}
}
diff --git a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/TwitterSearchOutboundGateway.java b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/TwitterSearchOutboundGateway.java
index a2b5dcab87..1e77ec5473 100644
--- a/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/TwitterSearchOutboundGateway.java
+++ b/spring-integration-twitter/src/main/java/org/springframework/integration/twitter/outbound/TwitterSearchOutboundGateway.java
@@ -22,7 +22,6 @@ import java.util.List;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.TypeLocator;
-import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardTypeLocator;
import org.springframework.integration.expression.IntegrationEvaluationContextAware;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
@@ -50,7 +49,7 @@ public class TwitterSearchOutboundGateway extends AbstractReplyProducingMessageH
private final Twitter twitter;
- private volatile Expression searchArgsExpression = new SpelExpressionParser().parseExpression("payload");
+ private volatile Expression searchArgsExpression;
private volatile EvaluationContext evaluationContext;
@@ -102,7 +101,13 @@ public class TwitterSearchOutboundGateway extends AbstractReplyProducingMessageH
@Override
protected Object handleRequestMessage(Message> requestMessage) {
- Object args = this.searchArgsExpression.getValue(this.evaluationContext, requestMessage);
+ Object args;
+ if (this.searchArgsExpression != null) {
+ args = this.searchArgsExpression.getValue(this.evaluationContext, requestMessage);
+ }
+ else {
+ args = requestMessage.getPayload();
+ }
Assert.notNull(args, "The twitter search expression cannot evaluate to 'null'.");
SearchParameters searchParameters;
if (args instanceof SearchParameters) {
diff --git a/spring-integration-twitter/src/main/resources/org/springframework/integration/twitter/config/spring-integration-twitter-4.0.xsd b/spring-integration-twitter/src/main/resources/org/springframework/integration/twitter/config/spring-integration-twitter-4.0.xsd
index 5fc97d0120..e810d9989b 100644
--- a/spring-integration-twitter/src/main/resources/org/springframework/integration/twitter/config/spring-integration-twitter-4.0.xsd
+++ b/spring-integration-twitter/src/main/resources/org/springframework/integration/twitter/config/spring-integration-twitter-4.0.xsd
@@ -113,6 +113,16 @@
twitter-template reference.
-
+ <int-twitter:outbound-channel-adapter>
+ supports a tweet-data-expression to populate the text of which will be used for the <int-twitter:outbound-channel-adapter>)
+ now supports the tweet-data-expression attribute to build a
+