| 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 |
|
|
| 23 |
|
|
| 24 |
|
@link |
| 25 |
|
|
| 26 |
|
@author |
| 27 |
|
|
|
|
|
| 13,5% |
Uncovered Elements: 77 (89) |
Complexity: 22 |
Complexity Density: 0,35 |
|
| 28 |
|
public class PostToTwitterBlogEntryListener extends BlogEntryListenerSupport { |
| 29 |
|
|
| 30 |
|
|
| 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 |
|
|
| 37 |
|
|
| 38 |
|
@param |
| 39 |
|
|
| 40 |
|
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 6 |
Complexity Density: 0,33 |
|
| 41 |
0
|
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 |
|
|
| 52 |
|
|
| 53 |
|
|
| 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 |
|
|
| 74 |
|
@param |
| 75 |
|
@return |
| 76 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
12
|
boolean checkUrl(String longUrl) {... |
| 78 |
12
|
return ! (longUrl.contains("://localhost:") || longUrl.contains("://localhost/")); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@param |
| 85 |
|
@param |
| 86 |
|
@param |
| 87 |
|
@return |
| 88 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
| 89 |
6
|
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 |
|
|
| 103 |
|
@param |
| 104 |
|
@return |
| 105 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 106 |
0
|
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 |
|
|
| 114 |
|
@param |
| 115 |
|
@return |
| 116 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 117 |
0
|
private String getTwitterPassword(BlogEntry blogEntry) {... |
| 118 |
0
|
return getProperty(blogEntry, "password"); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
@param |
| 124 |
|
@return |
| 125 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 126 |
0
|
private String getTwitterUsername(BlogEntry blogEntry) {... |
| 127 |
0
|
return getProperty(blogEntry, "username"); |
| 128 |
|
} |
| 129 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0,27 |
|
| 130 |
0
|
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 |
|
|
| 150 |
|
@param |
| 151 |
|
@param |
| 152 |
|
@param |
| 153 |
|
@param |
| 154 |
|
@throws |
| 155 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 156 |
0
|
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 |
|
|
| 166 |
|
@param |
| 167 |
|
@return |
| 168 |
|
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 4 |
Complexity Density: 0,22 |
|
| 169 |
0
|
private String makeTinyURL(String url) {... |
| 170 |
|
|
| 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 |
|
} |