HTMLToPDFTransactionCallback.java 11.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.html2pdf.HTML2PDFConverterService
 *  com.adobe.html2pdf.HTML2PDFConverterServiceHelper
 *  com.adobe.html2pdf.Html2PdfFontPaths
 *  com.adobe.html2pdf.Html2PdfSettings
 *  com.adobe.html2pdf.HtmlToPdfFailureException
 *  com.adobe.native2pdf.xml.FiletypeSettings
 *  com.adobe.native2pdf.xml.FiletypeSettings$Settings
 *  com.adobe.native2pdf.xml.FiletypeSettings$Settings$Html2Pdf
 *  com.adobe.native2pdf.xml.Html2PdfSettings
 *  com.adobe.pdfg.common.AESProperties
 *  com.adobe.pdfg.common.JobConfiguration
 *  com.adobe.pdfg.common.Utils
 *  com.adobe.pdfg.exception.ErrorCode
 *  com.adobe.pdfg.exception.PDFGError
 *  com.adobe.pdfg.exception.PDFGRuntimeException
 *  com.adobe.pdfg.logging.PDFGLogger
 *  com.adobe.service.ConnectionFactory
 *  org.apache.commons.io.FileUtils
 */
package com.adobe.pdfg.callbacks;

import com.adobe.html2pdf.HTML2PDFConverterService;
import com.adobe.html2pdf.HTML2PDFConverterServiceHelper;
import com.adobe.html2pdf.Html2PdfFontPaths;
import com.adobe.html2pdf.HtmlToPdfFailureException;
import com.adobe.native2pdf.xml.FiletypeSettings;
import com.adobe.native2pdf.xml.Html2PdfSettings;
import com.adobe.pdfg.common.AESProperties;
import com.adobe.pdfg.common.JobConfiguration;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.PDFGError;
import com.adobe.pdfg.exception.PDFGRuntimeException;
import com.adobe.pdfg.impl.Html2PDFPhantomJS;
import com.adobe.pdfg.logging.PDFGLogger;
import com.adobe.pdfg.transaction.TransactionCallback;
import com.adobe.service.ConnectionFactory;
import java.io.File;
import java.math.BigDecimal;
import org.apache.commons.io.FileUtils;
import org.omg.CORBA.COMM_FAILURE;
import org.omg.CORBA.Object;
import org.omg.CORBA.SystemException;

public class HTMLToPDFTransactionCallback
implements TransactionCallback {
    private ConnectionFactory htmlToPdfFactory;
    private static final int MAX_RETRY_COUNT = 5;
    private static final long WAIT_TIME = 60000;
    private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(HTMLToPDFTransactionCallback.class);
    private static Html2PDFPhantomJS.Pool phantomJSPool;
    String m_strSourceURLPath = null;
    String m_strDestinationPath = null;
    long m_ltimeout = 480000;
    boolean m_usePhantomJS = false;
    com.adobe.html2pdf.Html2PdfSettings htmlConversionSettings = new com.adobe.html2pdf.Html2PdfSettings();
    Html2PdfFontPaths fontsPath = new Html2PdfFontPaths();

    public HTMLToPDFTransactionCallback(ConnectionFactory htmlToPdfFactory) {
        this.htmlToPdfFactory = htmlToPdfFactory;
        this.htmlToPdfFactory.setServiceTimeout((long)AESProperties.getGlobalTimeout());
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public java.lang.Object doInTransaction() {
        String jobIdentityId = (String)Utils.threadLocalValue.get();
        StringBuilder debugMsgs = new StringBuilder();
        boolean debugMsgsLogged = false;
        debugMsgs.append("\nInside HTMLToPDFTransactionCallback.doInTransaction for job=" + jobIdentityId);
        if (this.m_usePhantomJS) {
            Html2PDFPhantomJS.Process phantomJS = null;
            try {
                Boolean bl;
                block16 : {
                    try {
                        String controlFilePath;
                        debugMsgs.append("\nBefore obtaining a PhantomJS process instance for job=" + jobIdentityId);
                        phantomJS = Html2PDFPhantomJS.getPool().acquire();
                        debugMsgs.append("\nBefore phantomJS.convertToPdf() for job=" + jobIdentityId);
                        pdfgLogger.debug(debugMsgs.toString());
                        debugMsgsLogged = true;
                        String destPdfPathOrig = this.m_strDestinationPath;
                        if (!this.m_strDestinationPath.endsWith(".pdf")) {
                            this.m_strDestinationPath = this.m_strDestinationPath + ".pdf";
                        }
                        if ((controlFilePath = new File(this.m_strDestinationPath).getParent()) != null) {
                            controlFilePath = controlFilePath + File.separator + "ControlFile.txt";
                        }
                        phantomJS.convertToPdf(this.m_strSourceURLPath, this.m_strDestinationPath, controlFilePath, this.htmlConversionSettings, this.fontsPath, this.m_ltimeout);
                        FileUtils.waitFor((File)new File(controlFilePath), (int)((int)this.m_ltimeout / 1000));
                        if (!new File(controlFilePath).exists()) {
                            phantomJS.rogueProcess = true;
                            throw new Exception("Conversion timed out");
                        }
                        File convertedFile = new File(this.m_strDestinationPath);
                        convertedFile.renameTo(new File(destPdfPathOrig));
                        this.m_strDestinationPath = destPdfPathOrig;
                        bl = true;
                        java.lang.Object var10_20 = null;
                        Html2PDFPhantomJS.getPool().release(phantomJS);
                        if (debugMsgsLogged) break block16;
                    }
                    catch (Exception e) {
                        pdfgLogger.info(e.getMessage(), (java.lang.Object[])null);
                        PDFGError error = new PDFGError(ErrorCode.JOB_TIMEOUT_ERROR, (java.lang.Object)e.getMessage());
                        throw new PDFGRuntimeException(error);
                    }
                    pdfgLogger.debug(debugMsgs.toString());
                }
                return bl;
            }
            catch (Throwable var9_22) {
                java.lang.Object var10_21 = null;
                Html2PDFPhantomJS.getPool().release(phantomJS);
                if (!debugMsgsLogged) {
                    pdfgLogger.debug(debugMsgs.toString());
                }
                throw var9_22;
            }
        }
        HTML2PDFConverterService agent = null;
        try {
            Boolean error;
            try {
                debugMsgs.append("\nbefore obtaining a CORBA connection for job=" + jobIdentityId);
                Object connection = (Object)this.htmlToPdfFactory.getConnection();
                debugMsgs.append("\nbefore narrowing the CORBA connection for job=" + jobIdentityId);
                agent = HTML2PDFConverterServiceHelper.narrow((Object)connection);
                debugMsgs.append("\nbefore agent.ConvertToPDF for job=" + jobIdentityId);
                pdfgLogger.debug(debugMsgs.toString());
                debugMsgsLogged = true;
                agent.ConvertToPDF(this.m_strSourceURLPath, this.m_strDestinationPath, this.htmlConversionSettings, this.fontsPath, this.m_ltimeout);
                error = true;
                java.lang.Object var12_23 = null;
                if (debugMsgsLogged) return error;
            }
            catch (HtmlToPdfFailureException pfe) {
                pdfgLogger.trace(pfe.getMessage(), null, (Throwable)pfe);
                PDFGError error2 = new PDFGError(pfe.errorCode, (java.lang.Object)pfe.getMessage());
                throw new PDFGRuntimeException(error2);
            }
            catch (COMM_FAILURE pfe) {
                pdfgLogger.trace(pfe.getMessage(), null, (Throwable)pfe);
                PDFGError error3 = new PDFGError(52016, (java.lang.Object)pfe.getMessage());
                throw new PDFGRuntimeException(error3);
            }
            catch (SystemException pfe) {
                pdfgLogger.trace(pfe.getMessage(), null, (Throwable)pfe);
                PDFGError error4 = new PDFGError(52016, (java.lang.Object)pfe.getMessage());
                throw new PDFGRuntimeException(error4);
            }
            pdfgLogger.debug(debugMsgs.toString());
            return error;
        }
        catch (Throwable var11_25) {
            java.lang.Object var12_24 = null;
            if (!debugMsgsLogged) {
                pdfgLogger.debug(debugMsgs.toString());
            }
            throw var11_25;
        }
    }

    public void setTimeout(int timeout) {
        this.m_ltimeout = (long)timeout * 1000;
    }

    public void setDestinationPath(String destinationPath) {
        this.m_strDestinationPath = destinationPath;
    }

    public void setSourcePath(String sourcePath) {
        this.m_strSourceURLPath = sourcePath;
    }

    public void setIsURL(boolean isURL) {
        this.htmlConversionSettings.isURL = isURL;
    }

    public void setUsePhantomJS(boolean usePhantomJS) {
        this.m_usePhantomJS = usePhantomJS;
    }

    public void setHtml2PDFparams(JobConfiguration config) {
        FiletypeSettings.Settings.Html2Pdf html2pdf = config.getFiletypeSettings().getHtml2Pdf();
        Html2PdfSettings htmlSettingsType = html2pdf.getHtml2PdfSettings();
        this.htmlConversionSettings.spideringLevel = htmlSettingsType.getLevels();
        this.htmlConversionSettings.getEntireSite = false;
        this.htmlConversionSettings.isSamePath = htmlSettingsType.isSamePath();
        this.htmlConversionSettings.isSameServer = htmlSettingsType.isSameServer();
        this.htmlConversionSettings.isPlaceFooter = htmlSettingsType.isPlaceFooter();
        this.htmlConversionSettings.isPlaceHeader = htmlSettingsType.isPlaceHeader();
        this.htmlConversionSettings.MarginBottom = htmlSettingsType.getMarginBottom().doubleValue();
        this.htmlConversionSettings.MarginLeft = htmlSettingsType.getMarginLeft().doubleValue();
        this.htmlConversionSettings.MarginRight = htmlSettingsType.getMarginRight().doubleValue();
        this.htmlConversionSettings.MarginTop = htmlSettingsType.getMarginTop().doubleValue();
        this.htmlConversionSettings.Orientation = htmlSettingsType.getOrientation();
        this.htmlConversionSettings.PageHeight = htmlSettingsType.getPageHeight().doubleValue();
        this.htmlConversionSettings.PageWidth = htmlSettingsType.getPageWidth().doubleValue();
        this.htmlConversionSettings.isBookmarks = htmlSettingsType.isCreateBookmarks();
        this.htmlConversionSettings.isTagging = htmlSettingsType.isCreateTaggedPDF();
        this.htmlConversionSettings.inputEncoding = htmlSettingsType.getInputEncoding();
        if (this.htmlConversionSettings.inputEncoding == null) {
            this.htmlConversionSettings.inputEncoding = "";
        }
        this.htmlConversionSettings.isForceEncoding = htmlSettingsType.isForceEncoding();
    }

    public void setFontsPath(Html2PdfFontPaths fontPath) {
        this.fontsPath.adobeFontPath = fontPath.adobeFontPath != null ? fontPath.adobeFontPath : "";
        this.fontsPath.customerFontPath = fontPath.customerFontPath != null ? fontPath.customerFontPath : "";
        this.fontsPath.systemFontPath = fontPath.systemFontPath != null ? fontPath.systemFontPath : "";
    }

    public void setHTMLFont(String useHtmlFont) {
        this.htmlConversionSettings.fallbackHTMLFont = useHtmlFont != null ? useHtmlFont : "";
    }

    public void setHtmlToPdfFactory(ConnectionFactory htmlToPdfFactory) {
        this.htmlToPdfFactory = htmlToPdfFactory;
    }
}