1
0

BaseResponse.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 string[] $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. /**
  112. * @param array $array
  113. * @param \XMLWriter $writer
  114. */
  115. protected function toXML(array $array, \XMLWriter $writer) {
  116. foreach ($array as $k => $v) {
  117. if (\is_string($k) && strpos($k, '@') === 0) {
  118. $writer->writeAttribute(substr($k, 1), $v);
  119. continue;
  120. }
  121. if (\is_numeric($k)) {
  122. $k = 'element';
  123. }
  124. if (\is_array($v)) {
  125. $writer->startElement($k);
  126. $this->toXML($v, $writer);
  127. $writer->endElement();
  128. } else {
  129. $writer->writeElement($k, $v);
  130. }
  131. }
  132. }
  133. public function getOCSStatus() {
  134. return parent::getStatus();
  135. }
  136. }