Skip to content

Commit 2cb0ac3

Browse files
java: Add TDM samples
Assisted-by: Codex
1 parent c6c041c commit 2cb0ac3

2 files changed

Lines changed: 173 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
import okhttp3.*;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
public class TDMReservedPDF {
10+
11+
// By default, we use the US-based API service. This is the primary endpoint for global use.
12+
private static final String API_URL = "https://api.pdfrest.com";
13+
14+
// For GDPR compliance and enhanced performance for European users, you can switch to the EU-based
15+
// service by commenting out the URL above and uncommenting the URL below.
16+
// For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
17+
// private static final String API_URL = "https://eu-api.pdfrest.com";
18+
19+
// Specify the path to your file here, or as the first argument when running the program.
20+
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";
21+
22+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
23+
// You can also put the environment variable in a .env file.
24+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
25+
26+
public static void main(String[] args) {
27+
File inputFile;
28+
if (args.length > 0) {
29+
inputFile = new File(args[0]);
30+
} else {
31+
inputFile = new File(DEFAULT_FILE_PATH);
32+
}
33+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
34+
35+
String uploadString = uploadFile(inputFile);
36+
JSONObject uploadJSON = new JSONObject(uploadString);
37+
if (uploadJSON.has("error")) {
38+
System.out.println("Error during upload: " + uploadString);
39+
return;
40+
}
41+
JSONArray fileArray = uploadJSON.getJSONArray("files");
42+
43+
JSONObject fileObject = fileArray.getJSONObject(0);
44+
45+
String uploadedId = fileObject.get("id").toString();
46+
47+
String tdmReservedPdfJson =
48+
String.format("{\"id\":\"%s\", \"policy\":\"https://example.com/tdm-policy\"}", uploadedId);
49+
50+
final RequestBody requestBody =
51+
RequestBody.create(tdmReservedPdfJson, MediaType.parse("application/json"));
52+
53+
Request request =
54+
new Request.Builder()
55+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
56+
.url(API_URL + "/tdm-reserved-pdf")
57+
.post(requestBody)
58+
.build();
59+
try {
60+
OkHttpClient client =
61+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
62+
63+
Response response = client.newCall(request).execute();
64+
System.out.println("TDM metadata Result code " + response.code());
65+
if (response.body() != null) {
66+
System.out.println(prettyJson(response.body().string()));
67+
}
68+
} catch (IOException e) {
69+
throw new RuntimeException(e);
70+
}
71+
}
72+
73+
private static String prettyJson(String json) {
74+
// https://stackoverflow.com/a/9583835/11996393
75+
return new JSONObject(json).toString(4);
76+
}
77+
78+
// This function is just a copy of the 'Upload.java' file to upload a binary file
79+
private static String uploadFile(File inputFile) {
80+
81+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
82+
83+
final RequestBody requestBody =
84+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
85+
86+
Request request =
87+
new Request.Builder()
88+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
89+
.header("Content-Filename", "File.pdf")
90+
.url(API_URL + "/upload")
91+
.post(requestBody)
92+
.build();
93+
try {
94+
OkHttpClient client = new OkHttpClient().newBuilder().build();
95+
Response response = client.newCall(request).execute();
96+
System.out.println("Upload Result code " + response.code());
97+
if (response.body() != null) {
98+
return response.body().string();
99+
}
100+
} catch (IOException e) {
101+
throw new RuntimeException(e);
102+
}
103+
return "";
104+
}
105+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
import okhttp3.*;
6+
import org.json.JSONObject;
7+
8+
public class TDMReservedPDF {
9+
10+
// By default, we use the US-based API service. This is the primary endpoint for global use.
11+
private static final String API_URL = "https://api.pdfrest.com";
12+
13+
// For GDPR compliance and enhanced performance for European users, you can switch to the EU-based
14+
// service by commenting out the URL above and uncommenting the URL below.
15+
// For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
16+
// private static final String API_URL = "https://eu-api.pdfrest.com";
17+
18+
// Specify the path to your file here, or as the first argument when running the program.
19+
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";
20+
21+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
22+
// You can also put the environment variable in a .env file.
23+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
24+
25+
public static void main(String[] args) {
26+
File inputFile;
27+
if (args.length > 0) {
28+
inputFile = new File(args[0]);
29+
} else {
30+
inputFile = new File(DEFAULT_FILE_PATH);
31+
}
32+
33+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
34+
35+
final RequestBody inputFileRequestBody =
36+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
37+
RequestBody requestBody =
38+
new MultipartBody.Builder()
39+
.setType(MultipartBody.FORM)
40+
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
41+
.addFormDataPart("policy", "https://example.com/tdm-policy")
42+
.addFormDataPart("output", "pdfrest_tdm_reserved_pdf")
43+
.build();
44+
Request request =
45+
new Request.Builder()
46+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
47+
.url(API_URL + "/tdm-reserved-pdf")
48+
.post(requestBody)
49+
.build();
50+
try {
51+
OkHttpClient client =
52+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
53+
54+
Response response = client.newCall(request).execute();
55+
System.out.println("TDM metadata result code " + response.code());
56+
if (response.body() != null) {
57+
System.out.println(prettyJson(response.body().string()));
58+
}
59+
} catch (IOException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
64+
private static String prettyJson(String json) {
65+
// https://stackoverflow.com/a/9583835/11996393
66+
return new JSONObject(json).toString(4);
67+
}
68+
}

0 commit comments

Comments
 (0)