Html2PDFWebkit.java 13.9 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.html2pdf.Html2PdfFontPaths
 *  com.adobe.native2pdf.xml.FiletypeSettings
 *  com.adobe.native2pdf.xml.FiletypeSettings$Settings
 *  com.adobe.native2pdf.xml.FiletypeSettings$Settings$Html2Pdf
 *  com.adobe.native2pdf.xml.InitialView
 *  com.adobe.native2pdf.xml.SecuritySettings
 *  com.adobe.native2pdf.xml.SecuritySettings$Settings
 *  com.adobe.pdfg.common.AESProperties
 *  com.adobe.pdfg.common.JobConfiguration
 *  com.adobe.pdfg.common.PDFGGlobalCache
 *  com.adobe.pdfg.common.SettingValidator
 *  com.adobe.pdfg.common.Utils
 *  com.adobe.pdfg.exception.ConversionException
 *  com.adobe.pdfg.exception.FileFormatNotSupportedException
 *  com.adobe.pdfg.exception.InvalidParameterException
 *  com.adobe.pdfg.logging.PDFGLogger
 *  com.adobe.pdfg.service.api.PDFGConfigService
 *  com.adobe.service.ConnectionFactory
 *  com.day.cq.dam.handler.gibson.fontmanager.FontManagerService
 */
package com.adobe.pdfg.impl;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.html2pdf.Html2PdfFontPaths;
import com.adobe.native2pdf.xml.FiletypeSettings;
import com.adobe.native2pdf.xml.InitialView;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.callbacks.HTMLToPDFTransactionCallback;
import com.adobe.pdfg.common.AESProperties;
import com.adobe.pdfg.common.JobConfiguration;
import com.adobe.pdfg.common.PDFGGlobalCache;
import com.adobe.pdfg.common.SettingValidator;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ConversionException;
import com.adobe.pdfg.exception.FileFormatNotSupportedException;
import com.adobe.pdfg.exception.InvalidParameterException;
import com.adobe.pdfg.impl.GeneratePDFServiceImpl;
import com.adobe.pdfg.impl.GeneratePDFUtil;
import com.adobe.pdfg.impl.Html2PDFConvertorHelper;
import com.adobe.pdfg.impl.utils.AdjustableSemaphore;
import com.adobe.pdfg.logging.PDFGLogger;
import com.adobe.pdfg.postprocess.PostProcessFileInfo;
import com.adobe.pdfg.service.api.PDFGConfigService;
import com.adobe.pdfg.transaction.TransactionCallback;
import com.adobe.pdfg.transaction.TransactionTemplate;
import com.adobe.service.ConnectionFactory;
import com.day.cq.dam.handler.gibson.fontmanager.FontManagerService;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Html2PDFWebkit
implements Html2PDFConvertorHelper.IHtml2PDFConvertor {
    protected PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(Html2PDFWebkit.class);

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Converted monitor instructions to comments
     * Lifted jumps to return sites
     */
    public Map createPDF(GeneratePDFServiceImpl coreSvcImpl, String inputUrl, String fileTypeSettingsName, String securitySettingsName, Document settingsDoc, Document xmpDoc, String configString, File pdfgTmpDir, int spideringLevel, boolean isURL) throws ConversionException, FileFormatNotSupportedException, InvalidParameterException, Exception {
        String jobIdentityId = (String)Utils.threadLocalValue.get();
        long waitTime = 0;
        StringBuilder debugMsgs = new StringBuilder();
        long startTime = System.currentTimeMillis();
        if (coreSvcImpl.getHtmlToPdfConversionLock() == null) {
            debugMsgs.append("\nbefore synchronized(Html2PDFWebkit.class) for job=" + jobIdentityId);
            Class<Html2PDFWebkit> class_ = Html2PDFWebkit.class;
            // MONITORENTER : com.adobe.pdfg.impl.Html2PDFWebkit.class
            if (coreSvcImpl.getHtmlToPdfConversionLock() == null) {
                debugMsgs.append("\nbefore initializeHTMLConversionLock for job=" + jobIdentityId);
                coreSvcImpl.initializeHtmlToPdfConversionLock();
                debugMsgs.append("\nafter initializeHTMLConversionLock for job=" + jobIdentityId);
            }
            // MONITOREXIT : class_
        }
        this.pdfgLogger.info("001-024", new Object[]{inputUrl, new Date(startTime), jobIdentityId});
        this.pdfgLogger.info("001-016", new Object[]{inputUrl, "HTML to PDF"});
        boolean success = true;
        debugMsgs.append("\nbefore htmlConversionLock.acquire() for job=" + jobIdentityId);
        long waitStartTime = System.currentTimeMillis();
        coreSvcImpl.getHtmlToPdfConversionLock().acquire();
        long waitEndTime = System.currentTimeMillis();
        debugMsgs.append("\nafter htmlConversionLock.acquire() for job=" + jobIdentityId);
        waitTime -= waitStartTime;
        try {
            Map xmpFile2;
            try {
                File tempPdf = new File(pdfgTmpDir, "TempHtmlToPdf");
                File pdfFile = new File(pdfgTmpDir, "HtmlToPdfResult");
                JobConfiguration config = null;
                try {
                    config = PDFGGlobalCache.getJobConfiguration((String)configString);
                }
                catch (Exception e) {
                    throw new InvalidParameterException(1001, (Throwable)e);
                }
                if (config == null) {
                    throw new InvalidParameterException(80001);
                }
                int timeoutSeconds = AESProperties.getMaximumTimeout();
                timeoutSeconds = Math.max(10, timeoutSeconds);
                timeoutSeconds = config.getConversionTimeoutSetting(10, timeoutSeconds, timeoutSeconds);
                HTMLToPDFTransactionCallback transactionCallback = new HTMLToPDFTransactionCallback(coreSvcImpl.getHtmlToPdfFactory());
                transactionCallback.setSourcePath(inputUrl);
                transactionCallback.setDestinationPath(tempPdf.getAbsolutePath());
                transactionCallback.setTimeout(timeoutSeconds);
                transactionCallback.setHtml2PDFparams(config);
                transactionCallback.setIsURL(isURL);
                transactionCallback.setFontsPath(this.retrieveHtml2PdfFontPaths(coreSvcImpl));
                transactionCallback.setHTMLFont(coreSvcImpl.getFallbackFontForHTMLConversions());
                TransactionTemplate _txTemplate = coreSvcImpl.getTransactionTemplate();
                _txTemplate.setPropagationBehavior(0);
                _txTemplate.setTimeout(timeoutSeconds + 200);
                this.pdfgLogger.debug("before calling _txTemplate.execute for job=" + jobIdentityId);
                _txTemplate.execute(transactionCallback);
                FiletypeSettings.Settings.Html2Pdf html2pdf = config.getFiletypeSettings().getHtml2Pdf();
                InitialView initialView = html2pdf == null ? null : html2pdf.getInitialView();
                SecuritySettings.Settings securitySettings = config.getSecuritySettings();
                boolean shouldApplySecurity = Utils.shouldApplySecurity((SecuritySettings.Settings)securitySettings);
                String targetPdfVersion = null;
                if (shouldApplySecurity) {
                    SettingValidator validator = new SettingValidator(null, securitySettings);
                    if (validator.areEncryptionPasswordsIdentical()) {
                        throw new InvalidParameterException(80009);
                    }
                    int acrobatVersion = securitySettings.getAcrobatVersion();
                    if (acrobatVersion > 5) {
                        targetPdfVersion = "1." + (acrobatVersion - 1);
                    }
                    GeneratePDFUtil.updateSecuritySettings(coreSvcImpl.getConfigService(), securitySettings);
                } else {
                    securitySettings = null;
                }
                PostProcessFileInfo pp = new PostProcessFileInfo();
                pp.attachmentFilePath = "";
                pp.logFilePath = "";
                pp.tempPdfFilePath = tempPdf.getCanonicalPath();
                pp.pdfFilePath = pdfFile.getCanonicalPath();
                pp.xmpFilePath = "";
                if (xmpDoc != null) {
                    File xmpFile2 = new File(pdfgTmpDir, "HtmlToPdfXmp");
                    xmpDoc.copyToFile(xmpFile2);
                    this.pdfgLogger.info("001-020", new Object[]{inputUrl, xmpFile2.getName()});
                    pp.xmpFilePath = xmpFile2.getAbsolutePath();
                }
                coreSvcImpl.pdfPostProcess(pp, null, initialView, securitySettings, true, targetPdfVersion, "PDF generator", GeneratePDFUtil.applyWaterMark());
                xmpFile2 = this.getResponse(pp.pdfFilePath, null, null, pp.postProcessedDoc);
                Object var36_36 = null;
                debugMsgs.append("\nbefore htmlConversionLock.release() for job=" + jobIdentityId);
            }
            catch (Exception e) {
                this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
                success = false;
                throw e;
            }
            coreSvcImpl.getHtmlToPdfConversionLock().release();
            debugMsgs.append("\nafter htmlConversionLock.release() for job=" + jobIdentityId);
            this.pdfgLogger.debug(debugMsgs.toString());
            long endTime = System.currentTimeMillis();
            if (success) {
                this.pdfgLogger.info("001-027", inputUrl);
            } else {
                this.pdfgLogger.info("001-028", inputUrl);
            }
            this.pdfgLogger.info("001-025", new Object[]{inputUrl, new Date(), jobIdentityId});
            this.pdfgLogger.info("001-030", new Object[]{inputUrl, waitTime, jobIdentityId});
            this.pdfgLogger.info("001-026", new Object[]{inputUrl, endTime - startTime - waitTime, jobIdentityId});
            return xmpFile2;
        }
        catch (Throwable var35_40) {
            Object var36_37 = null;
            debugMsgs.append("\nbefore htmlConversionLock.release() for job=" + jobIdentityId);
            coreSvcImpl.getHtmlToPdfConversionLock().release();
            debugMsgs.append("\nafter htmlConversionLock.release() for job=" + jobIdentityId);
            this.pdfgLogger.debug(debugMsgs.toString());
            long endTime = System.currentTimeMillis();
            if (success) {
                this.pdfgLogger.info("001-027", inputUrl);
            } else {
                this.pdfgLogger.info("001-028", inputUrl);
            }
            this.pdfgLogger.info("001-025", new Object[]{inputUrl, new Date(), jobIdentityId});
            this.pdfgLogger.info("001-030", new Object[]{inputUrl, waitTime, jobIdentityId});
            this.pdfgLogger.info("001-026", new Object[]{inputUrl, endTime - startTime - waitTime, jobIdentityId});
            throw var35_40;
        }
    }

    private Html2PdfFontPaths retrieveHtml2PdfFontPaths(GeneratePDFServiceImpl coreSvcImpl) {
        Html2PdfFontPaths fontPaths = new Html2PdfFontPaths();
        fontPaths.customerFontPath = "";
        fontPaths.systemFontPath = "";
        fontPaths.adobeFontPath = "";
        try {
            fontPaths.customerFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getCustomerFontDirectory());
        }
        catch (Exception ex2) {
            this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
            this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
        }
        try {
            fontPaths.systemFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getSystemFontDirectory());
        }
        catch (Exception ex2) {
            this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
            this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
        }
        try {
            fontPaths.adobeFontPath = this.getNormalizedFontPath(coreSvcImpl.getFontManagerService().getAdobeServerFontDirectory());
        }
        catch (Exception ex2) {
            this.pdfgLogger.warning(ex2.getLocalizedMessage(), "");
            this.pdfgLogger.trace(ex2.getMessage(), null, (Throwable)ex2);
        }
        return fontPaths;
    }

    private String getNormalizedFontPath(String path) {
        String fileSeparator = System.getProperty("file.separator");
        String pathSeparator = ";";
        String modifiedPath = "";
        if (path == null) {
            return modifiedPath;
        }
        StringTokenizer tokens = new StringTokenizer(path, ";");
        while (tokens.hasMoreTokens()) {
            path = tokens.nextToken();
            if (path != null && !"".equals(path)) {
                modifiedPath = !path.endsWith(fileSeparator) ? modifiedPath + path + fileSeparator : modifiedPath + path;
            }
            if (!tokens.hasMoreTokens()) continue;
            modifiedPath = modifiedPath + ";";
        }
        return modifiedPath;
    }

    private Map getResponse(String pdfFilePath, String jdfFilePath, String logFilePath, Document postProcessedDoc) {
        File logFile;
        File pdfFile;
        HashMap<String, Document> response = new HashMap<String, Document>();
        Document convertedDoc = postProcessedDoc;
        if (convertedDoc == null && pdfFilePath != null && (pdfFile = new File(pdfFilePath)).exists() && pdfFile.length() > 0) {
            convertedDoc = new Document(pdfFile, true);
        }
        if (pdfFilePath != null && pdfFilePath.indexOf(".pdf") < 0) {
            convertedDoc.setAttribute("file", (Object)(pdfFilePath + ".pdf"));
            convertedDoc.setContentType("application/pdf");
        }
        Document logDoc = null;
        if (logFilePath != null && (logFile = new File(logFilePath)).exists() && logFile.length() > 0) {
            logDoc = new Document(logFile, true);
            if (logFilePath.indexOf(".log") < 0) {
                logDoc.setAttribute("file", (Object)(logFilePath + ".log"));
                logDoc.setContentType("text/plain");
            }
        }
        if (convertedDoc != null) {
            response.put("ConvertedDoc", convertedDoc);
        }
        if (logDoc != null) {
            response.put("LogDoc", logDoc);
        }
        return response;
    }
}