Author: dblevins
Date: Wed Jun 1 05:08:17 2011
New Revision: 1130001
URL: http://svn.apache.org/viewvc?rev=1130001&view=rev
Log:
Another patch from Vishwanath (@stratwine) OPENEJB-1565: OpenEJB Retweet Bot
Thanks, Vishwanath!
Added:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java
(with props)
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java
(with props)
openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java
(with props)
Modified:
openejb/trunk/sandbox/tools/pom.xml
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AuthorizationUrlGenerator.java
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/Retweet.java
openejb/trunk/sandbox/tools/src/main/resources/RetweetTool.properties
Modified: openejb/trunk/sandbox/tools/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/pom.xml?rev=1130001&r1=1130000&r2=1130001&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/pom.xml (original)
+++ openejb/trunk/sandbox/tools/pom.xml Wed Jun 1 05:08:17 2011
@@ -79,6 +79,13 @@
<version>3.0.5.RELEASE</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.1</version>
+ <scope>compile</scope>
+ </dependency>
+
</dependencies>
</project>
Added:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java?rev=1130001&view=auto
==============================================================================
---
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java
(added)
+++
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java
Wed Jun 1 05:08:17 2011
@@ -0,0 +1,83 @@
+/**
+ * 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.openejb.tools.twitter;
+
+import java.util.Scanner;
+
+import oauth.signpost.exception.OAuthCommunicationException;
+import oauth.signpost.exception.OAuthExpectationFailedException;
+import oauth.signpost.exception.OAuthMessageSignerException;
+import oauth.signpost.exception.OAuthNotAuthorizedException;
+import static org.apache.openejb.tools.twitter.AuthorizationUrlGenerator.*;
+
+public class AccessTokenGenerator {
+
+ public static void main(String[] args)
+ {
+ AuthorizationUrlGenerator.getAuthorizationUrlForUser();
+ getTokensToReadWriteIntoTwitterAccountOfUser();
+
+ }
+
+ private static void getTokensToReadWriteIntoTwitterAccountOfUser()
+ {
+
+ try {
+ retrieveAccessTokens();
+ } catch (OAuthMessageSignerException e) {
+ e.printStackTrace();
+ } catch (OAuthNotAuthorizedException e) {
+ e.printStackTrace();
+ } catch (OAuthExpectationFailedException e) {
+ e.printStackTrace();
+ } catch (OAuthCommunicationException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void retrieveAccessTokens()
+ throws OAuthMessageSignerException,
OAuthNotAuthorizedException,
+ OAuthExpectationFailedException,
OAuthCommunicationException {
+
+ Scanner keyBoardInputScanner = new Scanner(System.in);
+ String pinFromUser = scanPIN(keyBoardInputScanner);
+
+ System.out.println("User has provided this PIN:"+pinFromUser);
+
+ provider.retrieveAccessToken(consumer,pinFromUser);
+
+ System.out.println("Consumer Token: (Copy and Paste this value
in the RetweetTool.properties against
retweetApp.authorizedUser.consumer.token)");
+ System.out.println(consumer.getToken());
+
+
+ System.out.println("Consumer Token Secret: (Copy and Paste this
value in RetweetTool.properties against
retweetApp.authorizedUser.consumer.tokenSecret)");
+ System.out.println(consumer.getTokenSecret());
+
+ }
+
+ private static String scanPIN(Scanner keyBoardInputScanner) {
+ String pinFromUser = null;
+ System.out.println("Please enter the PIN number to complete
authorization:");
+ if(keyBoardInputScanner.hasNext())
+ {
+ pinFromUser=keyBoardInputScanner.next();
+ }
+ return pinFromUser;
+ }
+
+
+}
Propchange:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AccessTokenGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AuthorizationUrlGenerator.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AuthorizationUrlGenerator.java?rev=1130001&r1=1130000&r2=1130001&view=diff
==============================================================================
---
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AuthorizationUrlGenerator.java
(original)
+++
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/AuthorizationUrlGenerator.java
Wed Jun 1 05:08:17 2011
@@ -16,8 +16,6 @@
*/
package org.apache.openejb.tools.twitter;
-import java.io.FileNotFoundException;
-import java.io.IOException;
import java.util.Properties;
import oauth.signpost.OAuth;
@@ -30,40 +28,19 @@ import oauth.signpost.exception.OAuthExp
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
-import org.springframework.core.io.ClassPathResource;
+import org.apache.openejb.tools.twitter.util.RetweetAppUtil;
public class AuthorizationUrlGenerator {
static Properties retweetToolProperties;
static OAuthConsumer consumer;
static OAuthProvider provider;
- public static void main(String[] args) {
- retweetToolProperties = getTwitterAppProperties();
+ public static String getAuthorizationUrlForUser(){
+ retweetToolProperties =
RetweetAppUtil.getTwitterAppProperties();
intializeOAuthConsumerAndProvider();
- getAuthorizationUrl();
+ return getAuthorizationUrl();
- }
-
- private static Properties getTwitterAppProperties() {
-
- Properties retweetAppProperties = new Properties();
- try {
- ClassPathResource retweetToolPropertiesFile = new
ClassPathResource(
- "RetweetTool.properties");
- retweetAppProperties.load(retweetToolPropertiesFile
- .getInputStream());
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- System.out.print("Using the following properties: ");
- System.out.print("---------------------------------");
- retweetAppProperties.list(System.out);
- return retweetAppProperties;
-
- }
+ }
private static void intializeOAuthConsumerAndProvider() {
@@ -80,10 +57,11 @@ public class AuthorizationUrlGenerator {
}
- private static void getAuthorizationUrl() {
+ private static String getAuthorizationUrl() {
+ String authUrl=null;
try {
- getAccessAuthorizationURL();
+ authUrl=getAccessAuthorizationURL();
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
@@ -93,10 +71,12 @@ public class AuthorizationUrlGenerator {
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
+
+ return authUrl;
}
- private static void getAccessAuthorizationURL()
+ private static String getAccessAuthorizationURL()
throws OAuthMessageSignerException, OAuthNotAuthorizedException,
OAuthExpectationFailedException, OAuthCommunicationException {
@@ -106,7 +86,10 @@ public class AuthorizationUrlGenerator {
System.out.println("Paste the below URL in the browser and
authorize");
System.out.println(authUrl);
System.out.println("#####################");
-
+
+ return authUrl;
+
+
}
Modified:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/Retweet.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/Retweet.java?rev=1130001&r1=1130000&r2=1130001&view=diff
==============================================================================
---
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/Retweet.java
(original)
+++
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/Retweet.java
Wed Jun 1 05:08:17 2011
@@ -16,21 +16,18 @@
*/
package org.apache.openejb.tools.twitter;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import oauth.signpost.OAuthConsumer;
-import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
-import oauth.signpost.basic.DefaultOAuthProvider;
-import oauth.signpost.exception.OAuthCommunicationException;
-import oauth.signpost.exception.OAuthExpectationFailedException;
-import oauth.signpost.exception.OAuthMessageSignerException;
-import oauth.signpost.exception.OAuthNotAuthorizedException;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.util.Assert;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.openejb.tools.twitter.util.RetweetAppUtil;
/**
*
@@ -65,29 +62,72 @@ public class Retweet {
// retweet - HTTP POST
http://api.twitter.com/1/statuses/retweet/<statusid>.xml
// Little bit of Apache Commons HTTPClient and Signpost and we're good to
go
+
+ public static OAuthConsumer consumer;
+ static Properties
retweetToolProperties=RetweetAppUtil.getTwitterAppProperties();
public static void main(String[] args) {
- // Register a Twitter App
- // User should authorize it with READ/WRITE access. See
AuthorizationUrlGenerator.java (user=OpenEJB Twitter Account)
-
- // On Authorization, user is given a PIN number.
-
- // Based on the PIN number get the AccessToken. And save the access
token permanently (File / DB )
-
- // Use this access token for any READ/WRITE
-
- // Grab the http://twitter.com/#!/OpenEJB/contributors feed via the
Twitter API
-
+ initConsumer();
+ getStatusesFromOpenEJBContributorsList();
+
// Scan for new tweets from the last hour
// Retweet any tweets that haven't been retweeted
// We could look at the OpenEJB twitter feed itself to determine if a
tweet
- // has already been retweeted
-
+
+ // has already been retweeted
+
+
}
+
+ public static void initConsumer() {
+ consumer=new DefaultOAuthConsumer(
+
retweetToolProperties.getProperty("retweetApp.consumer.key"),
+ retweetToolProperties
+ .getProperty("retweetApp.consumerSecret.key"));
+
+
consumer.setTokenWithSecret(retweetToolProperties.getProperty("retweetApp.authorizedUser.consumer.token"),
+
retweetToolProperties.getProperty("retweetApp.authorizedUser.consumer.tokenSecret"));
+
+ }
+
+ public static HttpResponse getStatusesFromOpenEJBContributorsList() {
+ String listName="contributors";
+ String ownerScreenName="OpenEJB";
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpGet httpGet =
getHttpRequestToRetrieveListStatuses(listName, ownerScreenName);
+ HttpResponse contributorsListStatusesResponse =
getContribListStatusesResponse(httpClient, httpGet);
+
+ return contributorsListStatusesResponse;
+ }
+
+ private static HttpGet getHttpRequestToRetrieveListStatuses(String
listName,
+ String ownerScreenName) {
+ HttpGet httpGet = new
HttpGet("http://api.twitter.com/1/lists/statuses.json?slug="+listName
+ +"&owner_screen_name="+ownerScreenName);
+ System.out.println("Getting list using "+httpGet.getURI());
+ return httpGet;
+ }
+
+ private static HttpResponse getContribListStatusesResponse(HttpClient
httpClient,
+ HttpGet httpGet) {
+ HttpResponse response = null;
+ try {
+ response = httpClient.execute(httpGet);
+ } catch (ClientProtocolException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return response;
+ }
+
+
+
+
Added:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java?rev=1130001&view=auto
==============================================================================
---
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java
(added)
+++
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java
Wed Jun 1 05:08:17 2011
@@ -0,0 +1,48 @@
+/**
+ * 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.openejb.tools.twitter.util;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.springframework.core.io.ClassPathResource;
+
+public class RetweetAppUtil {
+
+ public static Properties getTwitterAppProperties() {
+
+ Properties retweetAppProperties = new Properties();
+ try {
+ ClassPathResource retweetToolPropertiesFile = new
ClassPathResource(
+ "RetweetTool.properties");
+ retweetAppProperties.load(retweetToolPropertiesFile
+ .getInputStream());
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ System.out.print("Using the following properties: ");
+ System.out.print("---------------------------------");
+ retweetAppProperties.list(System.out);
+ return retweetAppProperties;
+
+ }
+
+}
Propchange:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/twitter/util/RetweetAppUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openejb/trunk/sandbox/tools/src/main/resources/RetweetTool.properties
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/resources/RetweetTool.properties?rev=1130001&r1=1130000&r2=1130001&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/resources/RetweetTool.properties
(original)
+++ openejb/trunk/sandbox/tools/src/main/resources/RetweetTool.properties Wed
Jun 1 05:08:17 2011
@@ -1,5 +1,11 @@
+# These keys don't change
retweetApp.api.key=XMC5YWizyrLHanUoyhSf6Q
retweetApp.consumer.key=XMC5YWizyrLHanUoyhSf6Q
retweetApp.consumerSecret.key=9xIaVpmIGVLc9EKThtbziKkS6R0HUnBr3F7rjQGoE
retweetApp.requestToken.url=https://api.twitter.com/oauth/request_token
retweetApp.authorize.url=https://api.twitter.com/oauth/authorize
+
+# Below tokens now reflect a mock twitter account (stratwine2) that authorized
access
+# TODO: Change to Open EJB Twitter Account, once this works fine
+retweetApp.authorizedUser.consumer.token=307322883-5jiw2tpV1A5b1487TKHHKPjBjH54m3PZINuiuSyQ
+retweetApp.authorizedUser.consumer.tokenSecret=06fM8tNXGPOhLsFB8QuIlzIhbdriqdE5598pLeJk
Added:
openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java?rev=1130001&view=auto
==============================================================================
---
openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java
(added)
+++
openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java
Wed Jun 1 05:08:17 2011
@@ -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.openejb.tools;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import oauth.signpost.exception.OAuthCommunicationException;
+import oauth.signpost.exception.OAuthExpectationFailedException;
+import oauth.signpost.exception.OAuthMessageSignerException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.apache.openejb.tools.twitter.Retweet;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+public class RetweetTest {
+
+
+ @BeforeClass
+ public static void setUp()
+ {
+ Retweet.initConsumer();
+ }
+
+ @Test
+ public void basicRequestShouldGiveValidResponse() throws
OAuthMessageSignerException, OAuthExpectationFailedException,
OAuthCommunicationException, IOException
+ {
+ URL url = new URL("http://twitter.com/statuses/mentions.xml");
+ HttpURLConnection request = (HttpURLConnection)
url.openConnection();
+
+ // sign the request
+ Retweet.consumer.sign(request);
+
+ // send the request
+ request.connect();
+
+ // response status should be 200 OK
+ int statusCode = request.getResponseCode();
+ System.out.println("Status Code:"+statusCode);
+
+ assertTrue(statusCode==200);
+
+ }
+
+ @Test
+ public void contributorsListStatusesShouldBeRetrieved() throws
ClientProtocolException, IOException
+ {
+ HttpResponse
response=Retweet.getStatusesFromOpenEJBContributorsList();
+
+ assertTrue(response.getStatusLine().getStatusCode()==200);
+
+ ResponseHandler<String> responseHander = new
BasicResponseHandler();
+ String responseBody =
(String)responseHander.handleResponse(response);
+ System.out.println(responseBody);
+ }
+}
Propchange:
openejb/trunk/sandbox/tools/src/test/java/org/apache/openejb/tools/RetweetTest.java
------------------------------------------------------------------------------
svn:eol-style = native