Expand my Community achievements bar.

SOLVED

What is the best approach to expose an API hosted on AEM as a REST API

Avatar

Level 4

Env. - AEM-Forms 6.5v, on-premise setup.

 

We want to use OutputService/ FormsService/ AssemblerService to generate a PDF using XML payload (coming from 3rd party system) and XDP template stored in AEM repository. So what is the best approach to expose this API as REST which will be hosted on AEM-Forms.

 

3rd party system will be calling AEM-Forms API with a xml payload to generate the PDF. Some of the searches suggested following [1], do let us know your opinions:

- A a path based Sling servlet with POST method

- an OSGi service  

 

[1] https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/exposing-rest-web-services...

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor
4 Replies

Avatar

Employee Advisor

@hrai 

As you know, sling servlet is one of the options to expose functionality in a RESTful way. More details about the related method for each service are here[0], along with the javadocs.

You can review the reference implementation for the Assembler service[1] and Forms/Output service [2] and adapt these as per your use case.

 

[0] - https://experienceleague.adobe.com/docs/experience-manager-65/forms/install-aem-forms/osgi-installat...

[1] - https://experienceleague.adobe.com/docs/experience-manager-learn/forms/document-services/using-assem... 

[2] - https://experienceleague.adobe.com/docs/experience-manager-learn/forms/document-services/output-and-...

 

 

Avatar

Level 4

The best approach for exposing the OutputService/FormsService/AssemblerService as a REST API hosted on AEM-Forms would likely be to create a custom Sling Servlet. The servlet should be configured to handle requests using the POST method, and should be able to process the XML payload from the 3rd party system and use it to generate a PDF using the XDP template stored in the AEM repository.

Alternatively, you can use an OSGi service with the same functionality, it will have the same functionality as the Sling Servlet. This will make the service available to other OSGi components in the system and can be easily configured and managed through the OSGi framework.

In either case, it is important to ensure that the API is properly secured and that only authorized 3rd party systems are able to access it.

You can refer to this article in case you want to see which servlet approach suits.

  1. Mastering Servlet Creation in AEM: 4 Approaches and Best Practices - https://medium.com/@monendra80/mastering-servlet-creation-in-aem-4-approaches-and-best-practices-95c...
  2. Securing Servlets from Cyber Attacks: Code Examples, Best Practices and Prevention Techniques - https://medium.com/@monendra80/the-ultimate-guide-to-securing-servlets-in-aem-code-examples-best-pra...

 

 

Avatar

Level 4

>>  The best approach for exposing the OutputService/FormsService/AssemblerService as a REST API hosted on AEM-Forms would likely be to create a custom Sling Servlet. The servlet should be configured to handle requests using the POST method, and should be able to process the XML payload from the 3rd party system and use it to generate a PDF using the XDP template stored in the AEM repository.

 

Okay, I assume then path based sling servlet can be used here, which will be exposed as REST API (already tested with Postman) works fine.

@component(service = { Servlet.class }, property = { SLING_SERVLET_METHODS + "=POST",
SLING_SERVLET_EXTENSIONS + "=json", SLING_SERVLET_PATHS + "=/bin/generatePDFService" })

public class GeneratePDFService extends SlingAllMethodsServlet {

----

Document doc = outputService.generatePDFOutput(xdpDoc, xmlDoc, outputOptions);

---

}

Postman-screen-shot1.PNG

Avatar

Correct answer by
Employee Advisor