Lost Password?

  #1 (permalink)  
Old 02-19-2008, 07:12 PM
bcoe bcoe is offline
Newbie
 
Join Date: Jan 2008
Posts: 4
Rep Power: 0
bcoe is on a distinguished road
Cool XML-RPC Java and You

Originally posted on my blog.
----------------------------------

What is XML-RPC? One answer to this question is that it is the way of the future… A less rhetorical/frustrating answer would be that it is a technology that leverages HTTP and XML to remotely, and platform independently, execute external services. With the interplay that takes place between web-services in the Web 2.0 environment, technologies such as XML-RPC facilitate the sharing of both information and functionality between various sources.

Examples of applications of XML-RPC include pinging third party sites such as Technorati regarding blog updates — as was done when I posted this new blog entry — and publishing remotely to your blog (this option is offered by sites like digg). Countless other quasi standards exist taking advantage of this technology.

I myself am a Java™ programmer (well, I call myself a Ninja), and in this tutorial I present two separate fully developed scenarios: creating a fully functional XML-RPC client, and creating an XML-RPC server application. Both these examples use the Apache XML-RPC libraries.
An RPC Pinging Client

Rather than present a client and its corresponding server, as is often done in this sort of tutorial, I thought I would present two separate and useful applications. In this example I demonstrate how you could create your own tool for pinging various blog search engines regarding a website update — presumably on your nifty Java™ powered website.

Without further adieu, Ping.java:


Java Code:
  1. import org.apache.xmlrpc.client.XmlRpcClient;
  2. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  3. import java.net.URL;
  4. import java.util.*;
  5.  
  6.  
  7. public class Ping{
  8.   public static final String PingMe[] =
  9.     new String[]{"http://rpc.pingomatic.com/"};
  10.  
  11.  
  12.   //Connects to an RPC pinging service.
  13.   public Ping(String Title,String Url,String UrlChanges,String UrlRSS){
  14.     for(int i=0;i<PingMe.length;i++){
  15.       try{
  16.         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  17.     config.setServerURL(new URL(PingMe[i]));
  18.     XmlRpcClient client = new XmlRpcClient();
  19.     client.setConfig(config);
  20.         Object[] params = new Object[]{Title,Url,UrlChanges,UrlRSS};
  21.         HashMap result =
  22.         (HashMap) client.execute("weblogUpdates.extendedPing", params);
  23.     //Output the response from the server.
  24.     System.out.println(result);
  25.       }catch(Exception e){
  26.         e.printStackTrace();
  27.       }
  28.     }
  29.   }
  30.  
  31.  
  32.   //Testing main.
  33.   public static void main(String args[]){
  34.     Ping MyPing=new Ping("PLink News",
  35.                          "http://www.plink-search.com/blog/news/",
  36.                          "http://www.plink-search.com/blog/news/",
  37.                          "http://www.plink-search.com/plink.xml?fetch=news");
  38.   }
  39. }

This simple little application will ping whatever sites are included in the PingMe array. In this case it just pings Ping-o-Matic! — a site that, in-turn, pings several other sites.

The actual XML-RPC is run with a call to client.execute. As can be seen, I am executing the weblogUpdates.extendedPing procedure. The array of objects I pass in gets translated into XML-RPC types, and is transported over that magic series of tubes that is the Internet.
A MetaWeblog API Server Application

Having seen a useful client in action, what exactly is happening on the server side of things? The server entry points for your Java™-based XML-RPC take the form of a the most basic of Java™ classes — no fancy libraries need even be imported. All the work is in naming conventions, reading information pertaining to the specific specification you wish to implement, and making sure you understand the XML-RPC types. It is also assumed that you have a working Tomcat environment, or equivalent.

For PLink I decided to implement portions of the MetaWeblog API. This API is designed for allowing an RPC request to publish data to your blog — I was interested in adding digg’s Blog It functionality to PLink. Let’s begin by looking at the MetaWeblog RFC. Take note of the three main entry points:

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

metaWeblog.editPost (postid, username, password, struct, publish) returns true

metaWeblog.getPost (postid, username, password) returns struct

Unfortunately, in the semi-standardized environment that is the Internet, I found it necessary to implement an additional function to allow for digg support:

getUsersBlogs(String appkey,String user,String pass) returns struct[]

Note: This procedure call is actually drawn from the Blogger API.

Having examined these function calls let’s look at the corresponding Java™ class, here’s metaweblog.java:

Java Code:
  1. import java.io.*;
  2. import java.util.*;
  3. public class metaweblog{
  4.  
  5.  
  6.     public String newPost(String blogid,String user,String pass,
  7.                               Map struct,Boolean publish) throws Exception{
  8.         //Parse values from struct corresponding to RSS specification.
  9.         String title=(String)struct.get("title");
  10.         String description=(String)struct.get("description");
  11.         String postid="";
  12.         //Database access and the like.
  13.         return(postid);
  14.     }
  15.  
  16.  
  17.     public Boolean editPost(String blogid,String username,String password,
  18.                                 Map struct,Boolean publish) throws Exception{
  19.         //Parse values from struct corresponding to RSS specification.
  20.         String title=(String)struct.get("title");
  21.         String description=(String)struct.get("description");
  22.         //Database access and the like.
  23.         return(new Boolean(true));
  24.     }
  25.  
  26.  
  27.     public Map getPost(String postid,String username,String password) throws Exception{
  28.         HashMap returnMe=new HashMap();
  29.         //Database access and the like.
  30.         return(returnMe);
  31.     }
  32.  
  33.  
  34.     public Object[] getUsersBlogs(String appkey,String user,String pass) throws Exception{
  35.         String url="http://www.plink-search.com/";
  36.         String blogName="My Crazy Blog!";
  37.         //Return information pertaining to blogs that a given user owns on your server.
  38.         Map returnMe[]=new Map[1];
  39.         returnMe[0]=new HashMap();
  40.         returnMe[0].put("userID",user);
  41.         returnMe[0].put("url",url);
  42.         returnMe[0].put("blogid",user);
  43.         returnMe[0].put("blogName",blogName);
  44.         return(returnMe);
  45.     }
  46. }

Finally, to connect this code to incoming XML-RPC calls we can use the default XmlRpcServlet — assuming that we have installed ws-xmlrpc on our server — by adding the following to our web.xml file:


XML Code:
  1. <servlet>
  2.         <servlet-name>XmlRpcServlet</servlet-name>
  3.         <servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
  4.         <init-param>
  5.           <param-name>enabledForExtensions</param-name>
  6.           <param-value>true</param-value>
  7.           <description>
  8.             Sets, whether the servlet supports vendor extensions for XML-RPC.
  9.           </description>
  10.         </init-param>
  11.     </servlet>
  12.     <servlet-mapping>
  13.         <servlet-name>XmlRpcServlet</servlet-name>
  14.         <url-pattern>/xmlrpc</url-pattern>
  15.     </servlet-mapping>

The final step is to add a file to org/apache/xmlrpc/webserver/ named XmlRpcServlet.properties this file tells your XmlRpcServlet what remote applications can be executed. Here is the entry corresponding to the MetaWeblog example:

metaWeblog=metaweblog

The value before the equals indicates the request . The value after the equals maps this incoming request to a specific class — in our case the compiled version of metaweblog.java.

There you have it: Individuals, once all those stubs are filled out, could now publish remotely to your fancy blogging software using XML-RPC. This would take on the form of a call similar to,


Java Code:
  1. HashMap Post=new HashMap();
  2.   Post.put("description","My Story");
  3.   Post.put("title","My Title");
  4.   Object[] params = new Object[]{"Blog ID","user","pass",Post,new Boolean(true)};
  5.   HashMap result = (HashMap) client.execute("metaWeblog.newPost", params);

XML-RPC is a cool technology, and I hope this has provided other Java™ programmers like myself with a good primer.

-Ben (Developer Hack Wars - The Game of Virtual Hacking)

Last edited by Jordan; 02-20-2008 at 07:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 03:53 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads