use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDF.
/**
* Parse the response in this thread using the provided {@link RDFHandler}. All HTTP connections are
* closed and released in this method
*/
protected void getRDF(HttpUriRequest method, RDFHandler handler, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
String mimeType = getResponseMIMEType(response);
try {
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setParserConfig(getParserConfig());
parser.setParseErrorListener(new ParseErrorLogger());
parser.setRDFHandler(handler);
parser.parse(response.getEntity().getContent(), method.getURI().toASCIIString());
} catch (RDFParseException e) {
throw new RepositoryException("Malformed query result from server", e);
}
} finally {
EntityUtils.consumeQuietly(response.getEntity());
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class SPARQLProtocolSession method getRDFBackground.
/**
* Parse the response in a background thread. HTTP connections are dealt with in the
* {@link BackgroundGraphResult} or (in the error-case) in this method.
*/
protected GraphQueryResult getRDFBackground(HttpUriRequest method, boolean requireContext) throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException, UnauthorizedException, QueryInterruptedException {
boolean submitted = false;
// Specify which formats we support using Accept headers
Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
if (rdfFormats.isEmpty()) {
throw new RepositoryException("No tuple RDF parsers have been registered");
}
GraphQueryResult gRes = null;
// send the tuple query
HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
try {
// if we get here, HTTP code is 200
String mimeType = getResponseMIMEType(response);
RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats).orElseThrow(() -> new RepositoryException("Server responded with an unsupported file format: " + mimeType));
RDFParser parser = Rio.createParser(format, getValueFactory());
parser.setParserConfig(getParserConfig());
parser.setParseErrorListener(new ParseErrorLogger());
Charset charset = null;
// SES-1793 : Do not attempt to check for a charset if the format is
// defined not to have a charset
// This prevents errors caused by people erroneously attaching a
// charset to a binary formatted document
HttpEntity entity = response.getEntity();
if (format.hasCharset() && entity != null && entity.getContentType() != null) {
// required?
try {
charset = ContentType.parse(entity.getContentType().getValue()).getCharset();
} catch (IllegalCharsetNameException e) {
// work around for Joseki-3.2
// Content-Type: application/rdf+xml;
// charset=application/rdf+xml
}
if (charset == null) {
charset = UTF8;
}
}
if (entity == null) {
throw new RepositoryException("Server response was empty.");
}
String baseURI = method.getURI().toASCIIString();
gRes = background.parse(parser, entity.getContent(), charset, baseURI);
submitted = true;
return gRes;
} finally {
if (!submitted) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class HTTPRepositoryConnection method addModel.
private void addModel(Model m) throws RepositoryException {
// TODO we should dynamically pick a format from the available writers
// perhaps?
RDFFormat format = RDFFormat.BINARY;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Rio.write(m, out, format);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
client.addData(in, null, format);
} catch (RDFHandlerException e) {
throw new RepositoryException("error while writing statement", e);
} catch (RDFParseException e) {
throw new RepositoryException(e);
} catch (IOException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project rdf4j by eclipse.
the class HTTPRepositoryConnection method removeModel.
private void removeModel(Model m) throws RepositoryException {
RDFFormat format = RDFFormat.BINARY;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Rio.write(m, out, format);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
client.removeData(in, null, format);
} catch (RDFHandlerException e) {
throw new RepositoryException("error while writing statement", e);
} catch (RDFParseException e) {
throw new RepositoryException(e);
} catch (IOException e) {
throw new RepositoryException(e);
}
}
use of org.eclipse.rdf4j.rio.RDFFormat in project opentheso by miledrousset.
the class DownloadBean method brancheToFile.
/**
* Cette fonction permet de retourner une branche en SKOS
*
* @param idConcept
* @param idTheso
* @return
*/
public StreamedContent brancheToFile(String idConcept, String idTheso, int type) {
RDFFormat format = null;
String extention = "";
switch(type) {
case 0:
format = RDFFormat.RDFXML;
extention = "_skos.xml";
break;
case 1:
format = RDFFormat.JSONLD;
extention = "_json-ld.json";
break;
case 2:
format = RDFFormat.TURTLE;
extention = "_turtle.ttl";
break;
}
NodePreference nodePreference = new PreferencesHelper().getThesaurusPreference(connect.getPoolConnexion(), idTheso);
if (nodePreference == null)
return null;
ExportRdf4jHelper exportRdf4jHelper = new ExportRdf4jHelper();
exportRdf4jHelper.setInfos(connect.getPoolConnexion(), "dd-mm-yyyy", false, idTheso, nodePreference.getCheminSite());
exportRdf4jHelper.setNodePreference(nodePreference);
exportRdf4jHelper.addBranch(idTheso, idConcept);
WriteRdf4j writeRdf4j = new WriteRdf4j(exportRdf4jHelper.getSkosXmlDocument());
ByteArrayOutputStream out;
out = new ByteArrayOutputStream();
Rio.write(writeRdf4j.getModel(), out, format);
file = new ByteArrayContent(out.toByteArray(), "application/xml", idTheso + " " + extention);
return file;
}
Aggregations