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-docume...
Additionally, I have a servlet identical to the below link, which calls the above class in order to retrieve a certified PDF.
I have put a snippet of the class I have created to use the DocAssurance.
public class PassEncryptCertifyExtend {
@reference
private DocAssuranceService docAssuranceService;
@reference
private SlingRepository slingRepository;
@reference
private ResourceResolverFactory jcrResourceResolverFactory ;
/**
*
* @Param inputFile - path to the pdf document stored at disk
* @Param 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
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
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
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
Hello team,
Thanks for the response. Just to clarify, I was trying to encrypt the document in my earlier code using the getPassEncryptionOptions() parameter. I have went ahead to try and certify the document using the code mentioned above and ensuring to follow all steps, retrieve and start bundle, ensure correct user (the one in sample resolver link and also tried with the fd-service user created in the article) yet I still get a null pointer exception.
06.10.2021 12:58:46.615 *INFO* [0:0:0:0:0:0:0:1 [1633539526614] POST /bin/mergedataWithAcroform HTTP/1.1] classname ***** Going into new class ***** 06.10.2021 12:58:46.616 *INFO* [0:0:0:0:0:0:0:1 [1633539526614] POST /bin/mergedataWithAcroform HTTP/1.1] classname ***** Inside CertifyDocument ***** 06.10.2021 12:58:46.620 *ERROR* [0:0:0:0:0:0:0:1 [1633539526614] POST /bin/mergedataWithAcroform HTTP/1.1] classname java.lang.NullPointerException at com.goaforms.core.models.impl.PassEncryptCertifyExtend.certifyDocument(PassEncryptCertifyExtend.java:243) at com.goaforms.core.servlets.MyFirstAEMFormsServlet.doPost(MyFirstAEMFormsServlet.java:89) at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:146) at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:342) at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:374) at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:82)
Line 88 & 89 from MyFirstAEMFormsServlet:
What is this class - newClass?
what about the debug statement in this certifyDocument method
newClass.certifyDocument(convertPdf, "ares", "ares", file_path);
send the entire code and the entire log
public InputStream certifyDocument(com.adobe.aemfd.docmanager.Document documentToCertify, String credentialAlias, String fieldName, String file_path) { this.credentialAlias = credentialAlias; this.fieldName = fieldName; ResourceResolver resourceResolver = null; com.adobe.aemfd.docmanager.Document certifiedDocument = null; logger.info("***** Inside CertifyDocument *****"); try { try { resourceResolver = getResolver.getFormsServiceResolver(); logger.info("***** Going into docAssurance *****"); certifiedDocument = docAssuranceService.secureDocument(documentToCertify, null, getCertificationOptions(resourceResolver), null, null); logger.info("Certified the document"); System.out.println("Certified document"); certifiedDocument.copyToFile(new File(file_path)); } catch (Exception e) { // e.printStackTrace(); StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter( writer ); e.printStackTrace( printWriter ); printWriter.flush(); String stackTrace = writer.toString(); logger.error(stackTrace); } } finally { try { return certifiedDocument.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }
import com.mergeandfuse.getserviceuserresolver.GetResolver; @Reference private GetResolver getResolver;
resourceResolver = getResolver.getFormsServiceResolver();
in your project's POM.xml file add the following
<dependency>
<groupId>com.day.cq</groupId>
<artifactId>serviceuser</artifactId>
<version>1.0</version>
<type>bundle</type>
<scope>system</scope>
<systemPath>C:\AEMFormsBundles\DevelopingWithServiceUser.jar</systemPath>
</dependency>
The systemPath value will depend on the location of the DevelopingWithServiceUSer.jar on your file system. Update the project and then you should be good to go
Thanks for your comment - unfortunately it still does not pick it up. I had a colleague try as well and they experienced the same problem. Unsure if this jar is correct.
The general idea is to use ResourceResolver to obtain the service user somehow. In this case, by
resourceResolver = getResolver.getFormsServiceResolver();
There has been other ways to utilize such as here :
ResourceResolver resourceResolver = null; try { /** resourceResolver with admin privileges to be passed to SignatureServiceAPI and Reader Extensions the resource resolver for signature options has to be corresponding to the user who has the signing certificate in his granite key store the resource resolver for signature options has to be corresponding to the user who has the credential for reader extension in his granite key store here we are using the same resource resolver */ adminSession = slingRepository.loginAdministrative(null); resourceResolver = jcrResourceResolverFactory.getResourceResolver(adminSession); VerificationTime verificationTime = getVerificationTimeForPades(); ValidationPreferences dssPrefs = getValidationPreferences(); //retrieve specifications for each of the services, you may pass null if you don't want to use that service //for encrypted document pass Unlock Options - see the method getUnlockOptions() below outDoc = docAssuranceService.applyDocumentTimeStamp(inDoc, verificationTime, dssPrefs, resourceResolver, null); } finally{ /** * always close the PDFDocument object after your processing is done. */ if(inDoc != null){ inDoc.close(); } if(adminSession != null && adminSession.isLive()){ if(resourceResolver != null){ resourceResolver.close(); } adminSession.logout(); } } outDoc.copyToFile(outFile);
but in this case, the jcrResourceResolverFactory has been depricated. Workarounds are similar but not able to give the same format. In addition, the slingRepository.loginAdministrative has deprecated and now have to use loginService.
The main solution needed would be to use ResourceResolver to obtain the service user as mentioned here.
Then use it when calling
(documentToCertify, null, getCertificationOptions(resourceResolver), null, null);
Did you add the folllwing in the user mapper configuration in configMgr?
Yes, I have followed the steps. I have ensured to follow Steps 1-9 and 1-5 from the link here. I uploaded and started the DevelopingWithServiceUser.jar bundle and ensured it was active. I have also added certificate to global trust store, corresponding private key and user mapping settings for fd-service user as mentioned here.
Are you not able to import the developing with service user bundle in your code?
did you add the bundle to the project?
Let me also check the bundle and get back to you
Yes, thats correct. Even after uploading, starting and adding the bundle, the import is still not resolving.
Did you include the bundle in the eclipse/IntelliJ project?
i just included the DevelopingWithServiceUSer.jar in my project and was able to get everything working
import com.mergeandfuse.getserviceuserresolver.GetResolver;
@reference
GetResolver getResolver;
ResourceResolver rr = getResolver.getFormsServiceResolver();
my eclipe project
Hello Girish, thank you for providing the info, I have ensured to add the jar inside the project. It is the same way you have added in your Java Build Path within the Libraries section:
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>
... continuing the rest of pom.xml from root directory as it was too large for previous message:
<!-- ====================================================================== --> <!-- D E P E N D E N C I E S --> <!-- ====================================================================== --> <dependencyManagement> <dependencies> <!-- OSGi Dependencies --> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.annotation.versioning</artifactId> <version>1.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.annotation.bundle</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.metatype.annotations</artifactId> <version>1.4.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.component.annotations</artifactId> <version>1.4.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.component</artifactId> <version>1.4.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.cm</artifactId> <version>1.6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.event</artifactId> <version>1.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.service.log</artifactId> <version>1.4.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.resource</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.framework</artifactId> <version>1.9.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.util.tracker</artifactId> <version>1.5.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.servlets.annotations</artifactId> <version>1.2.4</version> <scope>provided</scope> </dependency> <!-- Logging Dependencies --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!-- Adobe AEM Dependencies --> <dependency> <groupId>com.adobe.aem</groupId> <artifactId>uber-jar</artifactId> <version>6.5.0</version> <classifier>apis</classifier> <scope>provided</scope> </dependency> <!-- Apache Sling Dependencies --> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.models.api</artifactId> <version>1.3.6</version> <scope>provided</scope> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> <scope>provided</scope> </dependency> <!-- JCR --> <dependency> <groupId>javax.jcr</groupId> <artifactId>jcr</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- Taglibs --> <dependency> <groupId>com.day.cq.wcm</groupId> <artifactId>cq-wcm-taglib</artifactId> <version>5.7.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.core</artifactId> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.content</artifactId> <type>zip</type> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.config</artifactId> <type>zip</type> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.extensions.amp</artifactId> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.extensions.amp.content</artifactId> <type>zip</type> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.examples.ui.apps</artifactId> <type>zip</type> <version>${core.wcm.components.version}</version> </dependency> <dependency> <groupId>com.adobe.cq</groupId> <artifactId>core.wcm.components.examples.ui.content</artifactId> <type>zip</type> <version>${core.wcm.components.version}</version> </dependency> <!-- Testing --> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.6.2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.3.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.3.3</version> <scope>test</scope> </dependency> <dependency> <groupId>junit-addons</groupId> <artifactId>junit-addons</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>io.wcm</groupId> <artifactId>io.wcm.testing.aem-mock.junit5</artifactId> <version>3.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>uk.org.lidalia</groupId> <artifactId>slf4j-test</artifactId> <version>1.0.1</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
not sure if this will help, but can you try puttingthe DevelopingWithServiceUser.jar in another folder and update the pom.xml. For example create a folder called customjars under users and put the jar in that folder and try
Hello,
Thanks for your comment. Yes, I tried to reference the jar from another area but unfortunately its still not resolving. I also ensured to update pom.xml file to accomodate the change.
Views
Likes
Replies
Views
Likes
Replies