Attempting to "really" understand the internals of XML-RPC, Blogging APIs and Erik’s Scratchpad..;-)
Posted : May 20, 2004 at 5:22 pm [America/Los_Angeles]

I started blogging the easy way - installed Movable Type, tinkered with it for a few hours and I had my blog up. And while I did customize the default MT template, played with and used a few MT plugins here and there, converted to using PHP (and it’s include feature) as opposed to plain HTML as my default blog pages and continue to use rich blogging client like w.bloggar to do blog posting (thanks to XML-RPC and support of Blogger API in MT), somehow I never really took a minute to understand some of the internals of the whole process.
In particular, this whole talk of MT supporting Blogger API, metaWeblog API (planning to support Atom API) used to literally make my head spin. In fact, I even remember reading about these APIs a few months back and muttering to myself (ever so silently) “What are these APIs and what in god’s name is making this author so excited about it”.
Well, thanks to my new project and Erik’s “How do I do it? post, I think I am getting to know more about Blogger/Movable Type API and XML-RPC API than I ever would have cared to know
Ok, this past week, it finally dawned on me that I could use the Net::Blogger API in Perl to do posts using my own CGI interface. Why would I want to do that? Well, that’s something I plan to talk about more after my project goes live. Basically, Net::Blogger API makes HTTP calls, talks using XML-RPC protocol with it’s XML-RPC server-side counterpart, which in my case is my blog engine (basically MT) and invokes Blogger API methods (which MT supports). Plain and simple.
However, Erik’s post confused me a little bit. Especially, the following lines:
..I figured I might be able to post directly to my blog, and thus forgo my trusted text editor.
..I grabbed a copy of Apache’s XML-RPC to handle the remote procedure calls thru a Servlet. And used the jTalk SqlQueryBean and Utility classes to handle all of the database work.
..Less than 15 minutes later, I had successfully made my first blog entry via the NewzCrawler Blog Client.
From these lines, it seemed like writing an XML-RPC endpoint which was also Blogger API-compatible was really not that big a deal. I don’t know why, but I was making this thing out to be a really big deal. Something which would probably need a Blogging engine to provide for me 
I dumped all my project work, and decided to go figure out what these APIs/Protocol stacks were and how they really worked their magic. After some research, I wrote my first Java based standalone application (using Apache’s XML-RPC) which successfully interacted with my Movable Type’s XML-RPC end-point (/mt/mt-xmlrpc.cgi, by default). While it felt good, I had already done this with Perl, so it really was no big deal. That’s when it struck me:
What if I created a servlet (my very own HelloXMLRPC servlet), make it an XML-RPC Server end point, add a handler to the servlet which stubs a few of the Blogger API methods (like blogger.newPost) and see if I could make an XML-RPC client talk to it accurately.
If I could, I would have just implemented my very own Blogger API compliant XML-RPC end point which could then be invoked from rich clients like NewzCrawler’s Blog This! or w.bloggar.
Needless to say, I had a few problems. I had never written an XML-RPC Server, let alone a servlet which implemented that. Also, I had no idea how I would simulate a XML-RPC client call. Well, the second problem was solved, thanks to Andre Torrez’s XMLPad. Thank you Andre! Great job.
Ok, how do I implement the XML-RPC end point? After almost an hour of digging around, I ended up with two very simple pieces of Java code - a very simple servlet (HelloXMLRPC) and a handler code (MyBlogger):
/*
* HelloXMLRPC.java
*
*/
package com.indrayam.xmlrpc;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.xmlrpc.XmlRpcServer;
/**
* Class description goes here.
*
* @author $Author$
* @version $Revision$ $Date$
*/
public class HelloXMLRPC extends HttpServlet {
public static XmlRpcServer xmlrpc = new XmlRpcServer();
public void init(ServletConfig config) throws ServletException {
xmlrpc = new XmlRpcServer();
xmlrpc.addHandler(“blogger”, new com.indrayam.xmlrpc.MyBlogger());
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
byte[] result = xmlrpc.execute(req.getInputStream(), null, null);
res.setContentType(“text/xml”);
res.setContentLength(result.length);
OutputStream output = res.getOutputStream();
output.write(result);
output.flush();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req,res);
}
}
/*
* MyBlogger.java
*
*/
package com.indrayam.xmlrpc;
/**
* Class description goes here.
*
* @author $Author$
* @version $Revision$ $Date$
*/
public class MyBlogger {
public String newPost(String appkey, String blogid, String username,
String password, String content, boolean publish) {
System.out.println(“appkey = “ + appkey);
System.out.println(“blogid = “ + blogid);
System.out.println(“username = “ + username);
System.out.println(“password = “ + password);
System.out.println(“content = “ + content);
System.out.println(“publish = “ + publish );
return “12345″;
}
}
Also, added Apache’s XML-RPC API in <webapp-root>/WEB-INF/lib, and had the following web.xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>xmlrpc</servlet-name> <servlet-class>com.indrayam.xmlrpc.HelloXMLRPC</servlet-class> </servlet> <servlet-mapping> <servlet-name>xmlrpc</servlet-name> <url-pattern>/blogger</url-pattern> </servlet-mapping> </web-app>
That’s it. Deployed the application, fired up Tomcat and pointed XMLPad.exe to the local XML-RPC end point. The results are below for you to see:



I obviously cannot say for sure if Erik’s “…I grabbed a copy of Apache’s XML-RPC to handle the remote procedure calls thru a Servlet…” is basically a more elaborate version of something like this or it’s just some blogging engine acting as his “ScratchPad” area.
In any case, it sure was fun trying to create your very own servlet acting as an XML-RPC HTTP end point which rich clients like NewzCrawler could directly “post” to. And come to think of it, it really was not that bad 
- Anand
Category: Application Development
5 Comments
Hey Anand - Great blog entry and thanks for the tip on XMLPad. I wish I had that utiltiy a few weeks ago
–Vinnyhttp://www.j2eegeek.com/blog/
Posted by: Vinny Carpenter at May 20, 2004 @ 8:05 pm
Thanks Vinny. Yes, I must say XMLPad saved the day for me today. Needless to say, it’s now happily perched on my “Toolapalooza” (read, tools..;-)) list.
Posted by: Anand Sharma at May 20, 2004 @ 8:59 pm
I pretty much used the same technique.
I also implemented the MetaWeblog API for my linkblog, in order to easily deal with categories.
E.
The Truth Is Out There!
Posted by: Erik C. Thauvin at May 21, 2004 @ 3:04 am
Thanks for the validation and the tip about MetaWeblog API, Erik..:-)
Posted by: Anand Sharma at May 21, 2004 @ 11:08 am
hi Anand, is this any way of doing this without using a servlet,
what I mean is, can we make a request to some xmlrpc service(not the servlet), which will then call the appropriate xmlrpc method, with provided arguments.
Posted by: Rahul at April 21, 2005 @ 9:29 pm