Search in sources :

Example 46 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class SSE method readFile.

/**
 * Read a file and obtain an SSE item expression
 */
public static Item readFile(String filename, PrefixMapping pmap) {
    FileInputStream in = null;
    try {
        in = new FileInputStream(filename);
        long len = in.getChannel().size();
        if (len == 0)
            return Item.nil;
        return parse(in, pmap);
    } catch (FileNotFoundException ex) {
        throw new NotFoundException("Not found: " + filename);
    } catch (IOException ex) {
        throw new ARQException("IOExeption: " + filename, ex);
    } finally {
        IO.close(in);
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) NotFoundException(org.apache.jena.shared.NotFoundException)

Example 47 with ARQException

use of org.apache.jena.sparql.ARQException in project jena by apache.

the class UpdateValidatorHTML method executeHTML.

// static final String paramSyntaxExtended   = "extendedSyntax";
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        String[] args = httpRequest.getParameterValues(paramUpdate);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No update parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many update parameters");
            return;
        }
        final String updateString = httpRequest.getParameter(paramUpdate).replaceAll("(\r|\n| )*$", "");
        String updateSyntax = httpRequest.getParameter(paramSyntax);
        if (updateSyntax == null || updateSyntax.equals(""))
            updateSyntax = "SPARQL";
        Syntax language = Syntax.lookup(updateSyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + updateSyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        // Currently default.
        boolean outputSPARQL = true;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Update Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Update Validator</h1>");
        // Print as received
        outStream.println("<p>Input:</p>");
        output(outStream, (out) -> out.print(updateString), lineNumbers);
        // Attempt to parse it.
        UpdateRequest request = null;
        try {
            request = UpdateFactory.create(updateString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        // Because we pass into anon inner classes
        final UpdateRequest updateRequest = request;
        // OK?  Pretty print
        if (updateRequest != null && outputSPARQL) {
            outStream.println("<p>Formatted, parsed update request:</p>");
            output(outStream, (out) -> updateRequest.output(out), lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) UpdateRequest(org.apache.jena.update.UpdateRequest) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException)

Example 48 with ARQException

use of org.apache.jena.sparql.ARQException in project zeppelin by apache.

the class JenaInterpreter method query.

@Override
public InterpreterResult query(String query) {
    LOGGER.info("SPARQL: Run Query '" + query + "' against " + serviceEndpoint);
    try {
        queryExecution = QueryExecutionFactory.sparqlService(serviceEndpoint, query);
        PrefixMapping prefixMapping = queryExecution.getQuery().getPrefixMapping();
        // execute query and get Results
        ResultSet results = queryExecution.execSelect();
        // transform ResultSet to TSV-String
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ResultSetFormatter.outputAsTSV(outputStream, results);
        String tsv = new String(outputStream.toByteArray());
        if (replaceURIs) {
            LOGGER.info("SPARQL: Replacing URIs");
            tsv = replaceURIs(tsv, prefixMapping);
        }
        if (removeDatatypes) {
            LOGGER.info("SPARQL: Removing datatypes");
            tsv = removeDatatypes(tsv);
        }
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TABLE, tsv);
    } catch (QueryParseException e) {
        LOGGER.error(e.toString());
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Error: " + e.getMessage());
    } catch (QueryExceptionHTTP e) {
        LOGGER.error(e.toString());
        int responseCode = e.getResponseCode();
        if (responseCode == HttpStatus.SC_UNAUTHORIZED) {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Unauthorized.");
        } else if (responseCode == HttpStatus.SC_NOT_FOUND) {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Endpoint not found, please check endpoint in the configuration.");
        } else {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Error: " + e.getMessage());
        }
    } catch (ARQException e) {
        return new InterpreterResult(InterpreterResult.Code.INCOMPLETE, "Query cancelled.");
    }
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) ARQException(org.apache.jena.sparql.ARQException) ResultSet(org.apache.jena.query.ResultSet) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) QueryParseException(org.apache.jena.query.QueryParseException)

Aggregations

ARQException (org.apache.jena.sparql.ARQException)48 IOException (java.io.IOException)11 Var (org.apache.jena.sparql.core.Var)7 ArrayList (java.util.ArrayList)5 Lang (org.apache.jena.riot.Lang)5 BufferedReader (java.io.BufferedReader)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 Node (org.apache.jena.graph.Node)4 Syntax (org.apache.jena.query.Syntax)4 RDFNode (org.apache.jena.rdf.model.RDFNode)4 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2 Triple (org.apache.jena.graph.Triple)2 Query (org.apache.jena.query.Query)2 Model (org.apache.jena.rdf.model.Model)2 Resource (org.apache.jena.rdf.model.Resource)2 RiotException (org.apache.jena.riot.RiotException)2 HttpParams (org.apache.jena.sparql.engine.http.HttpParams)2 Item (org.apache.jena.sparql.sse.Item)2 UpdateRequest (org.apache.jena.update.UpdateRequest)2 BufferedWriter (java.io.BufferedWriter)1