You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/08/07 15:33:31 UTC
svn commit: r1370255 - in /camel/trunk/components/camel-twitter/src:
main/java/org/apache/camel/component/twitter/producer/
test/java/org/apache/camel/component/twitter/
Author: ningjiang
Date: Tue Aug 7 13:33:31 2012
New Revision: 1370255
URL: http://svn.apache.org/viewvc?rev=1370255&view=rev
Log:
CAMEL-5479 support to retrieve the unique identifier for the published tweet
Added:
camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOnlyTest.java
camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOutTest.java
Modified:
camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java
Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java?rev=1370255&r1=1370254&r2=1370255&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java (original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java Tue Aug 7 13:33:31 2012
@@ -19,6 +19,7 @@ package org.apache.camel.component.twitt
import org.apache.camel.Exchange;
import org.apache.camel.component.twitter.TwitterEndpoint;
+import twitter4j.Status;
import twitter4j.StatusUpdate;
/**
@@ -34,26 +35,42 @@ public class UserProducer extends Twitte
public void process(Exchange exchange) throws Exception {
// update user's status
Object in = exchange.getIn().getBody();
+ Status response;
if (in instanceof StatusUpdate) {
- updateStatus((StatusUpdate) in);
+ response = updateStatus((StatusUpdate) in);
} else {
String s = exchange.getIn().getMandatoryBody(String.class);
- updateStatus(s);
+ response = updateStatus(s);
+ }
+
+ /*
+ * Support the InOut exchange pattern in order to provide access to
+ * the unique identifier for the published tweet which is returned in the response
+ * by the Twitter REST API: https://dev.twitter.com/docs/api/1/post/statuses/update
+ */
+ if (exchange.getPattern().isOutCapable()) {
+ // here we just copy the header of in message to the out message
+ exchange.getOut().copyFrom(exchange.getIn());
+ exchange.getOut().setBody(response);
}
}
- private void updateStatus(StatusUpdate status) throws Exception {
- te.getProperties().getTwitter().updateStatus(status);
+ private Status updateStatus(StatusUpdate status) throws Exception {
+ Status reponse = te.getProperties().getTwitter().updateStatus(status);
log.debug("Updated status: {}", status);
+ log.debug("Status id: {}", reponse.getId());
+ return reponse;
}
- private void updateStatus(String status) throws Exception {
+ private Status updateStatus(String status) throws Exception {
if (status.length() > 160) {
log.warn("Message is longer than 160 characters. Message will be truncated!");
status = status.substring(0, 160);
}
- te.getProperties().getTwitter().updateStatus(status);
+ Status response = te.getProperties().getTwitter().updateStatus(status);
log.debug("Updated status: {}", status);
+ log.debug("Status id: {}", response.getId());
+ return response;
}
}
Added: camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOnlyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOnlyTest.java?rev=1370255&view=auto
==============================================================================
--- camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOnlyTest.java (added)
+++ camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOnlyTest.java Tue Aug 7 13:33:31 2012
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.twitter;
+
+import java.util.Date;
+import java.util.List;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Tests posting a twitter update with the default In Message Exchange Pattern
+ */
+public class UserProducerInOnlyTest extends CamelTwitterTestSupport {
+ private static final transient Logger LOG = LoggerFactory.getLogger(UserProducerInOnlyTest.class);
+
+ @EndpointInject(uri = "mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @Test
+ public void testPostStatusUpdateRequestResponse() throws Exception {
+ Date now = new Date();
+ String tweet = "UserProducerInOnlyTest: This is a tweet posted on " + now.toString();
+ LOG.info("Tweet: " + tweet);
+ ProducerTemplate producerTemplate = context.createProducerTemplate();
+ // send tweet to the twitter endpoint
+ producerTemplate.sendBodyAndHeader("direct:tweets", tweet, "customHeader", 12312);
+
+
+ resultEndpoint.expectedMessageCount(1);
+ resultEndpoint.expectedBodyReceived().body(String.class);
+ // Message headers should be preserved
+ resultEndpoint.expectedHeaderReceived("customHeader", 12312);
+ resultEndpoint.assertIsSatisfied();
+
+ List<Exchange> tweets = resultEndpoint.getExchanges();
+ assertNotNull(tweets);
+ assertThat(tweets.size(), is(1));
+ String receivedTweet = tweets.get(0).getIn().getBody(String.class);
+ assertThat(receivedTweet, is(tweet));
+ }
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:tweets")
+ //.to("log:org.apache.camel.component.twitter?level=INFO&showAll=true&multiline=true")
+ .to("twitter://timeline/user?" + getUriTokens())
+ //.to("log:org.apache.camel.component.twitter?level=INFO&showAll=true&multiline=true")
+ .to("mock:result");
+ }
+ };
+ }
+}
Added: camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOutTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOutTest.java?rev=1370255&view=auto
==============================================================================
--- camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOutTest.java (added)
+++ camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/UserProducerInOutTest.java Tue Aug 7 13:33:31 2012
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.twitter;
+
+import java.util.Date;
+import java.util.List;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import twitter4j.Status;
+
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Tests posting a twitter update and getting the status update id from the Twitter API response
+ */
+public class UserProducerInOutTest extends CamelTwitterTestSupport {
+ private static final transient Logger LOG = LoggerFactory.getLogger(UserProducerInOutTest.class);
+
+ @EndpointInject(uri = "mock:result")
+ protected MockEndpoint resultEndpoint;
+
+ @Test
+ public void testPostStatusUpdateRequestResponse() throws Exception {
+ Date now = new Date();
+ String tweet = "UserProducerInOutTest: This is a tweet posted on " + now.toString();
+ LOG.info("Tweet: " + tweet);
+ ProducerTemplate producerTemplate = context.createProducerTemplate();
+ // send tweet to the twitter endpoint
+ producerTemplate.sendBodyAndHeader("direct:tweets", tweet, "customHeader", 12312);
+
+
+ resultEndpoint.expectedMessageCount(1);
+ resultEndpoint.expectedBodyReceived().body(Status.class);
+ // Message headers should be preserved
+ resultEndpoint.expectedHeaderReceived("customHeader", 12312);
+ resultEndpoint.assertIsSatisfied();
+
+ List<Exchange> tweets = resultEndpoint.getExchanges();
+ assertNotNull(tweets);
+ assertThat(tweets.size(), is(1));
+ Status receivedTweet = tweets.get(0).getIn().getBody(Status.class);
+ assertNotNull(receivedTweet);
+ // The identifier for the published tweet should be there
+ assertNotNull(receivedTweet.getId());
+ }
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:tweets")
+ //.to("log:org.apache.camel.component.twitter?level=INFO&showAll=true&multiline=true")
+ .inOut("twitter://timeline/user?" + getUriTokens())
+ //.to("log:org.apache.camel.component.twitter?level=INFO&showAll=true&multiline=true")
+ //.transform().simple("The tweet '${body.text}' was published with the id '${body.id}'")
+ //.to("log:org.apache.camel.component.twitter?level=INFO&showAll=true&multiline=true")
+ .to("mock:result");
+ }
+ };
+ }
+}