進行API呼叫

[AEM Forms as a Cloud Service]{class="badge informative"}

套用使用許可權

取得存取Token後,下一步就是提出API要求,將使用許可權套用至指定的PDF。 這涉及在請求標頭中包含存取權杖以驗證呼叫,確保安全和授權處理檔案。

以下函式套用使用許可權

public void applyUsageRights(String accessToken,String endPoint) {

            String host = "https://" + BUCKET + ".adobeaemcloud.com";
            String url = host + endPoint;
            String usageRights = "{\"comments\":true,\"embeddedFiles\":true,\"formFillIn\":true,\"formDataExport\":true}";

            logger.info("Request URL: {}", url);
            logger.info("Access Token: {}", accessToken);

            ClassLoader classLoader = DocumentGeneration.class.getClassLoader();
            URL pdfFile = classLoader.getResource("pdffiles/withoutusagerights.pdf");

            if (pdfFile == null) {
                logger.error("PDF file not found!");
                return;
            }

            File fileToApplyRights = new File(pdfFile.getPath());
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            InputStream generatedPDF = null;
            FileOutputStream outputStream = null;

            try {
                httpClient = HttpClients.createDefault();
                byte[] fileContent = FileUtils.readFileToByteArray(fileToApplyRights);
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addBinaryBody("document", fileContent, ContentType.create("application/pdf"),fileToApplyRights.getName());
                builder.addTextBody("usageRights", usageRights, ContentType.APPLICATION_JSON);

                HttpPost httpPost = new HttpPost(url);
                httpPost.addHeader("Authorization", "Bearer " + accessToken);
                httpPost.addHeader("X-Adobe-Accept-Experimental", "1");
                httpPost.setEntity(builder.build());

                response = httpClient.execute(httpPost);
                generatedPDF = response.getEntity().getContent();
                byte[] bytes = IOUtils.toByteArray(generatedPDF);

                outputStream = new FileOutputStream(SAVE_LOCATION + File.separator + "ReaderExtended.pdf");
                outputStream.write(bytes);
                logger.info("ReaderExtended File is  saved at "+SAVE_LOCATION);
            } catch (IOException e) {
                logger.error("Error applying usage rights", e);
            } finally {
                try {
                    if (generatedPDF != null) generatedPDF.close();
                    if (response != null) response.close();
                    if (httpClient != null) httpClient.close();
                    if (outputStream != null) outputStream.close();
                } catch (IOException e) {
                    logger.error("Error closing resources", e);
                }
            }
        }

功能劃分:

  • 設定API端點和承載

    • 使用提供的endPoint和預先定義的BUCKET建構API URL。

    • 定義JSON字串(usageRights),指定要套用的許可權,例如:

      • 評論
      • 內嵌檔案
      • 表單填寫
      • 表單資料匯出
  • 載入PDF檔案

    • pdffiles目錄中擷取withoutusagerights.pdf檔案。
    • 記錄錯誤,並會在找不到檔案時結束。
  • 準備HTTP要求

    • 將PDF檔案讀入位元組陣列。

    • 使用MultipartEntityBuilder建立包含下列內容的多部分要求:

      • 作為二進位主體的PDF檔案。
      • usageRights JSON為文字內文。
    • 設定具有標頭的HTTP POST要求:

      • Authorization: Bearer <accessToken>以進行驗證。
      • X-Adobe-Accept-Experimental: 1 (可能是API相容性的必要專案)。
  • 傳送要求與處理回應

    • 使用httpClient.execute(httpPost)執行HTTP要求。
    • 讀取回應(預期為已套用使用許可權的更新PDF)。
    • SAVE_LOCATION將接收的PDF內容寫入​ "ReaderExtended.pdf"
  • 錯誤處理與清理

    • 擷取並記錄任何IOException錯誤。
    • 確保finally區塊中的所有資源(串流、HTTP使用者端、回應)已正確關閉。

以下是叫用applyUsageRights函式的main.java程式碼

package com.aemformscs.communicationapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        try {
            String accessToken = new AccessTokenService().getAccessToken();
            DocumentGeneration docGen = new DocumentGeneration();

            docGen.applyUsageRights(accessToken, "/adobe/document/assure/usagerights");

            // Uncomment as needed
            // docGen.extractPDFProperties(accessToken, "/adobe/document/extract/pdfproperties");
            // docGen.mergeDataWithXdpTemplate(accessToken, "/adobe/document/generate/pdfform");

        } catch (Exception e) {
            logger.error("Error occurred: {}", e.getMessage(), e);
        }
    }
}

main方法會透過從AccessTokenService呼叫getAccessToken()來初始化,預期此方法會傳回有效的權杖。

  • 然後它會從DocumentGeneration類別呼叫applyUsageRights(),並傳入:

    • 已擷取的accessToken
    • 用於套用使用許可權的API端點。

後續步驟

部署範例專案

recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69