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.

OCSController.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. namespace OCP\AppFramework;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\Response;
  31. use OCP\IRequest;
  32. /**
  33. * Base class to inherit your controllers from that are used for RESTful APIs
  34. * @since 8.1.0
  35. */
  36. abstract class OCSController extends ApiController {
  37. /**
  38. * @since 22.0.0
  39. */
  40. public const RESPOND_UNAUTHORISED = 997;
  41. /**
  42. * @since 22.0.0
  43. */
  44. public const RESPOND_SERVER_ERROR = 996;
  45. /**
  46. * @since 22.0.0
  47. */
  48. public const RESPOND_NOT_FOUND = 998;
  49. /**
  50. * @since 22.0.0
  51. */
  52. public const RESPOND_UNKNOWN_ERROR = 999;
  53. /** @var int */
  54. private $ocsVersion;
  55. /**
  56. * constructor of the controller
  57. * @param string $appName the name of the app
  58. * @param IRequest $request an instance of the request
  59. * @param string $corsMethods comma separated string of HTTP verbs which
  60. * should be allowed for websites or webapps when calling your API, defaults to
  61. * 'PUT, POST, GET, DELETE, PATCH'
  62. * @param string $corsAllowedHeaders comma separated string of HTTP headers
  63. * which should be allowed for websites or webapps when calling your API,
  64. * defaults to 'Authorization, Content-Type, Accept'
  65. * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS
  66. * request should be cached, defaults to 1728000 seconds
  67. * @since 8.1.0
  68. */
  69. public function __construct($appName,
  70. IRequest $request,
  71. $corsMethods = 'PUT, POST, GET, DELETE, PATCH',
  72. $corsAllowedHeaders = 'Authorization, Content-Type, Accept, OCS-APIRequest',
  73. $corsMaxAge = 1728000) {
  74. parent::__construct($appName, $request, $corsMethods,
  75. $corsAllowedHeaders, $corsMaxAge);
  76. $this->registerResponder('json', function ($data) {
  77. return $this->buildOCSResponse('json', $data);
  78. });
  79. $this->registerResponder('xml', function ($data) {
  80. return $this->buildOCSResponse('xml', $data);
  81. });
  82. }
  83. /**
  84. * @param int $version
  85. * @since 11.0.0
  86. * @internal
  87. */
  88. public function setOCSVersion($version) {
  89. $this->ocsVersion = $version;
  90. }
  91. /**
  92. * Since the OCS endpoints default to XML we need to find out the format
  93. * again
  94. * @param mixed $response the value that was returned from a controller and
  95. * is not a Response instance
  96. * @param string $format the format for which a formatter has been registered
  97. * @throws \DomainException if format does not match a registered formatter
  98. * @return Response
  99. * @since 9.1.0
  100. */
  101. public function buildResponse($response, $format = 'xml') {
  102. return parent::buildResponse($response, $format);
  103. }
  104. /**
  105. * Unwrap data and build ocs response
  106. * @param string $format json or xml
  107. * @param DataResponse $data the data which should be transformed
  108. * @since 8.1.0
  109. * @return \OC\AppFramework\OCS\BaseResponse
  110. */
  111. private function buildOCSResponse($format, DataResponse $data) {
  112. if ($this->ocsVersion === 1) {
  113. return new \OC\AppFramework\OCS\V1Response($data, $format);
  114. }
  115. return new \OC\AppFramework\OCS\V2Response($data, $format);
  116. }
  117. }