DocAssurance Service for encrypting PDF | Community
Skip to main content
Level 2
October 5, 2021
Solved

DocAssurance Service for encrypting PDF

  • October 5, 2021
  • 1 reply
  • 5863 views

Hello team,

 

I am trying to utilize the DocAssuranceService to encrypt PDFs. I have created a class using the Sample 1 under the Securing Documents section here:
https://experienceleague.adobe.com/docs/experience-manager-64/forms/use-document-services/aem-document-services-programmatically.html?lang=en#securing-documents

 

Additionally, I have a servlet identical to the below link, which calls the above class in order to retrieve a certified PDF.

https://experienceleague.adobe.com/docs/experience-manager-learn/forms/creating-your-first-osgi-bundle/create-servlet.html?lang=en

 

I have put a snippet of the class I have created to use the DocAssurance.

 

 

 

public class PassEncryptCertifyExtend { @3214626 private DocAssuranceService docAssuranceService; @3214626 private SlingRepository slingRepository; @3214626 private ResourceResolverFactory jcrResourceResolverFactory ; /** * * @90521 inputFile - path to the pdf document stored at disk * @90521 outputFile - path to the pdf document where the output needs to be stored * @throws Exception */ Document outDoc; Logger logger = Logger.getLogger("classname"); public Document SecureDocument(Document inDoc) throws Exception{ try { logger.info("***** CALLING ENCRYPTION OPTIONS *****"); outDoc = null; outDoc = docAssuranceService.secureDocument(inDoc, getPassEncryptionOptions(), null, null, null); logger.info("***** EXITING ENCRYPTION OPTIONS *****"); } catch(Exception e) { logger.error(e); } return outDoc; }

 

 

 

 
outDoc = docAssuranceService.secureDocument(inDoc, getPassEncryptionOptions(), null, null, null);

 

This line is providing a nullpointer exception when run. I can verify that the inDoc being passed is without any issues. In addition, thegetPassEncryptionOptions() method is being run successfully. I have tried various options such as just passing inDoc with other parameters as null, passing in with all other parameters, yet all variations return a nullPointer exception. The logger indicating the exit of the docAssuranceService.secureDocument does not run and the code returns back to the servlet. I was wondering if I could receive any guidance in this regard.

 

Error:

05.10.2021 15:30:09.987 *INFO* [0:0:0:0:0:0:0:1 [1633462209985] POST /bin/mergedataWithAcroform HTTP/1.1] classname ***** CALLING ENCRYPTION OPTIONS *****

05.10.2021 15:30:10.007 *ERROR* [0:0:0:0:0:0:0:1 [1633462209985] POST /bin/mergedataWithAcroform HTTP/1.1] classname java.lang.NullPointerException

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by workflowuser

public InputStream certifyDocument(com.adobe.aemfd.docmanager.Document documentToCertify, String credentialAlias,
String fieldName) {
this.credentialAlias = credentialAlias;
this.fieldName = fieldName;
ResourceResolver resourceResolver = null;
com.adobe.aemfd.docmanager.Document certifiedDocument = null;

try {

try {
resourceResolver = getResolver.getFormsServiceResolver();


certifiedDocument = docAssuranceService.secureDocument(documentToCertify, null,
getCertificationOptions(resourceResolver), null, null);
log.info("Certified the document");
System.out.println("Certified document");
certifiedDocument.copyToFile(new File("c:\\scrap\\abc.pdf"));
} catch (Exception e) {
e.printStackTrace();
}

} finally {


try {
return certifiedDocument.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

 

you can get the sample resolver from this link. You will have to deploy the bundle and add the user mapper settings as shown in the document

1 reply

workflowuserAdobe EmployeeAccepted solution
Adobe Employee
October 6, 2021

public InputStream certifyDocument(com.adobe.aemfd.docmanager.Document documentToCertify, String credentialAlias,
String fieldName) {
this.credentialAlias = credentialAlias;
this.fieldName = fieldName;
ResourceResolver resourceResolver = null;
com.adobe.aemfd.docmanager.Document certifiedDocument = null;

try {

try {
resourceResolver = getResolver.getFormsServiceResolver();


certifiedDocument = docAssuranceService.secureDocument(documentToCertify, null,
getCertificationOptions(resourceResolver), null, null);
log.info("Certified the document");
System.out.println("Certified document");
certifiedDocument.copyToFile(new File("c:\\scrap\\abc.pdf"));
} catch (Exception e) {
e.printStackTrace();
}

} finally {


try {
return certifiedDocument.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

 

you can get the sample resolver from this link. You will have to deploy the bundle and add the user mapper settings as shown in the document

Adobe Employee
October 6, 2021

This article should also help you in certifying documents

Ashar97Author
Level 2
October 20, 2021

And does this work?

if not send me the Pom.xml


Hello Girish,
Here are the pom files.

core>pom.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!--
 |  Copyright 2017 Adobe Systems Incorporated
 |
 |  Licensed under the Apache License, Version 2.0 (the "License");
 |  you may not use this file except in compliance with the License.
 |  You may obtain a copy of the License at
 |
 |      http://www.apache.org/licenses/LICENSE-2.0
 |
 |  Unless required by applicable law or agreed to in writing, software
 |  distributed under the License is distributed on an "AS IS" BASIS,
 |  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 |  See the License for the specific language governing permissions and
 |  limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.ashforms</groupId>
        <artifactId>ashforms</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>ashforms.core</artifactId>
    <name>ash Forms - Core</name>
    <description>Core bundle for ash Forms</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.sling</groupId>
                <artifactId>sling-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>biz.aQute.bnd</groupId>
                <artifactId>bnd-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>bnd-process</id>
                        <goals>
                            <goal>bnd-process</goal>
                        </goals>
                        <configuration>
                            <bnd><![CDATA[
Import-Package: javax.annotation;version=0.0.0,*
                                ]]></bnd>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>biz.aQute.bnd</groupId>
                <artifactId>bnd-baseline-maven-plugin</artifactId>
                <configuration>
                    <failOnMissing>false</failOnMissing>
                </configuration>
                <executions>
                    <execution>
                        <id>baseline</id>
                        <goals>
                            <goal>baseline</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>uk.org.lidalia</groupId>
            <artifactId>slf4j-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- OSGi Dependencies -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.annotation.versioning</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.annotation.bundle</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.metatype.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.component.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.component</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.cm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.event</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.log</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.resource</artifactId>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.util.tracker</artifactId>
        </dependency>





        <!-- AEM Forms -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.21</version>
        </dependency>


        <dependency>
            <groupId>com.adobe.aemfd</groupId>
            <artifactId>aemfd-client-sdk</artifactId>
            <version>6.3.0</version>
        </dependency>
       
        <!-- Other Dependencies -->

        <dependency>
            <groupId>com.day.cq</groupId>
            <artifactId>serviceuser</artifactId>
            <version>1.0</version>
            <type>bundle</type>
            <scope>system</scope>
            <systemPath>/Users/asharazmat/Documents/Projects/ash/ash-AEM/mylibs/DevelopingWithServiceUser.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.servlets.annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.adobe.aem</groupId>
            <artifactId>uber-jar</artifactId>
            <classifier>apis</classifier>
        </dependency>
        <dependency>
            <groupId>com.adobe.cq</groupId>
            <artifactId>core.wcm.components.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.models.api</artifactId>
        </dependency>

        <!-- Testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit-addons</groupId>
            <artifactId>junit-addons</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.wcm</groupId>
            <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.sling</groupId>
                    <artifactId>org.apache.sling.models.impl</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <!-- Required to be able to support injection with @Self and @Via -->
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.models.impl</artifactId>
            <version>1.4.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>