11.3 Generating a LiveJournal Post

 < Day Day Up > 

Example 11-5 shows how a connection can be made to the LiveJournal service. Notice that a MD5 hashed version of the journal password is passed to the system: it's not a significant security aid, but it's slightly better than nothing.

The LiveJournal connection emulates a named parameter method invocation, closer to the named-pair values you see in HTTP parameters. This is done by passing an XML-RPC struct. One advantage of this approach is that the arguments do not have to be passed in any specific order, which makes it easier to build and also easier to debug.

Example 11-5. LiveJournal connectivity
package com.cascadetg.ch11; import org.apache.xmlrpc.*; import java.util.Hashtable; import java.util.Vector; public class LiveJournalDriver implements WeblogDriverInterface {     public static void main(String[] args)     {         LiveJournalDriver myDriver = new LiveJournalDriver( );         myDriver.setPost("Test @ " + new java.util.Date( ).toString( ));         myDriver.setPost("Foo!");         System.out.println("LiveJournal test " + myDriver.post( ));     }     String post;     public void setPost(String post)     {         this.post = post;     }     public String getPost( )     {         return post;     }     String title = "";     public String getTitle( )     {         return title;     }     public void setTitle(String title)     {         this.title = title;     }     /** Post to LiveJournal using the LJ-specific XML-RPC interface. */     public boolean post( )     {         /*          * Note that we are using a utility class to send the password          * encrypted as an MD5 hash. This isn't much in the way of          * security - all it means is that if intercepted, the          * intercepter will get a hashed version of the password, not          * the original plain-text version.          */         String password = MD5Hash.hash(BlogTokens.livejournal_password);         Hashtable method_calls = new Hashtable( );         try         {             String lj_url =                 "http://www.livejournal.com/interface/xmlrpc";             XmlRpcClient xmlrpc = new XmlRpcClient(lj_url);             Vector params = new Vector( );             method_calls.put(                 "username",                 BlogTokens.livejournal_username);             method_calls.put(                 "password",                 BlogTokens.livejournal_password);             method_calls.put("ver", "1");             method_calls.put("clientversion", "WebServiceBook/0.0.1");             java.util.Date now = new java.util.Date( );             method_calls.put("event", post);             method_calls.put("lineendings", "\n");             method_calls.put("subject", title);             method_calls.put("year", new Integer(now.getYear( ) + 1900));             method_calls.put("mon", new Integer(now.getMonth( ) + 1));             method_calls.put("day", new Integer(now.getDate( )));             method_calls.put("hour", new Integer(now.getHours( )));             method_calls.put("min", new Integer(now.getMinutes( )));                          params.add(method_calls);             Object result =                 xmlrpc.execute("LJ.XMLRPC.postevent", params);         } catch (Exception e)         {             e.printStackTrace( );             return false;         }         return true;     } }

Figure 11-2 shows an example of a post made to the LiveJournal site. The template chosen for the LiveJournal account determines the design and format of the post.

Figure 11-2. LiveJournal generated post

Example 11-6 shows how to make a post to the Blogger. The only potential area of confusion is that the parameters must be provided in the order shown. The biggest weakness of this API is the lack of a mechanism to specify the title of the post.

The Blogger 1.0 API does not include any mechanism for posting entries to Blogger with a title. Blogger did some work on a Blogger 2.0 API, but this was dropped in favor of Atom. The Blogger 2.0 API did include a mechanism for sending a post with a title, but Blogger has officially discontinued efforts on the Blogger 2.0 effort. Blogger has left a preliminary Blogger 2.0 API implementation active as of this writing, but it's impossible to tell when that functionality will disappear.

Aside from the missing title parameter, the Blogger API in Example 11-6 is quite straightforward.

Example 11-6. Blogger connectivity
package com.cascadetg.ch11; import org.apache.xmlrpc.*; import java.util.Vector; public class BloggerDriver implements WeblogDriverInterface {     public static void main(String[] args)     {         BloggerDriver myDriver = new BloggerDriver( );         myDriver.setPost("Test @ " + new java.util.Date( ).toString( ));         System.out.println("Blogger test " + myDriver.post( ));     }     /**      * Note that the Blogger XML-RPC interface does not support setting      * the title of a post, so this is only provided to adhere to the      * expected behavior for this pattern as required by the interface.      */     private String title = "";     public String getTitle( )     {         return title;     }     public void setTitle(String title)     {         this.title = title;     }     String body;     public void setPost(String post)     {         this.body = post;     }     public String getPost( )     {         return body;     }     /**      * Makes the post to the Blogger interface using the Blogger 1.0      * XML-RPC interface.      */     public boolean post( )     {         String url = "http://plant.blogger.com/api/RPC2";         try         {             XmlRpcClient xmlrpc = new XmlRpcClient(url);             Vector params = new Vector( );             params.add(BlogTokens.blogger_app_key);             params.add(BlogTokens.blogger_blogid);             params.add(BlogTokens.blogger_username);             params.add(BlogTokens.blogger_password);             params.add(body);             params.add(Boolean.TRUE);             Object result = xmlrpc.execute("blogger.newPost", params);         } catch (Exception e)         {             e.printStackTrace( );             return false;         }         return true;     } }

To change the system to use the Blogger driver instead of LiveJournal, merely comment out the line for LiveJournal and uncomment the Blogger line, in your code from Example 11-1:

// This is where we choose which weblog we will be posting to. // myPoster.setWeblog(new LiveJournalDriver( )); myPoster.setWeblog(new BloggerDriver( ));

     < Day Day Up > 

    Real World Web Services
    Real World Web Services
    ISBN: 059600642X
    EAN: 2147483647
    Year: 2006
    Pages: 83
    Authors: Will Iverson

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net