BaseResponse.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\AppFramework\OCS;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  26. use OCP\AppFramework\Http\Response;
  27. abstract class BaseResponse extends Response {
  28. /** @var array */
  29. protected $data;
  30. /** @var string */
  31. protected $format;
  32. /** @var string */
  33. protected $statusMessage;
  34. /** @var int */
  35. protected $itemsCount;
  36. /** @var int */
  37. protected $itemsPerPage;
  38. /**
  39. * BaseResponse constructor.
  40. *
  41. * @param DataResponse|null $dataResponse
  42. * @param string $format
  43. * @param string|null $statusMessage
  44. * @param int|null $itemsCount
  45. * @param int|null $itemsPerPage
  46. */
  47. public function __construct(DataResponse $dataResponse,
  48. $format = 'xml',
  49. $statusMessage = null,
  50. $itemsCount = null,
  51. $itemsPerPage = null) {
  52. $this->format = $format;
  53. $this->statusMessage = $statusMessage;
  54. $this->itemsCount = $itemsCount;
  55. $this->itemsPerPage = $itemsPerPage;
  56. $this->data = $dataResponse->getData();
  57. $this->setHeaders($dataResponse->getHeaders());
  58. $this->setStatus($dataResponse->getStatus());
  59. $this->setETag($dataResponse->getETag());
  60. $this->setLastModified($dataResponse->getLastModified());
  61. $this->setCookies($dataResponse->getCookies());
  62. $this->setContentSecurityPolicy(new EmptyContentSecurityPolicy());
  63. if ($format === 'json') {
  64. $this->addHeader(
  65. 'Content-Type', 'application/json; charset=utf-8'
  66. );
  67. } else {
  68. $this->addHeader(
  69. 'Content-Type', 'application/xml; charset=utf-8'
  70. );
  71. }
  72. }
  73. /**
  74. * @param string[] $meta
  75. * @return string
  76. */
  77. protected function renderResult($meta) {
  78. // TODO rewrite functions
  79. return \OC_API::renderResult($this->format, $meta, $this->data);
  80. }
  81. public function getOCSStatus() {
  82. return parent::getStatus();
  83. }
  84. }