Browse Source
Split OCS version handling
Split OCS version handling
This cleans up a bit the OCSController/Middleware. Since the 2 versions of OCS differ a bit. Moved a lot of stuff internal since it is of no concern to the outside.pull/1275/head
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
7 changed files with 390 additions and 79 deletions
-
85lib/private/AppFramework/Middleware/OCSMiddleware.php
-
85lib/private/AppFramework/OCS/BaseResponse.php
-
78lib/private/AppFramework/OCS/V1Response.php
-
76lib/private/AppFramework/OCS/V2Response.php
-
32lib/public/AppFramework/OCSController.php
-
63tests/lib/AppFramework/Controller/OCSControllerTest.php
-
50tests/lib/AppFramework/Middleware/OCSMiddlewareTest.php
@ -0,0 +1,85 @@ |
|||
<?php |
|||
/** |
|||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @author Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @license GNU AGPL version 3 or any later version |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
namespace OC\AppFramework\OCS; |
|||
|
|||
use OCP\AppFramework\Http\DataResponse; |
|||
use OCP\AppFramework\Http\Response; |
|||
|
|||
abstract class BaseResponse extends Response { |
|||
/** @var array */ |
|||
protected $data; |
|||
|
|||
/** @var string */ |
|||
protected $format; |
|||
|
|||
/** @var string */ |
|||
protected $statusMessage; |
|||
|
|||
/** @var int */ |
|||
protected $itemsCount; |
|||
|
|||
/** @var int */ |
|||
protected $itemsPerPage; |
|||
|
|||
/** |
|||
* BaseResponse constructor. |
|||
* |
|||
* @param DataResponse|null $dataResponse |
|||
* @param string $format |
|||
* @param string|null $statusMessage |
|||
* @param int|null $itemsCount |
|||
* @param int|null $itemsPerPage |
|||
*/ |
|||
public function __construct(DataResponse $dataResponse, |
|||
$format = 'xml', |
|||
$statusMessage = null, |
|||
$itemsCount = null, |
|||
$itemsPerPage = null) { |
|||
$this->format = $format; |
|||
$this->statusMessage = $statusMessage; |
|||
$this->itemsCount = $itemsCount; |
|||
$this->itemsPerPage = $itemsPerPage; |
|||
|
|||
$this->data = $dataResponse->getData(); |
|||
|
|||
$this->setHeaders($dataResponse->getHeaders()); |
|||
$this->setStatus($dataResponse->getStatus()); |
|||
$this->setETag($dataResponse->getETag()); |
|||
$this->setLastModified($dataResponse->getLastModified()); |
|||
$this->setCookies($dataResponse->getCookies()); |
|||
$this->setContentSecurityPolicy($dataResponse->getContentSecurityPolicy()); |
|||
} |
|||
|
|||
/** |
|||
* @param string[] $meta |
|||
* @return string |
|||
*/ |
|||
protected function renderResult($meta) { |
|||
// TODO rewrite functions
|
|||
return \OC_API::renderResult($this->format, $meta, $this->data); |
|||
} |
|||
|
|||
public function getOCSStatus() { |
|||
return parent::getStatus(); |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
<?php |
|||
/** |
|||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @author Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @license GNU AGPL version 3 or any later version |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
namespace OC\AppFramework\OCS; |
|||
|
|||
use OCP\API; |
|||
use OCP\AppFramework\Http; |
|||
|
|||
class V1Response extends BaseResponse { |
|||
|
|||
/** |
|||
* The V1 endpoint has very limited http status codes basically everything |
|||
* is status 200 except 401 |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getStatus() { |
|||
$status = parent::getStatus(); |
|||
if ($status === Http::STATUS_FORBIDDEN || $status === API::RESPOND_UNAUTHORISED) { |
|||
return Http::STATUS_UNAUTHORIZED; |
|||
} |
|||
|
|||
return Http::STATUS_OK; |
|||
} |
|||
|
|||
/** |
|||
* In v1 all OK is 100 |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getOCSStatus() { |
|||
$status = parent::getOCSStatus(); |
|||
|
|||
if ($status === Http::STATUS_OK) { |
|||
return 100; |
|||
} |
|||
|
|||
return $status; |
|||
} |
|||
|
|||
/** |
|||
* Construct the meta part of the response |
|||
* And then late the base class render |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function render() { |
|||
$meta = [ |
|||
'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
|||
'statuscode' => $this->getOCSStatus(), |
|||
'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage, |
|||
]; |
|||
|
|||
$meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : ''; |
|||
$meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: ''; |
|||
|
|||
return $this->renderResult($meta); |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
<?php |
|||
/** |
|||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @author Roeland Jago Douma <roeland@famdouma.nl> |
|||
* |
|||
* @license GNU AGPL version 3 or any later version |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
namespace OC\AppFramework\OCS; |
|||
|
|||
use OCP\AppFramework\Http; |
|||
use OCP\API; |
|||
|
|||
class V2Response extends BaseResponse { |
|||
|
|||
/** |
|||
* The V2 endpoint just passes on status codes. |
|||
* Of course we have to map the OCS specific codes to proper HTTP status codes |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getStatus() { |
|||
|
|||
$status = parent::getStatus(); |
|||
if ($status === API::RESPOND_UNAUTHORISED) { |
|||
return Http::STATUS_UNAUTHORIZED; |
|||
} else if ($status === API::RESPOND_NOT_FOUND) { |
|||
return Http::STATUS_NOT_FOUND; |
|||
} else if ($status === API::RESPOND_SERVER_ERROR || $status === API::RESPOND_UNKNOWN_ERROR) { |
|||
return Http::STATUS_INTERNAL_SERVER_ERROR; |
|||
} else if ($status < 200 || $status > 600) { |
|||
return Http::STATUS_BAD_REQUEST; |
|||
} |
|||
|
|||
return $status; |
|||
} |
|||
|
|||
/** |
|||
* Construct the meta part of the response |
|||
* And then late the base class render |
|||
* |
|||
* @return string |
|||
*/ |
|||
public function render() { |
|||
$status = parent::getStatus(); |
|||
|
|||
$meta = [ |
|||
'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure', |
|||
'statuscode' => $this->getOCSStatus(), |
|||
'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage, |
|||
]; |
|||
|
|||
if ($this->itemsCount !== null) { |
|||
$meta['totalitems'] = $this->itemsCount; |
|||
} |
|||
if ($this->itemsPerPage !== null) { |
|||
$meta['itemsperpage'] = $this->itemsPerPage; |
|||
} |
|||
|
|||
return $this->renderResult($meta); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue