use of org.eclipse.rdf4j.query.QueryResultHandlerException in project rdf4j by eclipse.
the class SPARQLProtocolSession method getTupleQueryResult.
/**
* Parse the response in this thread using the provided {@link TupleQueryResultHandler}. All HTTP
* connections are closed and released in this method
*/
protected void getTupleQueryResult(HttpUriRequest method, TupleQueryResultHandler handler) throws IOException, TupleQueryResultHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
// Specify which formats we support
Set<QueryResultFormat> tqrFormats = TupleQueryResultParserRegistry.getInstance().getKeys();
if (tqrFormats.isEmpty()) {
throw new RepositoryException("No tuple query result parsers have been registered");
}
// send the tuple query
HttpResponse response = sendTupleQueryViaHttp(method, tqrFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
try {
QueryResultFormat format = TupleQueryResultFormat.matchMIMEType(mimeType, tqrFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
TupleQueryResultParser parser = QueryResultIO.createTupleParser(format, getValueFactory());
parser.setQueryResultHandler(handler);
parser.parseQueryResult(response.getEntity().getContent());
} catch (QueryResultParseException e) {
throw new RepositoryException("Malformed query result from server", e);
} catch (QueryResultHandlerException e) {
if (e instanceof TupleQueryResultHandlerException) {
throw (TupleQueryResultHandlerException) e;
} else {
throw new TupleQueryResultHandlerException(e);
}
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
}
use of org.eclipse.rdf4j.query.QueryResultHandlerException in project rdf4j by eclipse.
the class BooleanTextParser method parse.
@Override
public synchronized boolean parse(InputStream in) throws IOException, QueryResultParseException {
Reader reader = new InputStreamReader(in, Charset.forName("US-ASCII"));
String value = IOUtil.readString(reader, 16);
value = value.trim();
boolean result = false;
if (value.equalsIgnoreCase("true")) {
result = true;
} else if (value.equalsIgnoreCase("false")) {
result = false;
} else {
throw new QueryResultParseException("Invalid value: " + value);
}
if (this.handler != null) {
try {
this.handler.handleBoolean(result);
} catch (QueryResultHandlerException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new QueryResultParseException("Found an issue with the query result handler", e);
}
}
}
return result;
}
use of org.eclipse.rdf4j.query.QueryResultHandlerException in project rdf4j by eclipse.
the class AbstractSPARQLXMLWriter method endQueryResult.
@Override
public void endQueryResult() throws TupleQueryResultHandlerException {
try {
if (!documentOpen) {
startDocument();
}
if (!headerOpen) {
startHeader();
}
if (!headerComplete) {
endHeader();
}
if (!tupleVariablesFound) {
throw new IllegalStateException("Could not end query result as startQueryResult was not called first.");
}
xmlWriter.endTag(RESULT_SET_TAG);
endDocument();
} catch (IOException e) {
throw new TupleQueryResultHandlerException(e);
} catch (TupleQueryResultHandlerException e) {
throw e;
} catch (QueryResultHandlerException e) {
throw new TupleQueryResultHandlerException(e);
}
}
use of org.eclipse.rdf4j.query.QueryResultHandlerException in project rdf4j by eclipse.
the class AbstractSPARQLXMLWriter method startDocument.
@Override
public void startDocument() throws QueryResultHandlerException {
if (!documentOpen) {
documentOpen = true;
headerOpen = false;
headerComplete = false;
tupleVariablesFound = false;
try {
xmlWriter.setPrettyPrint(getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT));
if (getWriterConfig().get(XMLWriterSettings.INCLUDE_XML_PI)) {
xmlWriter.startDocument();
}
xmlWriter.setAttribute("xmlns", NAMESPACE);
if (getWriterConfig().get(BasicQueryWriterSettings.ADD_SESAME_QNAME)) {
xmlWriter.setAttribute("xmlns:q", SESAMEQNAME.NAMESPACE);
}
for (String nextPrefix : namespaceTable.keySet()) {
this.log.debug("Adding custom prefix for <{}> to map to <{}>", nextPrefix, namespaceTable.get(nextPrefix));
xmlWriter.setAttribute("xmlns:" + namespaceTable.get(nextPrefix), nextPrefix);
}
} catch (IOException e) {
throw new QueryResultHandlerException(e);
}
}
}
use of org.eclipse.rdf4j.query.QueryResultHandlerException in project rdf4j by eclipse.
the class AbstractSPARQLJSONWriter method startQueryResult.
@Override
public void startQueryResult(List<String> columnHeaders) throws TupleQueryResultHandlerException {
try {
if (!documentOpen) {
startDocument();
}
if (!headerOpen) {
startHeader();
}
tupleVariablesFound = true;
jg.writeArrayFieldStart("vars");
for (String nextColumn : columnHeaders) {
jg.writeString(nextColumn);
}
jg.writeEndArray();
} catch (IOException e) {
throw new TupleQueryResultHandlerException(e);
} catch (TupleQueryResultHandlerException e) {
throw e;
} catch (QueryResultHandlerException e) {
throw new TupleQueryResultHandlerException(e);
}
}
Aggregations