Author: chabotc
Date: Sat Oct 11 17:18:15 2008
New Revision: 703751

URL: http://svn.apache.org/viewvc?rev=703751&view=rev
Log:
Part 1 of reviving the REST write methods & hooking up the input converters 
again

Modified:
    incubator/shindig/trunk/php/src/common/UrlGenerator.php
    incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php
    incubator/shindig/trunk/php/src/social/service/ActivityHandler.php
    incubator/shindig/trunk/php/src/social/service/AppDataHandler.php
    incubator/shindig/trunk/php/src/social/service/RestRequestItem.php
    incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php
    incubator/shindig/trunk/php/src/social/spi/GroupId.php

Modified: incubator/shindig/trunk/php/src/common/UrlGenerator.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/common/UrlGenerator.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/common/UrlGenerator.php (original)
+++ incubator/shindig/trunk/php/src/common/UrlGenerator.php Sat Oct 11 17:18:15 
2008
@@ -24,7 +24,6 @@
        {
                $v = $gadget->getChecksum();
                $view = HttpUtil::getView($gadget, $context);
-               
                $up = '';
                $prefs = $context->getUserPrefs();
                foreach ($gadget->getUserPrefs() as $pref) {

Modified: 
incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php 
(original)
+++ incubator/shindig/trunk/php/src/social/converters/InputXmlConverter.php Sat 
Oct 11 17:18:15 2008
@@ -62,12 +62,14 @@
        public function convertAppData($requestParam)
        {
                $xml = simplexml_load_string($requestParam, 'SimpleXMLElement', 
LIBXML_NOCDATA);
-               if (! isset($xml->appdata)) {
+               if (! isset($xml->entry)) {
                        throw new Exception("Mallformed AppData xml");
                }
                $data = array();
-               foreach (get_object_vars($xml->appdata) as $key => $val) {
-                       $data[trim($key)] = trim($val);
+               foreach ($xml->entry as $entry) {
+                       $key = trim($entry->key);
+                       $val = isset($entry->value) ? trim($entry->value) : 
null;
+                       $data[$key] = $val;
                }
                return $data;
        }

Modified: incubator/shindig/trunk/php/src/social/service/ActivityHandler.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/ActivityHandler.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/ActivityHandler.php 
(original)
+++ incubator/shindig/trunk/php/src/social/service/ActivityHandler.php Sat Oct 
11 17:18:15 2008
@@ -39,8 +39,7 @@
                } elseif (count($userIds) > 1) {
                        throw new InvalidArgumentException("Multiple userIds 
not supported");
                }
-               
-               return $this->service . deleteActivities($userIds[0], 
$requestItem->getGroup(), $requestItem->getAppId(), $activityIds, 
$request->getToken());
+               return $this->service->deleteActivities($userIds[0], 
$requestItem->getGroup(), $requestItem->getAppId(), $activityIds, 
$request->getToken());
        }
 
        /**

Modified: incubator/shindig/trunk/php/src/social/service/AppDataHandler.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/AppDataHandler.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/AppDataHandler.php (original)
+++ incubator/shindig/trunk/php/src/social/service/AppDataHandler.php Sat Oct 
11 17:18:15 2008
@@ -82,28 +82,21 @@
        {
                $requestItem->applyUrlTemplate(self::$APP_DATA_PATH);
                $userIds = $requestItem->getUsers();
-               
                if (count($userIds) < 1) {
                        throw new InvalidArgumentException("No userId 
specified");
                } elseif (count($userIds) > 1) {
                        throw new InvalidArgumentException("Multiple userIds 
not supported");
-               }
+               }               
                $values = $requestItem->getParameter("data");
+               $fields = array();
                foreach (array_keys($values) as $key) {
+                       $fields[] = $key;
                        if (! $this->isValidKey($key)) {
                                throw new SocialSpiException("One or more of 
the app data keys are invalid: " . $key, ResponseError::$BAD_REQUEST);
                        }
                }
-               /*
-               $postFields = array();
-               if ($requestItem->getPostData() != null) {
-                       $data = $requestItem->getPostData();
-                       foreach ($data as $key => $val) {
-                               $postFields[] = $key;
-                       }
-               }
-               */
-               return $this->service->updatePersonData($userIds[0], 
$requestItem->getGroup(), $requestItem->getAppId(), $requestItem->getFields(), 
$values, $requestItem->getToken());
+               // this used to be $requestItem->getFields() instead of using 
the fields, but that makes no sense to me
+               return $this->service->updatePersonData($userIds[0], 
$requestItem->getGroup(), $requestItem->getAppId(), $fields, $values, 
$requestItem->getToken());
        }
 
        /**

Modified: incubator/shindig/trunk/php/src/social/service/RestRequestItem.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/RestRequestItem.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/RestRequestItem.php 
(original)
+++ incubator/shindig/trunk/php/src/social/service/RestRequestItem.php Sat Oct 
11 17:18:15 2008
@@ -23,35 +23,27 @@
 class RestRequestItem extends RequestItem {
        
        protected static $X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override";
-       
        private $url;
-       
        private $params;
-       
+       private $inputConverter;
+       private $outputConverter;
        private $postData;
 
-       public function __construct($service, $method, SecurityToken $token, 
$converter)
-       {
-               parent::__construct($service, $method, $token, $converter);
-       }
-
-       public static function createWithPath($path, $method, $postData, 
SecurityToken $token, $converter)
+       public function __construct($service, $method, SecurityToken $token, 
$inputConverter, $outputConverter)
        {
-               $restfulRequestItem = new 
RestRequestItem($this->getServiceFromPath($path), $method, $token, $converter);
-               $restfulRequestItem->setPostData($postData);
-               $restfulRequestItem->setUrl($path);
-               $restfulRequestItem->putUrlParamsIntoParameters();
-               return $restfulRequestItem;
+               parent::__construct($service, $method, $token);
+               $this->inputConverter = $inputConverter;
+               $this->outputConverter = $outputConverter;
        }
 
-       public static function createWithRequest($servletRequest, $token, 
$converter)
+       public static function createWithRequest($servletRequest, $token, 
$inputConverter, $outputConverter)
        {
-               $restfulRequestItem = new 
RestRequestItem(self::getServiceFromPath($servletRequest['url']), 
self::getMethod(), $token, $converter);
+               $restfulRequestItem = new 
RestRequestItem(self::getServiceFromPath($servletRequest['url']), 
self::getMethod(), $token, $inputConverter, $outputConverter);
                $restfulRequestItem->setUrl($servletRequest['url']);
+               
$restfulRequestItem->setParams($restfulRequestItem->createParameterMap());
                if (isset($servletRequest['postData'])) {
                        
$restfulRequestItem->setPostData($servletRequest['postData']);
                }
-               
$restfulRequestItem->setParams($restfulRequestItem->createParameterMap());
                return $restfulRequestItem;
        }
 
@@ -68,6 +60,25 @@
        public function setPostData($postData)
        {
                $this->postData = $postData;
+               $service = $this->getServiceFromPath($this->url);
+               switch ($service) {
+                       case DataServiceServlet::$PEOPLE_ROUTE:
+                               $data = 
$this->inputConverter->convertPeople($this->postData);
+                               break;
+                       case DataServiceServlet::$ACTIVITY_ROUTE:
+                               $data = 
$this->inputConverter->convertActivities($this->postData);
+                               break;
+                       case DataServiceServlet::$APPDATA_ROUTE:
+                               $data = 
$this->inputConverter->convertAppData($this->postData);
+                               break;
+                       case DataServiceServlet::$MESSAGE_ROUTE:
+                               $data = 
$this->inputConverter->convertMessages($this->postData);
+                               break;
+                       default:
+                               throw new Exception("Invalid or unknown service 
endpoint: $service");
+                               break;
+               }
+               $this->params['data'] = $data;
        }
 
        static function getServiceFromPath($pathInfo)
@@ -150,7 +161,7 @@
                if ($paramValue == null) {
                        return;
                }
-               $this->params[$paramName] = is_array($paramValue) ? $paramValue 
: array($paramValue);
+               $this->params[$paramName] = $paramValue;
        }
 
        /**
@@ -159,8 +170,8 @@
        public function getParameter($paramName, $defaultValue = null)
        {
                $paramValue = isset($this->params[$paramName]) ? 
$this->params[$paramName] : null;
-               if ($paramValue != null && ! empty($paramValue) && 
is_array($this->params[$paramName])) {
-                       return $paramValue[0];
+               if ($paramValue != null && ! empty($paramValue)) {
+                       return $paramValue;
                }
                return $defaultValue;
        }

Modified: incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php 
(original)
+++ incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php Sat 
Oct 11 17:18:15 2008
@@ -51,12 +51,18 @@
                                $this->sendSecurityError();
                                return;
                        }
-                       $converter = $this->getConverterForRequest();
-                       $this->handleSingleRequest($token, $converter);
+                       $inputConverter = $this->getInputConverterForRequest();
+                       $outputConverter = 
$this->getOutputConverterForRequest();
+                       $this->handleSingleRequest($token, $inputConverter, 
$outputConverter);
                } catch (Exception $e) {
-                       echo "<b>Exception: " . $e->getMessage() . 
"</b><br><pre>\n";
-                       echo $e->getTraceAsString();
-                       echo "</pre>";
+                       $code = '500 Internal Server Error';
+                       header("HTTP/1.0 $code", true);
+                       echo "<h1>$code - Internal Server Error</h1>\n". 
$e->getMessage();
+                       if (Config::get('debug')) {
+                               echo "\n\n<br>\nDebug 
backtrace:\n<br>\n<pre>\n";
+                               echo $e->getTraceAsString();
+                               echo "\n</pre>\n";
+                       }
                }
        }
 
@@ -64,22 +70,22 @@
        {
                $errorMessage = $responseItem->getErrorMessage();
                switch ($responseItem->getError()) {
-                       case BAD_REQUEST:
+                       case ResponseError::$BAD_REQUEST:
                                $code = '400 Bad Request';
                                break;
-                       case UNAUTHORIZED:
+                       case ResponseError::$UNAUTHORIZED:
                                $code = '401 Unauthorized';
                                break;
-                       case FORBIDDEN:
+                       case ResponseError::$FORBIDDEN:
                                $code = '403 Forbidden';
                                break;
-                       case FORBIDDEN:
+                       case ResponseError::$FORBIDDEN:
                                $code = '404 Not Found';
                                break;
-                       case NOT_IMPLEMENTED:
+                       case ResponseError::$NOT_IMPLEMENTED:
                                $code = '501 Not Implemented';
                                break;
-                       case INTERNAL_ERROR:
+                       case ResponseError::$INTERNAL_ERROR:
                        default:
                                $code = '500 Internal Server Error';
                                break;
@@ -90,26 +96,34 @@
        }
 
        /**
-        * Handler for non-batch requests
+        * Handler for non-batch requests (REST only has non-batch requests)
         */
-       private function handleSingleRequest(SecurityToken $token, $converter)
+       private function handleSingleRequest(SecurityToken $token, 
$inputConverter, $outputConverter)
        {
                $servletRequest = array(
-                               'url' => substr($_SERVER["REQUEST_URI"], 
strlen(Config::get('web_prefix') . '/social/rest')));
-               $requestItem = 
RestRequestItem::createWithRequest($servletRequest, $token, $converter);
+                               'url' => substr($_SERVER["REQUEST_URI"], 
strlen(Config::get('web_prefix') . '/social/rest'))
+               );
+               if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
+                       $servletRequest['postData'] = 
$GLOBALS['HTTP_RAW_POST_DATA'];
+                       if (get_magic_quotes_gpc()) {
+                               $servletRequest['postData'] = 
stripslashes($servletRequest['postData']);
+                       }
+               }
+               $requestItem = 
RestRequestItem::createWithRequest($servletRequest, $token, $inputConverter, 
$outputConverter);
                $responseItem = 
$this->getResponseItem($this->handleRequestItem($requestItem));
                if ($responseItem->getError() == null) {
-                       //FIXME does this code have to be here? bah, breaks our 
converters :)
-                       /*if (! ($responseItem instanceof DataCollection) && ! 
($responseItem instanceof RestfulCollection)) {
-                               $responseItem = array("entry" => $responseItem);
-                       }*/
-                       $converter->outputResponse($responseItem, $requestItem);
+                       $outputConverter->outputResponse($responseItem, 
$requestItem);
                } else {
                        $this->sendError($responseItem);
                }
        }
 
-       private function getConverterForRequest()
+       /**
+        * Returns the output converter to use
+        *
+        * @return OutputConverter
+        */
+       private function getOutputConverterForRequest()
        {
                $outputFormat = strtolower(trim(! 
empty($_POST[self::$FORMAT_PARAM]) ? $_POST[self::$FORMAT_PARAM] : (! 
empty($_GET[self::$FORMAT_PARAM]) ? $_GET[self::$FORMAT_PARAM] : 'json')));
                switch ($outputFormat) {
@@ -120,26 +134,77 @@
                        case 'json':
                                return new OutputJsonConverter();
                        default:
-                               throw new Exception("Unknown format param: 
$outputFormat");
+                               // if no output format is set, see if we can 
match an input format header
+                               // if not, default to json
+                               if (isset($_SERVER['CONTENT_TYPE'])) {
+                                       switch ($_SERVER['CONTENT_TYPE']) {
+                                               case 'application/atom+xml':
+                                                       return new 
OutputAtomConverter();
+                                               case 'application/xml':
+                                                       return new 
OutputXmlConverter();
+                                               default:
+                                               case 'application/json':
+                                                       return new 
OutputJsonConverter();
+                                       }
+                               }
+                               break;
+               }
+       }
+       
+       /**
+        * Returns the input converter to use
+        *
+        * @return InputConverter
+        */
+       private function getInputConverterForRequest()
+       {
+               $inputFormat = $this->getInputRequestFormat();
+               switch ($inputFormat) {
+                       case 'xml':
+                               return new InputXmlConverter();
+                       case 'atom':
+                               return new InputAtomConverter();
+                       case 'json':
+                               return new InputJsonConverter();
+                       default:
+                               throw new Exception("Unknown format param: 
$inputFormat");
                }
        }
 
-       private function getRequestFormat()
+       /**
+        * Tries to guess the input format based on the Content-Type
+        * header, of if none is set, the format query param
+        *
+        * @return string request format to use
+        */
+       private function getInputRequestFormat()
        {
+               // input format is defined by the Content-Type header
+               // if that isn't set we use the &format= param
+               // if that isn't set, we default to json
                if (isset($_SERVER['CONTENT_TYPE'])) {
                        switch ($_SERVER['CONTENT_TYPE']) {
                                case 'application/atom+xml':
                                        return 'atom';
+                               case 'application/xml':
+                                       return 'xml';
                                case 'application/json':
-                                       return 'json';
                                default:
-                                       throw new Exception("Invalid request 
content type");
+                                       return 'json';
                        }
+               } else {
+                       // if no Content-Type header is set, we assume the 
input format will be the same as the &format=<foo> param
+                       // if that isn't set either, we assume json
+                       return strtolower(trim(! 
empty($_POST[self::$FORMAT_PARAM]) ? $_POST[self::$FORMAT_PARAM] : (! 
empty($_GET[self::$FORMAT_PARAM]) ? $_GET[self::$FORMAT_PARAM] : 'json')));
                }
-               // if no Content-Type header is set, we assume json
-               return 'json';
        }
 
+       /**
+        * Returns the route to use (activities, people, appdata, messages)
+        *
+        * @param string $pathInfo
+        * @return string the route name
+        */
        private function getRouteFromParameter($pathInfo)
        {
                $pathInfo = substr($pathInfo, 1);

Modified: incubator/shindig/trunk/php/src/social/spi/GroupId.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/spi/GroupId.php?rev=703751&r1=703750&r2=703751&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/spi/GroupId.php (original)
+++ incubator/shindig/trunk/php/src/social/spi/GroupId.php Sat Oct 11 17:18:15 
2008
@@ -30,7 +30,13 @@
 
        static public function fromJson($jsonId)
        {
-               if (in_array(substr($jsonId, 1), GroupId::$types)) {
+               if (is_array($jsonId)) {
+                       if (in_array(substr($jsonId[0], 1), GroupId::$types)) {
+                               return new GroupId(substr($jsonId[0], 1), null);
+                       } else {
+                               return new GroupId('groupId', $jsonId);
+                       }
+               } elseif (in_array(substr($jsonId, 1), GroupId::$types)) {
                        return new GroupId(substr($jsonId, 1), null);
                }
                return new GroupId('groupId', $jsonId);


Reply via email to