JENA-491 Fuseki Support: always use exeConstructQuads() at the server side, and discards ResponseModel.
Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/75a7a77c Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/75a7a77c Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/75a7a77c Branch: refs/heads/master Commit: 75a7a77c9a92662a88c62dca5d80f85c1c478b78 Parents: de1d1c3 Author: confidencesun <[email protected]> Authored: Tue Aug 18 23:23:09 2015 +0800 Committer: confidencesun <[email protected]> Committed: Tue Aug 18 23:23:09 2015 +0800 ---------------------------------------------------------------------- .../main/java/org/apache/jena/fuseki/DEF.java | 17 ++- .../jena/fuseki/servlets/ResponseDataset.java | 28 +++- .../jena/fuseki/servlets/ResponseModel.java | 135 ------------------- .../jena/fuseki/servlets/SPARQL_Query.java | 25 +--- .../java/org/apache/jena/fuseki/TestQuery.java | 12 +- 5 files changed, 54 insertions(+), 163 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/75a7a77c/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/DEF.java ---------------------------------------------------------------------- diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/DEF.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/DEF.java index 953b724..46ef3be 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/DEF.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/DEF.java @@ -31,11 +31,24 @@ public class DEF public static final AcceptList jsonOffer = AcceptList.create(contentTypeJSON) ; - public static final AcceptList pureRdfOffer = AcceptList.create(contentTypeTurtle, + public static final AcceptList constructOffer = AcceptList.create(contentTypeTurtle, contentTypeTurtleAlt1, contentTypeTurtleAlt2, contentTypeNTriples, - contentTypeRDFXML + contentTypeNTriplesAlt, + contentTypeRDFXML, + contentTypeTriX, + contentTypeTriXxml, + contentTypeJSONLD, + contentTypeRDFJSON, + contentTypeRDFThrift, + + contentTypeTriG, + contentTypeTriGAlt1, + contentTypeTriGAlt2, + contentTypeNQuads, + contentTypeNQuadsAlt1, + contentTypeNQuadsAlt2 ) ; public static final AcceptList rdfOffer = AcceptList.create(contentTypeTurtle, http://git-wip-us.apache.org/repos/asf/jena/blob/75a7a77c/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseDataset.java ---------------------------------------------------------------------- diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseDataset.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseDataset.java index 91d615d..5242f4b 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseDataset.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseDataset.java @@ -19,6 +19,8 @@ package org.apache.jena.fuseki.servlets; import static org.apache.jena.riot.WebContent.charsetUTF8; +import static org.apache.jena.riot.WebContent.contentTypeNQuads; +import static org.apache.jena.riot.WebContent.contentTypeTriG; import static org.apache.jena.riot.WebContent.contentTypeJSONLD; import static org.apache.jena.riot.WebContent.contentTypeNTriples; import static org.apache.jena.riot.WebContent.contentTypeRDFJSON; @@ -41,7 +43,6 @@ import org.apache.jena.query.Dataset; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; import org.apache.jena.riot.RDFLanguages; -import org.apache.jena.riot.WebContent; import org.apache.jena.web.HttpSC; public class ResponseDataset @@ -49,6 +50,13 @@ public class ResponseDataset // Short names for "output=" private static final String contentOutputTriG = "trig" ; private static final String contentOutputNQuads = "n-quads" ; + private static final String contentOutputJSONLD = "json-ld" ; + private static final String contentOutputJSONRDF = "json-rdf" ; + private static final String contentOutputJSON = "json" ; + private static final String contentOutputXML = "xml" ; + private static final String contentOutputText = "text" ; + private static final String contentOutputTTL = "ttl" ; + private static final String contentOutputNT = "nt" ; public static Map<String,String> shortNamesModel = new HashMap<String, String>() ; @@ -56,8 +64,15 @@ public class ResponseDataset // Some short names. keys are lowercase. - ResponseOps.put(shortNamesModel, contentOutputNQuads, WebContent.contentTypeNQuads) ; - ResponseOps.put(shortNamesModel, contentOutputTriG, WebContent.contentTypeTriG) ; + ResponseOps.put(shortNamesModel, contentOutputNQuads, contentTypeNQuads) ; + ResponseOps.put(shortNamesModel, contentOutputTriG, contentTypeTriG) ; + ResponseOps.put(shortNamesModel, contentOutputJSONLD, contentTypeJSONLD) ; + ResponseOps.put(shortNamesModel, contentOutputJSONRDF, contentTypeRDFJSON) ; + ResponseOps.put(shortNamesModel, contentOutputJSON, contentTypeJSONLD) ; + ResponseOps.put(shortNamesModel, contentOutputXML, contentTypeRDFXML) ; + ResponseOps.put(shortNamesModel, contentOutputText, contentTypeTurtle) ; + ResponseOps.put(shortNamesModel, contentOutputTTL, contentTypeTurtle) ; + ResponseOps.put(shortNamesModel, contentOutputNT, contentTypeNTriples) ; } public static void doResponseDataset(HttpAction action, Dataset dataset) @@ -68,7 +83,7 @@ public class ResponseDataset String mimeType = null ; // Header request type // TODO Use MediaType throughout. - MediaType i = ConNeg.chooseContentType(request, DEF.quadsOffer, DEF.acceptNQuads) ; + MediaType i = ConNeg.chooseContentType(request, DEF.constructOffer, DEF.acceptNQuads) ; if ( i != null ) mimeType = i.getContentType() ; @@ -122,7 +137,10 @@ public class ResponseDataset ResponseResultSet.setHttpResponse(action, contentType, charset) ; response.setStatus(HttpSC.OK_200) ; ServletOutputStream out = response.getOutputStream() ; - RDFDataMgr.write(out, dataset, lang) ; + if (RDFLanguages.isQuads(lang)) + RDFDataMgr.write(out, dataset, lang) ; + else + RDFDataMgr.write(out, dataset.getDefaultModel(), lang) ; out.flush() ; } catch (Exception ex) { http://git-wip-us.apache.org/repos/asf/jena/blob/75a7a77c/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseModel.java ---------------------------------------------------------------------- diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseModel.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseModel.java deleted file mode 100644 index 68cf016..0000000 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ResponseModel.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.jena.fuseki.servlets; - -import java.util.HashMap ; -import java.util.Map ; - -import javax.servlet.ServletOutputStream ; -import javax.servlet.http.HttpServletRequest ; -import javax.servlet.http.HttpServletResponse ; - -import org.apache.jena.atlas.web.MediaType ; -import org.apache.jena.fuseki.DEF ; -import org.apache.jena.fuseki.Fuseki ; -import org.apache.jena.fuseki.conneg.ConNeg ; -import org.apache.jena.fuseki.conneg.WebLib ; -import org.apache.jena.rdf.model.Model ; -import org.apache.jena.riot.Lang ; -import org.apache.jena.riot.RDFDataMgr ; -import org.apache.jena.riot.RDFLanguages ; -import static org.apache.jena.riot.WebContent.* ; -import org.apache.jena.web.HttpSC ; - -public class ResponseModel -{ - // Short names for "output=" - private static final String contentOutputJSONLD = "json-ld" ; - private static final String contentOutputJSONRDF = "json-rdf" ; - private static final String contentOutputJSON = "json" ; - private static final String contentOutputXML = "xml" ; - private static final String contentOutputText = "text" ; - private static final String contentOutputTTL = "ttl" ; - private static final String contentOutputNT = "nt" ; - - public static Map<String,String> shortNamesModel = new HashMap<String, String>() ; - static { - - // Some short names. keys are lowercase. - ResponseOps.put(shortNamesModel, contentOutputJSONLD, contentTypeJSONLD) ; - ResponseOps.put(shortNamesModel, contentOutputJSONRDF, contentTypeRDFJSON) ; - ResponseOps.put(shortNamesModel, contentOutputJSON, contentTypeJSONLD) ; - ResponseOps.put(shortNamesModel, contentOutputXML, contentTypeRDFXML) ; - ResponseOps.put(shortNamesModel, contentOutputText, contentTypeTurtle) ; - ResponseOps.put(shortNamesModel, contentOutputTTL, contentTypeTurtle) ; - ResponseOps.put(shortNamesModel, contentOutputNT, contentTypeNTriples) ; - } - - public static void doResponseModel(HttpAction action, Model model) - { - HttpServletRequest request = action.request ; - HttpServletResponse response = action.response ; - - String mimeType = null ; // Header request type - - // TODO Use MediaType throughout. - MediaType i = ConNeg.chooseContentType(request, DEF.rdfOffer, DEF.acceptRDFXML) ; - if ( i != null ) - mimeType = i.getContentType() ; - - String outputField = ResponseOps.paramOutput(request, shortNamesModel) ; - if ( outputField != null ) - mimeType = outputField ; - - String writerMimeType = mimeType ; - - if ( mimeType == null ) - { - Fuseki.actionLog.warn("Can't find MIME type for response") ; - String x = WebLib.getAccept(request) ; - String msg ; - if ( x == null ) - msg = "No Accept: header" ; - else - msg = "Accept: "+x+" : Not understood" ; - ServletOps.error(HttpSC.NOT_ACCEPTABLE_406, msg) ; - } - - String contentType = mimeType ; - String charset = charsetUTF8 ; - - String forceAccept = ResponseOps.paramForceAccept(request) ; - if ( forceAccept != null ) - { - contentType = forceAccept ; - charset = charsetUTF8 ; - } - - Lang lang = RDFLanguages.contentTypeToLang(contentType) ; - if ( lang == null ) - ServletOps.errorBadRequest("Can't determine output content type: "+contentType) ; - -// if ( rdfw instanceof RDFXMLWriterI ) -// rdfw.setProperty("showXmlDeclaration", "true") ; - - // // Write locally to check it's possible. - // // Time/space tradeoff. - // try { - // OutputStream out = new NullOutputStream() ; - // RDFDataMgr.write(out, model, lang) ; - // IO.flush(out) ; - // } catch (JenaException ex) - // { - // SPARQL_ServletBase.errorOccurred(ex) ; - // } - - try { - ResponseResultSet.setHttpResponse(action, contentType, charset) ; - response.setStatus(HttpSC.OK_200) ; - ServletOutputStream out = response.getOutputStream() ; - RDFDataMgr.write(out, model, lang) ; - out.flush() ; - } - catch (Exception ex) { - action.log.info("Exception while writing the response model: "+ex.getMessage(), ex) ; - ServletOps.errorOccurred("Exception while writing the response model: "+ex.getMessage(), ex) ; - } - } -} - http://git-wip-us.apache.org/repos/asf/jena/blob/75a7a77c/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Query.java ---------------------------------------------------------------------- diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Query.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Query.java index cd0c410..8e81426 100644 --- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Query.java +++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_Query.java @@ -45,14 +45,10 @@ import javax.servlet.http.HttpServletResponse ; import org.apache.jena.atlas.io.IO ; import org.apache.jena.atlas.io.IndentedLineBuffer ; -import org.apache.jena.atlas.web.AcceptList ; import org.apache.jena.atlas.web.ContentType ; -import org.apache.jena.atlas.web.MediaType ; -import org.apache.jena.fuseki.DEF ; import org.apache.jena.fuseki.Fuseki ; import org.apache.jena.fuseki.FusekiException ; import org.apache.jena.fuseki.FusekiLib ; -import org.apache.jena.fuseki.conneg.WebLib ; import org.apache.jena.query.* ; import org.apache.jena.rdf.model.Model ; import org.apache.jena.riot.web.HttpNames ; @@ -319,18 +315,9 @@ public abstract class SPARQL_Query extends SPARQL_Protocol } if ( query.isConstructType() ) { - - MediaType rdfMediaType = AcceptList.match( DEF.pureRdfOffer, new AcceptList( WebLib.getAccept(action.getRequest()))); - - if ( ! rdfMediaType.getType().equals("*") ) { - Model model = queryExecution.execConstruct(); - action.log.info(format("[%d] exec/construct/model", action.id)); - return new SPARQLResult(model); - } else { - Dataset dataset = queryExecution.execConstructDataset(); - action.log.info(format("[%d] exec/construct/dataset", action.id)); - return new SPARQLResult(dataset); - } + Dataset dataset = queryExecution.execConstructDataset(); + action.log.info(format("[%d] exec/construct/dataset", action.id)); + return new SPARQLResult(dataset); } if ( query.isDescribeType() ) { @@ -391,12 +378,10 @@ public abstract class SPARQL_Query extends SPARQL_Protocol protected void sendResults(HttpAction action, SPARQLResult result, Prologue qPrologue) { if ( result.isResultSet() ) ResponseResultSet.doResponseResultSet(action, result.getResultSet(), qPrologue) ; - else if ( result.isGraph() ) - ResponseModel.doResponseModel(action, result.getModel()) ; - else if ( result.isBoolean() ) - ResponseResultSet.doResponseResultSet(action, result.getBooleanResult()) ; else if ( result.isDataset() ) ResponseDataset.doResponseDataset(action, result.getDataset()); + else if ( result.isBoolean() ) + ResponseResultSet.doResponseResultSet(action, result.getBooleanResult()) ; else ServletOps.errorOccurred("Unknown or invalid result type") ; } http://git-wip-us.apache.org/repos/asf/jena/blob/75a7a77c/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestQuery.java ---------------------------------------------------------------------- diff --git a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestQuery.java b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestQuery.java index 4cf5b1b..e721d9d 100644 --- a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestQuery.java +++ b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestQuery.java @@ -42,6 +42,7 @@ import org.apache.jena.sparql.sse.Item ; import org.apache.jena.sparql.sse.SSE ; import org.apache.jena.sparql.sse.builders.BuilderResultSet ; import org.apache.jena.sparql.util.Convert ; +import org.apache.jena.rdf.model.*; import org.junit.AfterClass ; import org.junit.Assert ; import org.junit.BeforeClass ; @@ -169,7 +170,6 @@ public class TestQuery extends BaseTest { Dataset result = qExec.execConstructDataset(); Assert.assertTrue(result.asDatasetGraph().find().hasNext()); Assert.assertEquals( "http://eg/g", result.asDatasetGraph().find().next().getGraph().getURI()); - } } @@ -182,6 +182,16 @@ public class TestQuery extends BaseTest { Assert.assertTrue(result.hasNext()); } } + + @Test + public void query_construct_02() + { + String query = " CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}" ; + try ( QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery, query) ) { + Model result = qExec.execConstruct(); + assertEquals(1, result.size()); + } + } private void execQuery(String queryString, int exceptedRowCount) { QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery, queryString) ;
