| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PostToTwitterBlogEntryListener |
|
| 3.111111111111111;3.111 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | package net.sourceforge.pebble.event.blogentry; | |
| 34 | ||
| 35 | import java.io.BufferedReader; | |
| 36 | import java.io.IOException; | |
| 37 | import java.io.InputStreamReader; | |
| 38 | import java.net.MalformedURLException; | |
| 39 | import java.net.URL; | |
| 40 | import java.net.URLConnection; | |
| 41 | ||
| 42 | import net.sourceforge.pebble.PluginProperties; | |
| 43 | import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent; | |
| 44 | import net.sourceforge.pebble.api.event.blogentry.BlogEntryListener; | |
| 45 | import net.sourceforge.pebble.domain.Blog; | |
| 46 | import net.sourceforge.pebble.domain.BlogEntry; | |
| 47 | ||
| 48 | import org.apache.commons.logging.Log; | |
| 49 | import org.apache.commons.logging.LogFactory; | |
| 50 | ||
| 51 | import twitter4j.Twitter; | |
| 52 | ||
| 53 | /** | |
| 54 | * Post new blog entries to twitter. | |
| 55 | * This class is based on a patch by Steve Carton (PEBBLE-15), but changes it to use the | |
| 56 | * pebble plugin mechanism by implementing the {@link BlogEntryListener} interface | |
| 57 | * | |
| 58 | * @author Steve Carton, Olaf Kock | |
| 59 | */ | |
| 60 | 24 | public class PostToTwitterBlogEntryListener extends BlogEntryListenerSupport { |
| 61 | ||
| 62 | /** the log used by this class */ | |
| 63 | 4 | private static final Log log = LogFactory |
| 64 | .getLog(PostToTwitterBlogEntryListener.class); | |
| 65 | private static final String DEFAULT_TWEET_URL="https://twitter.com/"; | |
| 66 | ||
| 67 | /** | |
| 68 | * Called when a blog entry has been published. | |
| 69 | * | |
| 70 | * @param event | |
| 71 | * a BlogEntryEvent instance | |
| 72 | */ | |
| 73 | public void blogEntryPublished(BlogEntryEvent event) { | |
| 74 | 0 | BlogEntry blogEntry = event.getBlogEntry(); |
| 75 | 0 | String twitterUsername = getTwitterUsername(blogEntry); |
| 76 | 0 | String twitterPassword = getTwitterPassword(blogEntry); |
| 77 | 0 | String twitterUrl = getTwitterUrl(blogEntry); |
| 78 | 0 | if(twitterUsername == null || twitterPassword == null) { |
| 79 | 0 | blogEntry.getBlog().error("Please configure twitter credentials in order to post to twitter"); |
| 80 | 0 | return; |
| 81 | } | |
| 82 | 0 | String longUrl = blogEntry.getLocalPermalink(); |
| 83 | // if(!checkUrl(longUrl)) { | |
| 84 | // blogEntry.getBlog().error("cowardly refusing to post url '" + longUrl + "' to twitter"); | |
| 85 | // return; | |
| 86 | // } | |
| 87 | 0 | String tinyUrl = makeTinyURL(longUrl); |
| 88 | 0 | if (tinyUrl.equalsIgnoreCase("error")) |
| 89 | 0 | tinyUrl = longUrl; |
| 90 | 0 | String msg = composeMessage(blogEntry.getTitle(), longUrl, tinyUrl); |
| 91 | try { | |
| 92 | 0 | if(getProperty(blogEntry, "simulate") != null) { |
| 93 | 0 | blogEntry.getBlog().info("Found property 'twitter.simulate' - This would have been posted to twitter with username '" + twitterUsername + "':\n" + msg); |
| 94 | } else { | |
| 95 | 0 | post(twitterUrl, twitterUsername, twitterPassword, msg); |
| 96 | } | |
| 97 | 0 | } catch (Exception e) { |
| 98 | 0 | e.printStackTrace(); |
| 99 | 0 | } |
| 100 | 0 | log.debug("Blog entry <a href=\"" + longUrl |
| 101 | + "\">" + blogEntry.getTitle() + "</a> tweeted."); | |
| 102 | 0 | } |
| 103 | ||
| 104 | /** | |
| 105 | * make sure that the url we are about to post is one that makes sense to post - e.g. don't post 'localhost' urls. | |
| 106 | * @param longUrl | |
| 107 | * @return true if the url passes the (simple) tests and may be posted | |
| 108 | */ | |
| 109 | boolean checkUrl(String longUrl) { | |
| 110 | 24 | return ! (longUrl.contains("://localhost:") || longUrl.contains("://localhost/")); |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * combine message with url. This will use longUrl when enough characters are left, tinyUrl otherwise. | |
| 115 | * If necessary, the title will be shortened in order to include the full URL | |
| 116 | * @param title | |
| 117 | * @param longUrl | |
| 118 | * @param tinyUrl | |
| 119 | * @return the "up to 140 character" message to post to twitter | |
| 120 | */ | |
| 121 | String composeMessage(String title, String longUrl, String tinyUrl) { | |
| 122 | 12 | if(longUrl.length() + title.length() > 139 ) { |
| 123 | 8 | if(tinyUrl.length() + title.length() > 139) { |
| 124 | 4 | return title.substring(0, 139-tinyUrl.length()) + " " + tinyUrl; |
| 125 | } else { | |
| 126 | 4 | return title + " " + tinyUrl; |
| 127 | } | |
| 128 | } else { | |
| 129 | 4 | return title + " " + longUrl ; |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * get twitter URL to post to. This can be overridden with the blog property twitter.url - e.g. for testing purposes. | |
| 135 | * @param blogEntry | |
| 136 | * @return | |
| 137 | */ | |
| 138 | private String getTwitterUrl(BlogEntry blogEntry) { | |
| 139 | 0 | String twitterUrl = getProperty(blogEntry, "url"); |
| 140 | 0 | if(twitterUrl == null) twitterUrl = DEFAULT_TWEET_URL; |
| 141 | 0 | return twitterUrl; |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * the password to post to twitter as configured in the blog properties | |
| 146 | * @param blogEntry | |
| 147 | * @return | |
| 148 | */ | |
| 149 | private String getTwitterPassword(BlogEntry blogEntry) { | |
| 150 | 0 | return getProperty(blogEntry, "password"); |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * the username to post to twitter as configured in the blog properties | |
| 155 | * @param blogEntry | |
| 156 | * @return | |
| 157 | */ | |
| 158 | private String getTwitterUsername(BlogEntry blogEntry) { | |
| 159 | 0 | return getProperty(blogEntry, "username"); |
| 160 | } | |
| 161 | ||
| 162 | private String getProperty(BlogEntry blogEntry, String property) { | |
| 163 | 0 | Blog blog = blogEntry.getBlog(); |
| 164 | 0 | String blogName = blog.getName(); |
| 165 | 0 | PluginProperties pluginProperties = blog.getPluginProperties(); |
| 166 | 0 | String result = pluginProperties.getProperty("twitter." + blogName + "." + property); |
| 167 | 0 | if(result == null) { |
| 168 | 0 | result = pluginProperties.getProperty("twitter." + property); |
| 169 | 0 | if(result == null) { |
| 170 | 0 | log.error("Twitter credentials (" + property + ") not found. Please configure twitter." + property + " in order to post to twitter"); |
| 171 | } else { | |
| 172 | 0 | log.debug("found twitter credentials in twitter." + property ); |
| 173 | } | |
| 174 | } else { | |
| 175 | 0 | log.debug("found twitter credentials in twitter." + blogName + "." + property); |
| 176 | } | |
| 177 | 0 | return result; |
| 178 | } | |
| 179 | ||
| 180 | /** | |
| 181 | * Post the given message to twitter, using the given postUrl and credentials. | |
| 182 | * @param twitterUrl URL to post to | |
| 183 | * @param twitterUsername username to post as | |
| 184 | * @param twitterPassword password to authenticate username | |
| 185 | * @param msg the message to post to twitter. | |
| 186 | * @throws Exception | |
| 187 | */ | |
| 188 | private void post(String twitterUrl, String twitterUsername, | |
| 189 | String twitterPassword, String msg) throws Exception { | |
| 190 | 0 | System.out.println("Posting to Twitter: " + msg); |
| 191 | 0 | Twitter twitter = new Twitter(twitterUsername, twitterPassword, twitterUrl); |
| 192 | 0 | twitter.updateStatus(msg); |
| 193 | 0 | } |
| 194 | ||
| 195 | ||
| 196 | /** | |
| 197 | * create a shortened version of the given url | |
| 198 | * @param url | |
| 199 | * @return | |
| 200 | */ | |
| 201 | private String makeTinyURL(String url) { | |
| 202 | // http://tinyurl.com/api-create.php?... | |
| 203 | 0 | StringBuffer response = new StringBuffer(); |
| 204 | try { | |
| 205 | 0 | URL turl = new URL("http://tinyurl.com/api-create.php?"+url); |
| 206 | 0 | URLConnection connection = turl.openConnection(); |
| 207 | 0 | connection.setDoInput(true); |
| 208 | 0 | connection.setDoOutput(false); |
| 209 | 0 | connection.setUseCaches(false); |
| 210 | 0 | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| 211 | String r; | |
| 212 | 0 | while ((r = in.readLine()) != null) { |
| 213 | 0 | response.append(r); |
| 214 | } | |
| 215 | 0 | in.close(); |
| 216 | } | |
| 217 | 0 | catch (MalformedURLException e) { |
| 218 | 0 | log.error(e.getMessage()); |
| 219 | 0 | return url; |
| 220 | } | |
| 221 | 0 | catch (IOException e) { |
| 222 | 0 | log.error(e.getMessage()); |
| 223 | 0 | return url; |
| 224 | 0 | } |
| 225 | 0 | log.debug("tinyurl for " + url + " is " + response.toString()); |
| 226 | 0 | return response.toString(); |
| 227 | } | |
| 228 | } |