2
2
3
3
namespace Pdsinterop \Solid \Resources ;
4
4
5
+ use Pdsinterop \Solid \SolidNotifications \SolidNotificationsInterface ;
5
6
use EasyRdf \Exception as RdfException ;
6
7
use EasyRdf \Graph as Graph ;
7
8
use Laminas \Diactoros \ServerRequest ;
11
12
use Psr \Http \Message \ResponseInterface as Response ;
12
13
use Psr \Http \Message \ServerRequestInterface as Request ;
13
14
use Throwable ;
14
- use WebSocket \Client ;
15
15
use pietercolpaert \hardf \TriGWriter ;
16
16
use pietercolpaert \hardf \TriGParser ;
17
17
@@ -37,6 +37,10 @@ class Server
37
37
private const MIME_TYPE_DIRECTORY = 'directory ' ;
38
38
private const QUERY_PARAM_HTTP_METHOD = 'http-method ' ;
39
39
40
+ private const NOTIFICATION_TYPE_CREATE = "Create " ;
41
+ private const NOTIFICATION_TYPE_UPDATE = "Update " ;
42
+ private const NOTIFICATION_TYPE_DELETE = "Delete " ;
43
+
40
44
/** @var string[] */
41
45
private $ availableMethods = [
42
46
'DELETE ' ,
@@ -55,8 +59,8 @@ class Server
55
59
private $ filesystem ;
56
60
/** @var Graph */
57
61
private $ graph ;
58
- /** @var string */
59
- private $ pubsub ;
62
+ /** @var SolidNotificationsInterface */
63
+ private $ notifications ;
60
64
/** @var Response */
61
65
private $ response ;
62
66
@@ -85,19 +89,17 @@ final public function setBaseUrl($url)
85
89
$ this ->basePath = $ serverRequest ->getUri ()->getPath ();
86
90
}
87
91
88
- final public function setPubSubUrl ( $ url )
92
+ final public function setNotifications ( SolidNotificationsInterface $ notifications )
89
93
{
90
- $ this ->pubsub = $ url ;
94
+ $ this ->notifications = $ notifications ;
91
95
}
92
-
93
96
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
94
97
95
98
// @TODO: The Graph should be injected by the caller
96
99
final public function __construct (Filesystem $ filesystem , Response $ response , Graph $ graph = null )
97
100
{
98
101
$ this ->basePath = '' ;
99
102
$ this ->baseUrl = '' ;
100
- $ this ->pubsub = '' ;
101
103
$ this ->filesystem = $ filesystem ;
102
104
$ this ->graph = $ graph ?? new Graph ();
103
105
$ this ->response = $ response ;
@@ -152,17 +154,6 @@ private function handle(string $method, string $path, $contents, $request): Resp
152
154
// Lets assume the worst...
153
155
$ response = $ response ->withStatus (500 );
154
156
155
- // Set Accept, Allow, and CORS headers
156
- // $response = $response
157
- // ->withHeader('Access-Control-Allow-Origin', '*')
158
- // ->withHeader('Access-Control-Allow-Credentials','true')
159
- // ->withHeader('Access-Control-Allow-Headers', '*, authorization, accept, content-type')
160
- // @FIXME: Add correct headers to resources (for instance allow DELETE on a GET resource)
161
- // ->withAddedHeader('Accept-Patch', 'text/ldpatch')
162
- // ->withAddedHeader('Accept-Post', 'text/turtle, application/ld+json, image/bmp, image/jpeg')
163
- // ->withHeader('Allow', 'GET, HEAD, OPTIONS, PATCH, POST, PUT')
164
- //;
165
-
166
157
switch ($ method ) {
167
158
case 'DELETE ' :
168
159
$ response = $ this ->handleDeleteRequest ($ response , $ path , $ contents );
@@ -175,9 +166,6 @@ private function handle(string $method, string $path, $contents, $request): Resp
175
166
$ response ->getBody ()->rewind ();
176
167
$ response ->getBody ()->write ('' );
177
168
$ response = $ response ->withStatus (204 ); // CHECKME: nextcloud will remove the updates-via header - any objections to give the 'HEAD' request a 'no content' response type?
178
- if ($ this ->pubsub ) {
179
- $ response = $ response ->withHeader ("updates-via " , $ this ->pubsub );
180
- }
181
169
}
182
170
break ;
183
171
@@ -350,7 +338,7 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
350
338
351
339
if ($ success ) {
352
340
$ this ->removeLinkFromMetaFileFor ($ path );
353
- $ this ->sendWebsocketUpdate ($ path );
341
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_UPDATE );
354
342
}
355
343
} catch (RdfException $ exception ) {
356
344
$ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
@@ -501,7 +489,7 @@ private function handleN3Update(Response $response, string $path, $contents): Re
501
489
502
490
if ($ success ) {
503
491
$ this ->removeLinkFromMetaFileFor ($ path );
504
- $ this ->sendWebsocketUpdate ($ path );
492
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_UPDATE );
505
493
}
506
494
} catch (RdfException $ exception ) {
507
495
$ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
@@ -551,7 +539,7 @@ private function handleCreateRequest(Response $response, string $path, $contents
551
539
$ this ->removeLinkFromMetaFileFor ($ path );
552
540
$ response = $ response ->withHeader ("Location " , $ this ->baseUrl . $ path );
553
541
$ response = $ response ->withStatus (201 );
554
- $ this ->sendWebsocketUpdate ($ path );
542
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_CREATE );
555
543
} else {
556
544
$ response = $ response ->withStatus (500 );
557
545
}
@@ -585,39 +573,21 @@ private function handleCreateDirectoryRequest(Response $response, string $path):
585
573
$ response = $ response ->withStatus ($ success ? 201 : 500 );
586
574
if ($ success ) {
587
575
$ this ->removeLinkFromMetaFileFor ($ path );
588
- $ this ->sendWebsocketUpdate ($ path );
576
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_CREATE );
589
577
}
590
578
}
591
579
592
580
return $ response ;
593
581
}
594
582
595
- private function sendWebsocketUpdate ($ path )
583
+ private function sendNotificationUpdate ($ path, $ type )
596
584
{
597
- $ pubsub = $ this ->pubsub ;
598
- if (!$ pubsub ) {
599
- return ; // no pubsub server available, don't even try;
600
- }
601
-
602
- $ pubsub = str_replace (["https:// " , "http:// " ], "ws:// " , $ pubsub );
603
-
604
585
$ baseUrl = $ this ->baseUrl ;
586
+ $ this ->notifications ->send ($ baseUrl . $ path , $ type );
605
587
606
- $ client = new Client ($ pubsub , array (
607
- 'headers ' => array (
608
- 'Sec-WebSocket-Protocol ' => 'solid-0.1 '
609
- )
610
- ));
611
-
612
- try {
613
- $ client ->send ("pub $ baseUrl$ path \n" );
614
-
615
- while ($ path !== "/ " ) {
616
- $ path = $ this ->parentPath ($ path );
617
- $ client ->send ("pub $ baseUrl$ path \n" );
618
- }
619
- } catch (\WebSocket \Exception $ exception ) {
620
- throw new Exception ('Could not write to pub-sup server ' , 502 , $ exception );
588
+ while ($ path !== "/ " ) {
589
+ $ path = $ this ->parentPath ($ path );
590
+ $ this ->notifications ->send ($ baseUrl . $ path , self ::NOTIFICATION_TYPE_UPDATE ); // checkme: delete on a directory triggers update notifications on parents
621
591
}
622
592
}
623
593
@@ -637,15 +607,15 @@ private function handleDeleteRequest(Response $response, string $path, $contents
637
607
} else {
638
608
$ success = $ filesystem ->deleteDir ($ path );
639
609
if ($ success ) {
640
- $ this ->sendWebsocketUpdate ($ path );
610
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_DELETE );
641
611
}
642
612
643
613
$ status = $ success ? 204 : 500 ;
644
614
}
645
615
} else {
646
616
$ success = $ filesystem ->delete ($ path );
647
617
if ($ success ) {
648
- $ this ->sendWebsocketUpdate ($ path );
618
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_DELETE );
649
619
}
650
620
$ status = $ success ? 204 : 500 ;
651
621
}
@@ -673,7 +643,7 @@ private function handleUpdateRequest(Response $response, string $path, string $c
673
643
$ response = $ response ->withStatus ($ success ? 201 : 500 );
674
644
if ($ success ) {
675
645
$ this ->removeLinkFromMetaFileFor ($ path );
676
- $ this ->sendWebsocketUpdate ($ path );
646
+ $ this ->sendNotificationUpdate ($ path, self :: NOTIFICATION_TYPE_UPDATE );
677
647
}
678
648
}
679
649
0 commit comments