SparqlExtension extends AbstractRexsterExtension { @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH) @ExtensionDescriptor(description = "execute SPARQL queries against a SAIL graph.", api = { @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.SHOW_TYPES, description = API_SHOW_TYPES), @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.RETURN_KEYS, description = API_RETURN_KEYS) }) public ExtensionResponse evaluateSparql(@RexsterContext final RexsterResourceContext context, @RexsterContext final Graph graph, @ExtensionRequestParameter(name = "query", description = API_QUERY) final String queryString) { if (queryString == null || queryString.isEmpty()) { final ExtensionMethod extMethod = context.getExtensionMethod(); return ExtensionResponse.error( "the query parameter cannot be empty", null, Response.Status.BAD_REQUEST.getStatusCode(), null, generateErrorJson(extMethod.getExtensionApiAsJson())); } final JSONObject requestObject = context.getRequestObject(); final boolean showTypes = RequestObjectHelper.getShowTypes(requestObject); final GraphSONMode mode = showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL; final Set<String> returnKeys = RequestObjectHelper.getReturnKeys(requestObject, WILDCARD); if (!(graph instanceof SailGraph)) { final ExtensionMethod extMethod = context.getExtensionMethod(); return ExtensionResponse.error( "the graph to which this extension is applied is not a SailGraph implementation", null, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), null, generateErrorJson(extMethod.getExtensionApiAsJson())); } try { final SailGraph sailGraph = (SailGraph) graph; final List<Map<String, Vertex>> sparqlResults = sailGraph.executeSparql(queryString); final JSONArray jsonArray = new JSONArray(); for (Map<String, Vertex> map : sparqlResults) { Map<String, JSONObject> mapOfJson = new HashMap<String, JSONObject>(); for (String key : map.keySet()) { mapOfJson.put(key, GraphSONUtility.jsonFromElement(map.get(key), returnKeys, mode)); } jsonArray.put(new JSONObject(mapOfJson)); } final HashMap<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put(Tokens.SUCCESS, true); resultMap.put(Tokens.RESULTS, jsonArray); final JSONObject resultObject = new JSONObject(resultMap); return ExtensionResponse.ok(resultObject); } catch (Exception mqe) { logger.error(mqe); return ExtensionResponse.error( "Error executing SPARQL query [" + queryString + "]", generateErrorJson()); } }  @ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH) @ExtensionDescriptor(description = "execute SPARQL queries against a SAIL graph.", api = { @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.SHOW_TYPES, description = API_SHOW_TYPES), @ExtensionApi(parameterName = Tokens.REXSTER + "." + Tokens.RETURN_KEYS, description = API_RETURN_KEYS) }) ExtensionResponse evaluateSparql(@RexsterContext final RexsterResourceContext context,
                                            @RexsterContext final Graph graph,
                                            @ExtensionRequestParameter(name = "query", description = API_QUERY) final String queryString); static final String EXTENSION_NAMESPACE; static final String EXTENSION_NAME; }
@Test public void evaluateSparqlNoReturnKeysNoShowTypes() { String sparqlQuery = "SELECT ?x ?y WHERE { ?x <http: this.ctx = new RexsterResourceContext(null, null, null, new JSONObject(), null, null, null, null); SparqlExtension extension = new SparqlExtension(); ExtensionResponse extensionResponse = extension.evaluateSparql(this.ctx, this.graph, sparqlQuery); Assert.assertNotNull(extensionResponse); Assert.assertFalse(extensionResponse.isErrorResponse()); Response jerseyResponse = extensionResponse.getJerseyResponse(); Assert.assertNotNull(jerseyResponse); Assert.assertEquals(Response.Status.OK.getStatusCode(), jerseyResponse.getStatus()); JSONObject jsonResponseEntity = (JSONObject) jerseyResponse.getEntity(); Assert.assertNotNull(jsonResponseEntity); Assert.assertTrue(jsonResponseEntity.has(Tokens.RESULTS)); JSONArray results = jsonResponseEntity.optJSONArray(Tokens.RESULTS); Assert.assertNotNull(results); Assert.assertEquals(2, results.length()); }