All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.platform.operation.internal.api.ExportApi Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2019 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */

package com.adobe.platform.operation.internal.api;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.adobe.platform.operation.internal.ExtensionMediaTypeMapping;
import com.adobe.platform.operation.internal.FileRefImpl;
import com.adobe.platform.operation.internal.InternalExecutionContext;
import com.adobe.platform.operation.internal.http.DefaultRequestHeaders;
import com.adobe.platform.operation.internal.http.HttpResponse;
import com.adobe.platform.operation.internal.cpf.builder.ExportPDFContentAnalyzerRequestsDtoBuilder;
import com.adobe.platform.operation.internal.cpf.constants.OperationKey;
import com.adobe.platform.operation.internal.cpf.context.ContentAnalyzerRequestBuilderContext;
import com.adobe.platform.operation.internal.cpf.context.ExportPDFRequestParamsContext;
import com.adobe.platform.operation.internal.cpf.dto.request.ContentAnalyzerRequestsDto;
import com.adobe.platform.operation.pdfops.options.exportpdf.ExportPDFTargetFormat;

public class ExportApi {

    public static String exportPDF(InternalExecutionContext context,
                                   FileRefImpl sourceFileRef,
                                   ExportPDFTargetFormat exportPDFTargetFormat) throws FileNotFoundException {
        try {
            // Construct the ContentAnalyzerRequestBuilderContext
            ContentAnalyzerRequestBuilderContext contentAnalyzerRequestBuilderContext
                    = new ContentAnalyzerRequestBuilderContext(sourceFileRef.getMediaType(), getOutputFormat(exportPDFTargetFormat));
            contentAnalyzerRequestBuilderContext.setOperationKey(OperationKey.EXPORT_PDF);

            ExportPDFRequestParamsContext exportPDFRequestParamsContext = ExportPDFRequestParamsContext.Builder()
                    .withTargetFormat(exportPDFTargetFormat.getFileExt())
                    .build();

            contentAnalyzerRequestBuilderContext.setExportPdfRequestParamsContext(exportPDFRequestParamsContext);

            ExportPDFContentAnalyzerRequestsDtoBuilder exportPDFContentAnalyzerRequestsDtoBuilder
                        = new ExportPDFContentAnalyzerRequestsDtoBuilder(contentAnalyzerRequestBuilderContext);
            ContentAnalyzerRequestsDto contentAnalyzerRequestsDto = exportPDFContentAnalyzerRequestsDtoBuilder.build();

            // Prepare the sourceFileRefList
            List sourceFileRefList = new ArrayList<>();
            sourceFileRefList.add(sourceFileRef);

            HttpResponse response = CPFApi.cpfPredictApi(context, contentAnalyzerRequestsDto, sourceFileRefList, String.class);
            return response.getHeaders().get(DefaultRequestHeaders.LOCATION_HEADER_NAME);
        } catch (FileNotFoundException fe) {
            throw fe;
        }
    }
    private static String getOutputFormat(ExportPDFTargetFormat exportPDFTargetFormat) {
        switch (exportPDFTargetFormat) {
            case PNG:
            case JPEG:
                return ExtensionMediaTypeMapping.ZIP.getMediaType();
            default:
                return ExtensionMediaTypeMapping
                        .getFromExtension(exportPDFTargetFormat.getFileExt()).getMediaType();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy