RequestManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Http\WellKnown;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\ServiceRegistration;
  10. use OCP\AppFramework\QueryException;
  11. use OCP\Http\WellKnown\IHandler;
  12. use OCP\Http\WellKnown\IRequestContext;
  13. use OCP\Http\WellKnown\IResponse;
  14. use OCP\Http\WellKnown\JrdResponse;
  15. use OCP\IRequest;
  16. use OCP\IServerContainer;
  17. use Psr\Log\LoggerInterface;
  18. use RuntimeException;
  19. use function array_reduce;
  20. class RequestManager {
  21. /** @var Coordinator */
  22. private $coordinator;
  23. /** @var IServerContainer */
  24. private $container;
  25. /** @var LoggerInterface */
  26. private $logger;
  27. public function __construct(Coordinator $coordinator,
  28. IServerContainer $container,
  29. LoggerInterface $logger) {
  30. $this->coordinator = $coordinator;
  31. $this->container = $container;
  32. $this->logger = $logger;
  33. }
  34. public function process(string $service, IRequest $request): ?IResponse {
  35. $handlers = $this->loadHandlers();
  36. $context = new class($request) implements IRequestContext {
  37. /** @var IRequest */
  38. private $request;
  39. public function __construct(IRequest $request) {
  40. $this->request = $request;
  41. }
  42. public function getHttpRequest(): IRequest {
  43. return $this->request;
  44. }
  45. };
  46. $subject = $request->getParam('resource');
  47. $initialResponse = new JrdResponse($subject ?? '');
  48. $finalResponse = array_reduce($handlers, function (?IResponse $previousResponse, IHandler $handler) use ($context, $service) {
  49. return $handler->handle($service, $context, $previousResponse);
  50. }, $initialResponse);
  51. if ($finalResponse instanceof JrdResponse && $finalResponse->isEmpty()) {
  52. return null;
  53. }
  54. return $finalResponse;
  55. }
  56. /**
  57. * @return IHandler[]
  58. */
  59. private function loadHandlers(): array {
  60. $context = $this->coordinator->getRegistrationContext();
  61. if ($context === null) {
  62. throw new RuntimeException('Well known handlers requested before the apps had been fully registered');
  63. }
  64. $registrations = $context->getWellKnownHandlers();
  65. $this->logger->debug(count($registrations) . ' well known handlers registered');
  66. return array_filter(
  67. array_map(function (ServiceRegistration $registration) {
  68. /** @var ServiceRegistration<IHandler> $registration */
  69. $class = $registration->getService();
  70. try {
  71. $handler = $this->container->get($class);
  72. if (!($handler) instanceof IHandler) {
  73. $this->logger->error("Well known handler $class is invalid");
  74. return null;
  75. }
  76. return $handler;
  77. } catch (QueryException $e) {
  78. $this->logger->error("Could not load well known handler $class", [
  79. 'exception' => $e,
  80. 'app' => $registration->getAppId(),
  81. ]);
  82. return null;
  83. }
  84. }, $registrations)
  85. );
  86. }
  87. }