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