Advertisement
Guest User

Untitled

a guest
Jan 30th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map.Entry;
  7.  
  8. import org.apache.xmlrpc.XmlRpcException;
  9. import org.apache.xmlrpc.XmlRpcRequest;
  10. import org.apache.xmlrpc.client.XmlRpcClient;
  11. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  12. import org.apache.xmlrpc.client.XmlRpcClientException;
  13. import org.apache.xmlrpc.client.XmlRpcSunHttpTransport;
  14. import org.apache.xmlrpc.client.XmlRpcSunHttpTransportFactory;
  15. import org.apache.xmlrpc.client.XmlRpcTransport;
  16. import org.apache.xmlrpc.client.XmlRpcTransportFactory;
  17.  
  18. public class RPC
  19. {
  20.  
  21.     private String          method;
  22.     private ArrayList<Object>   params  = null;
  23.     private Object          result  = null;
  24.  
  25.     public Object getResult()
  26.     {
  27.         return result;
  28.     }
  29.  
  30.     private XmlRpcClient    client;
  31.  
  32.     public RPC() throws MalformedURLException
  33.     {
  34.         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  35.         config.setServerURL(new URL(Config.serverURL));
  36.         // config.setEnabledForExtensions(true); // unsupported by server
  37.         this.client = new XmlRpcClient();
  38.         this.client.setConfig(config);
  39.     }
  40.  
  41.     public void setMethod(String method)
  42.     {
  43.         this.method = method;
  44.     }
  45.  
  46.     public void param(Object p)
  47.     {
  48.         if (this.params == null)
  49.         {
  50.             this.params = new ArrayList<Object>();
  51.         }
  52.  
  53.         // fix the longs issue
  54.         p = this.fixLongs(p);
  55.  
  56.         this.params.add(p);
  57.     }
  58.  
  59.     @SuppressWarnings("unchecked")
  60.     private Object fixLongs(Object p)
  61.     {
  62.         String t = p.getClass().getName();
  63.         if (t == "java.util.Map" || t == "java.util.HashMap" || t == "org.json.simple.JSONObject")
  64.         {
  65.             HashMap<Object, Object> n = new HashMap<Object, Object>(((HashMap<Object, Object>) p).size());
  66.             Iterator<Entry<Object, Object>> it = ((HashMap<Object, Object>) p).entrySet().iterator();
  67.             while (it.hasNext())
  68.             {
  69.                 Entry<Object, Object> e = it.next();
  70.                 n.put(this.fixLongs(e.getKey()), this.fixLongs(e.getValue()));
  71.             }
  72.             return n;
  73.         }
  74.         else if (t == "java.lang.Long")
  75.         {
  76.             return new Integer(((Long) p).intValue());
  77.         }
  78.         else
  79.         {
  80.             return p;
  81.         }
  82.     }
  83.  
  84.     public void setSessionCookie(final String sessionCookie)
  85.     {
  86.         XmlRpcTransportFactory factory = new XmlRpcSunHttpTransportFactory(this.client) {
  87.             public XmlRpcTransport getTransport()
  88.             {
  89.                 return new XmlRpcSunHttpTransport(client) {
  90.                     @Override
  91.                     protected void initHttpHeaders(XmlRpcRequest request) throws XmlRpcClientException
  92.                     {
  93.                         super.initHttpHeaders(request);
  94.                         setRequestHeader("Cookie", sessionCookie);
  95.                     }
  96.                 };
  97.             }
  98.         };
  99.         this.client.setTransportFactory(factory);
  100.     }
  101.  
  102.     public void go() throws XmlRpcException
  103.     {
  104.         this.go(Config.reset_params_default.booleanValue());
  105.     }
  106.  
  107.     public void go(boolean resetParams) throws XmlRpcException
  108.     {
  109.         try
  110.         {
  111.             this.result = this.client.execute(this.method, this.params);
  112.         }
  113.         catch (XmlRpcException e)
  114.         {
  115.             this.result = null;
  116.             throw e;
  117.         }
  118.         finally
  119.         {
  120.             if (resetParams)
  121.             {
  122.                 this.params = null;
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement