Adding JSON-LD to SPARQL form. Adding jsonld-java libraries. VIVO-255

This commit is contained in:
Brian Caruso 2013-08-27 15:10:34 -04:00
parent 2ba94ee167
commit 50945d5af1
8 changed files with 128 additions and 48 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -22,6 +22,10 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.Query;
import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSet;
@ -80,7 +84,7 @@ public class SparqlQueryServlet extends BaseEditController {
new ModelFormatConfig("N3", !CONVERT, ModelSerializationFormat.N3, null, "text/n3" ), new ModelFormatConfig("N3", !CONVERT, ModelSerializationFormat.N3, null, "text/n3" ),
new ModelFormatConfig("N-TRIPLE", !CONVERT, ModelSerializationFormat.NTRIPLE, null, "text/plain" ), new ModelFormatConfig("N-TRIPLE", !CONVERT, ModelSerializationFormat.NTRIPLE, null, "text/plain" ),
new ModelFormatConfig("TTL", CONVERT, ModelSerializationFormat.N3, "TTL", "application/x-turtle" ), 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 @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
@ -211,6 +215,7 @@ public class SparqlQueryServlet extends BaseEditController {
* @param rdfService * @param rdfService
* @throws IOException * @throws IOException
* @throws RDFServiceException * @throws RDFServiceException
* @throws
*/ */
private void doModelResultQuery( Query query, private void doModelResultQuery( Query query,
RDFService rdfService, String rdfResultFormatParam, RDFService rdfService, String rdfResultFormatParam,
@ -227,12 +232,24 @@ public class SparqlQueryServlet extends BaseEditController {
} }
response.setContentType( config.responseMimeType ); response.setContentType( config.responseMimeType );
OutputStream out = response.getOutputStream();
if( config.converstionFromWireFormat ){ if( config.converstionFromWireFormat ){
Model resultModel = RDFServiceUtils.parseModel( rawResult, config.wireFormat ); Model resultModel = RDFServiceUtils.parseModel( rawResult, config.wireFormat );
resultModel.write(out, config.jenaResponseFormat ); 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{ }else{
OutputStream out = response.getOutputStream();
resultModel.write(out, config.jenaResponseFormat );
}
}else{
OutputStream out = response.getOutputStream();
pipe( rawResult, out ); pipe( rawResult, out );
} }
} }
@ -338,8 +355,6 @@ public class SparqlQueryServlet extends BaseEditController {
req.setAttribute("prefixList", prefixList); 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("title","SPARQL Query");
req.setAttribute("bodyJsp", "/admin/sparqlquery/sparqlForm.jsp"); req.setAttribute("bodyJsp", "/admin/sparqlquery/sparqlForm.jsp");
@ -348,12 +363,13 @@ public class SparqlQueryServlet extends BaseEditController {
} }
protected static class ModelFormatConfig{ public static class ModelFormatConfig{
String valueFromForm; public String valueFromForm;
boolean converstionFromWireFormat; public boolean converstionFromWireFormat;
RDFService.ModelSerializationFormat wireFormat; public RDFService.ModelSerializationFormat wireFormat;
String jenaResponseFormat; public String jenaResponseFormat;
String responseMimeType; public String responseMimeType;
public ModelFormatConfig( String valueFromForm, public ModelFormatConfig( String valueFromForm,
boolean converstionFromWireFormat, boolean converstionFromWireFormat,
RDFService.ModelSerializationFormat wireFormat, RDFService.ModelSerializationFormat wireFormat,
@ -365,14 +381,15 @@ protected static class ModelFormatConfig{
this.jenaResponseFormat = jenaResponseFormat; this.jenaResponseFormat = jenaResponseFormat;
this.responseMimeType = responseMimeType; this.responseMimeType = responseMimeType;
} }
} }
public static class RSFormatConfig{
public String valueFromForm;
public boolean converstionFromWireFormat;
public ResultFormat wireFormat;
public ResultSetFormat jenaResponseFormat;
public String responseMimeType;
protected static class RSFormatConfig{
String valueFromForm;
boolean converstionFromWireFormat;
ResultFormat wireFormat;
ResultSetFormat jenaResponseFormat;
String responseMimeType;
public RSFormatConfig( String valueFromForm, public RSFormatConfig( String valueFromForm,
boolean converstionFromWireFormat, boolean converstionFromWireFormat,
ResultFormat wireFormat, ResultFormat wireFormat,

View file

@ -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));
}
}

View file

@ -52,6 +52,7 @@ LIMIT 20
<div> <div>
<h3>Format for SELECT query results:</h3> <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_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_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> <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_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_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_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> </div>
<input class="submit" type="submit" value="Run Query" /> <input class="submit" type="submit" value="Run Query" />
</form> </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 --> </div><!-- content -->
</body></html> </body></html>