-
Notifications
You must be signed in to change notification settings - Fork 11
/
ApixMiddleware.php
127 lines (110 loc) · 3.6 KB
/
ApixMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Islandora\Crayfish\Commons;
use Islandora\Chullo\IFedoraApi;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Retrieves a Fedora resource using the Apix-Ldp-Resource header.
*
* @package Islandora\Crayfish\Commons
*/
class ApixMiddleware implements EventSubscriberInterface
{
/**
* @var \Islandora\Chullo\IFedoraApi
*/
protected IFedoraApi $api;
/**
* @var null|\Psr\Log\LoggerInterface
*/
protected ?LoggerInterface $log;
/**
* ApixFedoraResourceRetriever constructor.
* @param \Islandora\Chullo\IFedoraApi $api
* @param \Psr\Log\LoggerInterface $log
*/
public function __construct(
IFedoraApi $api,
LoggerInterface $log
) {
$this->api = $api;
$this->log = $log;
}
/**
* The steps to take when a subscribed request comes in.
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*/
public function before(RequestEvent $event)
{
$request = $event->getRequest();
// Short circuit if this is an OPTIONS or HEAD request.
if (in_array(
strtoupper($request->getMethod()),
['OPTIONS', 'HEAD']
)) {
return;
}
// Short circuit if there's no Apix-Ldp-Resource header.
if (!$request->headers->has("Apix-Ldp-Resource")) {
$this->log->debug("No Apix-Ldp-Resource header present, no fedora_resource set");
$request->attributes->set('fedora_resource', false);
$event->setResponse(new Response(
"Malformed request, no Apix-Ldp-Resource header present",
400
));
return;
}
// Get the resource.
$fedora_resource = $this->getFedoraResource($request);
// Short circuit if the Fedora response is not 200.
$status = $fedora_resource->getStatusCode();
if ($status != 200) {
$this->log->debug("Fedora Resource: ", [
'body' => $fedora_resource->getBody(),
'status' => $fedora_resource->getStatusCode(),
'headers' => $fedora_resource->getHeaders()
]);
$event->setResponse(new Response(
$fedora_resource->getReasonPhrase(),
$status
));
return;
}
// Set the Fedora resource on the request.
$request->attributes->set('fedora_resource', $fedora_resource);
}
/**
* Get the Fedora Resource defined in the Request.
* @param \Symfony\Component\HttpFoundation\Request $request The request.
* @return \Psr\Http\Message\ResponseInterface The response with the resource.
*/
protected function getFedoraResource(Request $request): ResponseInterface
{
// Pass along auth headers if present.
$headers = [];
if ($request->headers->has("Authorization")) {
$headers['Authorization'] = $request->headers->get("Authorization");
}
$uri = $request->headers->get("Apix-Ldp-Resource");
return $this->api->getResource(
$uri,
$headers
);
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['before', 0],
],
];
}
}