22
33import java .io .IOException ;
44import java .util .List ;
5+ import java .util .Objects ;
56import java .util .function .Supplier ;
67
7- import org .glassfish .jersey .media .multipart .FormDataMultiPart ;
8-
8+ import com ._4point .aem .docservices .rest_services .client .RestClient ;
9+ import com ._4point .aem .docservices .rest_services .client .RestClient .ContentType ;
10+ import com ._4point .aem .docservices .rest_services .client .RestClient .MultipartPayload ;
11+ import com ._4point .aem .docservices .rest_services .client .RestClient .RestClientException ;
12+ import com ._4point .aem .docservices .rest_services .client .helpers .AemConfig ;
913import com ._4point .aem .docservices .rest_services .client .helpers .AemServerType ;
1014import com ._4point .aem .docservices .rest_services .client .helpers .Builder ;
1115import com ._4point .aem .docservices .rest_services .client .helpers .BuilderImpl ;
12- import com ._4point .aem .docservices .rest_services .client .helpers .MultipartTransformer ;
16+ import com ._4point .aem .docservices .rest_services .client .helpers .BuilderImpl . TriFunction ;
1317import com ._4point .aem .docservices .rest_services .client .helpers .RestServicesServiceAdapter ;
1418import com ._4point .aem .fluentforms .api .Document ;
1519import com ._4point .aem .fluentforms .api .docassurance .DocAssuranceService .DocAssuranceServiceException ;
2024import com .adobe .fd .encryption .client .EncryptionTypeResult ;
2125import com .adobe .fd .readerextensions .client .GetUsageRightsResult ;
2226import com .adobe .fd .readerextensions .client .ReaderExtensionsOptionSpec ;
27+ import com .adobe .fd .readerextensions .client .UsageRights ;
2328import com .adobe .fd .signatures .client .types .FieldMDPOptionSpec ;
2429import com .adobe .fd .signatures .client .types .PDFDocumentVerificationInfo ;
2530import com .adobe .fd .signatures .client .types .PDFSeedValueOptionSpec ;
3338import com .adobe .fd .signatures .pdf .inputs .ValidationPreferences ;
3439import com .adobe .fd .signatures .pki .client .types .common .RevocationCheckStyle ;
3540
36- import jakarta .ws .rs .client .Client ;
37- import jakarta .ws .rs .client .WebTarget ;
38- import jakarta .ws .rs .core .Response ;
39-
4041public class RestServicesDocAssuranceServiceAdapter extends RestServicesServiceAdapter implements TraditionalDocAssuranceService {
4142
4243 private static final String SECURE_DOCUMENT_SERVICE_NAME = "DocAssuranceService" ;
@@ -58,79 +59,62 @@ public class RestServicesDocAssuranceServiceAdapter extends RestServicesServiceA
5859 private static final String ENABLED_ONLINE_FORMS_PARAM = "usageRights.enabledOnlineForms" ;
5960 private static final String ENABLED_SUBMIT_STANDALONE_PARAM = "usageRights.enabledSubmitStandalone" ;
6061
61- // Only callable from Builder
62- private RestServicesDocAssuranceServiceAdapter (WebTarget target , Supplier <String > correlationId , AemServerType aemServerType ) {
63- super (target , correlationId , aemServerType );
62+
63+ private final RestClient secureDocumentRestClient ;
64+
65+ RestServicesDocAssuranceServiceAdapter (BuilderImpl builder , Supplier <String > correlationIdFn ) {
66+ super (correlationIdFn );;
67+ this .secureDocumentRestClient = builder .createClient (SECURE_DOCUMENT_SERVICE_NAME , SECURE_DOCUMENT_METHOD_NAME );
6468 }
6569
70+
6671 @ Override
6772 public Document secureDocument (Document inDocument , EncryptionOptions encryptionOptions , SignatureOptions signatureOptions , ReaderExtensionOptions readerExtensionOptions ,
6873 UnlockOptions unlockOptions ) throws DocAssuranceServiceException {
69- WebTarget secureDocTarget = baseTarget .path (constructStandardPath (SECURE_DOCUMENT_SERVICE_NAME , SECURE_DOCUMENT_METHOD_NAME ));
70-
71- try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
72- if (encryptionOptions != null ) {
73- // TODO Implement this
74- throw new UnsupportedOperationException ("Encryption support has not yet been added to FluentForms library." );
75- }
76-
77- if (signatureOptions != null ) {
78- // TODO Implement this
79- throw new UnsupportedOperationException ("Digital Signature support has not yet been added to FluentForms library." );
80- }
81-
82- if (readerExtensionOptions != null ) {
83- String credentialAlias = readerExtensionOptions .getCredentialAlias ();
84- ReaderExtensionsOptionSpec reOptionsSpec = readerExtensionOptions .getReOptions ();
85-
86- multipart .field (DOCUMENT_PARAM , inDocument .getInputStream (), APPLICATION_PDF );
87- multipart .field (CREDENTIAL_ALIAS_PARAM , credentialAlias );
88-
89- if (reOptionsSpec != null ) {
90- String message = reOptionsSpec .getMessage ();
91- Boolean isModeFinal = reOptionsSpec .isModeFinal ();
92- Boolean enabledBarcodeDecoding = reOptionsSpec .getUsageRights ().isEnabledBarcodeDecoding ();
93- Boolean enabledComments = reOptionsSpec .getUsageRights ().isEnabledComments ();
94- Boolean enabledCommentsOnline = reOptionsSpec .getUsageRights ().isEnabledCommentsOnline ();
95- Boolean enabledDigitalSignatures = reOptionsSpec .getUsageRights ().isEnabledDigitalSignatures ();
96- Boolean enabledDynamicFormFields = reOptionsSpec .getUsageRights ().isEnabledDynamicFormFields ();
97- Boolean enabledDynamicFormPages = reOptionsSpec .getUsageRights ().isEnabledDynamicFormPages ();
98- Boolean enabledEmbeddedFiles = reOptionsSpec .getUsageRights ().isEnabledEmbeddedFiles ();
99- Boolean enabledFormDateImportExport = reOptionsSpec .getUsageRights ().isEnabledFormDataImportExport ();
100- Boolean enabledFormFillIn = reOptionsSpec .getUsageRights ().isEnabledFormFillIn ();
101- Boolean enabledOnlineForms = reOptionsSpec .getUsageRights ().isEnabledOnlineForms ();
102- Boolean enabledSubmitStandalone = reOptionsSpec .getUsageRights ().isEnabledSubmitStandalone ();
103-
104- // Set fields for non-null values.
105- MultipartTransformer .create (multipart )
106- .transform ((t )->message == null ? t : t .field (MESSAGE_PARAM , message ))
107- .transform ((t )->isModeFinal == null ? t : t .field (IS_MODE_FINAL_PARAM , isModeFinal .toString ()))
108- .transform ((t )->enabledBarcodeDecoding == null ? t : t .field (ENABLED_BARCODED_DECODING_PARAM , enabledBarcodeDecoding .toString ()))
109- .transform ((t )->enabledComments == null ? t : t .field (ENABLED_COMMENTS_PARAM , enabledComments .toString ()))
110- .transform ((t )->enabledCommentsOnline == null ? t : t .field (ENABLED_COMMENTS_ONLINE_PARAM , enabledCommentsOnline .toString ()))
111- .transform ((t )->enabledDigitalSignatures == null ? t : t .field (ENABLED_DIGITAL_SIGNATURES_PARAM , enabledDigitalSignatures .toString ()))
112- .transform ((t )->enabledDynamicFormFields == null ? t : t .field (ENABLED_DYNAMIC_FORM_FIELDS_PARAM , enabledDynamicFormFields .toString ()))
113- .transform ((t )->enabledDynamicFormPages == null ? t : t .field (ENABLED_DYNAMIC_FORM_PAGES_PARAM , enabledDynamicFormPages .toString ()))
114- .transform ((t )->enabledEmbeddedFiles == null ? t : t .field (ENABLED_EMBEDDED_FILES_PARAM , enabledEmbeddedFiles .toString ()))
115- .transform ((t )->enabledFormDateImportExport == null ? t : t .field (ENABLED_FORM_DATA_IMPORT_EXPORT_PARAM , enabledFormDateImportExport .toString ()))
116- .transform ((t )->enabledFormFillIn == null ? t : t .field (ENABLED_FORM_FILL_IN_PARAM , enabledFormFillIn .toString ()))
117- .transform ((t )->enabledOnlineForms == null ? t : t .field (ENABLED_ONLINE_FORMS_PARAM , enabledOnlineForms .toString ()))
118- .transform ((t )->enabledSubmitStandalone == null ? t : t .field (ENABLED_SUBMIT_STANDALONE_PARAM , enabledSubmitStandalone .toString ()));
119- }
120- }
121-
122- if (unlockOptions != null ) {
123- // TODO Auto-generated method stub
124- }
125-
126- Response result = postToServer (secureDocTarget , multipart , APPLICATION_PDF );
127-
128- return responseToDoc (result , APPLICATION_PDF , msg ->new DocAssuranceServiceException (msg ));
129- } catch (IOException e ) {
130- throw new DocAssuranceServiceException ("I/O Error while reader extending the document. (" + baseTarget .getUri ().toString () + ")." , e );
131- } catch (RestServicesServiceException e ) {
132- throw new DocAssuranceServiceException ("Error while POSTing to server" , e );
74+ Objects .requireNonNull (inDocument , "Input document cannot be null." );
75+ if (encryptionOptions != null ) {
76+ // TODO Implement this
77+ throw new UnsupportedOperationException ("Encryption support has not yet been added to FluentForms library." );
78+ }
79+
80+ if (signatureOptions != null ) {
81+ // TODO Implement this
82+ throw new UnsupportedOperationException ("Digital Signature support has not yet been added to FluentForms library." );
83+ }
84+
85+ if (unlockOptions != null ) {
86+ // TODO Implement this
87+ throw new UnsupportedOperationException ("Unlock Options support has not yet been added to FluentForms library." );
13388 }
89+
90+ ReaderExtensionsOptionSpec reOptionsSpec = readerExtensionOptions != null ? readerExtensionOptions .getReOptions () : null ;
91+ String reCredentialAlias = readerExtensionOptions != null ? Objects .requireNonNull (readerExtensionOptions .getCredentialAlias (), "Reader Extensions credential alias cannot be null." ) : null ;
92+ UsageRights reUsageOptionsSpec = reOptionsSpec != null ? reOptionsSpec .getUsageRights () : null ;
93+ try (MultipartPayload payload = secureDocumentRestClient .multipartPayloadBuilder ()
94+ .add (DOCUMENT_PARAM , inDocument , ContentType .APPLICATION_PDF )
95+ .addIfNotNull (CREDENTIAL_ALIAS_PARAM , reCredentialAlias )
96+ .transformAndAdd (MESSAGE_PARAM , reOptionsSpec , ReaderExtensionsOptionSpec ::getMessage )
97+ .transformAndAddStringVersion (IS_MODE_FINAL_PARAM , reOptionsSpec , ReaderExtensionsOptionSpec ::isModeFinal )
98+ .transformAndAddStringVersion (ENABLED_BARCODED_DECODING_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledBarcodeDecoding )
99+ .transformAndAddStringVersion (ENABLED_COMMENTS_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledComments )
100+ .transformAndAddStringVersion (ENABLED_COMMENTS_ONLINE_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledCommentsOnline )
101+ .transformAndAddStringVersion (ENABLED_DIGITAL_SIGNATURES_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledDigitalSignatures )
102+ .transformAndAddStringVersion (ENABLED_DYNAMIC_FORM_FIELDS_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledDynamicFormFields )
103+ .transformAndAddStringVersion (ENABLED_DYNAMIC_FORM_PAGES_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledDynamicFormPages )
104+ .transformAndAddStringVersion (ENABLED_EMBEDDED_FILES_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledEmbeddedFiles )
105+ .transformAndAddStringVersion (ENABLED_FORM_DATA_IMPORT_EXPORT_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledFormDataImportExport )
106+ .transformAndAddStringVersion (ENABLED_FORM_FILL_IN_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledFormFillIn )
107+ .transformAndAddStringVersion (ENABLED_ONLINE_FORMS_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledOnlineForms )
108+ .transformAndAddStringVersion (ENABLED_SUBMIT_STANDALONE_PARAM , reUsageOptionsSpec , UsageRights ::isEnabledSubmitStandalone )
109+ .build ()) {
110+ return payload .postToServer (ContentType .APPLICATION_PDF )
111+ .map (RestServicesServiceAdapter ::responseToDoc )
112+ .orElseThrow ();
113+ } catch (IOException e ) {
114+ throw new DocAssuranceServiceException ("I/O Error while securing document. (" + secureDocumentRestClient .target () + ")." , e );
115+ } catch (RestClientException e ) {
116+ throw new DocAssuranceServiceException ("Error while POSTing to server (" + secureDocumentRestClient .target () + ")." , e );
117+ }
134118 }
135119
136120 @ Override
@@ -241,16 +225,15 @@ public Document applyDocumentTimeStamp(Document inDoc, VerificationTime verifica
241225 *
242226 * @return build object
243227 */
244- public static DocAssuranceServiceBuilder builder () {
245- return new DocAssuranceServiceBuilder ();
228+ public static DocAssuranceServiceBuilder builder (TriFunction < AemConfig , String , Supplier < String >, RestClient > clientFactory ) {
229+ return new DocAssuranceServiceBuilder (clientFactory );
246230 }
247231
248232 public static class DocAssuranceServiceBuilder implements Builder {
249- private BuilderImpl builder = new BuilderImpl ();
250- // private final static Supplier<Client> defaultClientFactory = ()->ClientBuilder.newClient();
233+ private final BuilderImpl builder ;
251234
252- private DocAssuranceServiceBuilder () {
253- super ( );
235+ private DocAssuranceServiceBuilder (TriFunction < AemConfig , String , Supplier < String >, RestClient > clientFactory ) {
236+ builder = new BuilderImpl ( clientFactory );
254237 }
255238
256239 @ Override
@@ -271,12 +254,6 @@ public DocAssuranceServiceBuilder useSsl(boolean useSsl) {
271254 return this ;
272255 }
273256
274- @ Override
275- public DocAssuranceServiceBuilder clientFactory (Supplier <Client > clientFactory ) {
276- builder .clientFactory (clientFactory );
277- return this ;
278- }
279-
280257 @ Override
281258 public DocAssuranceServiceBuilder basicAuthentication (String username , String password ) {
282259 builder .basicAuthentication (username , password );
@@ -294,11 +271,6 @@ public Supplier<String> getCorrelationIdFn() {
294271 return builder .getCorrelationIdFn ();
295272 }
296273
297- @ Override
298- public WebTarget createLocalTarget () {
299- return builder .createLocalTarget ();
300- }
301-
302274 @ Override
303275 public DocAssuranceServiceBuilder aemServerType (AemServerType serverType ) {
304276 builder .aemServerType (serverType );
@@ -311,7 +283,7 @@ public AemServerType getAemServerType() {
311283 }
312284
313285 public RestServicesDocAssuranceServiceAdapter build () {
314- return new RestServicesDocAssuranceServiceAdapter (this . createLocalTarget () , this .getCorrelationIdFn (), this . getAemServerType ());
286+ return new RestServicesDocAssuranceServiceAdapter (builder , this .getCorrelationIdFn ());
315287 }
316288 }
317289}
0 commit comments