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.
To create a new RDF Resource at the specified location in your Pod.
// 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);
|
|
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);
|
|
To update an RDF Resource in your Pod.
// ... 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);
|
|
To delete an RDF Resource in your Pod.
or
|
To create a new RDF Resource at the specified location in your Pod. |
|
To read an RDF Resource from your Pod and map to a specified class. |
|
To update an RDF Resource in your Pod.
|
|
To delete an RDF Resource in your Pod. |
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:
Request.Builder to build HTTP requests.
Request.BodyPublishers to handle request body payloads.
.send()
method in SolidSyncClient and SolidClient to send the request.Response.BodyHandlers to handle response body payloads.
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();
}