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);
}
}
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);
}
}
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.");
}
}
Aggregations