11package com ._4point .aem .docservices .rest_services .client .forms ;
22
33import java .io .IOException ;
4- import java .nio .file .Path ;
5- import java .util .List ;
6- import java .util .Locale ;
4+ import java .util .Objects ;
75import java .util .function .Supplier ;
86
9- import org .glassfish .jersey .media .multipart .FormDataMultiPart ;
10-
7+ import com ._4point .aem .docservices .rest_services .client .RestClient ;
8+ import com ._4point .aem .docservices .rest_services .client .RestClient .ContentType ;
9+ import com ._4point .aem .docservices .rest_services .client .RestClient .MultipartPayload ;
10+ import com ._4point .aem .docservices .rest_services .client .RestClient .RestClientException ;
11+ import com ._4point .aem .docservices .rest_services .client .helpers .AemConfig ;
1112import com ._4point .aem .docservices .rest_services .client .helpers .AemServerType ;
1213import com ._4point .aem .docservices .rest_services .client .helpers .Builder ;
1314import com ._4point .aem .docservices .rest_services .client .helpers .BuilderImpl ;
14- import com ._4point .aem .docservices .rest_services .client .helpers .MultipartTransformer ;
15+ import com ._4point .aem .docservices .rest_services .client .helpers .BuilderImpl . TriFunction ;
1516import com ._4point .aem .docservices .rest_services .client .helpers .RestServicesServiceAdapter ;
16- import com ._4point .aem .fluentforms .api .AbsoluteOrRelativeUrl ;
1717import com ._4point .aem .fluentforms .api .Document ;
18- import com ._4point .aem .fluentforms .api .PathOrUrl ;
1918import com ._4point .aem .fluentforms .api .forms .FormsService .FormsServiceException ;
2019import com ._4point .aem .fluentforms .api .forms .PDFFormRenderOptions ;
2120import com ._4point .aem .fluentforms .api .forms .ValidationOptions ;
2221import com ._4point .aem .fluentforms .api .forms .ValidationResult ;
2322import com ._4point .aem .fluentforms .impl .forms .TraditionalFormsService ;
24- import com .adobe .fd .forms .api .AcrobatVersion ;
25- import com .adobe .fd .forms .api .CacheStrategy ;
2623import com .adobe .fd .forms .api .DataFormat ;
27- import com .adobe .fd .forms .api .RenderAtClient ;
28-
29- import jakarta .ws .rs .client .Client ;
30- import jakarta .ws .rs .client .WebTarget ;
31- import jakarta .ws .rs .core .MediaType ;
32- import jakarta .ws .rs .core .Response ;
3324
3425public class RestServicesFormsServiceAdapter extends RestServicesServiceAdapter implements TraditionalFormsService {
3526
36- private static final String FORM_SERVICE_NAME = "FormsService" ;
27+ private static final String FORMS_SERVICE_NAME = "FormsService" ;
3728 private static final String IMPORT_DATA_METHOD_NAME = "ImportData" ;
3829 private static final String RENDER_PDF_FORM_METHOD_NAME = "RenderPdfForm" ;
3930 private static final String EXPORT_DATA_METHOD_NAME ="ExportData" ;
@@ -50,131 +41,100 @@ public class RestServicesFormsServiceAdapter extends RestServicesServiceAdapter
5041 private static final String SUBMIT_URL_PARAM = "renderOptions.submitUrl" ;
5142 private static final String TAGGED_PDF_PARAM = "renderOptions.taggedPdf" ;
5243 private static final String XCI_PARAM = "renderOptions.xci" ;
44+ private static final String DATA_FORMAT_PARAM = "dataformat" ;
45+ private static final String PDF_OR_XDP_PARAM = "pdforxdp" ;
5346
47+ private final RestClient renderFormRestClient ;
48+ private final RestClient importDataRestClient ;
49+ private final RestClient exportDataRestClient ;
50+
5451 // Only callable from Builder
55- private RestServicesFormsServiceAdapter (WebTarget target , Supplier <String > correlationId , AemServerType aemServerType ) {
56- super (target , correlationId , aemServerType );
57- }
58-
59- @ Override
60- public Document exportData (Document pdfOrXdp , DataFormat dataFormat ) throws FormsServiceException {
61- if (pdfOrXdp == null &&dataFormat == null ) {
62- throw new FormsServiceException ("Internal Error, must provide document and its dataformat" );
63- }
64- WebTarget exportDataTarget = baseTarget .path (constructStandardPath (FORM_SERVICE_NAME , EXPORT_DATA_METHOD_NAME ));
65- try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
66- multipart .field ("pdforxdp" , pdfOrXdp .getInputStream (), APPLICATION_PDF )
67- .field ("dataformat" , DataFormat .XmlData .name ());
68-
69- Response result = postToServer (exportDataTarget , multipart , MediaType .APPLICATION_XML_TYPE );//xml
70-
71-
72- return responseToDoc (result , MediaType .APPLICATION_XML_TYPE , msg ->new FormsServiceException (msg ));
73- } catch (IOException e ) {
74- throw new FormsServiceException ("I/O Error while exporting data. (" + baseTarget .getUri ().toString () + ")." , e );
75- } catch (RestServicesServiceException e ) {
76- throw new FormsServiceException ("Error while POSTing to server" , e );
77- }
78- }
79-
80- @ Override
81- public Document importData (Document pdf , Document data ) throws FormsServiceException {
82- WebTarget importDataTarget = baseTarget .path (constructStandardPath (FORM_SERVICE_NAME , IMPORT_DATA_METHOD_NAME ));
83-
84- try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
85- multipart .field (DATA_PARAM , data .getInputStream (), MediaType .APPLICATION_XML_TYPE )
86- .field (PDF_PARAM , pdf .getInputStream (), APPLICATION_PDF );
87-
88- Response result = postToServer (importDataTarget , multipart , APPLICATION_PDF );
89-
90- return responseToDoc (result , APPLICATION_PDF , msg ->new FormsServiceException (msg ));
91- } catch (IOException e ) {
92- throw new FormsServiceException ("I/O Error while importing data. (" + baseTarget .getUri ().toString () + ")." , e );
93- } catch (RestServicesServiceException e ) {
94- throw new FormsServiceException ("Error while POSTing to server" , e );
95- }
96- }
97-
98-
99- private Document internalRenderPDFForm (String urlOrfilename , Document template , Document data , PDFFormRenderOptions pdfFormRenderOptions ) throws FormsServiceException {
100- if (urlOrfilename != null && template != null ) {
101- throw new FormsServiceException ("Internal Error, must provide one or the other of template String or Document but not both." );
102- }
103- if ((urlOrfilename == null && template == null ) || (urlOrfilename != null && template != null )) {
104- throw new FormsServiceException ("Internal Error, must provide one or the other of template String or Document." );
105- }
106- WebTarget renderPdfTarget = baseTarget .path (constructStandardPath (FORM_SERVICE_NAME , RENDER_PDF_FORM_METHOD_NAME ));
107-
108- AcrobatVersion acrobatVersion = pdfFormRenderOptions .getAcrobatVersion ();
109- CacheStrategy cacheStrategy = pdfFormRenderOptions .getCacheStrategy ();
110- PathOrUrl contentRoot = pdfFormRenderOptions .getContentRoot ();
111- Path debugDir = pdfFormRenderOptions .getDebugDir ();
112- Boolean embedFonts = pdfFormRenderOptions .getEmbedFonts ();
113- Locale locale = pdfFormRenderOptions .getLocale ();
114- RenderAtClient renderAtClient = pdfFormRenderOptions .getRenderAtClient ();
115- List <AbsoluteOrRelativeUrl > submitUrls = pdfFormRenderOptions .getSubmitUrls ();
116- Boolean taggedPDF = pdfFormRenderOptions .getTaggedPDF ();
117- Document xci = pdfFormRenderOptions .getXci ();
118-
119- try (final FormDataMultiPart multipart = new FormDataMultiPart ()) {
120- if (data != null ) {
121- multipart .field (DATA_PARAM , data .getInputStream (), MediaType .APPLICATION_XML_TYPE );
122- }
123- if (urlOrfilename != null ) {
124- multipart .field (TEMPLATE_PARAM , urlOrfilename );
125- }
126- if (template != null ) {
127- multipart .field (TEMPLATE_PARAM , template .getInputStream (), APPLICATION_XDP );
128- }
129-
130- // This code sets the individual fields if they are not null.
131- MultipartTransformer .create (multipart )
132- .transform ((t )->acrobatVersion == null ? t : t .field (ACROBAT_VERSION_PARAM , acrobatVersion .toString ()))
133- .transform ((t )->cacheStrategy == null ? t : t .field (CACHE_STRATEGY_PARAM , cacheStrategy .toString ()))
134- .transform ((t )->contentRoot == null ? t : t .field (CONTENT_ROOT_PARAM , contentRoot .toString ()))
135- .transform ((t )->debugDir == null ? t : t .field (DEBUG_DIR_PARAM , debugDir .toString ()))
136- .transform ((t )->embedFonts == null ? t : t .field (EMBED_FONTS_PARAM , embedFonts .toString ()))
137- .transform ((t )->locale == null ? t : t .field (LOCALE_PARAM , locale .toString ()))
138- .transform ((t )->renderAtClient == null ? t : t .field (RENDER_AT_CLIENT_PARAM , renderAtClient .toString ()))
139- .transform ((t )->taggedPDF == null ? t : t .field (TAGGED_PDF_PARAM , taggedPDF .toString ()))
140- .transform ((t )->submitUrls == null ? t : setSubmitUrls (t , SUBMIT_URL_PARAM , submitUrls ))
141- .transform ((t )->{
142- try {
143- return xci == null ? t : t .field (XCI_PARAM , xci .getInlineData (), MediaType .APPLICATION_XML_TYPE );
144- } catch (IOException e ) {
145- // if we encounter an exception, then we just don't add this field. This should of error shouldn't ever happen.
146- return t ;
147- }
148- })
149- ;
150-
151- Response result = postToServer (renderPdfTarget , multipart , APPLICATION_PDF );
152-
153- return responseToDoc (result , APPLICATION_PDF , msg ->new FormsServiceException (msg ));
154- } catch (IOException e ) {
155- throw new FormsServiceException ("I/O Error while rendering PDF. (" + baseTarget .getUri ().toString () + ")." , e );
156- } catch (RestServicesServiceException e ) {
157- throw new FormsServiceException ("Error while POSTing to server" , e );
158- }
159-
160- }
161-
162- private MultipartTransformer setSubmitUrls (MultipartTransformer t , String submitUrlParam , List <AbsoluteOrRelativeUrl > submitUrls ) {
163- for (AbsoluteOrRelativeUrl submitUrl : submitUrls ) {
164- t = t .field (SUBMIT_URL_PARAM , submitUrl .toString ());
165- }
166- return t ;
167- }
168-
52+ private RestServicesFormsServiceAdapter (BuilderImpl builder , Supplier <String > correlationIdFn ) {
53+ super (correlationIdFn );
54+ this .renderFormRestClient = builder .createClient (FORMS_SERVICE_NAME , RENDER_PDF_FORM_METHOD_NAME );
55+ this .importDataRestClient = builder .createClient (FORMS_SERVICE_NAME , IMPORT_DATA_METHOD_NAME );
56+ this .exportDataRestClient = builder .createClient (FORMS_SERVICE_NAME , EXPORT_DATA_METHOD_NAME );
57+ }
58+
59+ @ Override
60+ public Document exportData (Document pdfOrXdp , DataFormat dataFormat ) throws FormsServiceException {
61+ Objects .requireNonNull (pdfOrXdp , "Document cannot be null." );
62+ Objects .requireNonNull (dataFormat , "Data format cannot be null." );
63+
64+ try (MultipartPayload payload = exportDataRestClient .multipartPayloadBuilder ()
65+ .add (PDF_OR_XDP_PARAM , pdfOrXdp , ContentType .APPLICATION_PDF )
66+ .addStringVersion (DATA_FORMAT_PARAM , dataFormat )
67+ .build ()) {
68+
69+ return payload .postToServer (ContentType .APPLICATION_XML )
70+ .map (RestServicesServiceAdapter ::responseToDoc )
71+ .orElseThrow ();
72+ } catch (IOException e ) {
73+ throw new FormsServiceException ("I/O Error while exporting data. (" + exportDataRestClient .target () + ")." , e );
74+ } catch (RestClientException e ) {
75+ throw new FormsServiceException ("Error while POSTing to server (" + exportDataRestClient .target () + ")." , e );
76+ }
77+ }
78+
79+ @ Override
80+ public Document importData (Document pdf , Document data ) throws FormsServiceException {
81+ Objects .requireNonNull (pdf , "PDF document cannot be null." );
82+ Objects .requireNonNull (data , "Data document cannot be null." );
83+
84+ try (MultipartPayload payload = importDataRestClient .multipartPayloadBuilder ()
85+ .add (PDF_PARAM , pdf , ContentType .APPLICATION_PDF )
86+ .add (DATA_PARAM , data , ContentType .APPLICATION_XML )
87+ .build ()) {
88+
89+ return payload .postToServer (ContentType .APPLICATION_PDF )
90+ .map (RestServicesServiceAdapter ::responseToDoc )
91+ .orElseThrow ();
92+ } catch (IOException e ) {
93+ throw new FormsServiceException ("I/O Error while importing data. (" + importDataRestClient .target () + ")." , e );
94+ } catch (RestClientException e ) {
95+ throw new FormsServiceException ("Error while POSTing to server (" + importDataRestClient .target () + ")." , e );
96+ }
97+ }
98+
99+ private Document internalRenderPDFForm (String urlOrfilename , Document template , Document data , PDFFormRenderOptions pdfFormRenderOptions ) throws FormsServiceException {
100+ Objects .requireNonNull (pdfFormRenderOptions , "PdfFormRenderOptions cannot be null." );
101+
102+ var xci = pdfFormRenderOptions .getXci ();
103+ try (MultipartPayload payload = renderFormRestClient .multipartPayloadBuilder ()
104+ .addIfNotNull (TEMPLATE_PARAM , urlOrfilename ) // Since this is internal, we know that one of these two will be null
105+ .addIfNotNull (TEMPLATE_PARAM , template , ContentType .APPLICATION_XML )
106+ .addIfNotNull (DATA_PARAM , data , ContentType .APPLICATION_XML )
107+ .addStringVersion (ACROBAT_VERSION_PARAM , pdfFormRenderOptions .getAcrobatVersion ())
108+ .addStringVersion (CACHE_STRATEGY_PARAM , pdfFormRenderOptions .getCacheStrategy ())
109+ .addStringVersion (CONTENT_ROOT_PARAM , pdfFormRenderOptions .getContentRoot ())
110+ .addStringVersion (DEBUG_DIR_PARAM , pdfFormRenderOptions .getDebugDir ())
111+ .addStringVersion (EMBED_FONTS_PARAM , pdfFormRenderOptions .getEmbedFonts ())
112+ .addStringVersion (LOCALE_PARAM , pdfFormRenderOptions .getLocale ())
113+ .addStringVersion (RENDER_AT_CLIENT_PARAM , pdfFormRenderOptions .getRenderAtClient ())
114+ .addStringVersion (SUBMIT_URL_PARAM , pdfFormRenderOptions .getSubmitUrls ())
115+ .addStringVersion (TAGGED_PDF_PARAM , pdfFormRenderOptions .getTaggedPDF ())
116+ .addIfNotNull (XCI_PARAM , xci , ContentType .APPLICATION_XML )
117+ .build ()) {
118+
119+ return payload .postToServer (ContentType .APPLICATION_PDF )
120+ .map (RestServicesServiceAdapter ::responseToDoc )
121+ .orElseThrow ();
122+ } catch (IOException e ) {
123+ throw new FormsServiceException ("I/O Error while rendering form. (" + renderFormRestClient .target () + ")." , e );
124+ } catch (RestClientException e ) {
125+ throw new FormsServiceException ("Error while POSTing to server (" + renderFormRestClient .target () + ")." , e );
126+ }
127+ }
128+
169129 @ Override
170130 public Document renderPDFForm (String urlOrfilename , Document data , PDFFormRenderOptions pdfFormRenderOptions )
171131 throws FormsServiceException {
172- return internalRenderPDFForm (urlOrfilename , null , data , pdfFormRenderOptions );
132+ return internalRenderPDFForm (Objects . requireNonNull ( urlOrfilename , "Template string cannot be null." ) , null , data , pdfFormRenderOptions );
173133 }
174134 @ Override
175135 public Document renderPDFForm (Document template , Document data , PDFFormRenderOptions pdfFormRenderOptions )
176136 throws FormsServiceException {
177- return internalRenderPDFForm (null , template , data , pdfFormRenderOptions );
137+ return internalRenderPDFForm (null , Objects . requireNonNull ( template , "Template document cannot be null." ) , data , pdfFormRenderOptions );
178138 }
179139
180140 @ Override
@@ -188,17 +148,16 @@ public ValidationResult validate(String template, Document data, ValidationOptio
188148 *
189149 * @return build object
190150 */
191- public static FormsServiceBuilder builder () {
192- return new FormsServiceBuilder ();
151+ public static FormsServiceBuilder builder (TriFunction < AemConfig , String , Supplier < String >, RestClient > clientFactory ) {
152+ return new FormsServiceBuilder (clientFactory );
193153 }
194154
195155 public static class FormsServiceBuilder implements Builder {
196- private BuilderImpl builder = new BuilderImpl ();
197- // private final static Supplier<Client> defaultClientFactory = ()->ClientBuilder.newClient();
156+ private BuilderImpl builder ;
198157
199- private FormsServiceBuilder () {
200- super ( );
201- }
158+ private FormsServiceBuilder (TriFunction < AemConfig , String , Supplier < String >, RestClient > clientFactory ) {
159+ this . builder = new BuilderImpl ( clientFactory );
160+ }
202161
203162 @ Override
204163 public FormsServiceBuilder machineName (String machineName ) {
@@ -218,12 +177,6 @@ public FormsServiceBuilder useSsl(boolean useSsl) {
218177 return this ;
219178 }
220179
221- @ Override
222- public FormsServiceBuilder clientFactory (Supplier <Client > clientFactory ) {
223- builder .clientFactory (clientFactory );
224- return this ;
225- }
226-
227180 @ Override
228181 public FormsServiceBuilder basicAuthentication (String username , String password ) {
229182 builder .basicAuthentication (username , password );
@@ -241,11 +194,6 @@ public Supplier<String> getCorrelationIdFn() {
241194 return builder .getCorrelationIdFn ();
242195 }
243196
244- @ Override
245- public WebTarget createLocalTarget () {
246- return builder .createLocalTarget ();
247- }
248-
249197 @ Override
250198 public FormsServiceBuilder aemServerType (AemServerType serverType ) {
251199 builder .aemServerType (serverType );
@@ -258,7 +206,7 @@ public AemServerType getAemServerType() {
258206 }
259207
260208 public RestServicesFormsServiceAdapter build () {
261- return new RestServicesFormsServiceAdapter (this . createLocalTarget () , this .getCorrelationIdFn (), this . getAemServerType ());
209+ return new RestServicesFormsServiceAdapter (builder , this .getCorrelationIdFn ());
262210 }
263211 }
264- }
212+ }
0 commit comments