Search in sources :

Example 1 with RDFFormat

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());
    }
}
Also used : ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpResponse(org.apache.http.HttpResponse) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 2 with RDFFormat

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());
        }
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ParseErrorLogger(org.eclipse.rdf4j.rio.helpers.ParseErrorLogger) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) Charset(java.nio.charset.Charset) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 3 with RDFFormat

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);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 4 with RDFFormat

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);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 5 with RDFFormat

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;
}
Also used : WriteRdf4j(mom.trd.opentheso.core.exports.rdf4j.WriteRdf4j) ExportRdf4jHelper(mom.trd.opentheso.core.exports.rdf4j.helper.ExportRdf4jHelper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayContent(org.primefaces.model.ByteArrayContent) NodePreference(mom.trd.opentheso.bdd.helper.nodes.NodePreference) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) PreferencesHelper(mom.trd.opentheso.bdd.helper.PreferencesHelper)

Aggregations

RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)5 WriteRdf4j (mom.trd.opentheso.core.exports.rdf4j.WriteRdf4j)4 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 PreferencesHelper (mom.trd.opentheso.bdd.helper.PreferencesHelper)3 NodePreference (mom.trd.opentheso.bdd.helper.nodes.NodePreference)3 ExportRdf4jHelper (mom.trd.opentheso.core.exports.rdf4j.helper.ExportRdf4jHelper)3 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)3 RDFParser (org.eclipse.rdf4j.rio.RDFParser)3 ByteArrayContent (org.primefaces.model.ByteArrayContent)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 URLConnection (java.net.URLConnection)2 ZipInputStream (java.util.zip.ZipInputStream)2 HttpResponse (org.apache.http.HttpResponse)2 UncloseableInputStream (org.eclipse.rdf4j.common.io.UncloseableInputStream)2 UnsupportedRDFormatException (org.eclipse.rdf4j.rio.UnsupportedRDFormatException)2