DoRServiceImpl.java
15.6 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.addon.dor.DoRGenerationException
* com.adobe.aemds.guide.addon.dor.DoROptions
* com.adobe.aemds.guide.addon.dor.DoRResult
* com.adobe.aemds.guide.addon.dor.DoRService
* com.adobe.aemds.guide.service.GuideLocalizationService
* com.adobe.aemds.guide.service.GuideModelTransformer
* com.adobe.aemds.guide.service.XFAModelTransformer
* com.adobe.aemds.guide.utils.GuideModelUtils
* com.adobe.aemds.guide.utils.GuideUtils
* com.adobe.aemds.guide.utils.JSONCreationOptions
* com.adobe.aemds.guide.utils.XMLUtils
* com.adobe.aemfd.docmanager.Document
* com.adobe.fd.assembler.client.AssemblerOptionSpec
* com.adobe.fd.assembler.client.AssemblerResult
* com.adobe.fd.assembler.service.AssemblerService
* com.adobe.forms.common.service.FileAttachmentWrapper
* com.adobe.granite.i18n.LocaleUtil
* com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
* org.apache.commons.io.IOUtils
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.xml.XML
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.addon.dor.service.impl;
import com.adobe.aemds.guide.addon.dor.DoRGenerationException;
import com.adobe.aemds.guide.addon.dor.DoROptions;
import com.adobe.aemds.guide.addon.dor.DoRResult;
import com.adobe.aemds.guide.addon.dor.DoRService;
import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.aemds.guide.service.GuideModelTransformer;
import com.adobe.aemds.guide.service.XFAModelTransformer;
import com.adobe.aemds.guide.utils.GuideModelUtils;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.JSONCreationOptions;
import com.adobe.aemds.guide.utils.XMLUtils;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.assembler.client.AssemblerOptionSpec;
import com.adobe.fd.assembler.client.AssemblerResult;
import com.adobe.fd.assembler.service.AssemblerService;
import com.adobe.forms.common.service.FileAttachmentWrapper;
import com.adobe.granite.i18n.LocaleUtil;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.xml.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="Document of Record Helper", description="Document of Record Helper Service", immediate=1)
@Service(value={DoRService.class})
public class DoRServiceImpl
implements DoRService {
private static final Logger logger = LoggerFactory.getLogger(DoRServiceImpl.class);
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private GuideLocalizationService guideLocalizationService;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private XFAModelTransformer xfaModelTransformerService;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private GuideModelTransformer guideModelTransformer;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
public AssemblerService assemblerService;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
public ResourceResolverHelper resourceResolverHelper;
public DoRResult render(DoROptions options) throws DoRGenerationException {
DoRResultImpl result;
if (this.xfaModelTransformerService == null) {
throw new DoRGenerationException("Unable to get the reference to the xfaModelTransformerService.");
}
Resource formResource = options.getFormResource();
if (formResource == null) {
throw new DoRGenerationException("Form Resource has not been assigned a Resource correctly.");
}
String afPath = formResource.getPath();
if (afPath.endsWith("/")) {
afPath = afPath.substring(0, afPath.length() - 1);
}
String xdpRef = null;
String data = options.getData();
String locale = options.getLocale().toString();
HashSet<String> supportedLocales = new HashSet<String>();
for (String lang : this.guideLocalizationService.getSupportedLocales()) {
supportedLocales.add(LocaleUtil.parseLocale((String)lang).toString());
}
if (!supportedLocales.contains(locale)) {
String language = options.getLocale().getLanguage();
locale = supportedLocales.contains(language) ? language : "en";
}
if (!afPath.contains("/content/dam/formsanddocuments/") && !afPath.contains("/content/forms/af/")) {
throw new DoRGenerationException("Adaptive Form is not found in the specified Path.");
}
if (!afPath.contains("/content/dam/formsanddocuments/")) {
afPath = GuideUtils.convertGuideContainerPathToFMAssetPath((String)afPath);
}
Resource metaDataResource = formResource.getResourceResolver().getResource(afPath + "/jcr:content/metadata");
ValueMap properties = metaDataResource.getValueMap();
String guideContainerPath = GuideUtils.convertFMAssetPathToContainerPath((String)afPath);
try {
if (properties.get((Object)"formmodel").equals("jsonschema") || properties.get((Object)"formmodel").equals("formdatamodel")) {
JSONObject dorCompliantJson = new JSONObject();
JSONObject dataJson = new JSONObject(data);
if (!dataJson.has("afData")) {
dorCompliantJson.put("data", (Object)dataJson);
} else {
dorCompliantJson = dataJson;
}
data = XML.toString((Object)dorCompliantJson);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
XMLUtils.disableExternalEntities((DocumentBuilderFactory)dbf);
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document dataDoc = db.parse(new ByteArrayInputStream(data.getBytes("UTF-8")));
org.w3c.dom.Document dorDoc = dbf.newDocumentBuilder().newDocument();
Resource guideResource = formResource.getResourceResolver().getResource(guideContainerPath);
JSONCreationOptions jsonCreationOptions = new JSONCreationOptions();
jsonCreationOptions.setFormContainerPath(guideContainerPath);
jsonCreationOptions.setIncludeFragmentJson(true);
jsonCreationOptions.setLocale(new Locale(locale));
JSONObject guideJSON = this.guideModelTransformer.exportGuideJsonObject(guideResource, jsonCreationOptions);
String dorData = GuideModelUtils.getDataForDORMerge((org.w3c.dom.Document)dorDoc, (org.w3c.dom.Document)dataDoc, (JSONObject)guideJSON);
if (GuideUtils.isDORConfigured((Resource)formResource)) {
xdpRef = GuideUtils.getDoRTemplateRef((String)afPath, (String)locale, (ResourceResolver)formResource.getResourceResolver());
}
if (StringUtils.isEmpty((CharSequence)xdpRef)) {
throw new DoRGenerationException("Document of record is not configured for given adaptive form" + afPath);
}
final String newxdpRef = xdpRef;
final String newdorData = dorData;
Map contentMap = (Map)this.resourceResolverHelper.callWith(formResource.getResourceResolver(), (Callable)new Callable<Map<String, Object>>(){
@Override
public Map<String, Object> call() throws Exception {
return DoRServiceImpl.this.xfaModelTransformerService.getPdfFromXfaDom(newxdpRef, newdorData);
}
});
Document outputDoc = null;
Document barePDF = null;
if (contentMap == null) {
throw new DoRGenerationException("Error during generation of document of record " + afPath);
}
result = new DoRResultImpl(new HashMap(contentMap));
if (options.getIncludeAttachments()) {
byte[] fileBytes = result.getContent();
List attachments = options.getFileAttachments();
barePDF = new Document((InputStream)new ByteArrayInputStream(fileBytes));
if (attachments != null && attachments.size() > 0) {
outputDoc = this.addAttachmentsToPdf(xdpRef, barePDF, attachments);
}
if (outputDoc == null) {
outputDoc = barePDF;
}
InputStream pdfStream = outputDoc.getInputStream();
byte[] attachmentPdf = IOUtils.toByteArray((InputStream)pdfStream);
result.setOutputContent(attachmentPdf);
}
}
catch (Exception e) {
throw new DoRGenerationException("Error during document of record generation.", (Throwable)e);
}
return result;
}
private Document addAttachmentsToPdf(String formName, Document barePdf, List<FileAttachmentWrapper> attachments) throws DoRGenerationException {
Document outputDoc = null;
try {
String ddxStart = "<DDX xmlns=\"http://ns.adobe.com/DDX/1.0/\">\n\t<PDF result=\"outDoc\">\n\t\t<PDF source=\"";
String startTag1 = "<";
String startTag2 = "</";
String endTag2 = "/>";
String endTag1 = ">";
String FILE_ATTACHMENT = "FileAttachments";
String FILE = "File";
String quote = "\"";
String ddxEnd = "\n\t" + startTag2 + "PDF" + endTag1 + "\n" + startTag2 + "DDX" + endTag1;
String inputFilename = formName.replace(".xdp", ".pdf");
StringBuilder ddx = new StringBuilder(ddxStart);
ddx.append(inputFilename).append(quote).append(endTag2);
HashMap<String, Document> inputMap = new HashMap<String, Document>();
inputMap.put(inputFilename, barePdf);
for (FileAttachmentWrapper attachment : attachments) {
String fileName;
String displayName = fileName.substring((fileName = attachment.getFileName()).lastIndexOf("%2F") != -1 ? fileName.lastIndexOf("%2F") + 3 : 0);
displayName = displayName.substring(displayName.lastIndexOf("__afAttachment__") != -1 ? displayName.lastIndexOf("__afAttachment__") + 16 : 0);
fileName = fileName.substring(fileName.indexOf("fileupload%2F") != -1 ? fileName.indexOf("fileupload%2F") + 13 : 0);
ddx.append("\n\t\t").append(startTag1).append(FILE_ATTACHMENT).append(" source=\"").append(fileName).append(quote).append(" nameKey=" + quote).append(fileName).append(quote).append(endTag1);
ddx.append("\n\t\t\t").append(startTag1).append(FILE).append(" filename=" + quote).append(displayName).append(quote).append(endTag2);
ddx.append("\n\t\t").append(startTag2).append(FILE_ATTACHMENT).append(endTag1);
inputMap.put(fileName, new Document(attachment.getInputStream()));
}
ddx.append(ddxEnd);
Document ddxDoc = new Document(ddx.toString().getBytes("UTF-8"));
AssemblerResult result = this.assemblerService.invoke(ddxDoc, inputMap, new AssemblerOptionSpec());
outputDoc = (Document)result.getDocuments().get("outDoc");
ddxDoc.dispose();
for (Map.Entry me : inputMap.entrySet()) {
if (inputFilename.equals(me.getKey())) continue;
Document attachmentDoc = (Document)me.getValue();
attachmentDoc.dispose();
}
}
catch (Exception e) {
throw new DoRGenerationException(e.getMessage(), (Throwable)e, true, true);
}
return outputDoc;
}
protected void bindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
this.guideLocalizationService = guideLocalizationService;
}
protected void unbindGuideLocalizationService(GuideLocalizationService guideLocalizationService) {
if (this.guideLocalizationService == guideLocalizationService) {
this.guideLocalizationService = null;
}
}
protected void bindXfaModelTransformerService(XFAModelTransformer xFAModelTransformer) {
this.xfaModelTransformerService = xFAModelTransformer;
}
protected void unbindXfaModelTransformerService(XFAModelTransformer xFAModelTransformer) {
if (this.xfaModelTransformerService == xFAModelTransformer) {
this.xfaModelTransformerService = null;
}
}
protected void bindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
this.guideModelTransformer = guideModelTransformer;
}
protected void unbindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
if (this.guideModelTransformer == guideModelTransformer) {
this.guideModelTransformer = null;
}
}
protected void bindAssemblerService(AssemblerService assemblerService) {
this.assemblerService = assemblerService;
}
protected void unbindAssemblerService(AssemblerService assemblerService) {
if (this.assemblerService == assemblerService) {
this.assemblerService = null;
}
}
protected void bindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
this.resourceResolverHelper = resourceResolverHelper;
}
protected void unbindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
if (this.resourceResolverHelper == resourceResolverHelper) {
this.resourceResolverHelper = null;
}
}
public final class DoRResultImpl
implements DoRResult {
public static final String OUTPUT_CONTENT = "outputContent";
public static final String CONTENT_TYPE = "contentType";
private Map<String, Object> contentMap;
private DoRResultImpl(Map<String, Object> contentMap) {
this.contentMap = contentMap;
}
public byte[] getContent() {
return (byte[])this.contentMap.get("outputContent");
}
public String getContentType() {
return (String)this.contentMap.get("contentType");
}
public Object getValue(String key) {
return this.contentMap.get(key);
}
private void setOutputContent(byte[] pdfData) {
this.contentMap.put("outputContent", pdfData);
}
}
}