use of org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter in project incubator-rya by apache.
the class RdfController method queryRdf.
@RequestMapping(value = "/queryrdf", method = { RequestMethod.GET, RequestMethod.POST })
public void queryRdf(@RequestParam("query") final String query, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, required = false) String auth, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_CV, required = false) final String vis, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_INFER, required = false) final String infer, @RequestParam(value = "nullout", required = false) final String nullout, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_RESULT_FORMAT, required = false) final String emit, @RequestParam(value = "padding", required = false) final String padding, @RequestParam(value = "callback", required = false) final String callback, final HttpServletRequest request, final HttpServletResponse response) {
// WARNING: if you add to the above request variables,
// Be sure to validate and encode since they come from the outside and could contain odd damaging character sequences.
SailRepositoryConnection conn = null;
final Thread queryThread = Thread.currentThread();
auth = StringUtils.arrayToCommaDelimitedString(provider.getUserAuths(request));
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
log.debug("interrupting");
queryThread.interrupt();
}
}, QUERY_TIME_OUT_SECONDS * 1000);
try {
final ServletOutputStream os = response.getOutputStream();
conn = repository.getConnection();
final Boolean isBlankQuery = StringUtils.isEmpty(query);
final ParsedOperation operation = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, null);
final Boolean requestedCallback = !StringUtils.isEmpty(callback);
final Boolean requestedFormat = !StringUtils.isEmpty(emit);
if (!isBlankQuery) {
if (operation instanceof ParsedGraphQuery) {
// Perform Graph Query
final RDFHandler handler = new RDFXMLWriter(os);
response.setContentType("text/xml");
performGraphQuery(query, conn, auth, infer, nullout, handler);
} else if (operation instanceof ParsedTupleQuery) {
// Perform Tuple Query
TupleQueryResultHandler handler;
if (requestedFormat && emit.equalsIgnoreCase("json")) {
handler = new SPARQLResultsJSONWriter(os);
response.setContentType("application/json");
} else {
handler = new SPARQLResultsXMLWriter(os);
response.setContentType("text/xml");
}
performQuery(query, conn, auth, infer, nullout, handler);
} else if (operation instanceof ParsedUpdate) {
// Perform Update Query
performUpdate(query, conn, os, infer, vis);
} else {
throw new MalformedQueryException("Cannot process query. Query type not supported.");
}
}
if (requestedCallback) {
os.print(")");
}
} catch (final Exception e) {
log.error("Error running query", e);
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (final RepositoryException e) {
log.error("Error closing connection", e);
}
}
}
timer.cancel();
}
use of org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter in project incubator-rya by apache.
the class QueryResultsOutputUtil method toBindingSetJSONFile.
/**
* Writes the results of a {@link QueryResultStream} to the output stream as JSON until the
* shutdown signal is set.
*
* @param out - The stream the JSON will be written to. (not null)
* @param query - The parsed SPARQL Query whose results are being output. This
* object is used to figure out which bindings may appear. (not null)
* @param resultsStream - The results stream that will be polled for results to
* write to {@code out}. (not null)
* @param shutdownSignal - Setting this signal will cause the thread that
* is processing this function to finish and leave. (not null)
* @throws TupleQueryResultHandlerException A problem was encountered while
* writing the JSON to the output stream.
* @throws IllegalStateException The {@code resultsStream} is closed.
* @throws RyaStreamsException Could not fetch the next set of results.
*/
public static void toBindingSetJSONFile(final OutputStream out, final TupleExpr query, final QueryResultStream<VisibilityBindingSet> resultsStream, final AtomicBoolean shutdownSignal) throws TupleQueryResultHandlerException, IllegalStateException, RyaStreamsException {
requireNonNull(out);
requireNonNull(query);
requireNonNull(resultsStream);
requireNonNull(shutdownSignal);
// Create a writer that does not pretty print.
final SPARQLResultsJSONWriter writer = new SPARQLResultsJSONWriter(out);
final WriterConfig config = writer.getWriterConfig();
config.set(BasicWriterSettings.PRETTY_PRINT, false);
// Start the JSON and enumerate the possible binding names.
writer.startQueryResult(Lists.newArrayList(query.getBindingNames()));
while (!shutdownSignal.get()) {
for (final VisibilityBindingSet result : resultsStream.poll(1000)) {
writer.handleSolution(result);
}
}
writer.endQueryResult();
}
Aggregations