You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Donquixote <marjunebatac@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Stefan Weil <sw@weilnetz.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. /**
  29. * Public interface of ownCloud for apps to use.
  30. * AppFramework\Controller class
  31. */
  32. namespace OCP\AppFramework;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\Http\Response;
  35. use OCP\IRequest;
  36. /**
  37. * Base class to inherit your controllers from that are used for RESTful APIs
  38. * @since 8.1.0
  39. */
  40. abstract class OCSController extends ApiController {
  41. /** @var int */
  42. private $ocsVersion;
  43. /**
  44. * constructor of the controller
  45. * @param string $appName the name of the app
  46. * @param IRequest $request an instance of the request
  47. * @param string $corsMethods comma separated string of HTTP verbs which
  48. * should be allowed for websites or webapps when calling your API, defaults to
  49. * 'PUT, POST, GET, DELETE, PATCH'
  50. * @param string $corsAllowedHeaders comma separated string of HTTP headers
  51. * which should be allowed for websites or webapps when calling your API,
  52. * defaults to 'Authorization, Content-Type, Accept'
  53. * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
  54. * request should be cached, defaults to 1728000 seconds
  55. * @since 8.1.0
  56. */
  57. public function __construct($appName,
  58. IRequest $request,
  59. $corsMethods='PUT, POST, GET, DELETE, PATCH',
  60. $corsAllowedHeaders='Authorization, Content-Type, Accept',
  61. $corsMaxAge=1728000) {
  62. parent::__construct($appName, $request, $corsMethods,
  63. $corsAllowedHeaders, $corsMaxAge);
  64. $this->registerResponder('json', function ($data) {
  65. return $this->buildOCSResponse('json', $data);
  66. });
  67. $this->registerResponder('xml', function ($data) {
  68. return $this->buildOCSResponse('xml', $data);
  69. });
  70. }
  71. /**
  72. * @param int $version
  73. * @since 11.0.0
  74. * @internal
  75. */
  76. public function setOCSVersion($version) {
  77. $this->ocsVersion = $version;
  78. }
  79. /**
  80. * Since the OCS endpoints default to XML we need to find out the format
  81. * again
  82. * @param mixed $response the value that was returned from a controller and
  83. * is not a Response instance
  84. * @param string $format the format for which a formatter has been registered
  85. * @throws \DomainException if format does not match a registered formatter
  86. * @return Response
  87. * @since 9.1.0
  88. */
  89. public function buildResponse($response, $format = 'xml') {
  90. return parent::buildResponse($response, $format);
  91. }
  92. /**
  93. * Unwrap data and build ocs response
  94. * @param string $format json or xml
  95. * @param DataResponse $data the data which should be transformed
  96. * @since 8.1.0
  97. * @return \OC\AppFramework\OCS\BaseResponse
  98. */
  99. private function buildOCSResponse($format, DataResponse $data) {
  100. if ($this->ocsVersion === 1) {
  101. return new \OC\AppFramework\OCS\V1Response($data, $format);
  102. }
  103. return new \OC\AppFramework\OCS\V2Response($data, $format);
  104. }
  105. }