-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDocScanService.java
594 lines (514 loc) · 26.8 KB
/
DocScanService.java
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package com.yoti.api.client.docs;
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_DELETE;
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_GET;
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_POST;
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_PUT;
import static com.yoti.api.client.spi.remote.call.YotiConstants.CONTENT_TYPE;
import static com.yoti.api.client.spi.remote.call.YotiConstants.CONTENT_TYPE_JSON;
import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_YOTI_DOCS_URL;
import static com.yoti.api.client.spi.remote.call.YotiConstants.PROPERTY_YOTI_DOCS_URL;
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
import static com.yoti.api.client.spi.remote.util.Validation.notNullOrEmpty;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import com.yoti.api.client.Media;
import com.yoti.api.client.docs.session.create.CreateSessionResult;
import com.yoti.api.client.docs.session.create.SessionSpec;
import com.yoti.api.client.docs.session.create.facecapture.CreateFaceCaptureResourcePayload;
import com.yoti.api.client.docs.session.create.facecapture.UploadFaceCaptureImagePayload;
import com.yoti.api.client.docs.session.devicemetadata.MetadataResponse;
import com.yoti.api.client.docs.session.instructions.Instructions;
import com.yoti.api.client.docs.session.retrieve.CreateFaceCaptureResourceResponse;
import com.yoti.api.client.docs.session.retrieve.GetSessionResult;
import com.yoti.api.client.docs.session.retrieve.configuration.SessionConfigurationResponse;
import com.yoti.api.client.docs.session.retrieve.instructions.ContactProfileResponse;
import com.yoti.api.client.docs.session.retrieve.instructions.InstructionsResponse;
import com.yoti.api.client.docs.support.SupportedDocumentsResponse;
import com.yoti.api.client.spi.remote.MediaValue;
import com.yoti.api.client.spi.remote.call.ResourceException;
import com.yoti.api.client.spi.remote.call.SignedRequest;
import com.yoti.api.client.spi.remote.call.SignedRequestBuilderFactory;
import com.yoti.api.client.spi.remote.call.SignedRequestResponse;
import com.yoti.api.client.spi.remote.call.factory.UnsignedPathFactory;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Service built to handle the interactions between the client and Doc Scan APIs
*/
final class DocScanService {
private static final Logger LOG = LoggerFactory.getLogger(DocScanService.class);
private static final TypeReference<List<MetadataResponse>> METADATA_RESPONSE_TYPE_REF = new TypeReference<List<MetadataResponse>>() {};
private static final int HTTP_STATUS_NO_CONTENT = 204;
private static final String YOTI_MULTIPART_BOUNDARY = "yoti-doc-scan-boundary";
private final UnsignedPathFactory unsignedPathFactory;
private final ObjectMapper objectMapper;
private final SignedRequestBuilderFactory signedRequestBuilderFactory;
private final String apiUrl;
private DocScanService(UnsignedPathFactory pathFactory,
ObjectMapper objectMapper,
SignedRequestBuilderFactory signedRequestBuilderFactory) {
this.unsignedPathFactory = pathFactory;
this.objectMapper = objectMapper;
this.signedRequestBuilderFactory = signedRequestBuilderFactory;
apiUrl = System.getProperty(PROPERTY_YOTI_DOCS_URL, DEFAULT_YOTI_DOCS_URL);
}
public static DocScanService newInstance() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.registerModule(new JavaTimeModule());
return new DocScanService(new UnsignedPathFactory(), objectMapper, new SignedRequestBuilderFactory());
}
/**
* Uses the supplied session specification to create a session
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionSpec the {@code SessionSpec}
* @return the session creation result
* @throws DocScanException if there was an error
*/
public CreateSessionResult createSession(String sdkId, KeyPair keyPair, SessionSpec sessionSpec) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNull(sessionSpec, "sessionSpec");
String path = unsignedPathFactory.createNewYotiDocsSessionPath(sdkId);
LOG.info("Creating session at '{}'", path);
try {
byte[] payload = objectMapper.writeValueAsBytes(sessionSpec);
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_POST)
.withPayload(payload)
.withHeader(CONTENT_TYPE, CONTENT_TYPE_JSON)
.build();
return signedRequest.execute(CreateSessionResult.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error posting the request: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
} catch (Exception ex) {
throw new DocScanException("Error creating the session: " + ex.getMessage(), ex);
}
}
/**
* Retrieves the current state of a given session
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionId the session ID
* @return the session state
* @throws DocScanException if there was an error
*/
public GetSessionResult retrieveSession(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createYotiDocsSessionPath(sdkId, sessionId);
LOG.info("Fetching session from '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(GetSessionResult.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
} catch (Exception ex) {
throw new DocScanException("Error retrieving the session: " + ex.getMessage(), ex);
}
}
/**
* Deletes a session and all of its associated content
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionId the session ID
* @throws DocScanException if there was an error
*/
public void deleteSession(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createYotiDocsSessionPath(sdkId, sessionId);
LOG.info("Deleting session from '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_DELETE)
.build();
signedRequest.execute();
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the DELETE: " + ex.getMessage(), ex);
} catch (IOException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
} catch (Exception ex) {
throw new DocScanException("Error deleting the session: " + ex.getMessage(), ex);
}
}
/**
* Retrieves {@link Media} content for a given session and media ID
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionId the session ID
* @param mediaId the media ID
* @return the {@code Media} content, null if 204 No Content
* @throws DocScanException if there was an error
*/
public Media getMediaContent(String sdkId, KeyPair keyPair, String sessionId, String mediaId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
notNullOrEmpty(mediaId, "mediaId");
String path = unsignedPathFactory.createMediaContentPath(sdkId, sessionId, mediaId);
LOG.info("Fetching media from '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
SignedRequestResponse response = signedRequest.execute();
if (response.getResponseCode() == HTTP_STATUS_NO_CONTENT) {
return null;
}
return new MediaValue(findContentType(response), response.getResponseBody());
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
/**
* Deletes media content for a given session and media ID
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionId the session ID
* @param mediaId the media ID
* @throws DocScanException if there was an error
*/
public void deleteMediaContent(String sdkId, KeyPair keyPair, String sessionId, String mediaId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
notNullOrEmpty(mediaId, "mediaId");
String path = unsignedPathFactory.createMediaContentPath(sdkId, sessionId, mediaId);
LOG.info("Deleting media at '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_DELETE)
.build();
signedRequest.execute();
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the DELETE: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public void putIbvInstructions(String sdkId, KeyPair keyPair, String sessionId, Instructions instructions) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
notNull(instructions, "instructions");
String path = unsignedPathFactory.createPutIbvInstructionsPath(sdkId, sessionId);
LOG.info("Setting IBV instructions at '{}'", path);
try {
byte[] payload = objectMapper.writeValueAsBytes(instructions);
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_PUT)
.withPayload(payload)
.build();
signedRequest.execute();
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the PUT: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public InstructionsResponse getIbvInstructions(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createFetchIbvInstructionsPath(sdkId, sessionId);
LOG.info("Fetching IBV instructions at '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(InstructionsResponse.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
/**
* Retrieves the current state of a given session
*
* @param sdkId the SDK ID
* @param keyPair the {@code KeyPair}
* @param sessionId the session ID
* @return the session state
* @throws DocScanException if there was an error
*/
public ContactProfileResponse fetchInstructionsContactProfile(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createFetchInstructionsContactProfilePath(sdkId, sessionId);
LOG.info("Fetching instruction contact profile from '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(ContactProfileResponse.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public Media getIbvInstructionsPdf(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createFetchIbvInstructionsPdfPath(sdkId, sessionId);
LOG.info("Fetching instructions PDF at '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
SignedRequestResponse response = signedRequest.execute();
if (response.getResponseCode() == HTTP_STATUS_NO_CONTENT) {
return null;
}
return new MediaValue(findContentType(response), response.getResponseBody());
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public SessionConfigurationResponse fetchSessionConfiguration(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createFetchSessionConfigurationPath(sdkId, sessionId);
LOG.info("Fetching session configuration from '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(SessionConfigurationResponse.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public CreateFaceCaptureResourceResponse createFaceCaptureResource(String sdkId,
KeyPair keyPair,
String sessionId,
CreateFaceCaptureResourcePayload createFaceCaptureResourcePayload) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
notNull(createFaceCaptureResourcePayload, "createFaceCaptureResourcePayload");
String path = unsignedPathFactory.createNewFaceCaptureResourcePath(sdkId, sessionId);
LOG.info("Creating new Face Capture resource at '{}'", path);
try {
byte[] payload = objectMapper.writeValueAsBytes(createFaceCaptureResourcePayload);
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withPayload(payload)
.withHttpMethod(HTTP_POST)
.build();
return signedRequest.execute(CreateFaceCaptureResourceResponse.class);
} catch (GeneralSecurityException ex) {
throw new DocScanException("Error signing the request: " + ex.getMessage(), ex);
} catch (ResourceException ex) {
throw new DocScanException("Error executing the POST: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public void uploadFaceCaptureImage(String sdkId, KeyPair keyPair, String sessionId, String resourceId, UploadFaceCaptureImagePayload faceCaptureImagePayload)
throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
notNullOrEmpty(resourceId, "resourceId");
notNull(faceCaptureImagePayload, "faceCaptureImagePayload");
String path = unsignedPathFactory.createUploadFaceCaptureImagePath(sdkId, sessionId, resourceId);
LOG.info("Uploading image to Face Capture resource at '{}'", path);
try {
signedRequestBuilderFactory.create()
.withMultipartBoundary(YOTI_MULTIPART_BOUNDARY)
.withMultipartBinaryBody(
"binary-content",
faceCaptureImagePayload.getImageContents(),
ContentType.parse(faceCaptureImagePayload.getImageContentType()),
"face-capture-image")
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_PUT)
.build()
.execute();
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the PUT: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public SupportedDocumentsResponse getSupportedDocuments(KeyPair keyPair, boolean includeNonLatin) throws DocScanException {
notNull(keyPair, "Application key Pair");
String path = unsignedPathFactory.createGetSupportedDocumentsPath(includeNonLatin);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(SupportedDocumentsResponse.class);
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public void triggerIbvEmailNotification(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createTriggerIbvEmailNotificationPath(sdkId, sessionId);
LOG.info("Triggering IBV email notification at '{}'", path);
try {
signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_POST)
.build()
.execute();
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the POST: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public List<MetadataResponse> getTrackedDevices(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createFetchTrackedDevices(sdkId, sessionId);
LOG.info("Fetching tracked devices at '{}'", path);
try {
SignedRequest signedRequest = signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_GET)
.build();
return signedRequest.execute(METADATA_RESPONSE_TYPE_REF);
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
public void deleteTrackedDevices(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");
String path = unsignedPathFactory.createDeleteTrackedDevices(sdkId, sessionId);
LOG.info("Deleting tracked devices at '{}'", path);
try {
signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_DELETE)
.build()
.execute();
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}
private String findContentType(SignedRequestResponse response) {
List<String> contentTypeValues = null;
for (Map.Entry<String, List<String>> entry : response.getResponseHeaders().entrySet()) {
if (entry.getKey() != null && entry.getKey().toLowerCase(Locale.ENGLISH).equals(CONTENT_TYPE.toLowerCase(Locale.ENGLISH))) {
contentTypeValues = entry.getValue();
break;
}
}
return contentTypeValues == null || contentTypeValues.isEmpty() ? "" : contentTypeValues.get(0);
}
}