|
1 | 1 | package com.docusign.controller.navigator.services; |
2 | 2 |
|
| 3 | +import com.docusign.core.model.BulkUploadJobInfo; |
3 | 4 | import com.docusign.iam.sdk.IamClient; |
| 5 | +import com.docusign.iam.sdk.models.components.CreateBulkJob; |
4 | 6 | import com.docusign.iam.sdk.models.operations.GetAgreementResponse; |
5 | 7 | import com.docusign.iam.sdk.models.operations.GetAgreementsListRequest; |
6 | 8 | import com.docusign.iam.sdk.models.operations.GetAgreementsListResponse; |
| 9 | +import com.docusign.iam.sdk.models.operations.UploadCompleteBulkJobResponse; |
7 | 10 | import com.fasterxml.jackson.annotation.JsonInclude; |
8 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 13 | +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; |
| 14 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
9 | 15 | import org.openapitools.jackson.nullable.JsonNullableModule; |
10 | 16 |
|
| 17 | +import java.util.List; |
| 18 | +import java.net.URI; |
| 19 | +import java.net.http.HttpClient; |
| 20 | +import java.net.http.HttpRequest; |
| 21 | +import java.net.http.HttpResponse; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.logging.Logger; |
| 24 | + |
11 | 25 | public class NavigatorMethodsService { |
| 26 | + private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); |
| 27 | + |
12 | 28 | //ds-snippet-start:NavigatorJavaStep2 |
13 | 29 | private static IamClient createIamClient(String accessToken) { |
14 | 30 | return IamClient.builder() |
15 | | - .accessToken(accessToken) |
16 | | - .build(); |
| 31 | + .accessToken(accessToken) |
| 32 | + .build(); |
17 | 33 | } |
18 | 34 | //ds-snippet-end:NavigatorJavaStep2 |
19 | 35 |
|
20 | 36 | public static GetAgreementsListResponse getAgreements(String accountId, String accessToken) throws Exception { |
21 | 37 | return createIamClient(accessToken) |
22 | | - .navigator() |
23 | | - .agreements() |
24 | | - .getAgreementsList() |
25 | | - .request(new GetAgreementsListRequest(accountId)) |
26 | | - .call(); |
| 38 | + .agreementManager() |
| 39 | + .agreements() |
| 40 | + .getAgreementsList() |
| 41 | + .request(new GetAgreementsListRequest(accountId)) |
| 42 | + .call(); |
27 | 43 | } |
28 | 44 |
|
29 | 45 | public static GetAgreementResponse getAgreement(String accountId, String accessToken, String agreementId) |
30 | | - throws Exception { |
| 46 | + throws Exception { |
31 | 47 | return createIamClient(accessToken) |
32 | | - .navigator() |
33 | | - .agreements() |
34 | | - .getAgreement() |
35 | | - .accountId(accountId) |
36 | | - .agreementId(agreementId) |
37 | | - .call(); |
| 48 | + .agreementManager() |
| 49 | + .agreements() |
| 50 | + .getAgreement() |
| 51 | + .accountId(accountId) |
| 52 | + .agreementId(agreementId) |
| 53 | + .call(); |
38 | 54 | } |
39 | 55 |
|
40 | 56 | public static String serializeObjectToJson(Object data) throws Exception { |
41 | 57 | var mapper = new ObjectMapper() |
42 | | - .setSerializationInclusion(JsonInclude.Include.NON_NULL) |
43 | | - .registerModule(new JsonNullableModule()); |
| 58 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL) |
| 59 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 60 | + .registerModule(new JavaTimeModule()) |
| 61 | + .registerModule(new Jdk8Module()) |
| 62 | + .registerModule(new JsonNullableModule()); |
44 | 63 |
|
45 | 64 | return mapper.writeValueAsString(data); |
46 | 65 | } |
| 66 | + |
| 67 | + public static BulkUploadJobInfo createBulkUploadJob( |
| 68 | + String accountId, |
| 69 | + String accessToken |
| 70 | + ) throws Exception { |
| 71 | + var client = createIamClient(accessToken); |
| 72 | + |
| 73 | + var demoDocs = getAvailableDemoDocuments(); |
| 74 | + if (demoDocs.isEmpty()) { |
| 75 | + throw new IllegalStateException("No demo documents found on classpath"); |
| 76 | + } |
| 77 | + |
| 78 | + var createJob = CreateBulkJob.builder() |
| 79 | + .jobName("Example bulk upload job") |
| 80 | + .expectedNumberOfDocs(demoDocs.size()) |
| 81 | + .language("en-US") |
| 82 | + .build(); |
| 83 | + |
| 84 | + var createResponse = client.agreementManager().bulkJob() |
| 85 | + .createBulkUploadJob(accountId, createJob); |
| 86 | + |
| 87 | + var bulkJob = createResponse.bulkJob().orElseThrow(); |
| 88 | + var jobId = bulkJob.id(); |
| 89 | + if (jobId == null || jobId.isBlank()) { |
| 90 | + throw new IllegalStateException("API returned a bulk job with no ID"); |
| 91 | + } |
| 92 | + |
| 93 | + var documents = bulkJob.embedded().orElseThrow().documents().orElseThrow(); |
| 94 | + |
| 95 | + var uploadUrls = new ArrayList<String>(); |
| 96 | + for (var doc : documents) { |
| 97 | + var actions = doc.actions().orElse(null); |
| 98 | + var url = actions != null ? actions.uploadDocument().orElse(null) : null; |
| 99 | + uploadUrls.add(url != null ? url : ""); |
| 100 | + } |
| 101 | + |
| 102 | + return new BulkUploadJobInfo(jobId, uploadUrls); |
| 103 | + } |
| 104 | + |
| 105 | + public static void uploadDocumentsToJob(List<String> uploadUrls) throws Exception { |
| 106 | + var demoDocs = getAvailableDemoDocuments(); |
| 107 | + |
| 108 | + for (int i = 0; i < demoDocs.size(); i++) { |
| 109 | + if (i >= uploadUrls.size()) |
| 110 | + break; |
| 111 | + |
| 112 | + var uploadUrl = uploadUrls.get(i); |
| 113 | + if (uploadUrl == null || uploadUrl.isEmpty()) |
| 114 | + continue; |
| 115 | + |
| 116 | + var docInfo = demoDocs.get(i); |
| 117 | + var bytes = loadClasspathResource(docInfo); |
| 118 | + if (bytes == null) |
| 119 | + continue; |
| 120 | + |
| 121 | + uploadToBlobStorage(bytes, getContentType(docInfo), docInfo, uploadUrl); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public static UploadCompleteBulkJobResponse completeBulkUploadJob( |
| 126 | + String accountId, |
| 127 | + String accessToken, |
| 128 | + String jobId |
| 129 | + ) throws Exception { |
| 130 | + return createIamClient(accessToken).agreementManager().bulkJob() |
| 131 | + .uploadCompleteBulkJob(accountId, jobId); |
| 132 | + } |
| 133 | + |
| 134 | + private static List<String> getAvailableDemoDocuments() { |
| 135 | + String[] candidates = { |
| 136 | + "World_Wide_Corp_Battle_Plan_Trafalgar.docx", |
| 137 | + "World_Wide_Corp_lorem.pdf", |
| 138 | + "doc_1.html", |
| 139 | + "Welcome.txt", |
| 140 | + "Id.jpg", |
| 141 | + }; |
| 142 | + |
| 143 | + var available = new ArrayList<String>(); |
| 144 | + for (var doc : candidates) { |
| 145 | + if (NavigatorMethodsService.class.getClassLoader().getResource(doc) != null) { |
| 146 | + available.add(doc); |
| 147 | + } |
| 148 | + } |
| 149 | + return available; |
| 150 | + } |
| 151 | + |
| 152 | + private static String getContentType(String filename) { |
| 153 | + String extension = ""; |
| 154 | + int lastDot = filename.lastIndexOf('.'); |
| 155 | + |
| 156 | + if (lastDot >= 0) { |
| 157 | + extension = filename.substring(lastDot).toLowerCase(); |
| 158 | + } |
| 159 | + |
| 160 | + switch (extension) { |
| 161 | + case ".docx": { |
| 162 | + return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
| 163 | + } |
| 164 | + case ".pdf": { |
| 165 | + return "application/pdf"; |
| 166 | + } |
| 167 | + case ".html": { |
| 168 | + return "text/html"; |
| 169 | + } |
| 170 | + case ".txt": { |
| 171 | + return "text/plain"; |
| 172 | + } |
| 173 | + case ".jpg": |
| 174 | + case ".jpeg": { |
| 175 | + return "image/jpeg"; |
| 176 | + } |
| 177 | + default: { |
| 178 | + return "application/octet-stream"; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static byte[] loadClasspathResource(String resourcePath) { |
| 184 | + try (var stream = NavigatorMethodsService.class.getClassLoader().getResourceAsStream(resourcePath)) { |
| 185 | + if (stream == null) |
| 186 | + return null; |
| 187 | + |
| 188 | + return stream.readAllBytes(); |
| 189 | + } catch (Exception e) { |
| 190 | + Logger.getLogger(NavigatorMethodsService.class.getName()) |
| 191 | + .warning("Could not load resource: " + resourcePath); |
| 192 | + return null; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private static void uploadToBlobStorage( |
| 197 | + byte[] bytes, |
| 198 | + String contentType, |
| 199 | + String fileName, |
| 200 | + String url |
| 201 | + ) throws Exception { |
| 202 | + var request = HttpRequest.newBuilder() |
| 203 | + .uri(URI.create(url)) |
| 204 | + .header("Content-Type", contentType) |
| 205 | + .header("x-ms-blob-type", "BlockBlob") |
| 206 | + .header("x-ms-meta-filename", fileName) |
| 207 | + .PUT(HttpRequest.BodyPublishers.ofByteArray(bytes)) |
| 208 | + .build(); |
| 209 | + var httpResponse = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.discarding()); |
| 210 | + |
| 211 | + if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { |
| 212 | + throw new java.io.IOException("Blob upload failed for " + fileName + ": HTTP " + httpResponse.statusCode()); |
| 213 | + } |
| 214 | + } |
47 | 215 | } |
0 commit comments