1. Introduction
In this tutorial, we will retrieve metadata and binary content of a DMS object of which we know the value for system:objectId.
2. Requirements
Please work through the following tutorials to get prepared:
-
Importing Documents via Core API.
From the Import Tutorial, store the
system:objectIdof an existing DMS object and use it later in this Tutorial as value for theobjectIdVariable.
3. Java Libraries
Import the following Java libraries (if not already done).
import okhttp3.*;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.Base64;
4. Variables with Login and Content Types Configuration
Specify your access credentials and content type configurations (if not already done).
Specify the objectId variable.
public static final String username = "root";
public static final String userpassword = "changeme";
public static final String auth = "Basic "+ Base64.getEncoder().encodeToString((username+":"+userpassword).getBytes());
public static final String tenant = "myfirsttenant";
public static final String baseUrl = "http://123.456.78.9:30080";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final MediaType PLAINTEXT = MediaType.parse("text/plain; charset=utf-8");
public static final String objectId = "ecc0f6cc-7467-4e02-9903-073b893afdfd";
5. Client
To send our requests, we again use the Java client.
try {
CookieJar cookieJar = new JavaNetCookieJar(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
OkHttpClient client = new OkHttpClient.Builder().cookieJar(cookieJar).build();
} catch (Exception e) {
e.printStackTrace();
}
5.1. Retrieving the Metadata
The direct access to the metadata of a DMS object is requested via the endpoint GET /api/dms/objects/{objectId}. The objectId serves as a unique identifier of the DMS object.
Request metadataRequest = new Request.Builder()
.header("Authorization", auth)
.header("X-ID-TENANT-NAME", tenant)
.url(baseUrl+ "/api/dms/objects/" + objectId)
.get().build();
Response response = client.newCall(metadataRequest).execute();
String responseString = response.body().string();
System.out.println(responseString);
The API call response will contain the current version of the objects' metadata in JSON format.
To retrieve the content of an object, a GET request is sent to the endpoint /api/dms/objects/{objectId}/contents/file. As we imported a plain text binary content file, we can download it in TXT format again. The result will be stored as downloaded_file.txt file in your project directory.
Request contentRequest = new Request.Builder()
.header("Authorization", auth)
.header("X-ID-TENANT-NAME", tenant)
.url(baseUrl+ "/api/dms/objects/" + objectId + "/contents/file")
.get().build();
Response contentResponse = client.newCall(contentRequest).execute();
InputStream in = contentResponse.body().byteStream();
FileOutputStream out = new FileOutputStream("downloaded_file.txt");
out.write(in.readAllBytes());
6. Summary
This tutorial showed how to use an OkHttpClient to retrieve documents via Core API.
A complete code example can be found in this Git repository.