import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.XmlRpcRequest; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcClientException; import org.apache.xmlrpc.client.XmlRpcSunHttpTransport; import org.apache.xmlrpc.client.XmlRpcSunHttpTransportFactory; import org.apache.xmlrpc.client.XmlRpcTransport; import org.apache.xmlrpc.client.XmlRpcTransportFactory; public class RPC { private String method; private ArrayList params = null; private Object result = null; public Object getResult() { return result; } private XmlRpcClient client; public RPC() throws MalformedURLException { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL(Config.serverURL)); // config.setEnabledForExtensions(true); // unsupported by server this.client = new XmlRpcClient(); this.client.setConfig(config); } public void setMethod(String method) { this.method = method; } public void param(Object p) { if (this.params == null) { this.params = new ArrayList(); } // fix the longs issue p = this.fixLongs(p); this.params.add(p); } @SuppressWarnings("unchecked") private Object fixLongs(Object p) { String t = p.getClass().getName(); if (t == "java.util.Map" || t == "java.util.HashMap" || t == "org.json.simple.JSONObject") { HashMap n = new HashMap(((HashMap) p).size()); Iterator> it = ((HashMap) p).entrySet().iterator(); while (it.hasNext()) { Entry e = it.next(); n.put(this.fixLongs(e.getKey()), this.fixLongs(e.getValue())); } return n; } else if (t == "java.lang.Long") { return new Integer(((Long) p).intValue()); } else { return p; } } public void setSessionCookie(final String sessionCookie) { XmlRpcTransportFactory factory = new XmlRpcSunHttpTransportFactory(this.client) { public XmlRpcTransport getTransport() { return new XmlRpcSunHttpTransport(client) { @Override protected void initHttpHeaders(XmlRpcRequest request) throws XmlRpcClientException { super.initHttpHeaders(request); setRequestHeader("Cookie", sessionCookie); } }; } }; this.client.setTransportFactory(factory); } public void go() throws XmlRpcException { this.go(Config.reset_params_default.booleanValue()); } public void go(boolean resetParams) throws XmlRpcException { try { this.result = this.client.execute(this.method, this.params); } catch (XmlRpcException e) { this.result = null; throw e; } finally { if (resetParams) { this.params = null; } } } }