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
  • 5847 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 6, 2021

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:

PassEncryptCertifyExtend newClass = new PassEncryptCertifyExtend();
newClass.certifyDocument(convertPdf, "ares", "ares", file_path);

Line 249 and below from PassEncryptCertifyExtend
resourceResolver = getResolver.getFormsServiceResolver();
logger.info("***** Going into docAssurance *****");
certifiedDocument = docAssuranceService.secureDocument(documentToCertify, null, getCertificationOptions(resourceResolver), null, null);