Wednesday, 31 July 2024

Servlet in AEM/Sling Servlet

 What is Servlet?

  • A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between the web browser, from which HTTP requests originate, and the application web server.
  • Servlets help us process data submitted by end users using POST requests and return data to the web browser using GET requests from the application web server.
  • Adobe Experience Manager (AEM) facilitates the creation and deployment of servlet classes on the web server, typically packaged in the form of an OSGi bundle rather than a WAR file, which is more common in traditional Java EE environments.


Over all flow / Architecture


  • The request will get originate from web browser to get and send data from to server.
  • Request will use HTTP or HTTPS protocol to get or send data to Dispatcher.
  • The request from web browser will first reach to dispatcher. Dispatcher will return cached data if found. Else it will reach out to AEM instance.
  • AEM instance will reach out to specific servlet class depending on servlet URL mapping, request type(GET, POST, DELETE etc. ), content type etc.
  • Web browser can send or get data from server in predefined formats such as JSON, XML, binary etc.
  • Servlet class will collect data from web browser. Servlet class will process the data collected from browser. Servlet class will and save/store same data in database. Servlet class can also collect data from database and send back to browser in same predefined format.
  • Servlet class will process data using third party API data and other classes within application server.

How are different ways to register servlet in AEM?

  • You can register a Servlet using the two Standard approaches:
1. Registering the servlet by path
2. Register servlet by ResourceType

Difference between PathTypeServlet and ResourceType Servlet?



How many ways to writing servlet?


  • Note: Felix Scr is depricated nowadays we are using only Declarative Service(1.2) and Declarative Service(1.2)
How many ways you can extend Servlet?


  • import org.apache.sling.api.servlets.SlingAllMethodsServlet;
  • import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

  • SlingSafeMethodServlet provides a capability to create read only servlets. It only supports GET operation.
  • SlingAllMethodServlet provides a capability to create both read and write servlet. It supports all operations such as GET, POST, PUT, DELETE, etc.

Note:
  •  @Component annotation can be use to register a servlet.
  • Below is the syntax to create a servlet by path.
@Component(service = { Servlet.class },
property = {
“sling.servlet.paths=/bin/declrativeservicer5” ,
“sling.servlet.methods=GET”,
“sling.servlet.extensions=txt”,
"sling.servlet.selector =" + "aem"
})
public class SlingServletByPath extends SlingSafeMethodsServlet {

sling.servlet.paths helps us to provide path to access servlet.

sling.servlet.methods is responsible to declare servlet method as Get, Post, Delete, etc.

sling.servlet.selectors is a special form of parameter which help us to pass data to backend servlet code. Below is the example where we are passing John to backend servlet code as part of URL.

sling.servlet.resourceTypes This is specifies that the servlet will respond to resources of the type.


sling.servlet.extensions is require to allow number of extensions as part of URL’s.

Basic pseudo code of Declarative Service(1.2)/ OSGi R5 annotaion


  • To define a servlet and bind it with a specific path, you use the sling.servlet.paths property. For example, to bind your servlet to /bin/declrativeservicer5, you can configure it as follows:
  • Note: You can replace /bin with any custom path. For instance, if you want to use /aem instead of /bin, you can modify the path accordingly:
@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Example Custom Path Servlet", "sling.servlet.paths=" + "/aem/declrativeservicer5", "sling.servlet.methods=GET" })

  • If you want to add a resource type to the servlet, you can use the sling.servlet.resourceTypes property. For example, to bind your servlet to the resource type /apps/we-retail/components/listwithcontentchanges, configure it like this:
@Component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "/apps/we-retail/components/listwithcontentchanges", "sling.servlet.methods=GET" })
  • To define the extension and selector for your servlet, use the sling.servlet.extensions and sling.servlet.selectors properties. For example, to handle requests with the .html extension and the aem selector, configure it like this:
@Component(service = Servlet.class, property = { 
 "sling.servlet.resourceTypes=" + "/apps/we-retail/components/listwithcontentchanges", "sling.servlet.extensions=" + "html",
 "sling.servlet.selectors=" + "aem"
 "sling.servlet.methods=GET" })

  • Path Binding: Use sling.servlet.paths to bind your servlet to a specific path.
  • Resource Type Binding: Use sling.servlet.resourceTypes to bind your servlet to a specific resource type.
  • Extension and Selector Binding: Use sling.servlet.extensions to specify the extension and sling.servlet.selectors to specify the selector your servlet should handle.


  • How To call your servlet using the GET method in a browser using the pathType?


    How To call your servlet using the GET method in a browser using the ResourceType?




    Basic pseudo code of Declarative Service(1.4)/ OSGi R7 annotaion


    Friday, 24 May 2024

    Workflow part-2(Custom Workflow Process and Custom Workflow Step )

    Process step is allow us to execute ECMAScript or OSGI service.

    • Implement WorkflowProcess interface and override execute() method to create custom workflow process. Refer this URL to read more about workflow, model and its steps.
    Follow below syntax to implement workflow process .



    • WorkItem: This contains workflow data as shown below.
    • WorkflowSession: It provides the complete capability to manage workflow models and instances. Workflow session allows us to provide complete on workflow model, items, active items, workflows(running, abort, completed).
    • MetaDataMap: It provide access to data map and arguments passed as an input.Process Step Implementation.
    Process Step Implementation
    • Follow below step to create process and consume as part of workflow process.
    1. Create a Java class. Implement WorkflowProcess interface and override execute method.


    2. Create workflow model and process step to consume above created custom process step. Open URL and click on Models tile for navigating to Models section.


    3. Click on Create button to create a brand new model.


    4. Provide Title for workflow model and click on Done button.


    5. Select newly created workflow model as click on Edit button to add workflow steps.

    6. Click on Drag components here parsys to drag and drop step.
    7. Select Process Step option.


    8. Configure Process Step and provide above create custom step as part of step1.

    9. Click on Sync will pick latest configurations from workflow model while execution.

    10. Open any page to Start Workflow as shown below.


    11. Select workflow created earlier as shown below and click on Start Workflow to start workflow execution.


    12. It will call execute() method from CustomProcess.java class as soon as click on Start Workflow button.


    14.In the repository, added pages have properties 'approval 1' and 'approval 2'.
    Custom Workflow Step with dialog in aem


    • A custom workflow process is a backend module that can be individually deployed to AEM. Once you deploy this custom workflow process in AEM. it will be available inside a process step dropdown.
    • It does not have its own dialog. If you need to pass some values to this, you must use the existing dialog of the process step.
    • This is not applicable on the workflow model page.
    • If you want to create a custom workflow step, you need a component (only a dialog) and an editConfig node.
    • A custom workflow process is an individual module that does not need a custom workflow step to work; it can be deployed individually to AEM.
    • A custom workflow step needs a process and a custom workflow process



    1.Go to Tools -> Workflow -> Models. Open Workflow Models.


    2.create Custom Workflow step 





    3.After created dialog

    4.Set the path in open properties
    5. 
    Configure the Custom Workflow Step to add our Custom Step.


    6. After authoring the Custom Workflow Step.
    7. We found our dialog, which we created for the customized workflow component.

    8.
    Once done with configuring, Click on Sync at the top rightmost corner of the       screen to propagate the changes at Runtime.

        
    9. Write a custom workflow process


    10.
    Your workflow is created. You can try running it.
    11. 
    In the Logger, print FirstName, LastName, and title.


    12.You can see that your custom workflow process step is working properly, which is         why it shows in the archive.



    Hope You can complete or step back the workflow step to take the necessary actions.

    Thanks for reading
    Matri Sharma


    Quotes of the day
    Just one small positive thought in the morning can change your whole day.


    Wednesday, 8 May 2024

    Workflow in AEM (Part -1)

     What is Workflow?

    • It allows you to automate your tasks, which can be individual tasks or a series of tasks performed sequentially, mainly on content, such as pages and assets.
    • When you create a Workflow, the first thing you need is a Workflow Model. The Workflow Model is the heart of the workflow, as it contains a series of steps. Each step can perform a specific task, providing complete functionality. You can reuse these steps in any workflow.
    • There are 'n' number of steps available out of the box in AEM. However, you can also create your own steps.



    When you create a Workflow Model, it is stored in two places in the repository.
    • Model Design [/conf/global/setting/workflow/model]
    • Runtime Model [/var/workflow/models]
    When we talk about steps, for example, step 1, step 2, up to step 'n', there are four types.

    • Container Step: When we want to trigger another model or workflow, for example, when we want to execute or trigger a child workflow, we use a Container Step. 
    • Participant Step: When we require human interaction within a workflow, such as assigning tasks to someone, we use a participant step.
    • Decision Step: When you need to perform a task based on a condition, typically when branching is required in your workflow, you use a decision step.
    • There are two types of decision steps:
    1. AND
    2. OR
    • Process Step: Custom workflow processes are implemented using the process step.
    After creating a workflow model, it can be invoked in three main ways.

    1. Launcher
    2. API/Code
    3. Manually
    • Launcher:- When we want to automate the triggering of a workflow, we create a Launcher.
    • API/code:-When we want to trigger this workflow using code, we can do so in any backend module, such as a servlet or Sling model. This can be achieved using API/Code.
    • Manually:- In AEM, we have a console where we can trigger this workflow manually.
    How do you write a Workflow Model?




    • Models: All the Out of the Box models will be present under models, and you can create your own model.
    • Instances: All the running instances of that workflow will be present under 'instances.
    • Launchers: All the Out Of the Box launchers will be present under 'launchers', and you can create your own launcher as well.
    • Archive:- All the successful workflow instances will be present under Archive.
    • Failures:- In case your workflow fails due to any reason, the instances of failed workflows will be present under 'Failures'.
    Step1:



    Step 2:



    Step 3:



    Step 4:




    Step 5:



    Step 6:



    Step 7:

    Manually Trigger Workflow



    Step 8:



    How to trigger a workflow through the Launcher

    Step 1:

    After create Workflow



    Step 2:


    Step 3:




    Step 4:



    How to trigger a workflow through the API/Code




    • For triggering a workflow via API in the backend, first, you need a WorkflowSession.
    • Once you have the WorkflowSession, you then need a WorkflowModel. You need the WorkflowModel when you are using the WorkflowSession to pass the exact path in your Runtime model, which you can then trigger.
    • After that, you need WorkflowData, which you use with the WorkflowSession, essentially using the payload of your workflow.
    • Once you have both the WorkflowModel and WorkflowData, you can start the workflow by using the WorkflowSession and passing both the WorkflowModel and WorkflowData.

    We are using a servlet as a backend module to trigger the workflow using API/code.

    Step1:


    Step 2:


    Step 3:

    In error.logs


    Step 4:



    Quotes
    Success is the sum of small efforts - repeated day in and day out.

    Thanks for Reading
    Matri Sharma

    Servlet in AEM/Sling Servlet

      What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...