Author: chabotc
Date: Sat Jul 12 08:45:59 2008
New Revision: 676193
URL: http://svn.apache.org/viewvc?rev=676193&view=rev
Log:
Proper structure for handling multiple output formats, the previous code flow
got a bit messed up with the batch support, also the RestRequestItem shouldn't
be in http/, its not a servlet
Added:
incubator/shindig/trunk/php/src/socialrest/OutputConverter.php
incubator/shindig/trunk/php/src/socialrest/RestRequestItem.php
- copied unchanged from r676180,
incubator/shindig/trunk/php/src/socialrest/http/RestRequestItem.php
incubator/shindig/trunk/php/src/socialrest/converters/
incubator/shindig/trunk/php/src/socialrest/converters/OutputAtomConverter.php
incubator/shindig/trunk/php/src/socialrest/converters/OutputJsonConverter.php
Removed:
incubator/shindig/trunk/php/src/socialrest/http/RestRequestItem.php
Modified:
incubator/shindig/trunk/php/src/socialrest/http/RestServlet.php
Added: incubator/shindig/trunk/php/src/socialrest/OutputConverter.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialrest/OutputConverter.php?rev=676193&view=auto
==============================================================================
--- incubator/shindig/trunk/php/src/socialrest/OutputConverter.php (added)
+++ incubator/shindig/trunk/php/src/socialrest/OutputConverter.php Sat Jul 12
08:45:59 2008
@@ -0,0 +1,27 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+/**
+ * Abstract class for the Output conversion of the RESTful API
+ *
+ */
+abstract class OutputConverter {
+ abstract function outputResponse(ResponseItem $responseItem);
+ abstract function outputBatch(Array $responses);
+}
Added:
incubator/shindig/trunk/php/src/socialrest/converters/OutputAtomConverter.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialrest/converters/OutputAtomConverter.php?rev=676193&view=auto
==============================================================================
---
incubator/shindig/trunk/php/src/socialrest/converters/OutputAtomConverter.php
(added)
+++
incubator/shindig/trunk/php/src/socialrest/converters/OutputAtomConverter.php
Sat Jul 12 08:45:59 2008
@@ -0,0 +1,35 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+/**
+ * Format = atom output converter
+ *
+ */
+class OutputAtomConverter extends OutputConverter {
+
+ function outputResponse(ResponseItem $responseItem)
+ {
+
+ }
+
+ function outputBatch(Array $responses)
+ {
+
+ }
+}
\ No newline at end of file
Added:
incubator/shindig/trunk/php/src/socialrest/converters/OutputJsonConverter.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialrest/converters/OutputJsonConverter.php?rev=676193&view=auto
==============================================================================
---
incubator/shindig/trunk/php/src/socialrest/converters/OutputJsonConverter.php
(added)
+++
incubator/shindig/trunk/php/src/socialrest/converters/OutputJsonConverter.php
Sat Jul 12 08:45:59 2008
@@ -0,0 +1,35 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+/**
+ * Format = json output converter
+ *
+ */
+class OutputJsonConverter extends OutputConverter {
+
+ function outputResponse(ResponseItem $responseItem)
+ {
+ echo json_encode($responseItem->getResponse());
+ }
+
+ function outputBatch(Array $responses)
+ {
+ echo json_encode(array("responses" => $responses, "error" =>
false));
+ }
+}
Modified: incubator/shindig/trunk/php/src/socialrest/http/RestServlet.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialrest/http/RestServlet.php?rev=676193&r1=676192&r2=676193&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/socialrest/http/RestServlet.php (original)
+++ incubator/shindig/trunk/php/src/socialrest/http/RestServlet.php Sat Jul 12
08:45:59 2008
@@ -40,20 +40,14 @@
require 'src/socialrest/UserId.php';
require 'src/socialrest/ResponseItem.php';
require 'src/socialrest/RestfulCollection.php';
-require 'src/socialrest/http/RestRequestItem.php';
+require 'src/socialrest/RestRequestItem.php';
+require 'src/socialrest/OutputConverter.php';
+require 'src/socialrest/converters/OutputAtomConverter.php';
+require 'src/socialrest/converters/OutputJsonConverter.php';
-/*
- * See:
- *
http://sites.google.com/a/opensocial.org/opensocial/Technical-Resources/opensocial-specification----implementation-version-08/restful-api-specification
- * OpenSocial uses standard HTTP methods: GET to retrieve, PUT to update in
place, POST to create new, and DELETE to remove.
- * POST is special; it operates on collections and creates new activities,
persons, or app data within those collections,
- * and returns the base URI for the created resource in the Location: header,
per AtomPub semantics.
- *
- * Error status is returned by HTTP error code, with the error message in the
html's body
- */
-
-//NOTE TO SELF: delete should respond with a 204 No Content to indicate
success?
+//FIXME Delete should respond with a 204 No Content to indicate success
+class RestException extends Exception {}
/*
* Internal error code representations, these get translated into http codes
in the outputError() function
@@ -65,8 +59,6 @@
define('BAD_REQUEST', "badRequest");
define('INTERNAL_ERROR', "internalError");
-class RestException extends Exception {}
-
class RestServlet extends HttpServlet {
private static $JSON_BATCH_ROUTE = "jsonBatch";
@@ -74,18 +66,31 @@
public function doPost($method = 'POST')
{
$this->setNoCache(true);
- $this->noHeaders = true;
// if oauth, create a token from it's values instead of one
based on $_get['st']/$_post['st']
// NOTE : if no token is provided an anonymous one is created
(owner = viewer = appId = modId = 0)
// keep this in mind when creating your data services..
$token = $this->getSecurityToken();
$req = null;
+ $outputFormat = $this->getOutputFormat();
+ switch ($outputFormat) {
+ case 'json':
+ //$this->setContentType('application/json');
+ $outputConverter = new OutputJsonConverter();
+ break;
+ case 'atom':
+ //$this->setContentType('application/xml');
+ $outputConverter = new OutputAtomConverter();
+ break;
+ default:
+ $this->outputError(new
ResponseItem(NOT_IMPLEMENTED, "Invalid output format"));
+ break;
+ }
if ($this->isBatchUrl()) {
$req = $this->handleBatchRequest($token);
- echo json_encode(array("responses" => $req, "error" =>
false));
+ $outputConverter->outputBatch($req);
} else {
$responseItem = $this->handleSingleRequest($token,
$method);
- echo json_encode($responseItem);
+ $outputConverter->outputResponse($responseItem);
}
}
@@ -130,18 +135,11 @@
$class = new $class(null);
$response = $class->handleMethod($requestItem);
}
- if ($this->getOutputFormat() == 'json') {
- if ($this->isBatchUrl()) {
- return $response;
- } else {
- //If the method exists, its an ResponseItem
Error
- if ($response->getError() != null) {
- $this->outputError($response);
- }
- return $response->getResponse();
- }
- } else { // output atom format
+ if ($response->getError() != null && !$this->isBatchUrl()) {
+ // Can't use http error codes in batch mode, instead we
return the error code in the response item
+ $this->outputError($response);
}
+ return $response;
}
private function getRequestParams()
@@ -242,7 +240,7 @@
private function getOutputFormat()
{
- return isset($_POST['format']) && $_POST['format'] == 'atom' ?
'atom' : 'json';
+ return !empty($_POST['format']) ?
strtolowe(trim($_POST['format'])) : 'json';
}
private function getListParams()