PdfPostProcessorImpl.java 20.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.aemfd.pdfdocmanager.SharedPdfDocumentContext
 *  com.adobe.internal.io.ByteReader
 *  com.adobe.internal.io.RandomAccessFileByteReader
 *  com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFDocumentInfo
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFile
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFileInfo
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFSaveFullOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFSaveIncrementalOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFSaveLinearOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFVersion
 *  com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpace
 *  com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceCMYK
 *  com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceGray
 *  com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceRGB
 *  com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImage
 *  com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocation
 *  com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocationMap
 *  com.adobe.internal.pdftoolkit.pdf.page.PDFPage
 *  com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree
 *  com.adobe.internal.pdftoolkit.services.imageconversion.ImageManager
 *  com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword
 *  com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata
 *  com.adobe.internal.pdftoolkit.services.xmp.XMPService
 *  com.adobe.native2pdf.xml.InitialView
 *  com.adobe.native2pdf.xml.SecuritySettings
 *  com.adobe.native2pdf.xml.SecuritySettings$Settings
 *  com.adobe.pdfg.common.Utils
 *  com.adobe.pdfg.exception.ErrorCode
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.pdfg.postprocess;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.pdfdocmanager.SharedPdfDocumentContext;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.RandomAccessFileByteReader;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocumentInfo;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFile;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFileInfo;
import com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveFullOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveIncrementalOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveLinearOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFVersion;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpace;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceCMYK;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceGray;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceDeviceRGB;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImage;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocation;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImageWithLocationMap;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree;
import com.adobe.internal.pdftoolkit.services.imageconversion.ImageManager;
import com.adobe.internal.pdftoolkit.services.security.SecurityLockPassword;
import com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata;
import com.adobe.internal.pdftoolkit.services.xmp.XMPService;
import com.adobe.native2pdf.xml.InitialView;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.postprocess.PdfPostProcessorUtilities;
import com.adobe.pdfg.postprocess.PostProcessFileInfo;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

public final class PdfPostProcessorImpl {
    private static final Logger logger = LoggerFactory.getLogger(PdfPostProcessorImpl.class);
    private Document fileToDispose = null;

    public void setFileToDispose(Document fileToDispose) {
        this.fileToDispose = fileToDispose;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public int doPostProcess(PostProcessFileInfo filePaths, String attachmentFileName, InitialView initialView, SecuritySettings.Settings encryptionSettings, boolean doWebOptimization, boolean doPdfAProcessing, boolean doImagePostProcessing, String targetPdfVersion, boolean forcePdfVersion, String pdfProducerString, int pdfColorSpace, boolean applyWatermark) {
        int errorCode;
        block49 : {
            errorCode = 0;
            String jobIdentityId = (String)Utils.threadLocalValue.get();
            StringBuilder debugMsgs = new StringBuilder();
            applyWatermark = false;
            try {
                debugMsgs.append("\nInside PdfPostProcessorImpl.doPostProcess for job=" + jobIdentityId);
                if ("".equals(filePaths.pdfFilePath)) {
                    errorCode = 16001;
                    break block49;
                }
                if ("".equals(filePaths.xmpFilePath) && "".equals(filePaths.attachmentFilePath) && initialView == null && encryptionSettings == null && !doWebOptimization && !doPdfAProcessing && !doImagePostProcessing && "".equals(targetPdfVersion) && (pdfProducerString == null || "".equals(pdfProducerString)) && !applyWatermark) {
                    errorCode = 0;
                    break block49;
                }
                File duplicatePdf = null;
                Object pdfReader = null;
                RandomAccessFileByteReader attachmentReader = null;
                InputStream xmpInput = null;
                PDFDocument pdfDocument = null;
                errorCode = 16003;
                Object pdfDocumentContext = null;
                try {
                    boolean bProducerUpdated;
                    PDFSaveIncrementalOptions docOptions;
                    duplicatePdf = filePaths.tempPdfFilePath != null && !filePaths.tempPdfFilePath.equals("") ? new File(filePaths.tempPdfFilePath) : new File(filePaths.pdfFilePath);
                    Document pdfInput = new Document(duplicatePdf, false);
                    debugMsgs.append("\nbefore creating pdfDocument for job=" + jobIdentityId);
                    PDFOpenOptions pdfOpts = PDFOpenOptions.newInstance();
                    pdfOpts.setSaveInPlace(false);
                    pdfDocument = SharedPdfDocumentContext.toPDFDocument((Document)pdfInput, (PDFOpenOptions)pdfOpts);
                    debugMsgs.append("\nbefore creating originalPdfVersion for job=" + jobIdentityId);
                    String originalPdfVersion = pdfDocument.getOriginalVersion().asString();
                    if ("".equals(targetPdfVersion) || !forcePdfVersion && targetPdfVersion.compareTo(originalPdfVersion) < 0) {
                        targetPdfVersion = originalPdfVersion;
                    }
                    debugMsgs.append("\nbefore getting pdfVersion for job=" + jobIdentityId);
                    PDFVersion pdfVersion = PDFVersion.getSupportedInstance((String)targetPdfVersion);
                    if (pdfVersion == null) {
                        pdfVersion = PDFVersion.vLatest;
                    }
                    pdfDocument.setToSaveVersion(pdfVersion);
                    debugMsgs.append("\nafter pdfDocument.setToSaveVersion for job=" + jobIdentityId);
                    if (!"".equals(filePaths.attachmentFilePath)) {
                        if (attachmentFileName.length() == 0 && !(attachmentFileName = new File(filePaths.attachmentFilePath).getName()).endsWith(".joboptions")) {
                            attachmentFileName = attachmentFileName + ".joboptions";
                        }
                        debugMsgs.append("\nbefore creating attachmentReader for job=" + jobIdentityId);
                        attachmentReader = new RandomAccessFileByteReader(new RandomAccessFile(filePaths.attachmentFilePath, "r"));
                        ASString name = new ASString(attachmentFileName.getBytes("UTF-8"));
                        debugMsgs.append("\nafter name = new ASString for job=" + jobIdentityId);
                        PDFEmbeddedFile pdfEmbeddedFile = PDFEmbeddedFile.newInstance((PDFDocument)pdfDocument, (PDFEmbeddedFileInfo)null, (ByteReader)attachmentReader);
                        debugMsgs.append("\nafter creating pdfEmbeddedFile for job=" + jobIdentityId);
                        PDFFileSpecification fileSpecification = PDFFileSpecification.newInstance((PDFDocument)pdfDocument, (ASString)name, (PDFEmbeddedFile)pdfEmbeddedFile);
                        debugMsgs.append("\nafter creating fileSpecification for job=" + jobIdentityId);
                        PDFNamedEmbeddedFiles embeddedFiles = pdfDocument.requireCatalog().procureNameDictionary().procureNamedEmbeddedFiles();
                        embeddedFiles.addEntry((Object)name, (Object)fileSpecification);
                    }
                    boolean bl = bProducerUpdated = pdfProducerString != null && !"".equals(pdfProducerString);
                    if (bProducerUpdated) {
                        debugMsgs.append("\nproducer needs to be updated for job=" + jobIdentityId);
                        if (pdfDocument.getDocumentInfo() == null) {
                            PDFDocumentInfo info = PDFDocumentInfo.newInstance((PDFDocument)pdfDocument);
                            pdfDocument.setDocumentInfo(info);
                        }
                        pdfDocument.getDocumentInfo().setProducer(pdfProducerString);
                    }
                    if (pdfColorSpace == 1 && (doImagePostProcessing || doPdfAProcessing)) {
                        try {
                            PDFPageTree pageTree = pdfDocument.requirePages();
                            PDFPage pdfPage = pageTree.getPage(0);
                            PDFXObjectImageWithLocationMap pdfXObjectImageMap = ImageManager.getPDFXObjectMap((PDFPage)pdfPage);
                            PDFColorSpace colorSpace = null;
                            for (Object obj : pdfXObjectImageMap.entrySet()) {
                                Map.Entry entry = (Map.Entry)obj;
                                PDFXObjectImageWithLocation imageWithLocation = (PDFXObjectImageWithLocation)entry.getValue();
                                PDFXObjectImage image = imageWithLocation.getXImage();
                                colorSpace = image.getColorSpace();
                            }
                            pdfColorSpace = colorSpace instanceof PDFColorSpaceDeviceGray ? 2 : (colorSpace instanceof PDFColorSpaceDeviceCMYK ? 4 : (colorSpace instanceof PDFColorSpaceDeviceRGB ? 3 : 1));
                        }
                        catch (Exception e) {
                            logger.info("Exception while obtaining the color space information from the pdf:" + e.getMessage());
                            logger.trace(e.getMessage(), (Throwable)e);
                        }
                    }
                    if (doImagePostProcessing || doPdfAProcessing) {
                        debugMsgs.append("\ndoImagePostProcessing is true for job=" + jobIdentityId);
                        PdfPostProcessorUtilities.addPdfAOutputIntent(pdfDocument, pdfColorSpace);
                    }
                    if (!"".equals(filePaths.xmpFilePath) || doImagePostProcessing || doPdfAProcessing || bProducerUpdated) {
                        DocumentMetadata md;
                        debugMsgs.append("\nDo the Post-Processing Related to XMP Import and PDF/A for job=" + jobIdentityId);
                        errorCode = ErrorCode.CorruptXMPFile;
                        boolean bNeedImport = false;
                        boolean bNeedSynchronization = false;
                        if (!"".equals(filePaths.xmpFilePath)) {
                            debugMsgs.append("\nRead XMP from the External File for job=" + jobIdentityId);
                            xmpInput = new BufferedInputStream(new FileInputStream(filePaths.xmpFilePath));
                            bNeedImport = true;
                            if (doPdfAProcessing) {
                                bNeedSynchronization = true;
                            }
                        } else {
                            debugMsgs.append("\nExtract the XMP Metadata from the PDF Document for job=" + jobIdentityId);
                            md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
                            md.commit();
                            ByteArrayOutputStream xmpOutput = new ByteArrayOutputStream();
                            pdfDocument.exportXMP((OutputStream)xmpOutput);
                            xmpInput = new ByteArrayInputStream(xmpOutput.toByteArray());
                        }
                        debugMsgs.append("\nafter creating variable xmpInput for job=" + jobIdentityId);
                        if (bProducerUpdated || doPdfAProcessing) {
                            debugMsgs.append("\nbProducerUpdated || doPdfAProcessing is true for job=" + jobIdentityId);
                            org.w3c.dom.Document document = PdfPostProcessorUtilities.parseXmpMetadata(xmpInput);
                            debugMsgs.append("\nafter PdfPostProcessorUtilities.parseXmpMetadata for job=" + jobIdentityId);
                            Element root = document.getDocumentElement();
                            Element parent = PdfPostProcessorUtilities.findPdfAEntryParent(root);
                            boolean bNeedSerialization = false;
                            if (doPdfAProcessing) {
                                debugMsgs.append("\ndo doPdfAProcessing for job=" + jobIdentityId);
                                if (!PdfPostProcessorUtilities.doesPdfAEntryExist(parent)) {
                                    debugMsgs.append("\nbefore addPdfAEntryToDOM for job=" + jobIdentityId);
                                    PdfPostProcessorUtilities.addPdfAEntryToDOM(document, parent);
                                    bNeedSerialization = true;
                                    bNeedImport = true;
                                }
                            }
                            if (bProducerUpdated) {
                                debugMsgs.append("\nbProducerUpdated is true for job=" + jobIdentityId);
                                PdfPostProcessorUtilities.updateProducerEntryInDOM(parent, pdfProducerString);
                                bNeedSerialization = true;
                                bNeedImport = true;
                            }
                            if (bNeedSerialization) {
                                debugMsgs.append("\nbNeedSerialization is true for job=" + jobIdentityId);
                                xmpInput = PdfPostProcessorUtilities.serializeXmpMetadata(document);
                            }
                        }
                        if (bNeedImport) {
                            try {
                                debugMsgs.append("\nbNeedImport is true for job=" + jobIdentityId);
                                md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
                                md.importXMP(xmpInput);
                            }
                            catch (Exception e) {
                                logger.debug("Import XMP failed due to exception: " + e);
                                logger.trace(e.getMessage(), (Throwable)e);
                            }
                        }
                        if (bNeedSynchronization) {
                            debugMsgs.append("\nbNeedSynchronization is true for job=" + jobIdentityId);
                            md = XMPService.getDocumentMetadata((PDFDocument)pdfDocument);
                            md.commit();
                        }
                    }
                    errorCode = 16003;
                    PdfPostProcessorUtilities.applyInitialView(initialView, pdfDocument);
                    debugMsgs.append("\nafter PdfPostProcessorUtilities.applyInitialView for job=" + jobIdentityId);
                    SecurityLockPassword lock = PdfPostProcessorUtilities.getLockPassword(encryptionSettings);
                    debugMsgs.append("\nafter PdfPostProcessorUtilities.getLockPassword for job=" + jobIdentityId);
                    if (lock != null || forcePdfVersion) {
                        debugMsgs.append("\nlock != null so securityLock is applied for job=" + jobIdentityId);
                        docOptions = !doWebOptimization ? (lock == null ? PDFSaveFullOptions.newInstance() : PDFSaveFullOptions.newInstance((SecurityLock)lock)) : (lock == null ? PDFSaveLinearOptions.newInstance() : PDFSaveLinearOptions.newInstance((SecurityLock)lock));
                    } else {
                        docOptions = doWebOptimization && !doImagePostProcessing && !doPdfAProcessing ? PDFSaveLinearOptions.newInstance() : PDFSaveIncrementalOptions.newInstance();
                    }
                    docOptions.setCloseAfterSave(true);
                    debugMsgs.append("\nafter creating SharedPdfDocumentOptions for job=" + jobIdentityId);
                    filePaths.postProcessedDoc = SharedPdfDocumentContext.toDocument((PDFDocument)pdfDocument, (PDFSaveOptions)docOptions);
                    debugMsgs.append("\nafter SharedPdfDocumentContext.toDocument for job=" + jobIdentityId);
                    filePaths.postProcessedDoc.setAttribute("file", (Object)filePaths.pdfFilePath);
                    errorCode = 0;
                }
                catch (IOException e) {
                    logger.debug("PdfPostProcessorConstants.doPostProcess() exception: " + e.getMessage());
                    logger.trace(e.getMessage(), (Throwable)e);
                    errorCode = 16002;
                }
                catch (Exception e) {
                    logger.debug("PdfPostProcessorConstants.doPostProcess() exception: " + e.getMessage());
                    logger.trace(e.getMessage(), (Throwable)e);
                }
                finally {
                    if (xmpInput != null) {
                        try {
                            xmpInput.close();
                        }
                        catch (IOException e) {
                            logger.debug("PdfPostProcessor: IO error while closing XMP stream", (Throwable)e);
                        }
                    }
                }
            }
            catch (Exception e) {
                logger.debug(e.getMessage(), (Object)null, (Object)e);
            }
            finally {
                debugMsgs.append("\nerror code =" + errorCode + " for job=" + jobIdentityId);
                logger.debug(debugMsgs.toString());
            }
        }
        return errorCode;
    }

    private void disposeReturnedDoc() {
        if (this.fileToDispose != null) {
            this.fileToDispose.dispose();
            this.fileToDispose = null;
        }
    }
}