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.7KB

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