Author: davsclaus
Date: Sun Mar 25 11:57:50 2012
New Revision: 1305013
URL: http://svn.apache.org/viewvc?rev=1305013&view=rev
Log:
CAMEL-5117: You can now configure twitter settings on twitter component level.
Made the example run out of the box.
Modified:
camel/trunk/components/camel-twitter/ (props changed)
camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java
camel/trunk/examples/camel-example-twitter-websocket/README.txt
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java
Propchange: camel/trunk/components/camel-twitter/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Mar 25 11:57:50 2012
@@ -14,3 +14,4 @@ eclipse-classes
*.ipr
*.iml
*.iws
+*.idea
Modified:
camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java?rev=1305013&r1=1305012&r2=1305013&view=diff
==============================================================================
---
camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java
(original)
+++
camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java
Sun Mar 25 11:57:50 2012
@@ -27,9 +27,23 @@ import org.apache.camel.impl.DefaultComp
*/
public class TwitterComponent extends DefaultComponent {
+ private String consumerKey;
+ private String consumerSecret;
+ private String accessToken;
+ private String accessTokenSecret;
+
protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters)
throws Exception {
+
TwitterConfiguration properties = new TwitterConfiguration();
+
+ // set options from component
+ properties.setConsumerKey(consumerKey);
+ properties.setConsumerSecret(consumerSecret);
+ properties.setAccessToken(accessToken);
+ properties.setAccessTokenSecret(accessTokenSecret);
+
+ // and then override from parameters
setProperties(properties, parameters);
TwitterEndpoint endpoint;
@@ -47,4 +61,35 @@ public class TwitterComponent extends De
return endpoint;
}
+ public String getAccessToken() {
+ return accessToken;
+ }
+
+ public void setAccessToken(String accessToken) {
+ this.accessToken = accessToken;
+ }
+
+ public String getAccessTokenSecret() {
+ return accessTokenSecret;
+ }
+
+ public void setAccessTokenSecret(String accessTokenSecret) {
+ this.accessTokenSecret = accessTokenSecret;
+ }
+
+ public String getConsumerKey() {
+ return consumerKey;
+ }
+
+ public void setConsumerKey(String consumerKey) {
+ this.consumerKey = consumerKey;
+ }
+
+ public String getConsumerSecret() {
+ return consumerSecret;
+ }
+
+ public void setConsumerSecret(String consumerSecret) {
+ this.consumerSecret = consumerSecret;
+ }
}
Modified: camel/trunk/examples/camel-example-twitter-websocket/README.txt
URL:
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/README.txt?rev=1305013&r1=1305012&r2=1305013&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-twitter-websocket/README.txt (original)
+++ camel/trunk/examples/camel-example-twitter-websocket/README.txt Sun Mar 25
11:57:50 2012
@@ -4,19 +4,15 @@ Twitter and Websocket Example
The example is demonstrating how to poll a constant feed of twitter searches
and publish results in real time using web socket to a web page.
-To use twitter, you need a twitter account which have setup an application to
be used.
-For twitter users, you may be familiar that twitter requires you to grant
applications
-access to your twitter account, such as twitter for iphone etc.
-The same applies for this example.
+This example is already configured using a testing purpose twitter account
named 'cameltweet'.
+And therefore the example is ready to run out of the box.
+
+This account is only for testing purpose, and should not be used in your
custom applications.
+For that you need to setup and use your own twitter account.
We have described this in more details at the Camel twitter documentation:
http://camel.apache.org/twitter
-When you have created an application, you get a number of details back from
twitter
-which you need to use the twitter component. Enter these details in the source
code at:
-
src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java
-in the constant fields, by replacing the values "INSERT HERE".
-
You will need to compile this example first:
mvn compile
Modified:
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java
URL:
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java?rev=1305013&r1=1305012&r2=1305013&view=diff
==============================================================================
---
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java
(original)
+++
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java
Sun Mar 25 11:57:50 2012
@@ -27,32 +27,25 @@ public final class CamelTwitterWebSocket
// In order to use camel-twitter with your account, you'll need to create
a new application
// within Twitter at https://dev.twitter.com/apps/new and grant the
application access to your account.
// Finally, generate your access token and secret.
- private static String consumerKey = "INSERT HERE";
- private static String consumerSecret = "INSERT HERE";
- private static String accessToken = "INSERT HERE";
- private static String accessTokenSecret = "INSERT HERE";
+
+ // This uses the Twitter 'cameltweet' account for testing purposes.
+ // do NOT use this twitter account in your applications!
+ private static String consumerKey = "NMqaca1bzXsOcZhP2XlwA";
+ private static String consumerSecret =
"VxNQiRLwwKVD0K9mmfxlTTbVdgRpriORypnUbHhxeQw";
+ private static String accessToken =
"26693234-W0YjxL9cMJrC0VZZ4xdgFMymxIQ10LeL1K8YlbBY";
+ private static String accessTokenSecret =
"BZD51BgzbOdFstWZYsqB5p5dbuuDV12vrOdatzhY4E";
private CamelTwitterWebSocketMain() {
- // utility class
+ // to pass checkstyle we have a private constructor
}
public static void main(String[] args) throws Exception {
- if (consumerKey.equals("INSERT HERE")) {
- System.out.println("\n\n\n\n");
-
System.err.println("============================================================================");
- System.err.println("Error you need to configure twitter
application authentication before using.");
- System.err.println("See more details in this source code.");
-
System.err.println("============================================================================");
- System.out.println("\n\n\n\n");
- System.exit(0);
- } else {
- System.out.println("\n\n\n\n");
-
System.out.println("===============================================");
- System.out.println("Open your web browser on
http://localhost:9090");
- System.out.println("Press ctrl+c to stop this example");
-
System.out.println("===============================================");
- System.out.println("\n\n\n\n");
- }
+ System.out.println("\n\n\n\n");
+ System.out.println("===============================================");
+ System.out.println("Open your web browser on http://localhost:9090");
+ System.out.println("Press ctrl+c to stop this example");
+ System.out.println("===============================================");
+ System.out.println("\n\n\n\n");
// create a new Camel Main so we can easily start Camel
Main main = new Main();
@@ -72,6 +65,9 @@ public final class CamelTwitterWebSocket
route.setSearchTerm("gaga");
route.setDelay(2);
+ // web socket on port 9090
+ route.setPort(9090);
+
// add our routes to Camel
main.addRouteBuilder(route);
Modified:
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java
URL:
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java?rev=1305013&r1=1305012&r2=1305013&view=diff
==============================================================================
---
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java
(original)
+++
camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java
Sun Mar 25 11:57:50 2012
@@ -17,6 +17,7 @@
package org.apache.camel.example.websocket;
import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.twitter.TwitterComponent;
import org.apache.camel.component.websocket.WebsocketComponent;
import org.apache.camel.component.websocket.WebsocketConstants;
@@ -26,9 +27,9 @@ import org.apache.camel.component.websoc
*/
public class TwitterWebSocketRoute extends RouteBuilder {
+ private int port = 9090;
private String searchTerm;
private int delay = 2;
-
private String consumerKey;
private String consumerSecret;
private String accessToken;
@@ -82,17 +83,30 @@ public class TwitterWebSocketRoute exten
this.searchTerm = searchTerm;
}
+ public int getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
@Override
public void configure() throws Exception {
// setup Camel web-socket component on port 9090
WebsocketComponent wc = getContext().getComponent("websocket",
WebsocketComponent.class);
- wc.setPort(9090);
+ wc.setPort(port);
wc.setStaticResources("src/main/resources");
+ // setup Twitter component
+ TwitterComponent tc = getContext().getComponent("twitter",
TwitterComponent.class);
+ tc.setAccessToken(accessToken);
+ tc.setAccessTokenSecret(accessTokenSecret);
+ tc.setConsumerKey(consumerKey);
+ tc.setConsumerSecret(consumerSecret);
+
// poll twitter search for new tweets, and push tweets to all web
socket subscribers on camel-tweet
- fromF("twitter://search?type=polling&delay=%s&keywords=%s"
- +
"&consumerKey=%s&consumerSecret=%s&accessToken=%s&accessTokenSecret=%s",
- delay, searchTerm, consumerKey, consumerSecret, accessToken,
accessTokenSecret)
+ fromF("twitter://search?type=polling&delay=%s&keywords=%s", delay,
searchTerm)
.setHeader(WebsocketConstants.SEND_TO_ALL).constant(true)
.to("websocket:camel-tweet");
}