Author: tyrell Date: Sat Jul 19 09:18:52 2008 New Revision: 19638 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19638
Log: Adding TwitterMap as a sample. Fixing MASHUP-996 Added: trunk/mashup/java/modules/samples/TwitterMap/ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.js trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/background-trans.png (contents, props changed) trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/bird.png (contents, props changed) trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/index.html trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.css trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.js trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.tags Modified: trunk/mashup/java/modules/distribution/pom.xml Modified: trunk/mashup/java/modules/distribution/pom.xml URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/distribution/pom.xml?rev=19638&r1=19637&r2=19638&view=diff ============================================================================== --- trunk/mashup/java/modules/distribution/pom.xml (original) +++ trunk/mashup/java/modules/distribution/pom.xml Sat Jul 19 09:18:52 2008 @@ -233,6 +233,7 @@ <fileset dir="../samples/formulaFlicks"/> <fileset dir="../samples/ExcelSample"/> <fileset dir="../samples/CSVSample"/> + <fileset dir="../samples/TwitterMap"/> </copy> <echo message="### Copy the Docs ###"/> Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.js URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.js?pathrev=19638 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.js Sat Jul 19 09:18:52 2008 @@ -0,0 +1,73 @@ +/* +* Copyright 2005-2007 WSO2, Inc. http://www.wso2.org +* +* Licensed 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. +* +* Created 2008 Tyrell Perera; [EMAIL PROTECTED] +* +*/ +this.serviceName = "TwitterMap"; +this.scope = "application"; +this.documentation = "A geographic visualization of posts to Twitter, using Twitter Feeds and Google Maps." ; + + +fetchTwitterMap.documentation = "Fetches the list of recent Tweets. Requires Twitter authentication for 'Friends Only' mode." ; +fetchTwitterMap.inputTypes = {"isFriendsOnly" : "boolean", "username" : "string", "password" : "string"}; +fetchTwitterMap.outputType = "xml"; +function fetchTwitterMap(isFriendsOnly, username, password){ + + var url = "http://twitter.com/statuses/public_timeline.xml"; + + var request = new WSRequest(); + + var options = new Array(); + options.useSOAP = false; + options.useWSA = false; + options.HTTPMethod = "GET"; + + if(isFriendsOnly){ + url = "http://twitter.com/statuses/friends_timeline.xml"; + options.username = username; + options.password = password; + } + + request.open(options, url, false); + request.send(null); + + return <twittermap>{request.responseXML}</twittermap>; +} + + +validateTwitterLogin.documentation = "Validates a username/password combination from Twitter." ; +validateTwitterLogin.inputTypes = {"username" : "string", "password" : "string"}; +validateTwitterLogin.outputType = "xml"; +function validateTwitterLogin(username, password){ + var request = new WSRequest(); + + var options = new Array(); + options.useSOAP = false; + options.useWSA = false; + options.HTTPMethod = "GET"; + options.username = username; + options.password = password; + + try{ + request.open(options, "http://twitter.com/account/verify_credentials.xml", false); + request.send(null); + return request.responseXML; + }catch(e){ + return <authorized>false</authorized>; + } + + +} Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/background-trans.png URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/background-trans.png?pathrev=19638 ============================================================================== Binary file. No diff available. Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/bird.png URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/images/bird.png?pathrev=19638 ============================================================================== Binary file. No diff available. Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/index.html URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/index.html?pathrev=19638 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/index.html Sat Jul 19 09:18:52 2008 @@ -0,0 +1,64 @@ +<!-- + ~ Copyright 2005-2007 WSO2, Inc. http://www.wso2.org + ~ + ~ Licensed 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. + ~ + ~ Created 2008 Tyrell Perera; [EMAIL PROTECTED] + ~ + --> + +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<html> +<head> + + <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> + <title>Welcome to TwitterMap</title> + + <link href="twittermap.css" rel="stylesheet" type="text/css"/> + + <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" + type="text/javascript"></script> + <script type="text/javascript" src="../../../js/wso2/WSRequest.js"></script> + <script type="text/javascript" src="../TwitterMap?stub"></script> + <script type="text/javascript" src="../../../wsasadmin/global_params.js"></script> + <script type="text/javascript" src="../../../wsasadmin/js/main.js"></script> + <script type="text/javascript" src="twittermap.js"></script> +</head> + +<body onload='init();'> +<img alt="WSO2 Mashup Server logo" src="/images/wso2_mashup_logo.gif"/> +<div>TwitterMap is a geographic visualization of posts to Twitter, using Twitter Feeds and Google Maps</div> +<hr/> +<div id="tweetCount"></div> +<div id="customizer" style="top: 7px;">You are currently tracking all public posts to Twitter. (<a href="#" onclick="overlay();">Track only my friends</a>)</div> +<div id="map_canvas" style="width: 99%; height: 90%; position: absolute;"></div> +<div id="error-console"></div> +<div id="overlay"> + <div> + <p>In order to track only your friends, you need to sign in to Twitter.</p> + <p>Note: Your login information will be stored in the browser cookies and NOT in our servers.</p> + <table summary="form: login information" id="yreglgtb"> + <tbody><tr> + <th><label for="username">Twitter ID:</label></th> + <td><input type="text" maxlength="96" size="17" value="" id="username" name="login"/></td> + </tr> + <tr> + <th><label for="passwd">Password:</label></th> + <td><input type="password" maxlength="64" size="17" value="" id="passwd" name="passwd"/></td> + </tr> + </tbody></table> + <p>[<a href='#' onclick='overlay()'>Cancel</a>] [<a href='#' onclick='saveFilter()'>Save</a>]</p> + </div> +</div> +</body> +</html> Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.css URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.css?pathrev=19638 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.css Sat Jul 19 09:18:52 2008 @@ -0,0 +1,85 @@ +/* +* Copyright 2005-2007 WSO2, Inc. http://www.wso2.org +* +* Licensed 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. +* +* Created 2008 Tyrell Perera; [EMAIL PROTECTED] +* +*/ + +body { + font: 75%/1.5 "Lucida Grande","Lucida Sans","Microsoft Sans Serif", "Lucida Sans Unicode",verdana,sans-serif,"trebuchet ms"; + color: #111; +} + +img { + margin-bottom: 0.5em +} + +span { + font-weight: bold +} + +.template { + margin-left:1em; + margin-bottom:1em; + font-size:125% +} + +#console { + color:red; + font-weight:bold +} + +#tweetCount { + position: absolute; + top: 7px; + left: 45%; + width: 200px; + background-color: 999999; + opacity: .8; + text-align: center; +} + +#customizer{ + position: absolute; + top: 7px; + right: 0.5%; + width: 300px; + background-color: #FFFFCC; + opacity: .8; + text-align: center; +} + +#overlay { + visibility: hidden; + position: absolute; + left: 0px; + top: 0px; + width:100%; + height:100%; + text-align:center; + z-index: 1000; + background:url("images/background-trans.png"); +} + +#overlay div { + width:300px; + margin: 100px auto; + background-color: #fff; + border:1px solid #000; + padding:15px; + text-align:center; +} + + Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.js URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.js?pathrev=19638 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.resources/www/twittermap.js Sat Jul 19 09:18:52 2008 @@ -0,0 +1,312 @@ +/* +* Copyright 2005-2007 WSO2, Inc. http://www.wso2.org +* +* Licensed 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. +* +* Created 2008 Tyrell Perera; [EMAIL PROTECTED] +* +*/ + +// This will hold the Google Map, once successfully initialized. +var map = ""; + +// This stores the level of detail required in the map +var zoomLevel = 4 + +// The GeoCoder, which converts address locations to GeoCodes. +var geocoder = ""; + +// An array of 'tweet' elements, which stores a retrieved set of posts. +var postCache = new Array(); + +// Stores the current iteration through the post cache +var currentItteration = 0; + +// Stores the number of posts +var postCount = 0; + +// Stores the currently processing post +var tweet = ""; + +// Marks the location of a user in the map +var marker = ""; + + +//Calls the toString operation of the 'TwitterMap' Mashup +function callMashup() { + + TwitterMap.setAddress(TwitterMap.endpoint, "services/TwitterMap"); + TwitterMap.fetchTwitterMap.onError = handleError; + TwitterMap.fetchTwitterMap.callback = function (response) { + fillData(response); + } + + if(readCookie("user") == null){ + TwitterMap.fetchTwitterMap("","",""); + }else{ + TwitterMap.fetchTwitterMap(true, readCookie("user"), readCookie("pwd")); + } + +} + +//Handles and error by displaying the reason in a dialog +function handleError(error) { + var console = document.getElementById("error-console"); + log(console, "Fault: " + error.reason + "\n\n" + error.detail); +} + +// Updates the post cache with retrieved data +function fillData(response) +{ + // Let's update the post cache + postCache = response.getElementsByTagName("status"); +} + +function log(console, data) { + var browser = WSRequest.util._getBrowser(); + if (browser == "ie" || browser == "ie7") + console.innerText = data; + else + console.textContent = data; +} + +function init() { + // Drawing the initial map + if (GBrowserIsCompatible()) { + // Use https if in 'Friend tweets Only' mode + if(!(readCookie("user") == null)){ + redirectToHttps("services/samples/TwitterMap/"); + + // update UI + document.getElementById("customizer").innerHTML = 'You are currently tracking your Twitter friends only. (<a href="#" onclick="trackPublic();">Track all public posts on Twitter</a>)' + } + + map = new GMap2(document.getElementById("map_canvas")); + var colombo = new GLatLng(6.927200, 79.872200); + map.setCenter(colombo, zoomLevel); + + // Customizing the marker + // Create our "tiny" marker icon + var birdIcon = new GIcon(G_DEFAULT_ICON); + birdIcon.image = "images/bird.png"; + + // Set up our GMarkerOptions object + markerOptions = { icon:birdIcon }; + + // Adding the welcome message marker + marker = new GMarker(colombo, markerOptions) + map.addOverlay(marker); + marker.openInfoWindowHtml("<img src='/images/wso2_mashup_logo.gif' alt='WSO2 Mashup Server logo'/><br/><b>Welcome to TwitterMap</b><br> Please wait while we fetch tweets ..."); + + // Initializing the GeoCoder, which will map GeoCodes for a given location + geocoder = new GClientGeocoder(); + + // Calling the backend service to fetch the initial data + callMashup(); + + // Scheudling the post processor for once every 5 seconds + setInterval(processPosts, 7 * 1000); + + } else { + alert("Sorry. It seems like your browser doesn't support Google Maps."); + } +} + +// Iterates through the posts cached and fetches new posts when the cache expires +function processPosts() { + if (postCache.length > 0) { + // Check whether we still have posts in cache + if (currentItteration < postCache.length) { + tweet = postCache[postCache.length - currentItteration]; + + var userLocation = ""; + + try { + userLocation = tweet.getElementsByTagName("location")[0].firstChild.nodeValue; + // Calling the GeoCoder passing the 'displayTweet' function as a callback + geocoder.getLatLng(userLocation, displayTweet); + } catch(error) { + // Swallowing the error, since there's nothing much to do if a user hasn't given the location. + } + + // Incrementing the itteration + currentItteration++; + } else { + // We are done with the cached posts. Let's fetch new ones from the server. + currentItteration = 0; + postCache = new Array(); + document.getElementById("tweetCount").innerHTML = "Fetching (hopefully) new tweets ..."; + callMashup(); + } + } +} + +// Renders the Tweet info when the GeoCoder fetches a point. +function displayTweet(point) { + if (!point) { + // Again swallowing the error. The user has stored an incorrect address. + } else { + if (!(marker == "")) { + // Removing the old markers + map.removeOverlay(marker); + } + marker = new GMarker(point, markerOptions); + map.addOverlay(marker); + marker.openInfoWindowHtml(generatePostHTML()); + + document.getElementById("tweetCount").innerHTML = + (postCache.length - currentItteration) + " tweets left"; + } +} + +// Creates a nicely formatted html post to be displayed. +function generatePostHTML() { + var postText = tweet.getElementsByTagName("text")[0].firstChild.nodeValue; + var userName = tweet.getElementsByTagName("name")[0].firstChild.nodeValue; + var userScreenName = tweet.getElementsByTagName("screen_name")[0].firstChild.nodeValue; + var userLocation = tweet.getElementsByTagName("location")[0].firstChild.nodeValue; + var userImage = tweet.getElementsByTagName("profile_image_url")[0].firstChild.nodeValue; + var postedOn = tweet.getElementsByTagName("created_at")[0].firstChild.nodeValue; + + var resp = "<img src='" + userImage + "' align='left' style='margin-right= 5px;'/><b>" + + userName + " (" + userScreenName + ")</b><br/>" + postText + + "<br/>twittering from <i>" + userLocation + " [ " + prettyDate(postedOn) + " ]</i>."; + + return resp; +} + +// Toggles the filter between public_timeline and friend_timeline +function changeFilter(isPublic){ + overlay(); +} + + +function overlay() { + document.getElementById("username").value = ""; + document.getElementById("passwd").value = ""; + el = document.getElementById("overlay"); + el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible"; +} + + +function createCookie(name,value,days) { + if (days) { + var date = new Date(); + date.setTime(date.getTime()+(days*24*60*60*1000)); + var expires = "; expires="+date.toGMTString(); + } + else var expires = ""; + document.cookie = name+"="+value+expires+"; path=/"; +} + +function readCookie(name) { + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for(var i=0;i < ca.length;i++) { + var c = ca[i]; + while (c.charAt(0)==' ') c = c.substring(1,c.length); + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); + } + return null; +} + +function eraseCookie(name) { + createCookie(name,"",-1); +} + + +// Validates and stores a +function saveFilter(){ + var userName = document.getElementById("username").value; + var password = document.getElementById("passwd").value; + + TwitterMap.setAddress(TwitterMap.endpoint, "services/TwitterMap"); + TwitterMap.validateTwitterLogin.onError = handleError; + TwitterMap.validateTwitterLogin.callback = function (response) { + saveFiltercallback(response); + } + + TwitterMap.validateTwitterLogin(userName, password); +} + + +function saveFiltercallback(response){ + var valid = response.firstChild.nodeValue; + + if(!(valid == "true")){ + alert ("Sorry! Twitter says it doesn't know you."); + }else { + var userName = document.getElementById("username").value; + var password = document.getElementById("passwd").value; + + createCookie("user", userName, 7); + createCookie("pwd", password, 7); + + // Refresh the page so that he new filter is picked up + location.reload(true); + } +} + + +// Redirects the current url to https. HTTPS us used full time in friends only mode. +function redirectToHttps(bounceback) { + wso2.wsf.Util.initURLs(); + var locationString = self.location.href; + var _tmpURL = locationString.substring(0, locationString.lastIndexOf('/')); + if (_tmpURL.indexOf('https') == -1) { + + //Re-direct to https + var redirectUrl = "https://" + self.location.hostname; + + if(!(URL.indexOf('://') == URL.lastIndexOf(':'))){ + redirectUrl += ":" + HTTPS_PORT; + } + + redirectUrl += "/" + decodeURI(bounceback); + + window.location = redirectUrl; + } +} + + +// Changes preference to track all public posts in Twitter +function trackPublic(){ + // Erasing cookies and re-loading + eraseCookie("user"); + eraseCookie("pwd"); + + // Refresh the page so that he new filter is picked up + location.reload(true); +} + + +function prettyDate(time){ + + var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), + diff = (((new Date()).getTime() - date.getTime()) / 1000), + day_diff = Math.floor(diff / 86400); + + if ( isNaN(day_diff) || day_diff <0 || day_diff>= 31 ) + return; + + return day_diff == 0 && ( + diff <60 && "just now" || + diff <120 && "1 minute ago" || + diff <3600 && Math.floor( diff / 60 ) + " minutes ago" || + diff <7200 && "1 hour ago" || + diff <86400 && Math.floor( diff / 3600 ) + " hours ago") || + day_diff == 1 && "Yesterday" || + day_diff <7 && day_diff + " days ago" || + day_diff <31 && Math.ceil( day_diff / 7 ) + " weeks ago"; +} + Added: trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.tags URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.tags?pathrev=19638 ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/samples/TwitterMap/TwitterMap.tags Sat Jul 19 09:18:52 2008 @@ -0,0 +1,22 @@ +<!-- + ~ Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + ~ + ~ Licensed 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. + --> +<tags> + <tag>Twitter</tag> + <tag>Maps</tag> + <tag>GeoCode</tag> + <tag>WSRequest</tag> + <tag>sample</tag> +</tags> _______________________________________________ Mashup-dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/mashup-dev
