OutputServiceImpl.java
11.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.forms.external.service.FormsContext
* com.adobe.forms.option.LCFormsOptions
* com.adobe.forms.service.AEMFormsHelperService
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
*/
package com.adobe.fd.output.impl;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.internal.utils.Utils;
import com.adobe.fd.output.api.BatchOptions;
import com.adobe.fd.output.api.BatchResult;
import com.adobe.fd.output.api.OutputService;
import com.adobe.fd.output.api.PDFOutputOptions;
import com.adobe.fd.output.api.PrintedOutputOptions;
import com.adobe.fd.output.internal.FlattenPDFFacade;
import com.adobe.fd.output.internal.NonInteractivePDFRenderService;
import com.adobe.fd.output.internal.exception.OutputServiceException;
import com.adobe.fd.output.internal.logging.OutputServiceLogger;
import com.adobe.forms.external.service.FormsContext;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.AEMFormsHelperService;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
@Component(label="AEMFD Output Service", description="AEMFD Output Service")
@Service(value={OutputService.class})
public class OutputServiceImpl
implements OutputService {
OutputServiceLogger logger = new OutputServiceLogger(OutputServiceImpl.class);
@Reference
AEMFormsHelperService aemFormsHelperService;
@Reference
FormsContext formsContext;
@Reference
NonInteractivePDFRenderService nonInteractivePDFRenderService;
@Override
public Document generatePDFOutput(Document inDoc, Document data, PDFOutputOptions pdfOutputOptions) throws OutputServiceException {
if (inDoc == null) {
throw new OutputServiceException("AEM_OUT_001_002", new Object[]{"inDoc"});
}
if (pdfOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "pdfOutputOptions");
pdfOutputOptions = new PDFOutputOptions();
}
try {
this.formsContext.begin();
Document document = this.generatePDFOutput(Utils.getBytes(inDoc), data, pdfOutputOptions);
return document;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Document handlePDFInput(byte[] inDocBytes, Document data, LCFormsOptions lcOptions, boolean retainUnsignedSignatureFields, boolean retainFormState) throws Exception {
FlattenPDFFacade flattenPDFFacade = new FlattenPDFFacade(lcOptions, this.nonInteractivePDFRenderService, retainUnsignedSignatureFields, retainFormState);
try {
flattenPDFFacade.init(inDocBytes);
Document document = flattenPDFFacade.flattenPDF(data);
return document;
}
finally {
if (flattenPDFFacade != null) {
flattenPDFFacade.cleanup();
}
}
}
private Document generatePDFOutput(byte[] inDocBytes, Document data, PDFOutputOptions options) throws OutputServiceException {
try {
if (inDocBytes.length < 4) {
throw new OutputServiceException();
}
LCFormsOptions lcOptions = Utils.create(options);
if (inDocBytes[0] == 37 && inDocBytes[1] == 80 && inDocBytes[2] == 68 && inDocBytes[3] == 70) {
return this.handlePDFInput(inDocBytes, data, lcOptions, options.getRetainUnsignedSignatureFields(), options.getRetainPDFFormState());
}
lcOptions.setTemplateBytes(new String(inDocBytes, "utf-8"));
byte[] resolveTemplate = this.aemFormsHelperService.resolvedTemplate(lcOptions);
return this.nonInteractivePDFRenderService.render(resolveTemplate, data, lcOptions, "", options.getRetainUnsignedSignatureFields(), false);
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
}
@Override
public Document generatePDFOutput(String urlOrFileName, Document data, PDFOutputOptions pdfOutputOptions) throws OutputServiceException {
if (urlOrFileName == null || urlOrFileName.length() < 1) {
throw new OutputServiceException("AEM_OUT_001_001", new Object[]{urlOrFileName, "urlOrFileName"});
}
if (pdfOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "pdfOutputOptions");
pdfOutputOptions = new PDFOutputOptions();
}
try {
Document doc;
this.formsContext.begin();
if (urlOrFileName.endsWith(".pdf")) {
Document document = this.generatePDFOutput(this.aemFormsHelperService.readResource(urlOrFileName, pdfOutputOptions.getContentRoot()), data, pdfOutputOptions);
return document;
}
LCFormsOptions lcOptions = Utils.create(pdfOutputOptions);
lcOptions.setTemplate(urlOrFileName);
byte[] resolveTemplate = this.aemFormsHelperService.resolvedTemplate(lcOptions);
Document document = doc = this.nonInteractivePDFRenderService.render(resolveTemplate, data, lcOptions, "", pdfOutputOptions.getRetainUnsignedSignatureFields(), false);
return document;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
@Override
public BatchResult generatePDFOutputBatch(Map<String, String> templates, Map<String, Document> data, PDFOutputOptions pdfOutputOptions, BatchOptions batchOptions) throws OutputServiceException {
if (templates == null) {
throw new OutputServiceException("AEM_OUT_001_002", new Object[]{"templates"});
}
if (data == null) {
this.logger.warn("AEM_OUT_001_002", "data");
data = new LinkedHashMap<String, Document>();
}
if (pdfOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "pdfOutputOptions");
pdfOutputOptions = new PDFOutputOptions();
}
if (batchOptions == null) {
this.logger.warn("AEM_OUT_001_002", "batchOptions");
batchOptions = new BatchOptions();
}
try {
this.formsContext.begin();
LCFormsOptions lcOptions = Utils.create(pdfOutputOptions);
BatchResult batchResult = this.nonInteractivePDFRenderService.renderBatch(templates, data, lcOptions, batchOptions);
return batchResult;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
@Override
public Document generatePrintedOutput(Document inDoc, Document data, PrintedOutputOptions printedOutputOptions) throws OutputServiceException {
if (inDoc == null) {
throw new OutputServiceException("AEM_OUT_001_002", new Object[]{"inDoc"});
}
if (printedOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "printedOutputOptions");
printedOutputOptions = new PrintedOutputOptions();
}
try {
this.formsContext.begin();
LCFormsOptions lcOptions = Utils.create(printedOutputOptions);
lcOptions.setTemplateBytes(new String(Utils.getBytes(inDoc), "utf-8"));
byte[] resolveTemplate = this.aemFormsHelperService.resolvedTemplate(lcOptions);
Document document = this.nonInteractivePDFRenderService.render(resolveTemplate, data, lcOptions, "", false, false);
return document;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
@Override
public Document generatePrintedOutput(String urlOrFileName, Document data, PrintedOutputOptions printedOutputOptions) throws OutputServiceException {
if (urlOrFileName == null || urlOrFileName.length() < 1) {
throw new OutputServiceException("AEM_OUT_001_001", new Object[]{urlOrFileName, "urlOrFileName"});
}
if (printedOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "printedOutputOptions");
printedOutputOptions = new PrintedOutputOptions();
}
try {
this.formsContext.begin();
LCFormsOptions lcOptions = Utils.create(printedOutputOptions);
lcOptions.setTemplate(urlOrFileName);
byte[] resolveTemplate = this.aemFormsHelperService.resolvedTemplate(lcOptions);
Document document = this.nonInteractivePDFRenderService.render(resolveTemplate, data, lcOptions, "", false, false);
return document;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
@Override
public BatchResult generatePrintedOutputBatch(Map<String, String> templates, Map<String, Document> data, PrintedOutputOptions printedOutputOptions, BatchOptions batchOptions) throws OutputServiceException {
if (templates == null) {
throw new OutputServiceException("AEM_OUT_001_002", new Object[]{"templates"});
}
if (data == null) {
this.logger.warn("AEM_OUT_001_002", "data");
data = new LinkedHashMap<String, Document>();
}
if (printedOutputOptions == null) {
this.logger.warn("AEM_OUT_001_002", "pdfOutputOptions");
printedOutputOptions = new PrintedOutputOptions();
}
if (batchOptions == null) {
this.logger.warn("AEM_OUT_001_002", "batchOptions");
batchOptions = new BatchOptions();
}
try {
this.formsContext.begin();
LCFormsOptions lcOptions = Utils.create(printedOutputOptions);
BatchResult batchResult = this.nonInteractivePDFRenderService.renderBatch(templates, data, lcOptions, batchOptions);
return batchResult;
}
catch (Exception e) {
throw new OutputServiceException("AEM_OUT_001_003", new Object[]{e.getMessage()}, e);
}
finally {
this.formsContext.end();
}
}
protected void bindAemFormsHelperService(AEMFormsHelperService aEMFormsHelperService) {
this.aemFormsHelperService = aEMFormsHelperService;
}
protected void unbindAemFormsHelperService(AEMFormsHelperService aEMFormsHelperService) {
if (this.aemFormsHelperService == aEMFormsHelperService) {
this.aemFormsHelperService = null;
}
}
protected void bindFormsContext(FormsContext formsContext) {
this.formsContext = formsContext;
}
protected void unbindFormsContext(FormsContext formsContext) {
if (this.formsContext == formsContext) {
this.formsContext = null;
}
}
protected void bindNonInteractivePDFRenderService(NonInteractivePDFRenderService nonInteractivePDFRenderService) {
this.nonInteractivePDFRenderService = nonInteractivePDFRenderService;
}
protected void unbindNonInteractivePDFRenderService(NonInteractivePDFRenderService nonInteractivePDFRenderService) {
if (this.nonInteractivePDFRenderService == nonInteractivePDFRenderService) {
this.nonInteractivePDFRenderService = null;
}
}
}