xml-rpc the quickest way to web services. the moves institute xml-rpc xml-rpc is an attempt to...
TRANSCRIPT
XML-RPC
The Quickest Way to Web Services
THE MOVES INSTITUTE
XML-RPC
XML-RPC is an attempt to implement conventional Remote Procedure Call (RPC) concepts using XML to transmit the RPC information
Traffic on wire in XML format
THE MOVES INSTITUTE
XML-RPC
You are specifying that a particular object method be called on a remote host with the arguments passed in, and a given return type.
There may be several types of method calls: blocking, non-blocking, fire-and-forget, etc.
THE MOVES INSTITUTE
Implementations
There are several implementations out there (Userland, Sun, Apache).
Some implementations have extensions that make your life easier, but are non-standard.
http://ws.apache.org/xmlrpc/ for the apache implementation; I used 3.x
You’ll also need commons codec jar file in the classpath if building from scratch
See https://movesinstitute.org/~mcgredo/WebServices/rpcExample.tar.gz for a pre-packaged example
THE MOVES INSTITUTE
Example
POST /RPC2 HTTP/1.0 User-Agent: Frontier/5.1.2 (WinNT) Host: betty.userland.com Content-Type: text/xml Content-length: 181
<?xml version="1.0"?> <methodCall> <methodName>Calculator.add</methodName> <params> <param> <value><i4>41</i4></value> </param> <param> <value><i4>17</i4></value> </param> </params> </methodCall>
THE MOVES INSTITUTE
Apache XML-RPC
On the client side the code is relatively straightforward:
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); XmlRpcClient client = new XmlRpcClient(); Object[] params = new Object[]{new Integer(1), new Integer(2)};System.out.println("preparing execute"); Integer result = (Integer) client.execute("Calculator.add", params);
THE MOVES INSTITUTE
Server Side
The server side is trickier. There should be java object receiving messages, but how to do this?
The apache implementation uses a properties file and a default implementation of a class called XmlRpcServlet to do this
XmlRpcServlet handles decoding the XML and encoding the response back
THE MOVES INSTITUTE
XmlRpcServlet
XmlRpcServlet
XML
Java Object
Some Method
ResponseXML Response
XMLRpcServlet is responsible for handling the conversion from and to XML, and calling the Java object that actuallyimplements the functionality
THE MOVES INSTITUTE
XmlRpcServlet
XmlRpcServlet accepts the HTTP, extracts the XML, and converts the XML into what is necessary to call the method on the local object. It accepts the return value from the object, converts it into XML-RPC, and places it into an HTTP response
You don’t have to write any of this code
THE MOVES INSTITUTE
XmlRpcServlet
To specify the object, you need to modify the xmlrpc-server-3.0.jar file.
• Extract the contents of the jar file in a temporary directory: jar -xvf xmlrpc-server-3.0.jar
• Add a file called XmlRpcServlet.properties in org/apache/xmlrpc/webserver
Contents are “objectName=fullyQualifiedClassName”• Repackage the jar file: “jar -cvf xmlrpc-server-3.0.jar
org”• Replace the old jar file with the new one
THE MOVES INSTITUTE
Set up servlet
Deploying the servlet is a little tricky. The environment in which a servlet program lives is standardized
AppName WEB-INF web.xml (config file) classes (your classes) lib (supporting jar files)
THE MOVES INSTITUTE
Web.xml
Contained inside of WEB-INF, describes the servlets, security, etc.
The example web.xml file works. It includes a subclass of XmlRpcServlet that adds logging capability
The ugly config stuff is done for you if you just use the example distribution
THE MOVES INSTITUTE
Example
The example distribution includes an Ant file that • Generates a xmlRpcExample.war file in the dist
directory• Runs a client that connects to the servletTo deploy the servlet, remove the old copy from
the Tomcat webapps directory , copy dist/xmlRpcExmaple.war it to the Tomcat /webapps directory, and restart Tomcat
The war file will be automatically unpacked and the servlet started
THE MOVES INSTITUTE
Data Types
You can pass Integer, Boolean, String, Double, Date, byte[], Map and Arrays of these
With enabledForExtensions switched on, you can pas Byte, Float, Long, XML DOM Node, Short, Serializable, BigDecimal, BigInteger, and Calendar objects
THE MOVES INSTITUTE
Modifying
To modify the example to do what you want:• Unpack the xmlrpc-server-3.0.jar file• Modify XmlRpcServlet.properties file with object name
on the LHS and fully qualified class name on the RHS• Repackage the jar file and replace the old jar file• Implement the class with a no-args constructor and
public methods that take only legal data types• ant dist, copy war file to tomcat webapps directory,
stop and restart tomcat