Author: davsclaus Date: Sat Mar 17 15:30:52 2012 New Revision: 1301951 URL: http://svn.apache.org/viewvc?rev=1301951&view=rev Log: Added twitter and websocket example
Added: camel/trunk/examples/camel-example-twitter-websocket/ camel/trunk/examples/camel-example-twitter-websocket/pom.xml (with props) camel/trunk/examples/camel-example-twitter-websocket/src/ camel/trunk/examples/camel-example-twitter-websocket/src/main/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java (with props) camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java (with props) camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/ camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html (with props) camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties (with props) Modified: camel/trunk/examples/pom.xml Added: camel/trunk/examples/camel-example-twitter-websocket/pom.xml URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/pom.xml?rev=1301951&view=auto ============================================================================== --- camel/trunk/examples/camel-example-twitter-websocket/pom.xml (added) +++ camel/trunk/examples/camel-example-twitter-websocket/pom.xml Sat Mar 17 15:30:52 2012 @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + + 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. + +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>examples</artifactId> + <version>2.10-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>camel-example-twitter-websocket</artifactId> + <packaging>jar</packaging> + <name>Camel :: Example :: Twitter WebSocket</name> + <description>An example that pushes new tweets to a web page using web-socket</description> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-twitter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-websocket</artifactId> + </dependency> + + <!-- logging to the console --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </dependency> + + </dependencies> + + <build> + <plugins> + <!-- Allows the example to be run via 'mvn compile exec:java' --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <configuration> + <mainClass>org.apache.camel.example.websocket.CamelTwitterWebSocketMain</mainClass> + <includePluginDependencies>false</includePluginDependencies> + </configuration> + </plugin> + </plugins> + + </build> + +</project> Propchange: camel/trunk/examples/camel-example-twitter-websocket/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/examples/camel-example-twitter-websocket/pom.xml ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/examples/camel-example-twitter-websocket/pom.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: 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=1301951&view=auto ============================================================================== --- camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java (added) +++ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java Sat Mar 17 15:30:52 2012 @@ -0,0 +1,78 @@ +/** + * 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.example.websocket; + +import org.apache.camel.main.Main; + +/** + * A main to start this example. + */ +public class CamelTwitterWebSocketMain { + + // Twitter now requires the use of OAuth for all client application authentication. + // 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"; + + 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"); + } + + // create a new Camel Main so we can easily start Camel + Main main = new Main(); + + // enable hangup support which mean we detect when the JVM terminates, and stop Camel graceful + main.enableHangupSupport(); + + TwitterWebSocketRoute route = new TwitterWebSocketRoute(); + + // setup twitter application authentication + route.setAccessToken(accessToken); + route.setAccessTokenSecret(accessTokenSecret); + route.setConsumerKey(consumerKey); + route.setConsumerSecret(consumerSecret); + + // poll for gaga, every 2nd second + route.setSearchTerm("gaga"); + route.setDelay(2); + + // add our routes to Camel + main.addRouteBuilder(route); + + // and run, which keeps blocking until we terminate the JVM (or stop CamelContext) + main.run(); + } + +} Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/CamelTwitterWebSocketMain.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: 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=1301951&view=auto ============================================================================== --- camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java (added) +++ camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java Sat Mar 17 15:30:52 2012 @@ -0,0 +1,99 @@ +/** + * 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.example.websocket; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.websocket.WebsocketComponent; +import org.apache.camel.component.websocket.WebsocketConstants; + +/** + * A Camel route that updates from twitter all tweets using having the search term. + * And post the changes to web-socket, that can be viewed from a web page + */ +public class TwitterWebSocketRoute extends RouteBuilder { + + private String searchTerm; + private int delay = 2; + + private String consumerKey; + private String consumerSecret; + private String accessToken; + private String accessTokenSecret; + + 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; + } + + public int getDelay() { + return delay; + } + + public void setDelay(int delay) { + this.delay = delay; + } + + public String getSearchTerm() { + return searchTerm; + } + + public void setSearchTerm(String searchTerm) { + this.searchTerm = searchTerm; + } + + @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.setStaticResources("src/main/resources"); + + // 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) + .setHeader(WebsocketConstants.SEND_TO_ALL).constant(true) + .to("websocket:camel-tweet"); + } +} Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/java/org/apache/camel/example/websocket/TwitterWebSocketRoute.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html?rev=1301951&view=auto ============================================================================== --- camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html (added) +++ camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html Sat Mar 17 15:30:52 2012 @@ -0,0 +1,73 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + + 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. + +--> + +<html> +<head> + <title>Camel Twitter WebSocket Example</title> + <script type='text/javascript'> + + if (!window.WebSocket) { + alert("WebSocket not supported by this browser"); + } + + function $() { return document.getElementById(arguments[0]); } + + var room = { + join: function(name) { + this._username=name; + var location = "ws://localhost:9090/camel-tweet"; + this._ws=new WebSocket(location); + this._ws.onmessage=this._onmessage; + this._ws.onclose=this._onclose; + }, + + _onmessage: function(m) { + if (m.data){ + var chat=$('chat'); + var spanText = document.createElement('span'); + spanText.className='text'; + spanText.innerHTML=m.data; + var lineBreak = document.createElement('br'); + chat.appendChild(spanText); + chat.appendChild(lineBreak); + chat.scrollTop = chat.scrollHeight - chat.clientHeight; + } + }, + + _onclose: function(m) { + this._ws=null; + } + + }; + + </script> + <style type='text/css'> + div { border: 0px solid black; } + div#chat { clear: both; width: 80em; height: 60ex; overflow: auto; background-color: #f0f0f0; padding: 4px; border: 1px solid black; } + </style> +</head> +<body> +<div id='chat'></div> +<script type='text/javascript'> + room.join("Testing"); +</script> +</body> +</html> Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/index.html ------------------------------------------------------------------------------ svn:mime-type = text/html Added: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties URL: http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties?rev=1301951&view=auto ============================================================================== --- camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties (added) +++ camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties Sat Mar 17 15:30:52 2012 @@ -0,0 +1,10 @@ +log4j.rootLogger=INFO, out + +# uncomment the following line to turn on Camel debugging +#log4j.logger.org.apache.camel.component.twitter=DEBUG +#log4j.logger.org.apache.camel.component.websocket=DEBUG + +# CONSOLE appender not used by default +log4j.appender.out=org.apache.log4j.ConsoleAppender +log4j.appender.out.layout=org.apache.log4j.PatternLayout +log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties ------------------------------------------------------------------------------ svn:keywords = Rev Date Propchange: camel/trunk/examples/camel-example-twitter-websocket/src/main/resources/log4j.properties ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: camel/trunk/examples/pom.xml URL: http://svn.apache.org/viewvc/camel/trunk/examples/pom.xml?rev=1301951&r1=1301950&r2=1301951&view=diff ============================================================================== --- camel/trunk/examples/pom.xml (original) +++ camel/trunk/examples/pom.xml Sat Mar 17 15:30:52 2012 @@ -65,6 +65,7 @@ <module>camel-example-spring-security</module> <module>camel-example-ssh</module> <module>camel-example-tracer</module> + <module>camel-example-twitter-websocket</module> </modules> <properties>