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

com.adobe.pdfservices.operation.pdfjobs.jobs.ProtectPDFJob Maven / Gradle / Ivy

Go to download

Adobe PDF Services SDK allows you to access RESTful APIs to create, convert, and manipulate PDFs within your applications. Older versions can be found under groupId: com.adobe.documentservices, artifactId: pdftools-sdk

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright 2024 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.pdfservices.operation.pdfjobs.jobs;

import com.adobe.pdfservices.operation.PDFServicesJob;
import com.adobe.pdfservices.operation.config.notifier.NotifierConfig;
import com.adobe.pdfservices.operation.exception.ServiceApiException;
import com.adobe.pdfservices.operation.internal.ExecutionContext;
import com.adobe.pdfservices.operation.internal.PDFServicesHelper;
import com.adobe.pdfservices.operation.internal.constants.CustomErrorMessages;
import com.adobe.pdfservices.operation.internal.constants.OperationHeaderInfoEndpointMap;
import com.adobe.pdfservices.operation.internal.dto.request.PlatformApiRequest;
import com.adobe.pdfservices.operation.internal.dto.request.protectpdf.ProtectPDFExternalAssetRequest;
import com.adobe.pdfservices.operation.internal.dto.request.protectpdf.ProtectPDFInternalAssetRequest;
import com.adobe.pdfservices.operation.internal.http.DefaultRequestHeaders;
import com.adobe.pdfservices.operation.internal.http.HttpResponse;
import com.adobe.pdfservices.operation.internal.util.ObjectUtil;
import com.adobe.pdfservices.operation.io.Asset;
import com.adobe.pdfservices.operation.io.CloudAsset;
import com.adobe.pdfservices.operation.io.ExternalAsset;
import com.adobe.pdfservices.operation.pdfjobs.params.protectpdf.EncryptionAlgorithm;
import com.adobe.pdfservices.operation.pdfjobs.params.protectpdf.ProtectPDFParams;

import java.util.List;
import java.util.UUID;


/**
 * A job that is used for securing PDF document with password(s).
 * The password(s) is used for encrypting the PDF document and setting the restriction on certain features
 * like printing, editing and copying in the PDF document.
 * 

* The supported algorithm for encrypting the PDF document are listed here. The {@link EncryptionAlgorithm} enum can * be used to set the * encryption algorithm. *

    *
  • AES-128
  • *
  • AES-256
  • *
*

* For AES-128 encryption the password supports LATIN-I characters only. * For AES-256 encryption the password supports Unicode character set. * *

* Sample Usage: *

{@code
 *             InputStream inputStream = new FileInputStream(new File("SOURCE_PATH"));
 *
 *             Credentials credentials = new ServicePrincipalCredentials(
 *                     System.getenv("PDF_SERVICES_CLIENT_ID"),
 *                     System.getenv("PDF_SERVICES_CLIENT_SECRET"));
 *
 *             PDFServices pdfServices = new PDFServices(credentials);
 *
 *             Permissions permissions = new Permissions();
 *             permissions.addPermission(Permission.PRINT_LOW_QUALITY);
 *             permissions.addPermission(Permission.EDIT_DOCUMENT_ASSEMBLY);
 *             permissions.addPermission(Permission.COPY_CONTENT);
 *             ProtectPDFParams protectPDFParams = ProtectPDFParams.passwordProtectOptionsBuilder()
 *                  .setUserPassword("openpassword")
 *                  .setOwnerPassword("permissionspassword")
 *                  .setEncryptionAlgorithm(EncryptionAlgorithm.AES_128)
 *                  .setContentEncryption(ContentEncryption.ALL_CONTENT_EXCEPT_METADATA)
 *                  .setPermissions(permissions)
 *                  .build();
 *
 *            ProtectPDFJob protectPDFJob = new ProtectPDFJob(asset, protectPDFParams);
 *
 *             String location = pdfServices.submit(protectPDFJob);
 *             PDFServicesResponse pdfServicesResponse = pdfServices.getJobResult(location, ProtectPDFResult.class);
 *
 *             Asset resultAsset = pdfServicesResponse.getResult().getAsset();
 *             StreamAsset streamAsset = pdfServices.getContent(resultAsset);
 * }
*/ public class ProtectPDFJob extends PDFServicesJob { private Asset inputAsset; private Asset outputAsset; private ProtectPDFParams protectPDFParams; /** * Constructs a new {@code ProtectPDFJob} instance. * * @param asset {@link Asset} object containing the input file; can not be null. * @param protectPDFParams {@link ProtectPDFParams} object containing the protect PDF parameters; can not be null. */ public ProtectPDFJob(Asset asset, ProtectPDFParams protectPDFParams) { ObjectUtil.requireNonNull(asset, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Input Asset")); ObjectUtil.requireNonNull(protectPDFParams, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Protect PDF parameters")); this.inputAsset = asset; this.protectPDFParams = protectPDFParams; } @Override protected String process(ExecutionContext executionContext) throws ServiceApiException { return this.process(executionContext, null); } @Override protected String process(ExecutionContext executionContext, List notifyConfigList) throws ServiceApiException { this.validate(executionContext); PlatformApiRequest protectPDFRequest = generatePlatformApiRequest(notifyConfigList); String xRequestId = UUID.randomUUID().toString(); HttpResponse response = PDFServicesHelper.submitJob(executionContext, protectPDFRequest, xRequestId, OperationHeaderInfoEndpointMap.PROTECT_PDF); return response.getHeaders().get(DefaultRequestHeaders.LOCATION_HEADER_NAME); } /** * Sets the output asset for the job. * {@code @note} External assets can be set as output only when input is external asset as well * * @param asset {@link Asset} object representing the output asset; can not be null. * @return {@code ProtectPDFJob} instance */ public ProtectPDFJob setOutput(Asset asset) { ObjectUtil.requireNonNull(asset, String.format(CustomErrorMessages.GENERIC_CAN_NOT_BE_NULL, "Output asset")); if (this.inputAsset instanceof CloudAsset) { throw new IllegalArgumentException(CustomErrorMessages.SET_OUTPUT_VALIDATE); } this.outputAsset = asset; return this; } private PlatformApiRequest generatePlatformApiRequest(List notifyConfigList) { PlatformApiRequest protectPDFRequest; if (this.inputAsset instanceof CloudAsset) { protectPDFRequest = new ProtectPDFInternalAssetRequest(( (CloudAsset) this.inputAsset ).getAssetId(), this.protectPDFParams, notifyConfigList); } else { protectPDFRequest = new ProtectPDFExternalAssetRequest((ExternalAsset) this.inputAsset, this.protectPDFParams, notifyConfigList).setOutput((ExternalAsset) outputAsset); } return protectPDFRequest; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy