FormValidationService.java
7.42 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.forms.option.LCFormsOptions
* com.adobe.forms.service.LCFormsOsgiService
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.forms.internal;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.ValidationOptions;
import com.adobe.fd.forms.api.ValidationResult;
import com.adobe.fd.forms.internal.exception.FormsServiceException;
import com.adobe.fd.forms.internal.utils.Utils;
import com.adobe.fd.forms.internal.utils.XMLUtils;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.LCFormsOsgiService;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import java.io.IOException;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
@Component(label="Process Form Submission Service", description="Process Form Submission Service for internal use")
@Service(value={FormValidationService.class})
public class FormValidationService {
static Logger logger = LoggerFactory.getLogger(FormValidationService.class);
@Reference
private LCFormsOsgiService lcFormsOsgiService;
private static final String XDP_SUBMIT_CONTENT_TYPE = "application/vnd.adobe.xdp";
private static final String XML_SUBMIT_CONTENT_TYPE = "application/xml";
private ValidationResult handleXDPSubmit(String template, byte[] dataBytes, ValidationOptions formSubmissionOptions) throws PDFSecurityException, PDFIOException, PDFInvalidDocumentException, IOException, PDFInvalidXMLException, TransformerException, SAXException, ParserConfigurationException {
logger.debug("Enter handleXDPSubmit");
if (template == null || formSubmissionOptions.getContentRoot() == null) {
org.w3c.dom.Document doc = XMLUtils.loadXMLbytes(dataBytes, false);
Node o = XMLUtils.selectNode(doc.getDocumentElement(), "xfa:datasets/xfa:data", 0);
if (formSubmissionOptions.getContentRoot() == null) {
String fscruri = XMLUtils.selectNodeValue(doc.getDocumentElement(), "FSCRURI_", 0);
logger.debug("handleXDPDataSubmit: FSCURI_ " + fscruri);
formSubmissionOptions.setContentRoot(fscruri);
}
if (template == null) {
template = XMLUtils.selectNodeValue((Element)o, "FSFORMQUERY_", 0);
logger.debug("handleXDPDataSubmit: FSFORMSQUERY_ " + template);
}
}
return this.doCommonFormValidation(template, dataBytes, formSubmissionOptions);
}
private ValidationResult handleXMLDataSubmit(String template, byte[] dataBytes, ValidationOptions validationOptions) throws IOException, ParserConfigurationException, TransformerException, SAXException {
logger.debug("Enter handleXMLDataSubmit");
if (template == null || validationOptions.getContentRoot() == null) {
org.w3c.dom.Document doc = XMLUtils.loadXMLbytes(dataBytes, false);
if (validationOptions.getContentRoot() == null) {
String fscruri = XMLUtils.selectNodeValue(doc.getDocumentElement(), "FSCRURI_", 0);
logger.debug("handleXMLDataSubmit: FSCURI_ " + fscruri);
validationOptions.setContentRoot(fscruri);
}
if (template == null) {
template = XMLUtils.selectNodeValue(doc.getDocumentElement(), "FSFORMQUERY_", 0);
logger.debug("handleXMLDataSubmit: FSFORMSQUERY_ " + template);
}
}
return this.doCommonFormValidation(template, dataBytes, validationOptions);
}
private ValidationResult doCommonFormValidation(String template, byte[] dataBytes, ValidationOptions validationOptions) throws IOException, ParserConfigurationException, SAXException, TransformerException {
LCFormsOptions options = new LCFormsOptions();
options.setScriptsToRun("both");
options.setTemplate(template);
options.setData(new String(dataBytes, "utf-8"));
options.setDebugDir(validationOptions.getDebugDir());
options.setContentRoot(validationOptions.getContentRoot());
Map ret = this.lcFormsOsgiService.exportXFA(options);
return this.buildValidationResult(ret);
}
private ValidationResult buildValidationResult(Map map) throws TransformerException, IOException, ParserConfigurationException, SAXException {
Node validationNode;
String procInfo;
org.w3c.dom.Document pInfoDoc;
ValidationResult formSubmissionResult = new ValidationResult();
String mergedformdom = (String)map.get("mergedformdom");
if (mergedformdom != null) {
formSubmissionResult.setDocument(new Document(mergedformdom.getBytes("UTF-8")));
}
if ((procInfo = (String)map.get("procInfo")) != null && !procInfo.isEmpty() && (pInfoDoc = XMLUtils.loadXMLbytes(procInfo.getBytes(), false)) != null && (validationNode = XMLUtils.selectNode(pInfoDoc.getDocumentElement(), "validationErrors", 0)) != null) {
byte[] validationNodeBytes = XMLUtils.toXML(new DOMSource(validationNode));
formSubmissionResult.setValidationResult(new Document(validationNodeBytes));
}
return formSubmissionResult;
}
public ValidationResult validate(String template, Document data, ValidationOptions options) throws IOException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidXMLException, ParserConfigurationException, SAXException, TransformerException, FormsServiceException {
String contentType;
byte[] dataBytes = Utils.getBytes(data);
String string = contentType = data.getContentType() != null ? data.getContentType() : "";
if (contentType.startsWith("application/vnd.adobe.xdp")) {
return this.handleXDPSubmit(template, dataBytes, options);
}
if (contentType.startsWith("application/xml")) {
return this.handleXMLDataSubmit(template, dataBytes, options);
}
throw new FormsServiceException("AEM_FRM_001_008", new Object[]{contentType});
}
protected void bindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
this.lcFormsOsgiService = lCFormsOsgiService;
}
protected void unbindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
if (this.lcFormsOsgiService == lCFormsOsgiService) {
this.lcFormsOsgiService = null;
}
}
}