diff --git a/corese-server/src/main/java/fr/inria/corese/server/webservice/GraphProtocol.java b/corese-server/src/main/java/fr/inria/corese/server/webservice/GraphProtocol.java index f28b6029b..99aaea7e6 100644 --- a/corese-server/src/main/java/fr/inria/corese/server/webservice/GraphProtocol.java +++ b/corese-server/src/main/java/fr/inria/corese/server/webservice/GraphProtocol.java @@ -23,24 +23,25 @@ /** * SPARQL 1.1 Graph Store HTTP Protocol * - * Olivier Corby + * @author Olivier Corby + * @author Pierre Maillot, P16 Wimmics INRIA I3S, 2024 */ @Path("rdf-graph-store") public class GraphProtocol { static private final Logger logger = LogManager.getLogger(GraphProtocol.class); - static final String NAMED_GRAPH_QUERY = "construct {?s ?p ?o} where {graph <%s> {?s ?p ?o}}"; - static final String DEFAULT_GRAPH_QUERY = "construct where {?s ?p ?o}"; + static final String NAMED_GRAPH_QUERY = "CONSTRUCT {?s ?p ?o} WHERE { GRAPH <%s> {?s ?p ?o}}"; + static final String DEFAULT_GRAPH_QUERY = "CONSTRUCT {?s ?p ?o}"; - static final String NAMED_GRAPH_INSERT = "insert data {graph <%s> {%s}}"; - static final String DEFAULT_GRAPH_INSERT = "insert data {%s}"; + static final String NAMED_GRAPH_INSERT = "INSERT DATA { GRAPH <%s> {%s}}"; + static final String DEFAULT_GRAPH_INSERT = "INSERT DATA {%s}"; Response get(HttpServletRequest request, String name, String graph, String pattern, String access, int format) { String query = pattern; if (name != null) { query = String.format(pattern, NSManager.nsm().toNamespace(graph)); } - return new SPARQLRestAPI().myGetResult(request, name, null, null, null, null, query, access, null, null, + return new SPARQLRestAPI().getResultFormat(request, name, null, null, null, null, query, access, null, null, format); } @@ -51,7 +52,7 @@ Response post(HttpServletRequest request, String name, String graph, String patt } else { query = String.format(NAMED_GRAPH_INSERT, NSManager.nsm().toNamespace(graph), pattern); } - return new SPARQLRestAPI().myGetResult(request, name, null, null, null, null, query, access, null, null, + return new SPARQLRestAPI().getResultFormat(request, name, null, null, null, null, query, access, null, null, format); } @@ -80,7 +81,7 @@ public Response getTurtle(@jakarta.ws.rs.core.Context HttpServletRequest request @QueryParam("mode") List mode) { logger.info("getTurtle"); - return get(request, name, graph, getQuery(name), access, ResultFormat.TURTLE_FORMAT); + return get(request, name, graph, getQuery(graph), access, ResultFormat.TURTLE_FORMAT); } @GET @@ -92,7 +93,7 @@ public Response getXML(@jakarta.ws.rs.core.Context HttpServletRequest request, @QueryParam("mode") List mode) { logger.info("getXML"); - return get(request, name, graph, getQuery(name), access, ResultFormat.RDF_XML_FORMAT); + return get(request, name, graph, getQuery(graph), access, ResultFormat.RDF_XML_FORMAT); } @GET @@ -104,58 +105,30 @@ public Response getJSON(@jakarta.ws.rs.core.Context HttpServletRequest request, @QueryParam("mode") List mode) { logger.info("getJSON"); - return get(request, name, graph, getQuery(name), access, ResultFormat.JSONLD_FORMAT); + return get(request, name, graph, getQuery(graph), access, ResultFormat.JSONLD_FORMAT); } @PUT + @Produces({ ResultFormat.SPARQL_RESULTS_XML, ResultFormat.XML}) public Response put(@jakarta.ws.rs.core.Context HttpServletRequest request, @PathParam("name") String name, @QueryParam("graph") String graph, - // @QueryParam("query") - String query, @QueryParam("access") String access, @QueryParam("mode") List mode) { logger.info("put"); - return post(request, name, graph, query, access, ResultFormat.XML_FORMAT); - } - - @PUT - @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public Response put2(@jakarta.ws.rs.core.Context HttpServletRequest request, - @PathParam("name") String name, - @FormParam("graph") String graph, - @FormParam("query") String query, - @FormParam("access") String access, - @FormParam("mode") List mode) { - - logger.info(String.format("put2: graph %s", graph)); - return post(request, name, graph, query, access, ResultFormat.XML_FORMAT); + return post(request, name, graph, getQuery(graph), access, ResultFormat.XML_FORMAT); } @POST public Response post(@jakarta.ws.rs.core.Context HttpServletRequest request, @PathParam("name") String name, @QueryParam("graph") String graph, - @QueryParam("query") String query, @QueryParam("access") String access, @QueryParam("mode") List mode) { logger.info("post"); - return post(request, name, graph, query, access, ResultFormat.XML_FORMAT); - } - - @POST - @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public Response post2(@jakarta.ws.rs.core.Context HttpServletRequest request, - @PathParam("name") String name, - @FormParam("graph") String graph, - @FormParam("query") String query, - @FormParam("access") String access, - @FormParam("mode") List mode) { - - logger.info(String.format("post2: graph %s", graph)); - return post(request, name, graph, query, access, ResultFormat.XML_FORMAT); + return post(request, name, graph, getQuery(graph), access, ResultFormat.XML_FORMAT); } } diff --git a/corese-server/src/test/java/fr/inria/corese/server/webservice/GraphStoreProtocolTest.java b/corese-server/src/test/java/fr/inria/corese/server/webservice/GraphStoreProtocolTest.java index a50e48c6f..8f70e624d 100644 --- a/corese-server/src/test/java/fr/inria/corese/server/webservice/GraphStoreProtocolTest.java +++ b/corese-server/src/test/java/fr/inria/corese/server/webservice/GraphStoreProtocolTest.java @@ -3,6 +3,7 @@ import static fr.inria.corese.core.print.ResultFormat.TURTLE_TEXT; import static fr.inria.corese.core.api.Loader.TURTLE_FORMAT; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.BufferedReader; @@ -174,7 +175,7 @@ public void getGraphStoreProtocolWithUnknownGraph() throws Exception{ @Test public void putGraphStoreProtocolNewGraph() throws Exception { - boolean absenceTest = ! SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH { a } }"); + boolean absenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH { a } }"); List> headers = new LinkedList<>(); List acceptHeader = new LinkedList<>(); @@ -194,9 +195,9 @@ public void putGraphStoreProtocolNewGraph() throws Exception { boolean presenceTest = SPARQLTestUtils.sendSPARQLAsk("ASK { GRAPH { a } }"); + assertFalse(absenceTest); assertEquals(201, status); assertTrue(presenceTest); - assertTrue(absenceTest); } @Test