### Eclipse Workspace Patch 1.0
#P REST Server 6
Index: rest_server.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.info,v
retrieving revision 1.1.2.1.2.1
diff -u -r1.1.2.1.2.1 rest_server.info
--- rest_server.info	10 Jan 2008 04:54:44 -0000	1.1.2.1.2.1
+++ rest_server.info	7 May 2008 13:54:17 -0000
@@ -1,6 +1,5 @@
-; $Id;
+; $Id$
 name = REST Server
 description = Provides a REST server.
 package = "Services - servers"
-dependancies[] = zend
 dependancies[] = services
Index: rest_server.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.module,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 rest_server.module
--- rest_server.module	5 Jan 2008 05:39:54 -0000	1.1.2.1
+++ rest_server.module	7 May 2008 13:54:17 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: rest_server.module,v 1.1.2.1 2008/01/05 05:39:54 robloach Exp $
+// $Id$
 
 /**
  * Implementation of hook_server_info()
@@ -9,51 +9,84 @@
     '#name' => 'REST',
     '#path' => 'rest',
   );
-} // function rest_server_server_info
+} 
 
 /**
  * Implementation of hook_server()
  */
 function rest_server_server() {
-  // Initiate the server and the request
-  if (zend_initialize('Zend_Rest_Server')) {
-    $request = $_REQUEST;
-    $server = new Zend_Rest_Server();
-    
-    // Check for which service we're calling
-    foreach (services_get_all() as $method) {
-      if ($method['#method'] == $request['method']) {
-        $request['method'] = $method['#callback'];
-        $server->addFunction($method['#callback']);
-        
-        // Special use cases for arguments of certain types
-        foreach ($method['#args'] as $argument) {
-          $argname = $argument['#name'];
-          switch ($argument['#type']) {
-            case 'array' :
-              if (empty($request[$argname])) {
-                $request[$argname] = array();
-              }
-              else {
-                $request[$argname] = explode(',', $request[$argname]);
-              }
-              break;
+  $request = $_REQUEST;
+  $args = array();
+  $rest_request = array();
+  // Check for which service we're calling
+  foreach (services_get_all() as $method) {
+    if ($method['#method'] == $request['method']) {
+      $methodname = $method['#method'];
+      // Special use cases for arguments of certain types
+      foreach ($method['#args'] as $argument) {
+        $argname = $argument['#name'];
+        switch ($argument['#type']) {
+          case 'array' :
+            if (empty($request[$argname])) {
+              $args[] = array();
+            }
+            else {
+              $args[] = explode(',', $request[$argname]);
+            }
+            break;
+          case 'string':
+          if(empty($request[$argname])) {
+            $args[] = '';
+          }
+          else {
+            $args[] = $request[$argname];
           }
+            
+          break;
         }
-        
-        // Found method to call, so exit
-        break;
       }
+     $rest_request['method'] = $methodname;
+     $rest_request['args'] = $args;
+      break;
     }
-    try {
-      echo $server->handle($request);
+  }
+  try {
+    $output = rest_server_call_wrapper($rest_request);
+    // Check for returned errors by rest_server_server_error function
+    if(array_key_exists('rest_server_server_error', $output)) {
+      echo convert_to_xml($output['rest_server_server_error'], 'error');
     }
-    catch (Exception $e) {
-      echo $e->getMessage();
+    else {
+      echo convert_to_xml($output, 'success');
     }
+    
   }
-  else {
-    drupal_set_message(t('The <em>REST Server</em> requires proper configuration of the <a href="@url" target="_blank">Zend Framework</a>. Please <a href="@configure">configure it</a> correctly and try again.', array('@url' => 'http://framework.zend.com', '@configure' => url('admin/settings/zend'))), 'error');
-    drupal_goto(variable_get('site_frontpage', 'node'));
+  catch (Exception $e) {
+    $output = $e->getMessage();
+    $arr = array('message' => $output);
+    echo convert_to_xml('Internal error', 'error');
   }
-} // function rest_server_server
+} 
+
+function rest_server_call_wrapper($request) {
+  $args = $request['args'];
+  $method_name = $request['method'];
+  return services_method_call($method_name, $args);
+}
+
+function convert_to_xml($response, $response_type) {
+  include_once('rest_xml_writer.inc');
+  $writer = new restXmlWriter();
+  $xml_contents = $writer->toXml($response, $response_type);
+  return $xml_contents;
+}
+
+/**
+ * Implementation of hook_server_error
+ *
+ * @param string $message
+ * @return array
+ */
+function rest_server_server_error($message) {
+  return array('rest_server_server_error' => $message);
+}
\ No newline at end of file
Index: rest_xml_writer.inc
===================================================================
RCS file: rest_xml_writer.inc
diff -N rest_xml_writer.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ rest_xml_writer.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,71 @@
+<?php 
+// $Id$
+class restXmlWriter {
+/**
+* The main function for converting to an XML document.
+*
+* @param array $data
+* @param string $status - POST/GET request status 
+* @param string $rootNodeName - what you want the root node to be - defaults to response.
+* @param SimpleXMLElement $xml - should only be used recursively
+* @return string XML
+*/
+public static function toXml($data, $status, $rootNodeName = 'response', $xml = NULL) {
+  if(empty($xml)) {
+    $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><rest generator='xml_writer' version='1.0'></rest>");
+  }
+  $node = $xml->addChild($rootNodeName);
+  
+  if(is_array($data) || is_object($data)) {
+    // loop through the data passed in.
+    foreach($data as $key => $value)
+    {
+      if (is_array($value) || (is_object($value)))
+      {
+        restXmlWriter::subsetToXml($value, $key, $node);
+      }
+      else
+      {
+        // add single node.
+        $value = htmlentities($value);
+        $xml->$rootNodeName->addChild($key,$value);
+      }
+    }
+  } else {
+    $xml->$rootNodeName = $data;
+  }
+  // Assign status message 
+  $xml->addChild('status', $status);
+  return $xml->asXML();
+}
+
+/**
+ * Helper function for toXML(). Since there's more specifics for highest elements we've separated these into two functions.
+ * 
+ *
+ * @param array $data
+ * @param string $rootNodeName
+ * @param SimpleXMLElement $xml
+ * @return string
+ */
+public static function subsetToXml($data, $rootNodeName, $xml) {
+  $node = $xml->addChild($rootNodeName);
+  if(is_array($data) || is_object($data)) {
+    foreach($data as $key => $value)
+    {
+      if (is_array($value) || (is_object($value)))
+      {
+        restXmlWriter::subsetToXml($value, $key, $node);
+      }
+      else
+      {
+        $value = htmlentities($value);
+        $xml->$rootNodeName->addChild($key,$value);
+      }
+    }
+  } else {
+    $xml->$rootNodeName = $data;
+  }
+  return $xml->asXML();
+  }
+}
