-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSignerClient.java
More file actions
289 lines (232 loc) · 12.4 KB
/
SignerClient.java
File metadata and controls
289 lines (232 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.lacunasoftware.signer.javaclient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
import com.lacunasoftware.signer.DocumentDownloadTypes;
import com.lacunasoftware.signer.InvoicesUpdateInvoicePaymentStatusRequest;
import com.lacunasoftware.signer.TicketModel;
import com.lacunasoftware.signer.DocumentTicketType;
import com.lacunasoftware.signer.documents.*;
import com.lacunasoftware.signer.javaclient.params.DocumentListParameters;
import com.lacunasoftware.signer.notifications.CreateFlowActionReminderRequest;
import com.lacunasoftware.signer.notifications.EmailListNotificationRequest;
import com.lacunasoftware.signer.folders.FolderInfoModel;
import com.lacunasoftware.signer.folders.FolderCreateRequest;
import com.lacunasoftware.signer.javaclient.responses.PaginatedSearchResponse;
import com.lacunasoftware.signer.javaclient.responses.CompleteSignatureResponse;
import com.lacunasoftware.signer.javaclient.responses.StartSignatureResponse;
import com.lacunasoftware.signer.javaclient.folders.FolderDetailsModel;
import com.lacunasoftware.signer.javaclient.models.UploadModel;
import com.lacunasoftware.signer.javaclient.params.PaginatedSearchParams;
import com.lacunasoftware.signer.javaclient.exceptions.RestException;
import com.lacunasoftware.signer.javaclient.requests.ElectronicSignatureRequest;
import com.lacunasoftware.signer.javaclient.requests.SendElectronicSignatureAuthenticationRequest;
import com.lacunasoftware.signer.javaclient.requests.StartSignatureRequest;
import com.lacunasoftware.signer.javaclient.requests.CompleteSignatureRequest;
import com.lacunasoftware.signer.refusal.RefusalRequest;
public class SignerClient {
protected String apiKey;
protected URI endpointUri;
protected RestClient restClient;
public SignerClient(String endpointUri, String apiKey) throws URISyntaxException {
this(new URI(endpointUri), apiKey);
}
public SignerClient(URI endpointUri, String apiKey) {
this.endpointUri = endpointUri;
this.apiKey = apiKey;
}
private RestClient getRestClient() {
if (restClient == null) {
restClient = new RestClient(endpointUri, apiKey);
}
return restClient;
}
// region SHARED
public ObjectMapper getJackson() {
return getRestClient().getJackson();
}
public Gson getGson() {
return getRestClient().getGson();
}
public byte[] getFile(TicketModel ticket) throws RestException {
return getRestClient().get(ticket.getLocation(), byte[].class);
}
public UploadModel uploadFile(String name, byte[] file, String mimeType) throws IOException, RestException {
UploadModel model;
try (ByteArrayInputStream stream = new ByteArrayInputStream(file)) {
model = uploadFile(name, stream, mimeType);
}
return model;
}
public UploadModel uploadFile(String name, InputStream fileStream, String mimeType) throws RestException {
return getRestClient().postMultipart("/api/uploads", fileStream, name, mimeType, UploadModel.class);
}
// endregion
// region DOCUMENT
@SuppressWarnings("unchecked")
public List<CreateDocumentResult> createDocument(CreateDocumentRequest request) throws RestException {
List<CreateDocumentResult> result = (List<CreateDocumentResult>)getRestClient().post("/api/documents", request, TypeToken.getParameterized(List.class, CreateDocumentResult.class));
return result;
}
public void deleteDocument(UUID id) throws RestException {
String requestUri = String.format("api/documents/%s", id.toString());
getRestClient().delete(requestUri);
}
public void addNewDocumentVersion(UUID id, DocumentAddVersionRequest versionRequest) throws RestException {
String requestUri = String.format("api/documents/%s/versions", id.toString());
getRestClient().post(requestUri, versionRequest);
}
public void refuseDocument(UUID id, RefusalRequest refusalRequest) throws RestException {
String requestUri = String.format("api/documents/%s/refusal", id.toString());
getRestClient().post(requestUri, refusalRequest);
}
public void cancelDocument(UUID id, CancelDocumentRequest cancelRequest) throws RestException {
String requestUri = String.format("api/documents/%s/cancellation", id.toString());
getRestClient().post(requestUri, cancelRequest);
}
public DocumentModel getDocumentDetails(UUID id) throws RestException {
String requestUri = String.format("api/documents/%s", id.toString());
DocumentModel document = getRestClient().get(requestUri, DocumentModel.class);
return document;
}
public TicketModel getDocumentDownloadTicket(UUID id, DocumentTicketType type) throws RestException {
String requestUri = String.format("/api/documents/%s/ticket?type=%s", id.toString(), type.getValue());
TicketModel ticket = getRestClient().get(requestUri, TicketModel.class);
return ticket;
}
/**
*
* This method does not use the new patterns to retrieve a document
* @deprecated use {@link #getDocumentContent} instead.
*/
@Deprecated
public InputStream getDocument(UUID id, DocumentTicketType type) throws RestException {
TicketModel ticket = getDocumentDownloadTicket(id, type);
return getRestClient().getStream(ticket.getLocation());
}
public InputStream getDocumentContent(UUID id, DocumentDownloadTypes type) throws RestException {
String requestUri = String.format("/api/documents/%s/content?type=%s", id.toString(), type.getValue());
InputStream documento = getRestClient().getStreamV2(requestUri);
return documento;
}
public byte[] getDocumentBytes(UUID id, DocumentTicketType type) throws IOException, RestException {
byte[] content;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (InputStream stream = getDocument(id, type)) {
int nRead;
byte[] data = new byte[1];
while((nRead = stream.read(data, 0, data.length)) != -1) {
baos.write(data, 0, nRead);
}
content = baos.toByteArray();
}
}
return content;
}
// endregion
// region ACTIONURL
public ActionUrlResponse getActionUrl(UUID documentId, ActionUrlRequest request) throws RestException {
String requestUri = String.format("/api/documents/%s/action-url", documentId.toString());
ActionUrlResponse response = getRestClient().post(requestUri, request, ActionUrlResponse.class);
return response;
}
// endregion
// region SIGNATURE
public StartSignatureResponse startPublicSignature(String key, StartSignatureRequest request) throws RestException {
String requestUri = String.format("/api/documents/keys/%s/signature/certificate", key);
StartSignatureResponse response = getRestClient().post(requestUri, request, StartSignatureResponse.class);
return response;
}
public CompleteSignatureResponse completePublicSignature(String key, CompleteSignatureRequest request) throws RestException {
String requestUri = String.format("/api/documents/keys/%s/signature", key);
CompleteSignatureResponse response = getRestClient().post(requestUri, request, CompleteSignatureResponse.class);
return response;
}
public StartSignatureResponse startSignature(UUID id, StartSignatureRequest request) throws RestException {
String requestUri = String.format("/api/documents/%s/signature/certificate", id.toString());
StartSignatureResponse response = getRestClient().post(requestUri, request, StartSignatureResponse.class);
return response;
}
public CompleteSignatureResponse completeSignature(UUID id, CompleteSignatureRequest request) throws RestException {
String requestUri = String.format("/api/documents/%s/signature", id.toString());
CompleteSignatureResponse response = getRestClient().post(requestUri, request, CompleteSignatureResponse.class);
return response;
}
public void sendElectronicSignatureAuthenticationCode(SendElectronicSignatureAuthenticationRequest request) throws RestException {
getRestClient().post("/api/documents/sms-authentication-code", request);
}
public void signElectronically(UUID id, ElectronicSignatureRequest request) throws RestException {
String requestUri = String.format("/api/documents/%s/electronic-signature", id.toString());
getRestClient().post(requestUri, request);
}
// endregion
// region FOLDER
public FolderInfoModel createFolder(FolderCreateRequest request) throws RestException {
FolderInfoModel folderInfo = getRestClient().post("api/folders", request, FolderInfoModel.class);
return folderInfo;
}
public FolderDetailsModel getFolderDetails(UUID folderId) throws RestException {
String requestUri = String.format("/api/folders/%s/details", folderId.toString());
FolderDetailsModel folderDetails = getRestClient().get(requestUri, FolderDetailsModel.class);
return folderDetails;
}
@SuppressWarnings("unchecked")
public PaginatedSearchResponse<FolderInfoModel> listFoldersPaginated(PaginatedSearchParams searchParams, UUID organizationId) throws RestException {
String orgIdStr = organizationId != null ? organizationId.toString() : "";
String requestUri = String.format("/api/folders%s&organizationId=%s", buildSearchPaginatedParamsString(searchParams), orgIdStr);
PaginatedSearchResponse<FolderInfoModel> model = (PaginatedSearchResponse<FolderInfoModel>)getRestClient().get(requestUri, TypeToken.getParameterized(PaginatedSearchResponse.class, FolderInfoModel.class));
return model;
}
public PaginatedSearchResponse<DocumentListModel> listDocuments(DocumentListParameters searchParams) throws RestException {
String requestUri = String.format("api/documents?%s", buildSearchDocumentListString(searchParams));
PaginatedSearchResponse<DocumentListModel> model = (PaginatedSearchResponse<DocumentListModel>)getRestClient().get(requestUri, TypeToken.getParameterized(PaginatedSearchResponse.class, DocumentListModel.class));
return model;
}
// endregion
// region NOTIFICATIONS
public void sendFlowActionReminder(UUID documentId, UUID flowActionId) throws RestException {
CreateFlowActionReminderRequest request = new CreateFlowActionReminderRequest();
request.setDocumentId(documentId);
request.setFlowActionId(flowActionId);
getRestClient().post("/api/notifications/flow-action-reminder", request);
}
public void sendNotifyPendingUsers(EmailListNotificationRequest emailListNotificationRequest) throws RestException {
getRestClient().post("/api/users/notify-pending", emailListNotificationRequest);
}
public void UpdateInvoiceStatus(int id, InvoicesUpdateInvoicePaymentStatusRequest request) throws RestException, IOException {
getRestClient().putAsJson(String.format("api/invoices/%s/payment", id), request);
}
// endregion
// region PRIVATE
private String buildSearchPaginatedParamsString(PaginatedSearchParams searchParams) {
return String.format("?q=%s&limit=%s&offset=%s", getParameterOrEmpty(searchParams.getQ()), searchParams.getLimit(), searchParams.getOffset());
}
private String buildSearchDocumentListString(DocumentListParameters searchParams) {
if (searchParams.getIsConcluded() && searchParams.getDocumentFilterStatus() == null) {
return String.format("IsConcluded=%s&OrganizationType=Normal&FolderType=%s&FilterByDocumentType=%s&Q=%s&Limit=%s&Offset=0&Order=%s&ParticipantQ=%s", searchParams.getIsConcluded(), searchParams.getFolderType(), searchParams.getFilterByDocumentType(), getParameterOrEmpty(searchParams.getQ()), searchParams.getLimit(), searchParams.getOrder(), searchParams.getParticipantQ());
}
else if(searchParams.getDocumentFilterStatus() == null){
return String.format("OrganizationType=Normal&FolderType=%s&FilterByDocumentType=%s&Q=%s&Limit=%s&Offset=0&Order=%s&ParticipantQ=%s&", searchParams.getFolderType(), searchParams.getFilterByDocumentType(), getParameterOrEmpty(searchParams.getQ()), searchParams.getLimit(), searchParams.getOrder(), searchParams.getParticipantQ());
} else{
return String.format("Status=%s&OrganizationType=Normal&FolderType=%s&FilterByDocumentType=%s&Q=%s&Limit=%s&Offset=0&Order=%s&ParticipantQ=%s&", searchParams.getDocumentFilterStatus(), searchParams.getFolderType(), searchParams.getFilterByDocumentType(), getParameterOrEmpty(searchParams.getQ()), searchParams.getLimit(), searchParams.getOrder(), searchParams.getParticipantQ());
}
}
private String getParameterOrEmpty(String parameter) {
if (parameter == null || parameter.length() == 0) {
return "";
}
try {
return URLEncoder.encode(parameter, "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
// endregion
}