Blakes 21 Days Chapter 20 Document


Day 20, XML Web Services

Introduction to XML-RPC

Communicating with XML-RPC

Sending a Request

Listing 20.1 An XML-RPC Request

The Explanation

Responding to a Request

Listing 20.2 An XML-RPC Response

The Explanation

Choosing an XML-RPC Implementation

Using an XML-RPC Web Service

Listing 20.3 - The Full Text of SiteClient.java


package com.java21days;

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.client.*;

public class SiteClient {
    public static void main(String arguments[]) {
        SiteClient client = new SiteClient();
        try {
            HashMap resp = client.getRandomSite();
            // Report the results
            if (resp.size() > 0) {
                System.out.println("URL: " + resp.get("url")
                    + "\nTitle: " + resp.get("title")
                    + "\nDescription: " + resp.get("description"));
            }
        } catch (IOException ioe) {
            System.out.println("Exception: " + ioe.getMessage());
        } catch (XmlRpcException xre) {
            System.out.println("Exception: " + xre.getMessage());
        }
    }

    public HashMap getRandomSite()
        throws IOException, XmlRpcException {

            // Create the client
            XmlRpcClientConfigImpl config = new
                XmlRpcClientConfigImpl();
            URL server = new URL("http://localhost:4413/");
            config.setServerURL(server);
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            // Create the parameters for the request
            ArrayList params = new ArrayList();
            // Send the request and get the response
            HashMap result = (HashMap) client.execute(
                "dmoz.getRandomSite", params);
            return result;
    }
}

The Explanation

Creating an XML-RPC Web Service

Listing 20.4 The Full Text of DmozServer.java


package com.java21days;

import java.io.*;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;

public class DmozServer {
    public static void main(String[] arguments) {
         try {
             startServer();
         } catch (IOException ioe) {
             System.out.println("Server error: " +
                 ioe.getMessage());
         } catch (XmlRpcException xre) {
             System.out.println("XML-RPC error: " +
                 xre.getMessage());
         }
    }

    public static void startServer() throws IOException,
        XmlRpcException {

        // Create the server
        System.out.println("Starting Dmoz server ...");
        WebServer server = new WebServer(4413);
        XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
        PropertyHandlerMapping phm = new PropertyHandlerMapping();

        // Register the handler
        phm.addHandler("dmoz", DmozHandlerImpl.class);
        xmlRpcServer.setHandlerMapping(phm);

        // Start the server
        server.start();
        System.out.println("Accepting requests ...");
    }
}

The Explanation

Listing 20.5 The Full Text of DmozHandler.java

Listing 20.6 The Full Text of DmozHandlerImpl.java


package com.java21days;

import java.sql.*;
import java.util.*;

public class DmozHandlerImpl implements  DmozHandler {

    public HashMap getRandomSite() {
        Connection conn = getMySqlConnection();
        HashMap response = new HashMap<>();
        try {
            Statement st = conn.createStatement();
            ResultSet rec = st.executeQuery(
                "SELECT * FROM cooldata ORDER BY RAND() LIMIT 1");
            if (rec.next()) {
                response.put("url", rec.getString("url"));
                response.put("title", rec.getString("title"));
                response.put("description",
                    rec.getString("description"));
            } else {
                response.put("error", "no database record found");
            }
            st.close();
            rec.close();
            conn.close();
        } catch (SQLException sqe) {
            response.put("error", sqe.getMessage());
        }
        return response;
    }

    private Connection getMySqlConnection() {
        Connection conn = null;
        String data = "jdbc:mysql://localhost/cool";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                data, "user", "d05eURCe");
        } catch (SQLException s) {
            System.out.println("SQL Error: " + s.toString() + " "
                + s.getErrorCode() + " " + s.getSQLState());
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                + e.getMessage());
        }
        return conn;
    }
}

The Explanantion

Figure 20.1 - Receiving Dmoz website data over XML-RPC. - goes here

576

Summary

Q & A

Quiz - Questions

  1. Which popular Internet protocol does XML-RPC not require ?
    1. HTML
    2. HTTP
    3. XML
  2. Which XML-RPC data type would be best suited to hold the number 8.67 ?
    1. boolean
    2. double
    3. int
  3. Which XML tag indicates that the data is an XML-RPC request ?
    1. methodCall
    2. methodResponse
    3. params

Answers

  1. A. XML-RPC uses HTTP (Hypertext Transfer Protocol) to transport data that is formatted as XML (Extensible Markup Language).
  2. B. All floating-point numbers such as 8.67 are represented by the double type in XML-RPC.
  3. A. The methodCall tag is used only in requests, methodResponse is used only in responses, and params is used in both.

Certification Practice

  1. What is the final value of y ?
    1. 3
    2. 4
    3. 5
    4. 6

Exercise