BaseResponse.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  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
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\AppFramework\OCS;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\Response;
  31. abstract class BaseResponse extends Response {
  32. /** @var array */
  33. protected $data;
  34. /** @var string */
  35. protected $format;
  36. /** @var ?string */
  37. protected $statusMessage;
  38. /** @var ?int */
  39. protected $itemsCount;
  40. /** @var ?int */
  41. protected $itemsPerPage;
  42. /**
  43. * BaseResponse constructor.
  44. *
  45. * @param DataResponse $dataResponse
  46. * @param string $format
  47. * @param string|null $statusMessage
  48. * @param int|null $itemsCount
  49. * @param int|null $itemsPerPage
  50. */
  51. public function __construct(DataResponse $dataResponse,
  52. $format = 'xml',
  53. $statusMessage = null,
  54. $itemsCount = null,
  55. $itemsPerPage = null) {
  56. parent::__construct();
  57. $this->format = $format;
  58. $this->statusMessage = $statusMessage;
  59. $this->itemsCount = $itemsCount;
  60. $this->itemsPerPage = $itemsPerPage;
  61. $this->data = $dataResponse->getData();
  62. $this->setHeaders($dataResponse->getHeaders());
  63. $this->setStatus($dataResponse->getStatus());
  64. $this->setETag($dataResponse->getETag());
  65. $this->setLastModified($dataResponse->getLastModified());
  66. $this->setCookies($dataResponse->getCookies());
  67. if ($dataResponse->isThrottled()) {
  68. $throttleMetadata = $dataResponse->getThrottleMetadata();
  69. $this->throttle($throttleMetadata);
  70. }
  71. if ($format === 'json') {
  72. $this->addHeader(
  73. 'Content-Type', 'application/json; charset=utf-8'
  74. );
  75. } else {
  76. $this->addHeader(
  77. 'Content-Type', 'application/xml; charset=utf-8'
  78. );
  79. }
  80. }
  81. /**
  82. * @param array<string,string|int> $meta
  83. * @return string
  84. */
  85. protected function renderResult(array $meta): string {
  86. $status = $this->getStatus();
  87. if ($status === Http::STATUS_NO_CONTENT ||
  88. $status === Http::STATUS_NOT_MODIFIED ||
  89. ($status >= 100 && $status <= 199)) {
  90. // Those status codes are not supposed to have a body:
  91. // https://stackoverflow.com/q/8628725
  92. return '';
  93. }
  94. $response = [
  95. 'ocs' => [
  96. 'meta' => $meta,
  97. 'data' => $this->data,
  98. ],
  99. ];
  100. if ($this->format === 'json') {
  101. return json_encode($response, JSON_HEX_TAG);
  102. }
  103. $writer = new \XMLWriter();
  104. $writer->openMemory();
  105. $writer->setIndent(true);
  106. $writer->startDocument();
  107. $this->toXML($response, $writer);
  108. $writer->endDocument();
  109. return $writer->outputMemory(true);
  110. }
  111. protected function toXML(array $array, \XMLWriter $writer): void {
  112. foreach ($array as $k => $v) {
  113. if ($k === '@attributes' && is_array($v)) {
  114. foreach ($v as $k2 => $v2) {
  115. $writer->writeAttribute($k2, $v2);
  116. }
  117. continue;
  118. }
  119. if (\is_string($k) && str_starts_with($k, '@')) {
  120. $writer->writeAttribute(substr($k, 1), $v);
  121. continue;
  122. }
  123. if (\is_numeric($k)) {
  124. $k = 'element';
  125. }
  126. if (\is_array($v)) {
  127. $writer->startElement($k);
  128. $this->toXML($v, $writer);
  129. $writer->endElement();
  130. } else {
  131. $writer->writeElement($k, $v);
  132. }
  133. }
  134. }
  135. public function getOCSStatus() {
  136. return parent::getStatus();
  137. }
  138. }