FileDocCategorySizeDatePackage
XmlRpcConfigurationServlet.javaAPI DocExample9308Sat Jun 03 18:24:44 BST 2000None

XmlRpcConfigurationServlet

public class XmlRpcConfigurationServlet extends HttpServlet
XmlRpcConfigurationServlet is an administration tool that allows configuration changes to be saved to the XML configuration file.
author
Brett McLaughlin
version
1.0

Fields Summary
private static final String
CONFIG_FILENAME
Store the XML-RPC configuration file as a constant
private static final String
FORM_ACTION
Point action back at this servlet (and the {@link #doPost()} method). In Servlet API 2.1 or 2.2, this can be done programmatically, but this example allows this to work in Servlet 2.0 as well
com.oreilly.xml.XmlRpcConfiguration
config
Configuration object to work with
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

GET requests are received when the client wants to see the current configuration information. This provides a view-only look at the data. The generated HTML form then submits back to this servlet through POST, which causes the {@link #doPost} method to be invoked.


                                                                
       
                       
           

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        // Load the configuration information with our utility class
        config = new XmlRpcConfiguration(CONFIG_FILENAME);

        // Output HTML user interface
        out.println("<html><head>");
        out.println("<title>XML-RPC Configurations</title>");
        out.println("</head><body>");
        out.println("<h2 align=\"center\">XML-RPC Configuration</h2>");
        out.println("<form action=\"" + FORM_ACTION + "\" " +
                    "method=\"POST\">");
        out.println("<b>Hostname:</b> ");
        out.println("<input type=\"text\" " +
                    "name=\"hostname\" " +
                    "value=\"" + config.getHostname() +
                    "\" />");
        //out.println("<br />");
        out.println("    ");
        out.println("<b>Port Number:</b> ");
        out.println("<input type=\"text\" " +
                    "name=\"port\" " +
                    "value=\"" + config.getPortNumber() +
                    "\" />");
        out.println("<br />");
        out.println("<b>SAX Driver Class:</b> ");
        out.println("<input type=\"text\" " +
                    "name=\"driverClass\" size=\"50\"" +
                    "value=\"" + config.getDriverClass() +
                    "\" />");
        out.println("<br />");
        out.println("<br />");
        out.println("<h3 align=\"center\">XML-RPC handlers</h3>");

        // Display current handlers
        Hashtable handlers = config.getHandlers();
        Enumeration keys = handlers.keys();
        int index = 0;
        while (keys.hasMoreElements()) {
            String handlerID =
                (String)keys.nextElement();
            String handlerClass =
                (String)handlers.get(handlerID);
            out.println("<b>Identifier:</b> ");
            out.println("<input type=\"text\" " +
                        "value=\"" + handlerID + "\" " +
                        "name=\"handlerID\" />  ");
            out.println("<b>Class:</b> ");
            out.println("<input type=\"text\" " +
                        "value=\"" + handlerClass + "\" " +
                        "size=\"30\" " +
                        "name=\"handlerClass\" />  ");
            out.println("<br />");
            index++;
        }

        // Display empty boxes for additional handlers
        for (int i=0; i<3; i++) {
            out.println("<b>Identifier:</b> ");
            out.println("<input type=\"text\" " +
                        "name=\"handlerID\" />  ");
            out.println("<b>Class:</b> ");
            out.println("<input type=\"text\" " +
                        "size=\"30\" " +
                        "name=\"handlerClass\" />  ");
            out.println("<br />");
            index++;
        }

        out.println("<br /><center>");
        out.println("<input type=\"submit\" value=\"Save Changes\" />");
        out.println("</center>");
        out.println("</form></body></html>");

        out.close();
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

This method receives requests for modification of the XML-RPC configuration information, all from the {@link #doGet} method. This will again use the utility class to update the configuration file, letting the {@link XmlRpcConfiguration} object handle the actual writing to a file.


        // Update the configuration information
        if (config == null) {
            config = new XmlRpcConfiguration(CONFIG_FILENAME);
        }

        // Save the hostname
        String hostname =
          req.getParameterValues("hostname")[0];
        if ((hostname != null) && (!hostname.equals(""))) {
          config.setHostname(hostname);
        }

        // Save the port number
        int portNumber;
        try {
          portNumber =
            Integer.parseInt(
              req.getParameterValues("port")[0]);
        } catch (Exception e) {
          portNumber = 0;
        }
        if (portNumber > 0) {
          config.setPortNumber(portNumber);
        }

        // Save the SAX driver class
        String driverClass =
          req.getParameterValues("driverClass")[0];
        if ((driverClass != null) && (!driverClass.equals(""))) {
          config.setDriverClass(driverClass);
        }

        // Save the handlers
        String[] handlerIDs =
          req.getParameterValues("handlerID");
        String[] handlerClasses =
          req.getParameterValues("handlerClass");
        Hashtable handlers = new Hashtable();
        for (int i=0; i<handlerIDs.length; i++) {
          handlers.put(handlerIDs[i], handlerClasses[i]);
        }
        config.setHandlers(handlers);

        try {
        // Request the changes be written to the configuration store
        config.saveConfiguration(CONFIG_FILENAME);
        } catch (IOException e) {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        out.println(e.getMessage());
        return;
        }

        // Output a confirmation message
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        out.println("Changes saved <br />");
        out.println("<a href=\"" + FORM_ACTION +
                    "\">Return to Configuration Administration" +
                    "</a>");
        out.close();