Adding JSON-LD to SPARQL form. Adding jsonld-java libraries. VIVO-255
This commit is contained in:
parent
2ba94ee167
commit
50945d5af1
8 changed files with 128 additions and 48 deletions
BIN
webapp/lib/jackson-annotations-2.2.1.jar
Normal file
BIN
webapp/lib/jackson-annotations-2.2.1.jar
Normal file
Binary file not shown.
BIN
webapp/lib/jackson-core-2.2.1.jar
Normal file
BIN
webapp/lib/jackson-core-2.2.1.jar
Normal file
Binary file not shown.
BIN
webapp/lib/jackson-databind-2.2.1.jar
Normal file
BIN
webapp/lib/jackson-databind-2.2.1.jar
Normal file
Binary file not shown.
BIN
webapp/lib/jsonld-java-0.3-SNAPSHOT.jar
Normal file
BIN
webapp/lib/jsonld-java-0.3-SNAPSHOT.jar
Normal file
Binary file not shown.
BIN
webapp/lib/jsonld-java-jena-0.3-SNAPSHOT.jar
Normal file
BIN
webapp/lib/jsonld-java-jena-0.3-SNAPSHOT.jar
Normal file
Binary file not shown.
|
@ -22,6 +22,10 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.github.jsonldjava.core.JSONLD;
|
||||
import com.github.jsonldjava.core.JSONLDProcessingError;
|
||||
import com.github.jsonldjava.impl.JenaRDFParser;
|
||||
import com.github.jsonldjava.utils.JSONUtils;
|
||||
import com.hp.hpl.jena.query.Query;
|
||||
import com.hp.hpl.jena.query.QuerySolution;
|
||||
import com.hp.hpl.jena.query.ResultSet;
|
||||
|
@ -56,7 +60,7 @@ 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.
|
||||
*/
|
||||
|
@ -66,7 +70,7 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
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") };
|
||||
new RSFormatConfig( "RS_JSON", !CONVERT, ResultFormat.JSON, null, "application/javascript") };
|
||||
|
||||
/**
|
||||
* format configurations for CONSTRUCT/DESCRIBE queries.
|
||||
|
@ -80,7 +84,7 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
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, null, "application/x-turtle" ) };
|
||||
new ModelFormatConfig("JSON-LD", CONVERT, ModelSerializationFormat.N3, "JSON-LD", "application/javascript" ) };
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
|
@ -142,8 +146,8 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
* Unfortunately, ARQ doesn't make it easy to
|
||||
* do this by implementing a new ResultSetFormat, because
|
||||
* ResultSetFormatter is hardwired with expected values.
|
||||
* This slightly ugly approach will have to do for now.
|
||||
*/
|
||||
* This slightly ugly approach will have to do for now.
|
||||
*/
|
||||
// if ( !("vitro:csv").equals(resultFormatParam) ) {
|
||||
// rsf = selectFormatSymbols.get(resultFormatParam);
|
||||
// }
|
||||
|
@ -211,6 +215,7 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
* @param rdfService
|
||||
* @throws IOException
|
||||
* @throws RDFServiceException
|
||||
* @throws
|
||||
*/
|
||||
private void doModelResultQuery( Query query,
|
||||
RDFService rdfService, String rdfResultFormatParam,
|
||||
|
@ -227,12 +232,24 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
}
|
||||
|
||||
response.setContentType( config.responseMimeType );
|
||||
OutputStream out = response.getOutputStream();
|
||||
|
||||
|
||||
if( config.converstionFromWireFormat ){
|
||||
Model resultModel = RDFServiceUtils.parseModel( rawResult, config.wireFormat );
|
||||
resultModel.write(out, config.jenaResponseFormat );
|
||||
Model resultModel = RDFServiceUtils.parseModel( rawResult, config.wireFormat );
|
||||
if( "JSON-LD".equals( config.jenaResponseFormat )){
|
||||
//since jena 2.6.4 doesn't support JSON-LD we do it
|
||||
try {
|
||||
JenaRDFParser parser = new JenaRDFParser();
|
||||
Object json = JSONLD.fromRDF(resultModel, parser);
|
||||
JSONUtils.write(response.getWriter(), json);
|
||||
} catch (JSONLDProcessingError e) {
|
||||
throw new RDFServiceException("Could not convert from Jena model to JSON-LD", e);
|
||||
}
|
||||
}else{
|
||||
OutputStream out = response.getOutputStream();
|
||||
resultModel.write(out, config.jenaResponseFormat );
|
||||
}
|
||||
}else{
|
||||
OutputStream out = response.getOutputStream();
|
||||
pipe( rawResult, out );
|
||||
}
|
||||
}
|
||||
|
@ -338,8 +355,6 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
|
||||
req.setAttribute("prefixList", prefixList);
|
||||
|
||||
// nac26: 2009-09-25 - this was causing problems in safari on localhost installations because the href did not include the context. The edit.css is not being used here anyway (or anywhere else for that matter)
|
||||
// req.setAttribute("css", "<link rel=\"stylesheet\" type=\"text/css\" href=\""+portal.getThemeDir()+"css/edit.css\"/>");
|
||||
req.setAttribute("title","SPARQL Query");
|
||||
req.setAttribute("bodyJsp", "/admin/sparqlquery/sparqlForm.jsp");
|
||||
|
||||
|
@ -348,31 +363,33 @@ public class SparqlQueryServlet extends BaseEditController {
|
|||
}
|
||||
|
||||
|
||||
protected static class ModelFormatConfig{
|
||||
String valueFromForm;
|
||||
boolean converstionFromWireFormat;
|
||||
RDFService.ModelSerializationFormat wireFormat;
|
||||
String jenaResponseFormat;
|
||||
String responseMimeType;
|
||||
public ModelFormatConfig( String valueFromForm,
|
||||
boolean converstionFromWireFormat,
|
||||
RDFService.ModelSerializationFormat wireFormat,
|
||||
String jenaResponseFormat,
|
||||
String responseMimeType){
|
||||
this.valueFromForm = valueFromForm;
|
||||
this.converstionFromWireFormat = converstionFromWireFormat;
|
||||
this.wireFormat = wireFormat;
|
||||
this.jenaResponseFormat = jenaResponseFormat;
|
||||
this.responseMimeType = responseMimeType;
|
||||
public static class ModelFormatConfig{
|
||||
public String valueFromForm;
|
||||
public boolean converstionFromWireFormat;
|
||||
public RDFService.ModelSerializationFormat wireFormat;
|
||||
public String jenaResponseFormat;
|
||||
public String responseMimeType;
|
||||
|
||||
public ModelFormatConfig( String valueFromForm,
|
||||
boolean converstionFromWireFormat,
|
||||
RDFService.ModelSerializationFormat wireFormat,
|
||||
String jenaResponseFormat,
|
||||
String responseMimeType){
|
||||
this.valueFromForm = valueFromForm;
|
||||
this.converstionFromWireFormat = converstionFromWireFormat;
|
||||
this.wireFormat = wireFormat;
|
||||
this.jenaResponseFormat = jenaResponseFormat;
|
||||
this.responseMimeType = responseMimeType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static class RSFormatConfig{
|
||||
String valueFromForm;
|
||||
boolean converstionFromWireFormat;
|
||||
ResultFormat wireFormat;
|
||||
ResultSetFormat jenaResponseFormat;
|
||||
String responseMimeType;
|
||||
public static class RSFormatConfig{
|
||||
public String valueFromForm;
|
||||
public boolean converstionFromWireFormat;
|
||||
public ResultFormat wireFormat;
|
||||
public ResultSetFormat jenaResponseFormat;
|
||||
public String responseMimeType;
|
||||
|
||||
public RSFormatConfig( String valueFromForm,
|
||||
boolean converstionFromWireFormat,
|
||||
ResultFormat wireFormat,
|
||||
|
@ -386,7 +403,7 @@ protected static class ModelFormatConfig{
|
|||
}
|
||||
}
|
||||
|
||||
static{
|
||||
static{
|
||||
/* move the lists of configs into maps for easy lookup */
|
||||
for( RSFormatConfig rsfc : rsfs ){
|
||||
rsFormats.put( rsfc.valueFromForm, rsfc );
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.controller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.github.jsonldjava.core.JSONLD;
|
||||
import com.github.jsonldjava.core.JSONLDProcessingError;
|
||||
import com.github.jsonldjava.impl.JenaRDFParser;
|
||||
import com.github.jsonldjava.utils.JSONUtils;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
|
||||
public class SparqlQueryServletTest {
|
||||
|
||||
@Test
|
||||
public void testJSONLD() throws JSONLDProcessingError {
|
||||
//just check if we can use JSONLD-JAVA
|
||||
|
||||
final String turtle = "@prefix const: <http://foo.com/> .\n"
|
||||
+ "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
|
||||
+ "<http://localhost:8080/foo1> const:code \"123\" .\n"
|
||||
+ "<http://localhost:8080/foo2> const:code \"ABC\"^^xsd:string .\n";
|
||||
|
||||
final List<Map<String, Object>> expected = new ArrayList<Map<String, Object>>() {
|
||||
{
|
||||
add(new LinkedHashMap<String, Object>() {
|
||||
{
|
||||
put("@id", "http://localhost:8080/foo1");
|
||||
put("http://foo.com/code", new ArrayList<Object>() {
|
||||
{
|
||||
add(new LinkedHashMap<String, Object>() {
|
||||
{
|
||||
put("@value", "123");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
add(new LinkedHashMap<String, Object>() {
|
||||
{
|
||||
put("@id", "http://localhost:8080/foo2");
|
||||
put("http://foo.com/code", new ArrayList<Object>() {
|
||||
{
|
||||
add(new LinkedHashMap<String, Object>() {
|
||||
{
|
||||
put("@value", "ABC");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
final Model modelResult = ModelFactory.createDefaultModel().read(
|
||||
new ByteArrayInputStream(turtle.getBytes()), "", "TURTLE");
|
||||
final JenaRDFParser parser = new JenaRDFParser();
|
||||
final Object json = JSONLD.fromRDF(modelResult, parser);
|
||||
|
||||
assertTrue(JSONUtils.equals(json, expected));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -52,6 +52,7 @@ LIMIT 20
|
|||
|
||||
<div>
|
||||
<h3>Format for SELECT query results:</h3>
|
||||
|
||||
<input id='RS_XML_BUTTON' type='radio' name='resultFormat' value='RS_XML'> <label for='RS_XML_BUTTON'>RS_XML</label>
|
||||
<input id='RS_TEXT_BUTTON' type='radio' name='resultFormat' value='RS_TEXT' checked='checked'> <label for='RS_TEXT_BUTTON'>RS_TEXT</label>
|
||||
<input id='RS_CSV_BUTTON' type='radio' name='resultFormat' value='vitro:csv'> <label for='RS_CSV_BUTTON'>CSV</label>
|
||||
|
@ -66,23 +67,11 @@ LIMIT 20
|
|||
<input id='RR_N3_BUTTON' type='radio' name='rdfResultFormat' value='N3'> <label for='RR_N3_BUTTON'>N3</label>
|
||||
<input id='RR_NTRIPLE_BUTTON' type='radio' name='rdfResultFormat' value='N-TRIPLE'> <label for='RR_NTRIPLE_BUTTON'>N-Triples</label>
|
||||
<input id='RR_TURTLE_BUTTON' type='radio' name='rdfResultFormat' value='TTL'> <label for='RR_TURTLE_BUTTON'>Turtle</label>
|
||||
<input id='RR_JSON_LD_BUTTON' type='radio' name='rdfResultFormat' value='JSON-LD'> <label for='RR_JSON_LD_BUTTON'>JSON-LD</label>
|
||||
</div>
|
||||
|
||||
<input class="submit" type="submit" value="Run Query" />
|
||||
</form>
|
||||
<%--
|
||||
<h4>Notes</h4>
|
||||
<p>CONSTRUCT and DESCRIBE queries always return RDF XML</p>
|
||||
<p>The parameter 'resultFormat' must not be null or zero length</p>
|
||||
<p>The parameter 'resultFormat' must be one of the following: <ul>
|
||||
<li>RS_XML</li>
|
||||
<li>RS_TEXT</li>
|
||||
<li>RS_RDF/N3</li>
|
||||
<li>RS_JSON</li>
|
||||
<li>RS_RDF</li>
|
||||
</ul>
|
||||
</p>
|
||||
--%>
|
||||
</div><!-- content -->
|
||||
</body></html>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue