CRUD#

Both SolidSyncClient and SolidClient provide methods for reading and writing resources to your Solid Pod; that is, performing create/read/update/delete (CRUD) operations. The resources can be RDF resources as well as non-RDF resources (such as .jpg, .pdf, .txt, .json files).

RDF Resources#

The SolidSyncClient and SolidClient provide methods to manage RDF/Solid resources:

Tip

See Modeling an RDF Resource on modeling Java class as an RDF class.

.create()

To create a new RDF Resource at the specified location in your Pod.

  • If the resource already exists at the location, the operation is a no-op.

  • If any container in the location path does not exist, the method creates the missing containers as well as the resource.

// import com.inrupt.client.Response;
// import com.inrupt.client.solid.SolidSyncClient;
// import java.net.URI;
// ...
// ...
// Expense newExpense = new Expense("https://...", ...);
// SolidSyncClient client = //...
// ...

// Create a RDF resource at the location
// specified in newExpense's identifier field.
// Mapping of the expense data is defined in the
// Expense class which extends SolidResource

client.create(newExpense);

.read()

To read a RDF Resource from your Pod and map to a specified class.

// Read a RDF resource at the specified location
// into Expense class which extends SolidResource

Expense myExpense = client.read(
   URI.create("https://..."),
   Expense.class);

.update()

To update an RDF Resource in your Pod.

  • If the resource does not exist at the location, creates a new resource.

  • If the resource already exists at the location, overwrites the resource.

// ... Modify the fetched expense
// myExpense.setXXX(...);
// myExpense.setYYY(...);

// Update the RDF resource at the location
// specified in the myExpense's identifier field.

Response<Void> response = client.update(myExpense);

.delete()

To delete an RDF Resource in your Pod.

  • You can specify the Object to delete:

    client.delete(
        new Expense(URI.create(resourceURL))
    );
    

or

  • You can specify the URL of the resource to delete:

    client.delete(URI.create(resourceURL));
    

WebID and Pod URLs#

A WebID is a unique URL that identifies an agent in the Solid ecosystem. Dereferencing the WebID yields a publicly readable WebID Profile document. A WebID Profile is an RDF Resource that contains data about the user, such as the user’s Pod URLs.

To facilitate reading WebID Profile, Inrupt’s Java Client Libraries provide the WebIdProfile class. For example, the following code reads the Pod URL(s) associated with the WebID.

import com.inrupt.client.webid.WebIdProfile;

// ...

public Set<URI> getPods(String webID) {
    try (final var profile = client.read(URI.create(webID), WebIdProfile.class)) {
         return profile.getStorage();
    }
}

Aside

Although an RDF Resource, the WebID Profile is not necessarily hosted on a Solid Pod and may not necessarily be a Solid Resource per the Solid Protocol. As such, Solid applications cannot rely on the Solid Protocol to read and write the WebID profile. See also https://solid.github.io/webid-profile/#introduction.

Non-RDF Resources#

For CRUD operations with non-RDF resources (e.g., .jpg, .txt, .pdf), you can issue HTTP request (e.g., POST, PUT, GET, DELETE). The Java Client Libraries includes:

For example, the following uses the Java Client Library to create a PUT request to save a file to a Pod and send the request:

import com.inrupt.client.Request;
import com.inrupt.client.Response;
import java.io.InputStream;
//...

// MultipartFile file = ...

try (final var fileStream = file.getInputStream()) {
    Request request = Request.newBuilder()
       .uri(URI.create(destinationURL))
       .header("Content-Type", file.getContentType())
       .PUT(Request.BodyPublishers.ofInputStream(fileStream))
       .build();
    Response<Void> response = client.send(
       request,
       Response.BodyHandlers.discarding());

} catch (java.io.IOException e1) {
    e1.printStackTrace();
}

You can also use the HTTP requests to read RDF content without mapping to a Java class. For example, the following uses the Java Client Library to create a GET request to read an RDF file and return the content as text/turtle:

public String getResourceAsTurtle(String resourceURL) {
   Request request = Request.newBuilder()
      .uri(URI.create(resourceURL))
      .header("Accept", "text/turtle")
      .GET()
      .build();

   Response<String> response = client.send(
      request,
      Response.BodyHandlers.ofString());
   return response.body();
}