Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
authorymh <ymh.work@gmail.com>
Tue, 19 Jan 2016 19:18:34 +0100
changeset 109 d22ed5792f8e
parent 108 be2d3b30b2e0
child 110 8e5fcebb9018
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
server/src/app/Libraries/Sparql/SparqlClient.php
server/src/tests/Repositories/DocumentRepositoryIntegrationTest.php
server/src/tests/libraries/Sparql/SparqlClientTest.php
--- a/server/src/app/Libraries/Sparql/SparqlClient.php	Tue Jan 19 19:16:23 2016 +0100
+++ b/server/src/app/Libraries/Sparql/SparqlClient.php	Tue Jan 19 19:18:34 2016 +0100
@@ -5,6 +5,7 @@
 use CorpusParole\Libraries\CorpusParoleException;
 use Config;
 use Log;
+use GuzzleHttp\Psr7\Request;
 
 /**
  *
@@ -38,7 +39,7 @@
         }
         //Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri'));
         $sesameRepository = config('corpusparole.sesame_repository');
-        $resp = $this->getHttpClient()->post("$sesameRepository/transactions");
+        $resp = $this->getHttpClient()->post("$sesameRepository/transactions", ['query' => ['isolation-level' => 'http://www.openrdf.org/schema/sesame#SNAPSHOT_READ']]);
         //$resp = $this->getHttpClient()->post('transactions');
         //TODO check errors
         if($resp->getStatusCode() != 201) {
@@ -97,28 +98,26 @@
     protected function updateData($operation, Graph $graph)
     {
         $graphUri = $graph->getUri();
-        $query = "$operation DATA {";
+
+        $query = $graph->serialise('ntriples');
         if ($graphUri) {
-            $query .= "GRAPH <$graphUri> {";
+            $query = "GRAPH <$graphUri> { $query }";
         }
-        $query .= $graph->serialise('ntriples');
-        if ($graphUri) {
-            $query .= "}";
-        }
-        $query .= '}';
+        $query = "$operation DATA { $query }";
+
 
+        // doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view
+        // cf. bug : https://openrdf.atlassian.net/browse/SES-2295
+        // and PR https://bitbucket.org/openrdf/sesame/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x
+        // The put form has been chosen over the post, because it seems that this is the form choosed in the sesame http client
         $resp = $this->getHttpClient()->put(
             $this->currentTransactionUrl, [
+                'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"],
                 'query' => ['action' => 'UPDATE'],
-                'form_params'=> ['update' => $query],
+                'body' => $query,
             ]
         );
-        if($resp->getStatusCode() != 204) {
-            throw new CorpusParoleException("Could not update in transaction with operation $operation: "
-                . $resp->getStatusCode() . " - "
-                . $resp->getReasonPhrase() . " : " . $resp->getBody()
-                . " : $query" , 1);
-        }
+
     }
 
     public function add(Graph $graph) {
--- a/server/src/tests/Repositories/DocumentRepositoryIntegrationTest.php	Tue Jan 19 19:16:23 2016 +0100
+++ b/server/src/tests/Repositories/DocumentRepositoryIntegrationTest.php	Tue Jan 19 19:18:34 2016 +0100
@@ -124,7 +124,7 @@
     }
 
     public function tearDown() {
-        $this->httpClient->delete("repositories/$this->sesameRepository");
+        //$this->httpClient->delete("repositories/$this->sesameRepository");
         parent::tearDown();
     }
 
--- a/server/src/tests/libraries/Sparql/SparqlClientTest.php	Tue Jan 19 19:16:23 2016 +0100
+++ b/server/src/tests/libraries/Sparql/SparqlClientTest.php	Tue Jan 19 19:18:34 2016 +0100
@@ -86,7 +86,7 @@
 
         $this->assertCount(1, $container, 'One request');
         $req = $container[0]['request'];
-        $this->assertEquals("$this->sesameRepository/transactions", (string)$req->getUri(), "url must be ok");
+        $this->assertEquals("$this->sesameRepository/transactions?isolation-level=http%3A%2F%2Fwww.openrdf.org%2Fschema%2Fsesame%23SNAPSHOT_READ", (string)$req->getUri(), "url must be ok");
         $this->assertEquals('POST', $container[0]['request']->getMethod(), "methos is POST");
     }
 
@@ -180,16 +180,14 @@
 
         $this->assertEquals($this->transactionUrl."?action=UPDATE", (string)$req->getUri(), "uri must be the transaction url");
         $this->assertEquals('PUT', $req->getMethod(), "Method must be PUT");
-        $this->assertEquals(['application/x-www-form-urlencoded'], $req->getHeader('Content-type'), "content type must be form urlencoded");
+        $this->assertEquals(['application/sparql-update; charset=utf-8'], $req->getHeader('Content-type'), "content type must be form urlencoded");
 
-        $data = [];
-        parse_str($req->getBody(), $data);
+        $body = (string)$req->getBody();
 
-        $this->assertArrayHasKey('update', $data, "Submitted data must have updarte parameter");
-        $this->assertContains('INSERT DATA {', $data['update'], 'update parameter must contain INSERT');
-        $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $data['update'], 'update parameter must contain GRAPH id');
-        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $data['update'], 'update parameter must contain dialogue');
-        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "drama"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> .', $data['update'], 'update parameter must contain drama');
+        $this->assertContains('INSERT DATA {', $body, 'update parameter must contain INSERT');
+        $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $body, 'update parameter must contain GRAPH id');
+        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $body, 'update parameter must contain dialogue');
+        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "drama"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> .', $body, 'update parameter must contain drama');
     }
 
     public function testDelete() {
@@ -213,15 +211,13 @@
 
         $this->assertEquals($this->transactionUrl."?action=UPDATE", (string)$req->getUri(), "uri must be the transaction url");
         $this->assertEquals('PUT', $req->getMethod(), "Method must be PUT");
-        $this->assertEquals(['application/x-www-form-urlencoded'], $req->getHeader('Content-type'), "content type must be form urlencoded");
+        $this->assertEquals(['application/sparql-update; charset=utf-8'], $req->getHeader('Content-type'), "content type must be form urlencoded");
 
-        $data = [];
-        parse_str($req->getBody(), $data);
+        $body = (string)$req->getBody();
 
-        $this->assertArrayHasKey('update', $data, "Submitted data must have updarte parameter");
-        $this->assertContains('DELETE DATA {', $data['update'], 'update parameter must contain DELETE');
-        $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $data['update'], 'update parameter must contain GRAPH id');
-        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $data['update'], 'update parameter must contain GRAPH id');
+        $this->assertContains('DELETE DATA {', $body, 'update parameter must contain DELETE');
+        $this->assertContains('GRAPH <'.$this->addGraph->getUri().'> {', $body, 'update parameter must contain GRAPH id');
+        $this->assertContains('<http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> <http://purl.org/dc/elements/1.1/type> "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type>', $body, 'update parameter must contain GRAPH id');
 
     }