XFANativeServiceImpl.java 20.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.bedrock.internal.OSGiUtils
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.aemfd.docmanager.DocumentVsDataBuffer
 *  com.adobe.aemfd.docmanager.TempFileManager
 *  com.adobe.document.xmlform.BatchInputForPrint
 *  com.adobe.document.xmlform.FileBufferAndId
 *  com.adobe.document.xmlform.FormFactory
 *  com.adobe.document.xmlform.FormFactoryHelper
 *  com.adobe.document.xmlform.PrintBatchOutput
 *  com.adobe.document.xmlform.PrintOutput
 *  com.adobe.document.xmlform.ProcessInformation
 *  com.adobe.document.xmlform.ProcessingOptions
 *  com.adobe.document.xmlform.RenderBatchOutputType
 *  com.adobe.document.xmlform.RenderException
 *  com.adobe.document.xmlform.RenderInputForPrint
 *  com.adobe.document.xmlform.RenderResultBuffer
 *  com.adobe.document.xmlform.RenderResultForPrint
 *  com.adobe.document.xmlform.RenderResultForPrintListHolder
 *  com.adobe.document.xmlform.ReturnStatus
 *  com.adobe.document.xmlform.SinglePieceOutput
 *  com.adobe.document.xmlform.WSAliasCredential
 *  com.adobe.document.xmlform.WSTemplateAliases
 *  com.adobe.forms.external.service.FormsContext
 *  com.adobe.forms.external.service.XFAService
 *  com.adobe.service.DataBuffer
 *  com.adobe.service.DataManager
 *  com.adobe.service.FileDataBuffer
 *  com.adobe.service.InlinedDataBuffer
 *  com.adobe.service.InlinedDataBufferHolder
 *  com.adobe.service.InvalidSourceException
 *  com.adobe.service.impl.Platform
 *  com.adobe.xmlformcallback.WSPair
 *  javax.transaction.Transaction
 *  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.forms.aem.impl;

import com.adobe.aemds.bedrock.internal.OSGiUtils;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.docmanager.DocumentVsDataBuffer;
import com.adobe.aemfd.docmanager.TempFileManager;
import com.adobe.document.xmlform.BatchInputForPrint;
import com.adobe.document.xmlform.FileBufferAndId;
import com.adobe.document.xmlform.FormFactory;
import com.adobe.document.xmlform.FormFactoryHelper;
import com.adobe.document.xmlform.PrintBatchOutput;
import com.adobe.document.xmlform.PrintOutput;
import com.adobe.document.xmlform.ProcessInformation;
import com.adobe.document.xmlform.ProcessingOptions;
import com.adobe.document.xmlform.RenderBatchOutputType;
import com.adobe.document.xmlform.RenderException;
import com.adobe.document.xmlform.RenderInputForPrint;
import com.adobe.document.xmlform.RenderResultBuffer;
import com.adobe.document.xmlform.RenderResultForPrint;
import com.adobe.document.xmlform.RenderResultForPrintListHolder;
import com.adobe.document.xmlform.ReturnStatus;
import com.adobe.document.xmlform.SinglePieceOutput;
import com.adobe.document.xmlform.WSAliasCredential;
import com.adobe.document.xmlform.WSTemplateAliases;
import com.adobe.fd.output.internal.XFABatchForPrintService;
import com.adobe.fd.output.internal.XFAPAExecuteService;
import com.adobe.forms.external.service.FormsContext;
import com.adobe.forms.external.service.XFAService;
import com.adobe.service.DataBuffer;
import com.adobe.service.DataManager;
import com.adobe.service.FileDataBuffer;
import com.adobe.service.InlinedDataBuffer;
import com.adobe.service.InlinedDataBufferHolder;
import com.adobe.service.InvalidSourceException;
import com.adobe.service.impl.Platform;
import com.adobe.xmlformcallback.WSPair;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.transaction.Transaction;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.omg.CORBA.Object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="AEM Forms XFANativeService", description="AEM Forms XFANativeService", immediate=1)
@Service(value={XFAService.class, XFAPAExecuteService.class, XFABatchForPrintService.class})
public class XFANativeServiceImpl
implements XFAService,
XFAPAExecuteService,
XFABatchForPrintService {
    protected static final long TEMPLATESIZETHRESHOLD = 0x800000;
    public static final String TEMP_FILE_PREFIX = "XFANativeServiceImpl";
    @Reference
    TempFileManager tempFileManager;
    @Reference
    FormsContext formsContext;
    private Logger logger = LoggerFactory.getLogger(XFANativeServiceImpl.class);

    InlinedDataBuffer createInlinedDataBuffer(byte[] data) throws IOException, InvalidSourceException {
        if (data == null) {
            data = new byte[]{};
        }
        if ((long)data.length < 0x800000) {
            return new InlinedDataBuffer(true, "", data, null);
        }
        DataManager dm = this.tempFileManager.getDataManager();
        File file = this.tempFileManager.getTransactionBoundTempFile();
        FileOutputStream fo = new FileOutputStream(file);
        fo.write(data);
        fo.close();
        FileDataBuffer fileDataBuffer = dm.createFileDataBuffer(file.getAbsolutePath());
        return new InlinedDataBuffer(false, "", new byte[0], (DataBuffer)fileDataBuffer);
    }

    InlinedDataBuffer createInlinedDataBuffer(Document dataDoc) throws IOException, InvalidSourceException {
        if (dataDoc == null) {
            return this.createInlinedDataBuffer(new byte[0]);
        }
        return DocumentVsDataBuffer.toInlinedDataBuffer((Document)dataDoc);
    }

    private byte[] getDataFromInlinedDataBufferHolder(InlinedDataBufferHolder idbh) {
        byte[] data = null;
        if (idbh != null && idbh.value != null) {
            data = idbh.value.isInlined ? idbh.value.inlinedData : idbh.value.remoteDataBuffer.getBytes(0, idbh.value.remoteDataBuffer.getBufLength());
        }
        return data;
    }

    private File getTempFile() throws IOException {
        return this.tempFileManager.getTempFile("XFANativeServiceImpl", "");
    }

    @Override
    public List<byte[]> paExecute(byte[] template, byte[] data, byte[] xci, byte[] xdc, boolean restoreDeltas, boolean runInitializeScripts, byte[] userXCI, byte[] resources, boolean signablePDF, boolean forceRestore) throws Exception {
        try {
            if (OSGiUtils.getTransactionManager() == null) {
                throw new Exception("TransactionManager is null");
            }
            OSGiUtils.getTransactionManager().begin();
            Object corbaObj = Platform.UTIL.lookup("XMLFormService");
            FormFactory xmlForm = FormFactoryHelper.narrow((Object)corbaObj);
            InlinedDataBuffer templateDataBuffer = this.createInlinedDataBuffer(template);
            InlinedDataBuffer dataDataBuffer = this.createInlinedDataBuffer(data);
            InlinedDataBuffer xciDataBuffer = this.createInlinedDataBuffer(xci);
            InlinedDataBuffer xdcDataBuffer = this.createInlinedDataBuffer(xdc);
            ProcessingOptions processingOptions = new ProcessingOptions();
            processingOptions.resourceMap = this.createInlinedDataBuffer(resources);
            processingOptions.restoreDeltas = restoreDeltas;
            processingOptions.runInitializeScripts = runInitializeScripts;
            processingOptions.resourceInfo = new WSPair[0][0];
            processingOptions.callBackContext = this.formsContext.getContextBytes();
            processingOptions.userXCI = this.createInlinedDataBuffer(userXCI);
            processingOptions.forceRestore = forceRestore;
            processingOptions.signablePrintPDF = signablePDF;
            WSAliasCredential[] credentialArray = new WSAliasCredential[]{};
            InlinedDataBufferHolder output = new InlinedDataBufferHolder();
            InlinedDataBufferHolder processingInfo = new InlinedDataBufferHolder();
            ReturnStatus status = xmlForm.PAexecute82(templateDataBuffer, dataDataBuffer, xciDataBuffer, xdcDataBuffer, processingOptions, credentialArray, output, processingInfo);
            if (status == ReturnStatus.XFA_RENDER_FAILURE || output == null || output.value == null) {
                if (processingInfo != null) {
                    this.logger.error(new String(this.getDataFromInlinedDataBufferHolder(processingInfo)));
                }
                throw new Exception("PAExecute Failure: " + status.toString());
            }
            ArrayList<byte[]> outputResult = new ArrayList<byte[]>(2);
            outputResult.add(this.getDataFromInlinedDataBufferHolder(processingInfo));
            outputResult.add(this.getDataFromInlinedDataBufferHolder(output));
            OSGiUtils.getTransactionManager().commit();
            return outputResult;
        }
        catch (RenderException e) {
            this.logger.error("Exception:" + e.reason, (Throwable)e);
            if (OSGiUtils.getTransactionManager().getTransaction() != null) {
                OSGiUtils.getTransactionManager().rollback();
            }
            throw new Exception(e.reason, (Throwable)e);
        }
        catch (Exception e) {
            this.logger.error("Exception:", (Throwable)e);
            if (OSGiUtils.getTransactionManager().getTransaction() != null) {
                OSGiUtils.getTransactionManager().rollback();
            }
            throw e;
        }
    }

    @Override
    public Map<String, java.lang.Object> renderBatchForPrint(Map<String, byte[]> templates, Map<String, Document> data, byte[] xci, boolean generateManyFiles, String documentType, byte[] userXCI, byte[] resources) throws Exception {
        try {
            if (OSGiUtils.getTransactionManager() == null) {
                throw new Exception("TransactionManager is null");
            }
            OSGiUtils.getTransactionManager().begin();
            Object corbaObj = Platform.UTIL.lookup("XMLFormService");
            FormFactory xmlForm = FormFactoryHelper.narrow((Object)corbaObj);
            InlinedDataBuffer inlineXCI = this.createInlinedDataBuffer(xci);
            RenderInputForPrint[] renderInputForPrints = new RenderInputForPrint[1];
            renderInputForPrints[0] = new RenderInputForPrint("", documentType, "", generateManyFiles ? RenderBatchOutputType.SELFCONTAINED : RenderBatchOutputType.SINGLEPIECEBATCH, "none", new String[]{"default"}, new ProcessInformation(new String[0]), 0);
            BatchInputForPrint[] templateBatch = new BatchInputForPrint[templates.size()];
            BatchInputForPrint[] dataBatch = new BatchInputForPrint[data.size()];
            int counter = 0;
            for (Map.Entry<String, byte[]> entry : templates.entrySet()) {
                InlinedDataBuffer templateBuff = this.createInlinedDataBuffer(templates.get(entry.getKey()));
                InlinedDataBuffer dataBuff = this.createInlinedDataBuffer(data.get(entry.getKey()));
                templateBatch[counter] = new BatchInputForPrint(entry.getKey(), "", "", new ProcessInformation(new String[0]), templateBuff);
                dataBatch[counter] = new BatchInputForPrint(entry.getKey(), "", "", new ProcessInformation(new String[0]), dataBuff);
                ++counter;
            }
            ProcessingOptions processingOptions = new ProcessingOptions();
            processingOptions.resourceMap = this.createInlinedDataBuffer(resources);
            processingOptions.restoreDeltas = false;
            processingOptions.runInitializeScripts = true;
            processingOptions.resourceInfo = new WSPair[0][0];
            processingOptions.callBackContext = this.formsContext.getContextBytes();
            WSTemplateAliases[] templateAliases = new WSTemplateAliases[]{};
            processingOptions.userXCI = this.createInlinedDataBuffer(userXCI);
            RenderResultForPrintListHolder renderResultForPrintListHolder = new RenderResultForPrintListHolder();
            boolean status = xmlForm.renderBatchForPrint82(inlineXCI, templateBatch, dataBatch, renderInputForPrints, processingOptions, templateAliases, renderResultForPrintListHolder);
            if (!status) {
                throw new Exception("RenderBatchForPrint82 Failure: " + status);
            }
            Map returnValue = this.processBatchResult(renderResultForPrintListHolder, generateManyFiles);
            OSGiUtils.getTransactionManager().commit();
            return returnValue;
        }
        catch (RenderException e) {
            this.logger.error("Exception:" + e.reason, (Throwable)e);
            if (OSGiUtils.getTransactionManager().getTransaction() != null) {
                OSGiUtils.getTransactionManager().rollback();
            }
            throw new Exception(e.reason, (Throwable)e);
        }
        catch (Exception e) {
            this.logger.error("Exception:", (Throwable)e);
            if (OSGiUtils.getTransactionManager().getTransaction() != null) {
                OSGiUtils.getTransactionManager().rollback();
            }
            throw e;
        }
    }

    private Map processSegmentedResultBuffer(RenderResultBuffer renderResultBuffer) throws Exception {
        PrintBatchOutput outputBuffer = renderResultBuffer.printBatchOutputBuffer;
        HashMap<String, java.lang.Object> returnVal = new HashMap<String, java.lang.Object>();
        if (outputBuffer != null) {
            if (outputBuffer.metaDataBuffer != null) {
                returnVal.put("metadata", (java.lang.Object)this.fileDataBuffersToDocument(outputBuffer.metaDataBuffer, null, null));
            }
            Document mergeDocument = this.fileDataBuffersToDocument(outputBuffer.initializePrintBuffer, outputBuffer.printDataBuffers, outputBuffer.finalizePrintBuffer);
            returnVal.put("batch_output", this.wrapInList(mergeDocument));
        }
        return returnVal;
    }

    private Map processSelfContainedResultBuffer(RenderResultBuffer renderResultBuffer, boolean generateManyFiles) throws Exception {
        PrintOutput outputBuffer = renderResultBuffer.printOutputBuffer;
        HashMap<String, java.lang.Object> returnVal = new HashMap<String, java.lang.Object>();
        if (outputBuffer != null) {
            if (outputBuffer.metaDataBuffer != null) {
                returnVal.put("metadata", (java.lang.Object)this.fileDataBuffersToDocument(outputBuffer.metaDataBuffer, null, null));
            }
            if (outputBuffer.outputBuffers != null) {
                if (generateManyFiles) {
                    ArrayList<Document> list = new ArrayList<Document>();
                    for (int i = 0; i < outputBuffer.outputBuffers.length; ++i) {
                        FileBufferAndId fileBufferAndId = outputBuffer.outputBuffers[i];
                        if (fileBufferAndId == null) continue;
                        list.add(this.fileDataBuffersToDocument(fileBufferAndId.buffer, null, null));
                    }
                    returnVal.put("batch_output", list);
                } else {
                    Document mergedDoc = this.fileDataBuffersToDocument(null, outputBuffer.outputBuffers, null);
                    returnVal.put("batch_output", this.wrapInList(mergedDoc));
                }
            }
        }
        return returnVal;
    }

    private Map processSinglePieceResultBuffer(RenderResultBuffer renderResultBuffer) throws Exception {
        SinglePieceOutput outputBuffer = renderResultBuffer.singlePieceOutputBuffer;
        HashMap<String, java.lang.Object> returnVal = new HashMap<String, java.lang.Object>();
        if (outputBuffer != null) {
            if (outputBuffer.metaDataBuffer != null) {
                returnVal.put("metadata", (java.lang.Object)this.fileDataBuffersToDocument(outputBuffer.metaDataBuffer, null, null));
            }
            if (outputBuffer.outputBuffer != null) {
                returnVal.put("batch_output", this.wrapInList(this.fileDataBuffersToDocument(outputBuffer.outputBuffer, null, null)));
            }
        }
        return returnVal;
    }

    private Map processBatchResult(RenderResultForPrintListHolder renderResultForPrintListHolder, boolean generateManyFiles) throws Exception {
        RenderResultForPrint renderResultForPrint;
        RenderResultBuffer renderResultBuffer;
        RenderResultForPrint[] resultForPrints = renderResultForPrintListHolder.value;
        Map returnValue = null;
        if (resultForPrints != null && resultForPrints.length > 0 && (renderResultForPrint = resultForPrints[0]) != null && (renderResultBuffer = renderResultForPrint.output) != null) {
            if (renderResultBuffer.outputType == RenderBatchOutputType.SEGMENTEDBATCH) {
                returnValue = this.processSegmentedResultBuffer(renderResultBuffer);
            } else if (renderResultBuffer.outputType == RenderBatchOutputType.SELFCONTAINED) {
                returnValue = this.processSelfContainedResultBuffer(renderResultBuffer, generateManyFiles);
            } else if (renderResultBuffer.outputType == RenderBatchOutputType.SINGLEPIECEBATCH) {
                returnValue = this.processSinglePieceResultBuffer(renderResultBuffer);
            }
        }
        return returnValue;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void appendDataBufferToOutStream(FileDataBuffer fdb, OutputStream fos) throws Exception {
        InputStream is = null;
        try {
            if (fdb != null && fdb.getFilePath() != null) {
                int length;
                is = new FileInputStream(fdb.getFilePath());
                int BUFFER = 2048;
                byte[] buff = new byte[BUFFER];
                while ((length = is.read(buff, 0, BUFFER)) != -1) {
                    fos.write(buff, 0, length);
                }
            }
        }
        finally {
            if (is != null) {
                try {
                    is.close();
                }
                catch (Exception e) {
                    this.logger.trace("Exception: ", (Throwable)e);
                }
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private Document fileDataBuffersToDocument(FileDataBuffer initializeDataBuffer, FileBufferAndId[] fileBufferAndIds, FileDataBuffer finalizeDataBuffer) throws Exception {
        File retFile;
        FileOutputStream outputStream = null;
        retFile = this.getTempFile();
        try {
            outputStream = new FileOutputStream(retFile);
            if (initializeDataBuffer != null) {
                this.appendDataBufferToOutStream(initializeDataBuffer, outputStream);
            }
            if (fileBufferAndIds != null) {
                for (int i = 0; i < fileBufferAndIds.length; ++i) {
                    FileBufferAndId fileBufferAndId = fileBufferAndIds[i];
                    if (fileBufferAndId == null) continue;
                    this.appendDataBufferToOutStream(fileBufferAndId.buffer, outputStream);
                }
            }
            if (finalizeDataBuffer != null) {
                this.appendDataBufferToOutStream(finalizeDataBuffer, outputStream);
            }
        }
        finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                }
                catch (Exception e) {
                    this.logger.trace("Exception:", (Throwable)e);
                }
            }
        }
        return new Document(retFile, true);
    }

    private List wrapInList(Document doc) {
        ArrayList<Document> list = new ArrayList<Document>();
        list.add(doc);
        return list;
    }

    public List<byte[]> paExecute(byte[] template, byte[] data, byte[] xci, byte[] xdc, boolean restoreDeltas, boolean runInitializeScripts, byte[] userXCI) throws Exception {
        return this.paExecute(template, data, xci, xdc, restoreDeltas, runInitializeScripts, userXCI, new byte[0], false, restoreDeltas);
    }

    protected void bindTempFileManager(TempFileManager tempFileManager) {
        this.tempFileManager = tempFileManager;
    }

    protected void unbindTempFileManager(TempFileManager tempFileManager) {
        if (this.tempFileManager == tempFileManager) {
            this.tempFileManager = null;
        }
    }

    protected void bindFormsContext(FormsContext formsContext) {
        this.formsContext = formsContext;
    }

    protected void unbindFormsContext(FormsContext formsContext) {
        if (this.formsContext == formsContext) {
            this.formsContext = null;
        }
    }
}