From cc95211afca268759417034fb92df2ee65bef973 Mon Sep 17 00:00:00 2001 From: Brian Caruso Date: Mon, 23 Sep 2013 11:44:32 -0400 Subject: [PATCH] minor changes to content negotiation for sparql servlet VIVO-282 --- .../webapp/controller/SparqlQueryServlet.java | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SparqlQueryServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SparqlQueryServlet.java index 72bb31f37..ec6d1d1eb 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SparqlQueryServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/SparqlQueryServlet.java @@ -63,34 +63,18 @@ import edu.cornell.mannlib.vitro.webapp.web.ContentType; */ public class SparqlQueryServlet extends BaseEditController { private static final Log log = LogFactory.getLog(SparqlQueryServlet.class.getName()); - - private final static boolean CONVERT = true; /** * format configurations for SELECT queries. */ protected static HashMap rsFormats = new HashMap(); - - private static RSFormatConfig[] rsfs = { - new RSFormatConfig( "RS_XML", !CONVERT, ResultFormat.XML, null, "text/xml"), - new RSFormatConfig( "RS_TEXT", !CONVERT, ResultFormat.TEXT, null, "text/plain"), - new RSFormatConfig( "vitro:csv", !CONVERT, ResultFormat.CSV, null, "text/csv"), - new RSFormatConfig( "RS_JSON", !CONVERT, ResultFormat.JSON, null, "application/javascript") }; - + /** * format configurations for CONSTRUCT/DESCRIBE queries. */ protected static HashMap modelFormats = new HashMap(); - private static ModelFormatConfig[] fmts = { - new ModelFormatConfig("RDF/XML", !CONVERT, ModelSerializationFormat.RDFXML, null, "application/rdf+xml" ), - new ModelFormatConfig("RDF/XML-ABBREV", CONVERT, ModelSerializationFormat.N3, "RDF/XML-ABBREV", "application/rdf+xml" ), - new ModelFormatConfig("N3", !CONVERT, ModelSerializationFormat.N3, null, "text/n3" ), - new ModelFormatConfig("N-TRIPLE", !CONVERT, ModelSerializationFormat.NTRIPLE, null, "text/plain" ), - new ModelFormatConfig("TTL", CONVERT, ModelSerializationFormat.N3, "TTL", "application/x-turtle" ), - new ModelFormatConfig("JSON-LD", CONVERT, ModelSerializationFormat.N3, "JSON-LD", "application/javascript" ) }; - /** * Use this map to decide which MIME type is suited for the "accept" header. */ @@ -194,12 +178,7 @@ public class SparqlQueryServlet extends BaseEditController { ResultSet rs = ResultSetFactory.fromJSON( results ); OutputStream out = response.getOutputStream(); - ResultSetFormatter.output(out, rs, formatConf.jenaResponseFormat); - - // } else { - // Writer out = response.getWriter(); - // toCsv(out, results); - //} + ResultSetFormatter.output(out, rs, formatConf.jenaResponseFormat); } } catch (RDFServiceException e) { throw new ServletException("Cannot get result from the RDFService",e); @@ -367,12 +346,32 @@ public class SparqlQueryServlet extends BaseEditController { rd.forward(req,res); } + /** Simple boolean vaule to improve the legibility of confiugrations. */ + private final static boolean CONVERT = true; + + /** Simple vaule to improve the legibility of confiugrations. */ + private final static String NO_CONVERSION = null; + public static class FormatConfig{ public String valueFromForm; public boolean converstionFromWireFormat; public String responseMimeType; } + private static ModelFormatConfig[] fmts = { + new ModelFormatConfig("RDF/XML", + !CONVERT, ModelSerializationFormat.RDFXML, NO_CONVERSION, "application/rdf+xml" ), + new ModelFormatConfig("RDF/XML-ABBREV", + CONVERT, ModelSerializationFormat.N3, "RDF/XML-ABBREV", "application/rdf+xml" ), + new ModelFormatConfig("N3", + !CONVERT, ModelSerializationFormat.N3, NO_CONVERSION, "text/n3" ), + new ModelFormatConfig("N-TRIPLE", + !CONVERT, ModelSerializationFormat.NTRIPLE, NO_CONVERSION, "text/plain" ), + new ModelFormatConfig("TTL", + CONVERT, ModelSerializationFormat.N3, "TTL", "application/x-turtle" ), + new ModelFormatConfig("JSON-LD", + CONVERT, ModelSerializationFormat.N3, "JSON-LD", "application/javascript" ) }; + public static class ModelFormatConfig extends FormatConfig{ public RDFService.ModelSerializationFormat wireFormat; public String jenaResponseFormat; @@ -390,6 +389,17 @@ public class SparqlQueryServlet extends BaseEditController { } } + + private static RSFormatConfig[] rsfs = { + new RSFormatConfig( "RS_XML", + !CONVERT, ResultFormat.XML, null, "text/xml"), + new RSFormatConfig( "RS_TEXT", + !CONVERT, ResultFormat.TEXT, null, "text/plain"), + new RSFormatConfig( "vitro:csv", + !CONVERT, ResultFormat.CSV, null, "text/csv"), + new RSFormatConfig( "RS_JSON", + !CONVERT, ResultFormat.JSON, null, "application/javascript") }; + public static class RSFormatConfig extends FormatConfig{ public ResultFormat wireFormat; public ResultSetFormat jenaResponseFormat;