OCSController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\AppFramework;
  8. use OCP\AppFramework\Http\DataResponse;
  9. use OCP\AppFramework\Http\Response;
  10. use OCP\IRequest;
  11. /**
  12. * Base class to inherit your controllers from that are used for RESTful APIs
  13. * @since 8.1.0
  14. */
  15. abstract class OCSController extends ApiController {
  16. /**
  17. * @since 22.0.0
  18. */
  19. public const RESPOND_UNAUTHORISED = 997;
  20. /**
  21. * @since 22.0.0
  22. */
  23. public const RESPOND_SERVER_ERROR = 996;
  24. /**
  25. * @since 22.0.0
  26. */
  27. public const RESPOND_NOT_FOUND = 998;
  28. /**
  29. * @since 22.0.0
  30. */
  31. public const RESPOND_UNKNOWN_ERROR = 999;
  32. /** @var int */
  33. private $ocsVersion;
  34. /**
  35. * constructor of the controller
  36. * @param string $appName the name of the app
  37. * @param IRequest $request an instance of the request
  38. * @param string $corsMethods comma separated string of HTTP verbs which
  39. * should be allowed for websites or webapps when calling your API, defaults to
  40. * 'PUT, POST, GET, DELETE, PATCH'
  41. * @param string $corsAllowedHeaders comma separated string of HTTP headers
  42. * which should be allowed for websites or webapps when calling your API,
  43. * defaults to 'Authorization, Content-Type, Accept'
  44. * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
  45. * request should be cached, defaults to 1728000 seconds
  46. * @since 8.1.0
  47. */
  48. public function __construct($appName,
  49. IRequest $request,
  50. $corsMethods = 'PUT, POST, GET, DELETE, PATCH',
  51. $corsAllowedHeaders = 'Authorization, Content-Type, Accept, OCS-APIRequest',
  52. $corsMaxAge = 1728000) {
  53. parent::__construct($appName, $request, $corsMethods,
  54. $corsAllowedHeaders, $corsMaxAge);
  55. $this->registerResponder('json', function ($data) {
  56. return $this->buildOCSResponse('json', $data);
  57. });
  58. $this->registerResponder('xml', function ($data) {
  59. return $this->buildOCSResponse('xml', $data);
  60. });
  61. }
  62. /**
  63. * @param int $version
  64. * @since 11.0.0
  65. * @internal
  66. */
  67. public function setOCSVersion($version) {
  68. $this->ocsVersion = $version;
  69. }
  70. /**
  71. * Since the OCS endpoints default to XML we need to find out the format
  72. * again
  73. * @param mixed $response the value that was returned from a controller and
  74. * is not a Response instance
  75. * @param string $format the format for which a formatter has been registered
  76. * @throws \DomainException if format does not match a registered formatter
  77. * @return Response
  78. * @since 9.1.0
  79. */
  80. public function buildResponse($response, $format = 'xml') {
  81. return parent::buildResponse($response, $format);
  82. }
  83. /**
  84. * Unwrap data and build ocs response
  85. * @param string $format json or xml
  86. * @param DataResponse $data the data which should be transformed
  87. * @since 8.1.0
  88. * @return \OC\AppFramework\OCS\BaseResponse
  89. */
  90. private function buildOCSResponse($format, DataResponse $data) {
  91. if ($this->ocsVersion === 1) {
  92. return new \OC\AppFramework\OCS\V1Response($data, $format);
  93. }
  94. return new \OC\AppFramework\OCS\V2Response($data, $format);
  95. }
  96. }