Package com.inrupt.client.jena


package com.inrupt.client.jena

Jena RDF support for the Inrupt Java Client Libraries.

The Jena module gives two possibilities to read RDF data: through the Service JenaService and through the BodyHandler JenaBodyHandlers.

A user of the JenaService should ensure that this implementation is available on the classpath by adding the following dependency:

     <dependency>
            <groupId>com.inrupt</groupId>
            <artifactId>inrupt-client-jena</artifactId>
            <version>${project.version}</version>
     </dependency>
 

Example of using the Jena Service toDataset() method to read triples from a trig file into a Dataset:


    RdfService service = ServiceProvider.getRdfService();
    Dataset dataset;
    try (InputStream input = Test.class.getResourceAsStream("/tripleExamples.trig")) {
        dataset = service.toDataset(RDFSyntax.TRIG, input);
    }
    System.out.println("Number of triples in file: " + dataset.size());
 

Example of using the Jena BodyHandler ofModel() method to read the triples from the same trig file into a Model:


    Request request = Request.newBuilder()
        .uri(URI.create("https://example.example/tripleExamples.trig"))
        .GET()
        .build();

    Response<Model> response = client.send(request, JenaBodyHandlers.ofModel()).toCompletableFuture().join();

    System.out.println("HTTP status code: " + response.statusCode());
    System.out.println("Number of triples in file: " + response.body().size());
 

The JenaBodyPublishers can be used to write triples. An example that uses the POST method to write a Model:


    Model model = ModelFactory.createDefaultModel();

    model.add(
        ResourceFactory.createResource("https://example.example/subject"),
        ResourceFactory.createProperty("https://example.example/predicate"),
        "object");

    Request request = Request.newBuilder()
            .uri("https://example.example/postEndpoint"))
            .header("Content-Type", "text/turtle")
            .POST(JenaBodyPublishers.ofModel(model))
            .build();

    Response<Void> response = client.send(request, Response.BodyHandlers.discarding()).toCompletableFuture().join();

    System.out.println("HTTP status code: " + response.statusCode());