Search in sources :

Example 1 with SearchParameters

use of org.springframework.social.twitter.api.SearchParameters in project spring-integration by spring-projects.

the class TwitterSearchOutboundGateway method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> 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) {
        searchParameters = (SearchParameters) args;
    } else if (args instanceof String) {
        searchParameters = new SearchParameters((String) args).count(DEFAULT_PAGE_SIZE);
    } else if (args instanceof List) {
        List<?> list = (List<?>) args;
        Assert.isTrue(list.size() > 0 && list.size() < 5, "Between 1 and 4 search arguments are required");
        Assert.isInstanceOf(String.class, list.get(0), "The first search argument (query) must be a String");
        searchParameters = new SearchParameters((String) list.get(0));
        if (list.size() > 1) {
            Assert.isInstanceOf(Number.class, list.get(1), "The second search argument (pageSize) must be a Number");
            searchParameters.count(((Number) list.get(1)).intValue());
            if (list.size() > 2) {
                Assert.isInstanceOf(Number.class, list.get(2), "The third search argument (sinceId) must be a Number");
                searchParameters.sinceId(((Number) list.get(2)).longValue());
            }
            if (list.size() > 3) {
                Assert.isInstanceOf(Number.class, list.get(3), "The fourth search argument (maxId) must be a Number");
                searchParameters.maxId(((Number) list.get(3)).longValue());
            }
        }
    } else {
        throw new IllegalArgumentException("Search Expression must evaluate to a 'SearchParameters', 'String' or 'List'.");
    }
    SearchResults results = this.getTwitter().searchOperations().search(searchParameters);
    if (results != null) {
        List<Tweet> tweets = (results.getTweets() != null ? results.getTweets() : Collections.<Tweet>emptyList());
        return this.getMessageBuilderFactory().withPayload(tweets).setHeader(TwitterHeaders.SEARCH_METADATA, results.getSearchMetadata());
    } else {
        return null;
    }
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) List(java.util.List) SearchResults(org.springframework.social.twitter.api.SearchResults)

Example 2 with SearchParameters

use of org.springframework.social.twitter.api.SearchParameters in project spring-integration by spring-projects.

the class TestSearchOutboundGateway method testSearch.

@Test
@Ignore
public /*
	 * In order to run this test you need to provide oauth properties in sample.properties on the classpath.
	 */
void testSearch() throws Exception {
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("TestSearchOutboundGateway-context.xml", this.getClass());
    MessageChannel search = ctx.getBean("search", MessageChannel.class);
    search.send(new GenericMessage<String>("#springintegration"));
    Thread.sleep(10000);
    search.send(new GenericMessage<SearchParameters>(new SearchParameters("#springintegration").count(5)));
    Thread.sleep(10000);
    search.send(new GenericMessage<SearchParameters>(new SearchParameters("#jjjjunk").count(5)));
    Thread.sleep(10000);
    ctx.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SearchParameters(org.springframework.social.twitter.api.SearchParameters) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SearchParameters

use of org.springframework.social.twitter.api.SearchParameters in project spring-integration by spring-projects.

the class TwitterSearchOutboundGatewayTests method testStringQueryCustomExpression.

@Test
public void testStringQueryCustomExpression() {
    this.gateway.setSearchArgsExpression(new SpelExpressionParser().parseExpression("{'bar', 1, 2, 3}"));
    Tweet tweet = mock(Tweet.class);
    SearchMetadata searchMetadata = mock(SearchMetadata.class);
    final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
    doAnswer(invocation -> {
        SearchParameters searchParameters = invocation.getArgument(0);
        assertEquals("bar", searchParameters.getQuery());
        assertEquals(Integer.valueOf(1), searchParameters.getCount());
        assertEquals(Long.valueOf(2), searchParameters.getSinceId());
        assertEquals(Long.valueOf(3), searchParameters.getMaxId());
        return searchResults;
    }).when(this.searchOps).search(any(SearchParameters.class));
    this.gateway.handleMessage(new GenericMessage<String>("foo"));
    Message<?> reply = this.outputChannel.receive(0);
    assertNotNull(reply);
    @SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
    assertEquals(1, tweets.size());
    assertSame(tweet, tweets.get(0));
    assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) Test(org.junit.Test)

Example 4 with SearchParameters

use of org.springframework.social.twitter.api.SearchParameters in project spring-integration by spring-projects.

the class SearchReceivingMessageSource method pollForTweets.

@Override
protected List<Tweet> pollForTweets(long sinceId) {
    SearchParameters searchParameters = new SearchParameters(this.query).count(this.getPageSize()).sinceId(sinceId);
    SearchResults results = this.getTwitter().searchOperations().search(searchParameters);
    return (results != null) ? results.getTweets() : Collections.<Tweet>emptyList();
}
Also used : SearchParameters(org.springframework.social.twitter.api.SearchParameters) SearchResults(org.springframework.social.twitter.api.SearchResults)

Example 5 with SearchParameters

use of org.springframework.social.twitter.api.SearchParameters in project spring-integration by spring-projects.

the class SearchReceivingMessageSourceTests method testPollForTweetsThreeResults.

/**
 * Verify that a polling operation returns in fact 3 results.
 */
@Test
public void testPollForTweetsThreeResults() {
    final TwitterTemplate twitterTemplate;
    final SearchOperations so = mock(SearchOperations.class);
    final List<Tweet> tweets = new ArrayList<Tweet>();
    tweets.add(mock(Tweet.class));
    tweets.add(mock(Tweet.class));
    tweets.add(mock(Tweet.class));
    final SearchResults results = new SearchResults(tweets, new SearchMetadata(111, 111));
    twitterTemplate = mock(TwitterTemplate.class);
    when(twitterTemplate.searchOperations()).thenReturn(so);
    SearchParameters params = new SearchParameters(SEARCH_QUERY).count(20).sinceId(0);
    when(twitterTemplate.searchOperations().search(params)).thenReturn(results);
    final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(twitterTemplate, "foo");
    messageSource.setQuery(SEARCH_QUERY);
    final List<Tweet> tweetSearchResults = messageSource.pollForTweets(0);
    assertNotNull(tweetSearchResults);
    assertEquals(3, tweetSearchResults.size());
}
Also used : TwitterTemplate(org.springframework.social.twitter.api.impl.TwitterTemplate) SearchParameters(org.springframework.social.twitter.api.SearchParameters) Tweet(org.springframework.social.twitter.api.Tweet) ArrayList(java.util.ArrayList) SearchMetadata(org.springframework.social.twitter.api.SearchMetadata) SearchResults(org.springframework.social.twitter.api.SearchResults) SearchOperations(org.springframework.social.twitter.api.SearchOperations) Test(org.junit.Test)

Aggregations

SearchParameters (org.springframework.social.twitter.api.SearchParameters)10 SearchResults (org.springframework.social.twitter.api.SearchResults)9 Test (org.junit.Test)8 Tweet (org.springframework.social.twitter.api.Tweet)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SearchMetadata (org.springframework.social.twitter.api.SearchMetadata)7 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)3 Ignore (org.junit.Ignore)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 MessageChannel (org.springframework.messaging.MessageChannel)1 SearchOperations (org.springframework.social.twitter.api.SearchOperations)1 TwitterTemplate (org.springframework.social.twitter.api.impl.TwitterTemplate)1