BaseResponse.php 4.5 KB

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