Search in sources :

Example 1 with UnsupportedRDFormatException

use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project rdf4j by eclipse.

the class SPARQLProtocolSession method execute.

protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException {
    boolean consume = true;
    if (params != null) {
        method.setParams(params);
    }
    HttpResponse response = httpClient.execute(method, httpContext);
    try {
        int httpCode = response.getStatusLine().getStatusCode();
        if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) {
            consume = false;
            // everything OK, control flow can continue
            return response;
        } else {
            switch(httpCode) {
                case // 401
                HttpURLConnection.HTTP_UNAUTHORIZED:
                    throw new UnauthorizedException();
                case // 503
                HttpURLConnection.HTTP_UNAVAILABLE:
                    throw new QueryInterruptedException();
                default:
                    ErrorInfo errInfo = getErrorInfo(response);
                    // Throw appropriate exception
                    if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) {
                        throw new RDFParseException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) {
                        throw new UnsupportedRDFormatException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) {
                        throw new MalformedQueryException(errInfo.getErrorMessage());
                    } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) {
                        throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage());
                    } else if (errInfo.toString().length() > 0) {
                        throw new RepositoryException(errInfo.toString());
                    } else {
                        throw new RepositoryException(response.getStatusLine().getReasonPhrase());
                    }
            }
        }
    } finally {
        if (consume) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) ErrorInfo(org.eclipse.rdf4j.http.protocol.error.ErrorInfo) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) HttpResponse(org.apache.http.HttpResponse) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 2 with UnsupportedRDFormatException

use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project rdf4j by eclipse.

the class LocalRepositoryManager method addRepositoryConfig.

private synchronized void addRepositoryConfig(RepositoryConfig config, boolean updateSystem) {
    File dataDir = getRepositoryDir(config.getID());
    if (!dataDir.exists()) {
        dataDir.mkdirs();
    }
    if (!dataDir.isDirectory()) {
        throw new RepositoryConfigException("Could not create directory: " + dataDir);
    }
    File configFile = new File(dataDir, CFG_FILE);
    if (!upgraded && !configFile.exists()) {
        upgraded = true;
        upgrade();
    }
    Model model = new TreeModel();
    String ns = configFile.toURI().toString() + "#";
    config.export(model, SimpleValueFactory.getInstance().createIRI(ns, config.getID()));
    File part = new File(configFile.getParentFile(), configFile.getName() + ".part");
    try (OutputStream output = new FileOutputStream(part)) {
        Rio.write(model, output, configFile.toURI().toString(), CONFIG_FORMAT, CFG_CONFIG);
    } catch (IOException | RDFHandlerException | UnsupportedRDFormatException | URISyntaxException e) {
        throw new RepositoryConfigException(e);
    }
    if (updateSystem) {
        super.addRepositoryConfig(config);
    }
    part.renameTo(configFile);
}
Also used : TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) TreeModel(org.eclipse.rdf4j.model.impl.TreeModel) Model(org.eclipse.rdf4j.model.Model) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RepositoryConfigException(org.eclipse.rdf4j.repository.config.RepositoryConfigException) File(java.io.File)

Example 3 with UnsupportedRDFormatException

use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project opentheso by miledrousset.

the class ReadRdfFileTest method test4.

@Test
public void test4() {
    // read the file 'example-data-artists.ttl' as an InputStream.
    InputStream is = null;
    try {
        is = new FileInputStream("test_unesco - Copie.rdf");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    Model model = null;
    try {
        model = Rio.parse(is, "", RDFFormat.RDFXML);
    } catch (IOException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (RDFParseException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedRDFormatException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Statement statement : model) {
        System.out.println(statement);
    }
    System.out.println("\n----------------");
    for (Statement st : model) {
        // we want to see the object values of each statement
        Value value = st.getObject();
        IRI property = st.getPredicate();
        if (property.getLocalName().equals("type"))
            System.out.println("\n......\n");
        if (property.getLocalName().equals("notation")) {
            Literal title = (Literal) value;
            System.out.println("notation: " + title.getLabel());
        } else if (property.getLocalName().equals("lat")) {
            Literal title = (Literal) value;
            System.out.println("latitude : " + title.getLabel());
        } else if (property.getLocalName().equals("long")) {
            Literal title = (Literal) value;
            System.out.println("longitude : " + title.getLabel());
        } else if (property.getLocalName().equals("created")) {
            Literal title = (Literal) value;
            System.out.println("created : " + title.getLabel());
        } else if (property.getLocalName().equals("modified")) {
            Literal title = (Literal) value;
            System.out.println("modified : " + title.getLabel());
        } else if (value instanceof Literal) {
            String pref = "";
            if (property.getLocalName().equals("prefLabel"))
                pref = " // pref label";
            Literal title = (Literal) value;
            System.out.println("language: " + title.getLanguage().orElse("unknown"));
            System.out.println(" title: " + title.getLabel() + pref);
        } else {
            System.out.println("   ****  " + value + " //// " + property.getLocalName());
            if (property.getLocalName().equals("type")) {
                System.out.println("        URL: " + st.getSubject());
            }
        }
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 4 with UnsupportedRDFormatException

use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project rdf4j by eclipse.

the class RDFLoader method load.

/**
 * Parses the RDF data that can be found at the specified URL to the
 * RDFHandler.
 *
 * @param url
 *            The URL of the RDF data.
 * @param baseURI
 *            The base URI to resolve any relative URIs that are in the data
 *            against. This defaults to the value of
 *            {@link java.net.URL#toExternalForm() url.toExternalForm()} if
 *            the value is set to <tt>null</tt>.
 * @param dataFormat
 *            The serialization format of the data. If set to <tt>null</tt>,
 *            the format will be automatically determined by examining the
 *            content type in the HTTP response header, and failing that,
 *            the file name extension of the supplied URL.
 * @param rdfHandler
 *            Receives RDF parser events.
 * @throws IOException
 *             If an I/O error occurred while reading from the URL.
 * @throws UnsupportedRDFormatException
 *             If no parser is available for the specified RDF format, or
 *             the RDF format could not be automatically determined.
 * @throws RDFParseException
 *             If an error was found while parsing the RDF data.
 * @throws RDFHandlerException
 *             If thrown by the RDFHandler
 */
public void load(URL url, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
    if (baseURI == null) {
        baseURI = url.toExternalForm();
    }
    URLConnection con = url.openConnection();
    // Set appropriate Accept headers
    if (dataFormat != null) {
        for (String mimeType : dataFormat.getMIMETypes()) {
            con.addRequestProperty("Accept", mimeType);
        }
    } else {
        Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
        List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, true, null);
        for (String acceptParam : acceptParams) {
            con.addRequestProperty("Accept", acceptParam);
        }
    }
    try (InputStream in = con.getInputStream()) {
        if (dataFormat == null) {
            // Try to determine the data's MIME type
            String mimeType = con.getContentType();
            int semiColonIdx = mimeType.indexOf(';');
            if (semiColonIdx >= 0) {
                mimeType = mimeType.substring(0, semiColonIdx);
            }
            dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElseGet(() -> Rio.getParserFormatForFileName(url.getPath()).orElseThrow(() -> new UnsupportedRDFormatException("Could not find RDF format for URL: " + url.getPath())));
        }
        load(in, baseURI, dataFormat, rdfHandler);
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) UncloseableInputStream(org.eclipse.rdf4j.common.io.UncloseableInputStream) InputStream(java.io.InputStream) URLConnection(java.net.URLConnection) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Example 5 with UnsupportedRDFormatException

use of org.eclipse.rdf4j.rio.UnsupportedRDFormatException in project timbuctoo by HuygensING.

the class Rdf4jRdfParser method importRdf.

@Override
public void importRdf(CachedLog input, String baseUri, String defaultGraph, RdfProcessor rdfProcessor) throws RdfProcessingFailedException {
    try {
        RDFFormat format = Rio.getParserFormatForMIMEType(input.getMimeType().toString()).orElseThrow(() -> new UnsupportedRDFormatException(input.getMimeType() + " is not a supported rdf type."));
        RDFParser rdfParser = Rio.createParser(format);
        rdfParser.setPreserveBNodeIDs(true);
        rdfParser.setRDFHandler(new TimRdfHandler(rdfProcessor, defaultGraph, input.getFile().getName()));
        rdfParser.parse(input.getReader(), baseUri);
    } catch (IOException | RDFParseException | UnsupportedRDFormatException e) {
        throw new RdfProcessingFailedException(e);
    } catch (RDFHandlerException e) {
        if (e.getCause() instanceof RdfProcessingFailedException) {
            throw (RdfProcessingFailedException) e.getCause();
        } else {
            throw new RdfProcessingFailedException(e);
        }
    }
}
Also used : UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TimRdfHandler(nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.rdf4j.parsers.TimRdfHandler) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Aggregations

UnsupportedRDFormatException (org.eclipse.rdf4j.rio.UnsupportedRDFormatException)5 IOException (java.io.IOException)3 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Model (org.eclipse.rdf4j.model.Model)2 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)2 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 RdfProcessingFailedException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)1 TimRdfHandler (nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.rdf4j.parsers.TimRdfHandler)1 HttpResponse (org.apache.http.HttpResponse)1