FormValidationService.java 7.42 KB
/*
 * 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;
        }
    }
}