Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart2.png 45% of files have more coverage
62   196   22   6,89
18   120   0,35   9
9     2,44  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  PostToTwitterBlogEntryListener       Line # 28 62 0% 22 77 13,5% 0.13483146
 
  (10)
 
1    package net.sourceforge.pebble.event.blogentry;
2   
3    import java.io.BufferedReader;
4    import java.io.IOException;
5    import java.io.InputStreamReader;
6    import java.net.MalformedURLException;
7    import java.net.URL;
8    import java.net.URLConnection;
9   
10    import net.sourceforge.pebble.PluginProperties;
11    import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent;
12    import net.sourceforge.pebble.api.event.blogentry.BlogEntryListener;
13    import net.sourceforge.pebble.domain.Blog;
14    import net.sourceforge.pebble.domain.BlogEntry;
15   
16    import org.apache.commons.logging.Log;
17    import org.apache.commons.logging.LogFactory;
18   
19    import twitter4j.Twitter;
20   
21    /**
22    * Post new blog entries to twitter.
23    * This class is based on a patch by Steve Carton (PEBBLE-15), but changes it to use the
24    * pebble plugin mechanism by implementing the {@link BlogEntryListener} interface
25    *
26    * @author Steve Carton, Olaf Kock
27    */
 
28    public class PostToTwitterBlogEntryListener extends BlogEntryListenerSupport {
29   
30    /** the log used by this class */
31    private static final Log log = LogFactory
32    .getLog(PostToTwitterBlogEntryListener.class);
33    private static final String DEFAULT_TWEET_URL="https://twitter.com/";
34   
35    /**
36    * Called when a blog entry has been published.
37    *
38    * @param event
39    * a BlogEntryEvent instance
40    */
 
41  0 toggle public void blogEntryPublished(BlogEntryEvent event) {
42  0 BlogEntry blogEntry = event.getBlogEntry();
43  0 String twitterUsername = getTwitterUsername(blogEntry);
44  0 String twitterPassword = getTwitterPassword(blogEntry);
45  0 String twitterUrl = getTwitterUrl(blogEntry);
46  0 if(twitterUsername == null || twitterPassword == null) {
47  0 blogEntry.getBlog().error("Please configure twitter credentials in order to post to twitter");
48  0 return;
49    }
50  0 String longUrl = blogEntry.getLocalPermalink();
51    // if(!checkUrl(longUrl)) {
52    // blogEntry.getBlog().error("cowardly refusing to post url '" + longUrl + "' to twitter");
53    // return;
54    // }
55  0 String tinyUrl = makeTinyURL(longUrl);
56  0 if (tinyUrl.equalsIgnoreCase("error"))
57  0 tinyUrl = longUrl;
58  0 String msg = composeMessage(blogEntry.getTitle(), longUrl, tinyUrl);
59  0 try {
60  0 if(getProperty(blogEntry, "simulate") != null) {
61  0 blogEntry.getBlog().info("Found property 'twitter.simulate' - This would have been posted to twitter with username '" + twitterUsername + "':\n" + msg);
62    } else {
63  0 post(twitterUrl, twitterUsername, twitterPassword, msg);
64    }
65    } catch (Exception e) {
66  0 e.printStackTrace();
67    }
68  0 log.debug("Blog entry <a href=\"" + longUrl
69    + "\">" + blogEntry.getTitle() + "</a> tweeted.");
70    }
71   
72    /**
73    * make sure that the url we are about to post is one that makes sense to post - e.g. don't post 'localhost' urls.
74    * @param longUrl
75    * @return true if the url passes the (simple) tests and may be posted
76    */
 
77  12 toggle boolean checkUrl(String longUrl) {
78  12 return ! (longUrl.contains("://localhost:") || longUrl.contains("://localhost/"));
79    }
80   
81    /**
82    * combine message with url. This will use longUrl when enough characters are left, tinyUrl otherwise.
83    * If necessary, the title will be shortened in order to include the full URL
84    * @param title
85    * @param longUrl
86    * @param tinyUrl
87    * @return the "up to 140 character" message to post to twitter
88    */
 
89  6 toggle String composeMessage(String title, String longUrl, String tinyUrl) {
90  6 if(longUrl.length() + title.length() > 139 ) {
91  4 if(tinyUrl.length() + title.length() > 139) {
92  2 return title.substring(0, 139-tinyUrl.length()) + " " + tinyUrl;
93    } else {
94  2 return title + " " + tinyUrl;
95    }
96    } else {
97  2 return title + " " + longUrl ;
98    }
99    }
100   
101    /**
102    * get twitter URL to post to. This can be overridden with the blog property twitter.url - e.g. for testing purposes.
103    * @param blogEntry
104    * @return
105    */
 
106  0 toggle private String getTwitterUrl(BlogEntry blogEntry) {
107  0 String twitterUrl = getProperty(blogEntry, "url");
108  0 if(twitterUrl == null) twitterUrl = DEFAULT_TWEET_URL;
109  0 return twitterUrl;
110    }
111   
112    /**
113    * the password to post to twitter as configured in the blog properties
114    * @param blogEntry
115    * @return
116    */
 
117  0 toggle private String getTwitterPassword(BlogEntry blogEntry) {
118  0 return getProperty(blogEntry, "password");
119    }
120   
121    /**
122    * the username to post to twitter as configured in the blog properties
123    * @param blogEntry
124    * @return
125    */
 
126  0 toggle private String getTwitterUsername(BlogEntry blogEntry) {
127  0 return getProperty(blogEntry, "username");
128    }
129   
 
130  0 toggle private String getProperty(BlogEntry blogEntry, String property) {
131  0 Blog blog = blogEntry.getBlog();
132  0 String blogName = blog.getName();
133  0 PluginProperties pluginProperties = blog.getPluginProperties();
134  0 String result = pluginProperties.getProperty("twitter." + blogName + "." + property);
135  0 if(result == null) {
136  0 result = pluginProperties.getProperty("twitter." + property);
137  0 if(result == null) {
138  0 log.error("Twitter credentials (" + property + ") not found. Please configure twitter." + property + " in order to post to twitter");
139    } else {
140  0 log.debug("found twitter credentials in twitter." + property );
141    }
142    } else {
143  0 log.debug("found twitter credentials in twitter." + blogName + "." + property);
144    }
145  0 return result;
146    }
147   
148    /**
149    * Post the given message to twitter, using the given postUrl and credentials.
150    * @param twitterUrl URL to post to
151    * @param twitterUsername username to post as
152    * @param twitterPassword password to authenticate username
153    * @param msg the message to post to twitter.
154    * @throws Exception
155    */
 
156  0 toggle private void post(String twitterUrl, String twitterUsername,
157    String twitterPassword, String msg) throws Exception {
158  0 System.out.println("Posting to Twitter: " + msg);
159  0 Twitter twitter = new Twitter(twitterUsername, twitterPassword, twitterUrl);
160  0 twitter.updateStatus(msg);
161    }
162   
163   
164    /**
165    * create a shortened version of the given url
166    * @param url
167    * @return
168    */
 
169  0 toggle private String makeTinyURL(String url) {
170    // http://tinyurl.com/api-create.php?...
171  0 StringBuffer response = new StringBuffer();
172  0 try {
173  0 URL turl = new URL("http://tinyurl.com/api-create.php?"+url);
174  0 URLConnection connection = turl.openConnection();
175  0 connection.setDoInput(true);
176  0 connection.setDoOutput(false);
177  0 connection.setUseCaches(false);
178  0 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
179  0 String r;
180  0 while ((r = in.readLine()) != null) {
181  0 response.append(r);
182    }
183  0 in.close();
184    }
185    catch (MalformedURLException e) {
186  0 log.error(e.getMessage());
187  0 return url;
188    }
189    catch (IOException e) {
190  0 log.error(e.getMessage());
191  0 return url;
192    }
193  0 log.debug("tinyurl for " + url + " is " + response.toString());
194  0 return response.toString();
195    }
196    }