com.adobe.platform.operation.internal.service.InsertPagesService Maven / Gradle / Ivy
/*
* 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.service;
import com.adobe.platform.operation.internal.FileRefImpl;
import com.adobe.platform.operation.internal.InternalExecutionContext;
import com.adobe.platform.operation.internal.api.CPFApi;
import com.adobe.platform.operation.internal.cpf.constants.CPFConstants;
import com.adobe.platform.operation.internal.cpf.dto.request.CombinePDFParams;
import com.adobe.platform.operation.internal.cpf.dto.request.ContentAnalyzerRequests;
import com.adobe.platform.operation.internal.cpf.dto.request.Engine;
import com.adobe.platform.operation.internal.http.DefaultRequestHeaders;
import com.adobe.platform.operation.internal.http.HttpResponse;
import com.adobe.platform.operation.internal.options.CombineOperationInput;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class InsertPagesService {
private static final String INPUT_OUTPUT_FORMAT_SPECIFIER = "application/pdf";
private static final int INPUT_SOURCE_LIMIT = 20;
private static InsertFilesInput getFilesToInsert(FileRefImpl baseFileRef,
Map> filesToInsert) {
List fileRefList = new ArrayList<>();
List pageRangeList = new ArrayList<>();
int baseFileStartIndex = 1;
for (Map.Entry> entry : filesToInsert.entrySet()) {
if (entry.getKey() != 1) {
fileRefList.add(baseFileRef);
pageRangeList.add(baseFileStartIndex + "-" + (entry.getKey() - 1));
baseFileStartIndex = entry.getKey();
}
for (CombineOperationInput combineOperationInput : entry.getValue()) {
fileRefList.add(combineOperationInput.getSourceFileRef());
pageRangeList.add(combineOperationInput.getPageRanges().toString());
}
}
fileRefList.add(baseFileRef);
pageRangeList.add(baseFileStartIndex + "-");
return new InsertFilesInput(fileRefList, pageRangeList);
}
public static String insertPages(InternalExecutionContext context,
FileRefImpl baseFileRef,
Map> filesToInsert,
String operation) throws FileNotFoundException {
try {
InsertFilesInput insertFilesInput = getFilesToInsert(baseFileRef, filesToInsert);
// Validate insert operation input source limit
if (insertFilesInput.fileRefList.size() > INPUT_SOURCE_LIMIT) {
throw new IllegalArgumentException("Too many insertions specified for the operation");
}
// Prepare the sourceFileRefList
List sourceFileRefList = insertFilesInput.fileRefList;
// Build Combine PDF Params for EnginesDto
CombinePDFParams combinePDFParams = CombinePDFParams.createFrom(insertFilesInput.pageRanges);
// Build EnginesDto for ContentAnalyzerRequest
Engine engine = Engine.builder(CPFConstants.CombinePDF.V2_ENGINE_EXECUTION_INFO,
INPUT_OUTPUT_FORMAT_SPECIFIER,
INPUT_OUTPUT_FORMAT_SPECIFIER)
.setParams(combinePDFParams)
.setNumOfInputs(sourceFileRefList.size())
.supportsMultipleInputs(true)
.build();
// Create ContentAnalyzerRequest
ContentAnalyzerRequests contentAnalyzerRequests
= new ContentAnalyzerRequests(CPFConstants.CombinePDF.V2_ENGINE_NAME, engine);
HttpResponse response = CPFApi.cpfPredictApi(context, contentAnalyzerRequests,
sourceFileRefList, String.class, operation);
return response.getHeaders().get(DefaultRequestHeaders.LOCATION_HEADER_NAME);
} catch (FileNotFoundException fe) {
throw fe;
}
}
private static class InsertFilesInput {
private List fileRefList;
private List pageRanges;
InsertFilesInput(List fileRefList, List pageRanges) {
this.fileRefList = fileRefList;
this.pageRanges = pageRanges;
}
}
}