Author: remi
Date: 2008-12-15 11:23:38 +0100 (Mon, 15 Dec 2008)
New Revision: 3128

Modified:
   
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxHTTPRequest.java
Log:
* added a high level requester.

Modified: 
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxHTTPRequest.java
===================================================================
--- 
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxHTTPRequest.java
 2008-12-15 10:10:33 UTC (rev 3127)
+++ 
software_suite_v2/tuxware/java-api/trunk/src/com/tuxisalive/api/TuxHTTPRequest.java
 2008-12-15 10:23:38 UTC (rev 3128)
@@ -35,6 +35,45 @@
                mutex = new SLock();
        }
        
+       /*
+        * Get a specific value from a hashtable structure.
+        */
+       @SuppressWarnings("unchecked")
+       private Object getValueFromStructure(Hashtable<Object,Object> struct,
+                       String valuePath)
+       {
+               String pathList[] = valuePath.split("\\.");
+               Hashtable<Object,Object> node = struct;
+               Object result = (Object)null;
+               
+               for (int i = 0; i < pathList.length; i++)
+               {
+                       String p = pathList[i];
+                       // Current node in path is valid
+                       if (node.containsKey(p))
+                       {
+                               // Path : leaf
+                               if (i == (pathList.length - 1))
+                               {
+                                       // Return the value of the matched path
+                                       result = (Object)node.get(p);
+                                       return result;
+                               }
+                               // Path : node
+                               else
+                               {
+                                       node = 
(Hashtable<Object,Object>)node.get(p);
+                               }
+                       }
+                       // Invalid path
+                       else
+                       {
+                               return result;
+                       }
+               }
+               return result;
+       }
+       
        /**
         * Make a request to the server.
         * 
@@ -88,6 +127,51 @@
                return xmlStruct;
        }
        
+       /**
+        * Make a request to the server.
+        * 
+        * @param cmd formated command in an url.
+        * @param varStruct structure definition of the requested values.
+        * @param varResult returned values in a structure.
+        * @return the success of the request.
+        */
+       public Boolean request(String cmd, Hashtable<Object,Object> varStruct, 
+                       Hashtable<Object,Object> varResult)
+       {
+               // Send the request and get the xml structure
+               Hashtable<Object,Object> xmlStruct = this.request(cmd);
+               // Check server run and the command success
+               if (!xmlStruct.get("server_run").equals("Success"))
+               {
+                       return false;
+               }
+               if (!xmlStruct.get("result").equals("Success"))
+               {
+                       return false;
+               }
+               // Get values from paths
+               if (varStruct.size() > 0)
+               {       
+                       for (Enumeration<Object> e = varStruct.keys(); 
e.hasMoreElements(); )
+                       {
+                               String valueName = (String)e.nextElement();
+                               String valuePath = 
(String)varStruct.get(valueName);
+                               Object value = getValueFromStructure(xmlStruct, 
valuePath);
+                               varResult.put(valueName, value);
+                       }
+               }
+               else
+               {
+                       for (Enumeration<Object> e = xmlStruct.keys(); 
e.hasMoreElements(); )
+                       {
+                               Object value = e.nextElement();
+                               varResult.put(value, xmlStruct.get(value));
+                       }
+               }
+               
+               return true;
+       }
+       
        /*
         * Parse the xml string to a data structure
         */


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to