Java ExampleΒΆ

This is an example of how to use the Memset API with Java and Apache XML-RPC.

Substitute API_KEY_HEX with a valid API key.

/*
* Memset API example using Java and Apache XML-RPC.
*/

import static java.lang.System.out;
import java.net.*;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.XmlRpcException;

import java.util.HashMap;

class xmlrpctest {
    private static final String API_URL = "https://api.memset.com/v1/xmlrpc";

    public static void main(String[] args) {

        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

        try {
            config.setServerURL(new URL(API_URL));
        } catch (MalformedURLException ex) {
            out.println("Wrong API_URL");
            return;
        }

        config.setBasicUserName("API_KEY_HEX");

        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);

        try {
            Object[] methods = (Object[]) client.execute("system.listMethods",
                                                         new Object[] {});

            out.println("Method list:");
            for(int i=0; i<methods.length; i++) {
                out.println(methods[i]);    
            }

            // we use named parameters
            HashMap params = new HashMap<String, Object>();
            params.put("name", "myserver1");

            HashMap service_info = (HashMap) client.execute("service.info", 
                                                            new Object[] { params });

            out.println("service_info:");
            out.println(service_info.toString());   

            // if a reboot has already been initiated/requested, XmlRpcException will be raised
            // otherwise we get information about the created job
            HashMap result = (HashMap) client.execute("server.reboot",
                                                      new Object[] { params });

            out.println("server reboot:");
            out.println(result.toString()); 

        } catch (XmlRpcException ex) {
            out.println(ex);
            return;
        }
    }       
}

Previous topic

Python Firewalling Example

Next topic

C# Example

This Page