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

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