OutputServiceImpl.java 11.9 KB
/*
 * 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;
        }
    }
}