Search in sources :

Example 1 with ParsedUpdate

use of org.openrdf.query.parser.ParsedUpdate 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();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) RepositoryException(org.openrdf.repository.RepositoryException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) SPARQLResultsJSONWriter(org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter) ParsedOperation(org.openrdf.query.parser.ParsedOperation) QueryResultHandlerException(org.openrdf.query.QueryResultHandlerException) RepositoryException(org.openrdf.repository.RepositoryException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) RDFParseException(org.openrdf.rio.RDFParseException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) UpdateExecutionException(org.openrdf.query.UpdateExecutionException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) ParsedUpdate(org.openrdf.query.parser.ParsedUpdate) Timer(java.util.Timer) TimerTask(java.util.TimerTask) RDFXMLWriter(org.openrdf.rio.rdfxml.RDFXMLWriter) MalformedQueryException(org.openrdf.query.MalformedQueryException) TupleQueryResultHandler(org.openrdf.query.TupleQueryResultHandler) RDFHandler(org.openrdf.rio.RDFHandler) ParsedTupleQuery(org.openrdf.query.parser.ParsedTupleQuery) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IOException (java.io.IOException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 QueryResultHandlerException (org.openrdf.query.QueryResultHandlerException)1 TupleQueryResultHandler (org.openrdf.query.TupleQueryResultHandler)1 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)1 UpdateExecutionException (org.openrdf.query.UpdateExecutionException)1 ParsedGraphQuery (org.openrdf.query.parser.ParsedGraphQuery)1 ParsedOperation (org.openrdf.query.parser.ParsedOperation)1 ParsedTupleQuery (org.openrdf.query.parser.ParsedTupleQuery)1 ParsedUpdate (org.openrdf.query.parser.ParsedUpdate)1 SPARQLResultsJSONWriter (org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter)1 SPARQLResultsXMLWriter (org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter)1 RepositoryException (org.openrdf.repository.RepositoryException)1 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)1 RDFHandler (org.openrdf.rio.RDFHandler)1 RDFHandlerException (org.openrdf.rio.RDFHandlerException)1