OCSController.php 3.6 KB

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