File Source: posttotwitterblogentrylistener.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.event.blogentry.PostToTwitterBlogEntryListener__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     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  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.event.blogentry.PostToTwitterBlogEntryListener()
          */
    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  	 */
        	 /* 
    P/P 	  *  Method: void blogEntryPublished(BlogEntryEvent)
        	  * 
        	  *  Preconditions:
        	  *    event != null
        	  *    (soft) net/sourceforge/pebble/domain/AbstractBlog.log != null
        	  *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
        	  *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
        	  * 
        	  *  Presumptions:
        	  *    blogEntry.blog != null
        	  *    blogEntry.blog.permalinkProvider != null
        	  *    blogEntry.blog.pluginProperties != null
        	  *    blogEntry.blog.pluginProperties@42 != null
        	  *    blogEntry.blog.pluginProperties@50 != null
        	  *    ...
        	  * 
        	  *  Test Vectors:
        	  *    java.lang.String:equalsIgnoreCase(...)@56: {0}, {1}
        	  */
    41  	public void blogEntryPublished(BlogEntryEvent event) {
    42  		BlogEntry blogEntry = event.getBlogEntry();
    43  		String twitterUsername = getTwitterUsername(blogEntry);
    44  		String twitterPassword = getTwitterPassword(blogEntry);
    45  		String twitterUrl = getTwitterUrl(blogEntry);
    46  		if(twitterUsername == null || twitterPassword == null) {
    47  			blogEntry.getBlog().error("Please configure twitter credentials in order to post to twitter");
    48  			return;
    49  		}
    50  		String longUrl = blogEntry.getLocalPermalink();
    51  //		if(!checkUrl(longUrl)) {
    52  //			blogEntry.getBlog().error("cowardly refusing to post url '" + longUrl + "' to twitter");
    53  //			return;
    54  //		}
    55  		String tinyUrl = makeTinyURL(longUrl);
+   56  		if (tinyUrl.equalsIgnoreCase("error"))
    57  			tinyUrl = longUrl;
+   58  		String msg = composeMessage(blogEntry.getTitle(), longUrl, tinyUrl);
    59  		try {
    60  			if(getProperty(blogEntry, "simulate") != null) {
    61  				blogEntry.getBlog().info("Found property 'twitter.simulate' - This would have been posted to twitter with username '" + twitterUsername + "':\n" + msg);
    62  			} else {
    63  				post(twitterUrl, twitterUsername, twitterPassword, msg);
    64  			}
    65  		} catch (Exception e) {
    66  			e.printStackTrace();
    67  		}
    68  		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  	 */
        	 /* 
    P/P 	  *  Method: bool checkUrl(String)
        	  * 
        	  *  Preconditions:
        	  *    longUrl != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  */
    77  	boolean checkUrl(String longUrl) {
    78  		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  	 */
        	 /* 
    P/P 	  *  Method: String composeMessage(String, String, String)
        	  * 
        	  *  Preconditions:
        	  *    longUrl != null
        	  *    title != null
        	  *    (soft) tinyUrl != null
        	  * 
        	  *  Presumptions:
        	  *    java.lang.String:length(...)@92 <= 2_147_483_787
        	  * 
        	  *  Postconditions:
        	  *    return_value != null
        	  */
    89  	String composeMessage(String title, String longUrl, String tinyUrl) {
    90  		if(longUrl.length() + title.length() > 139 ) {
    91  			if(tinyUrl.length() + title.length() > 139) {
    92  				return title.substring(0, 139-tinyUrl.length()) + " " + tinyUrl;
    93  			} else {
    94  				return title + " " + tinyUrl;
    95  			}
    96  		} else {
    97  			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  	 */
        	 /* 
    P/P 	  *  Method: String getTwitterUrl(BlogEntry)
        	  * 
        	  *  Preconditions:
        	  *    blogEntry != null
        	  *    blogEntry.blog != null
        	  *    blogEntry.blog.pluginProperties != null
        	  *    blogEntry.blog.properties != null
        	  * 
        	  *  Postconditions:
        	  *    return_value != null
        	  */
   106  	private String getTwitterUrl(BlogEntry blogEntry) {
   107  		String twitterUrl = getProperty(blogEntry, "url");
   108  		if(twitterUrl == null) twitterUrl = DEFAULT_TWEET_URL;
   109  		return twitterUrl;
   110  	}
   111  
   112  	/**
   113  	 * the password to post to twitter as configured in the blog properties
   114  	 * @param blogEntry
   115  	 * @return
   116  	 */
        	 /* 
    P/P 	  *  Method: String getTwitterPassword(BlogEntry)
        	  * 
        	  *  Preconditions:
        	  *    blogEntry != null
        	  *    blogEntry.blog != null
        	  *    blogEntry.blog.pluginProperties != null
        	  *    blogEntry.blog.properties != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  */
   117  	private String getTwitterPassword(BlogEntry blogEntry) {
   118  		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  	 */
        	 /* 
    P/P 	  *  Method: String getTwitterUsername(BlogEntry)
        	  * 
        	  *  Preconditions:
        	  *    blogEntry != null
        	  *    blogEntry.blog != null
        	  *    blogEntry.blog.pluginProperties != null
        	  *    blogEntry.blog.properties != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  */
   126  	private String getTwitterUsername(BlogEntry blogEntry) {
   127  		return getProperty(blogEntry, "username");
   128  	}
   129  	
        	 /* 
    P/P 	  *  Method: String getProperty(BlogEntry, String)
        	  * 
        	  *  Preconditions:
        	  *    blogEntry != null
        	  *    blogEntry.blog != null
        	  *    blogEntry.blog.pluginProperties != null
        	  *    blogEntry.blog.properties != null
        	  * 
        	  *  Presumptions:
        	  *    org.apache.commons.logging.LogFactory:getLog(...)@32 != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  * 
        	  *  Test Vectors:
        	  *    net.sourceforge.pebble.PluginProperties:getProperty(...)@134: Inverse{null}, Addr_Set{null}
        	  *    net.sourceforge.pebble.PluginProperties:getProperty(...)@136: Inverse{null}, Addr_Set{null}
        	  */
   130  	private String getProperty(BlogEntry blogEntry, String property) {
   131  		Blog blog = blogEntry.getBlog();
   132  		String blogName = blog.getName();
   133  		PluginProperties pluginProperties = blog.getPluginProperties();
   134  		String result = pluginProperties.getProperty("twitter." + blogName + "." + property);
   135  		if(result == null) {
   136  			result = pluginProperties.getProperty("twitter." + property);
   137  			if(result == null) {
   138  				log.error("Twitter credentials (" + property + ") not found. Please configure twitter." + property + " in order to post to twitter");
   139  			} else {
   140  				log.debug("found twitter credentials in twitter." + property );
   141  			}
   142  		} else {
   143  			log.debug("found twitter credentials in twitter." + blogName + "." + property);
   144  		}
   145  		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  	private void post(String twitterUrl, String twitterUsername,
   157  			String twitterPassword, String msg) throws Exception {
   158  		System.out.println("Posting to Twitter: " + msg);
        		 /* 
    P/P 		  *  Method: void post(String, String, String, String)
        		  * 
        		  *  Presumptions:
        		  *    java.lang.System.out != null
        		  */
   159  		Twitter twitter = new Twitter(twitterUsername, twitterPassword);
   160  		twitter.updateStatus(msg);
   161  	}
   162  
   163  
   164  	/**
   165  	 * create a shortened version of the given url
   166  	 * @param url
   167  	 * @return
   168  	 */
        	 /* 
    P/P 	  *  Method: String makeTinyURL(String)
        	  * 
        	  *  Presumptions:
        	  *    java.net.URL:openConnection(...)@174 != null
        	  *    org.apache.commons.logging.LogFactory:getLog(...)@32 != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  * 
        	  *  Test Vectors:
        	  *    java.io.BufferedReader:readLine(...)@180: Inverse{null}, Addr_Set{null}
        	  */
   169  	private String makeTinyURL(String url) {
   170  		// http://tinyurl.com/api-create.php?...
   171  		StringBuffer response = new StringBuffer();
   172  		try {
   173  			URL turl = new URL("http://tinyurl.com/api-create.php?"+url);
   174  			URLConnection connection = turl.openConnection();
   175  			connection.setDoInput(true);
   176  			connection.setDoOutput(false);
   177  			connection.setUseCaches(false);
   178  			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   179  			String r;
   180  			while ((r = in.readLine()) != null) {
   181  				response.append(r);
   182  			}
   183  			in.close();
   184  		}
   185  		catch (MalformedURLException e) {
   186  			log.error(e.getMessage());
   187  			return url;
   188  		}
   189  		catch (IOException e) {
   190  			log.error(e.getMessage());
   191  			return url;
   192  		}
   193  		log.debug("tinyurl for " + url + " is " + response.toString());
   194  		return response.toString();
   195  	}
   196  }








SofCheck Inspector Build Version : 2.22510
posttotwitterblogentrylistener.java 2010-Jul-19 19:58:36
posttotwitterblogentrylistener.class 2010-Jul-19 20:23:40