|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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:
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:
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:
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:
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. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|