Handling Http Requests, HttpClient, in AEM
When integrating with external services or APIs from Adobe Experience Manager (AEM), utilizing the HttpClient for outbound HTTP requests is a common requirement. It’s crucial to handle these connections efficiently, securely, and in a way that is compatible with AEM’s architecture to ensure optimal performance and maintainability of your code. AEM provides a specialized factory, […]
AEM Style System Component Information with Code Sling Models
As an AEM full-stack web developer who frequently works with AEM components, it’s not uncommon to require access to the AEM Style System configuration of a specific component. This information can prove invaluable when writing conditional algorithms to manipulate data output. However, the question remains: how can one retrieve the necessary AEM Style System information […]
WCM Core Components Sling Models Interfaces Design
While reviewing Adobe’s WCM Core components you may realize that the Sling model all have JAVA interfaces. Why do they follow this design pattern, and what benefits does it help to the overall design of their Sling models? In this article, I will share my understanding of why Adobe designed its Sling models like this. […]
Checking run mode from Sightly HTL in AEM
Sometimes during development, we would need to check the run mode in our AEM environment, so we can show different content based on the context. From the Sightly HTL API, it’s possible to check for the wcmmode, but not the runmode. In this article, we will set up a sling model helper which will be […]
How to Generate Unique AEM Component ID with Confidence
You frequently require a special ID for the AEM component. Either a backend server-side call or a custom JS action trigger are required. If the component is dragged and placed on the page more than once, the hard-coded field ID is not guaranteed to be unique. When considering ID, you might consider directly using the […]
JUnit 4: AEM How to Mock @RequestAttribute value in Sling Model
This blog article will show code an example for JUnit 4, for how the Request Attribute is being mocked. This example will only cover mocking the @RequestAttribute with the use of @PostConstruct runtime phase, When you are in the code runtime phase of the constructor injection, this will not work (I spent a bit of […]
Get Cookie Example of AEM Servlet and Sling Model
In this short article, we will show two code examples; for example, one will be showcasing how cookies values can be accessed for an AEM Servlet, and for example two, Sling Model. 1. AEM Servlet Get Cookie Example 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package com.sourcedcode.core.servlet; import com.cat.wcm.core.util.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import javax.servlet.Servlet; import […]
Set Cookie Example of AEM Servlet and Sling Model
In this short article, we will show two code examples; for example, one will be showcasing how cookies values can be set for an AEM Servlet, and for example two, Sling Model. 1. AEM Servlet Set Cookie Example 12345678910111213141516171819202122232425262728293031323334353637package com.sourcedcode.core.servlets; import com.cat.wcm.core.util.JSONObject; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import javax.servlet.Servlet; import javax.servlet.http.Cookie; import […]
How to Pass Data Parameters to Sling Modal from Sightly HTL Component
You can pass data parameters from Sightly HTL component to the Sling Model backend by request attributes. For implementation, utilising the org.apache.sling.models.annotations.Model, we will add required options parameter, adaptable, with the value of SlingHttpServletRequest.class. Next, we can utilize the annotation, org.apache.sling.models.annotations.injectorspecific.RequestAttribute (@RequestAttribute), which will find the matching name of the attribute that is being passed […]
JUnit 4: AEM Sling Models Unit Test Constructor Injection Example
In this example, we will take a look at how we can mock Sling Model Constructor Injection dependencies for a JUnit 4 Unit test. Scenario: The Sling Model must expose either the PROD_URL or the DEAFULT_URL endpoint based on the run mode or request parameters; this is the requirement. The example below will demonstrate the […]
AEM Sling Model Constructor Injection
Sling Models field injection, @inject, are used to support injection of AEM Library-specific context objects. However, there is an alternative way to inject AEM objects into Sling Models, and its called Constructor Injection. The benefits of Sling Model Constructor Injection: Does not store the injection references to the adaptable. Since it does not store injection […]
AEM Sling Model Field Injection vs Constructor Injection Memory Consumption
Article was last updated on June 7th, 2020. Sling Models field injectors are used to support injection of AEM Library-specific context objects. For example, @ScriptVariable SightlyWCMMode will inject the WCMMode object, @ScriptVariable Resource will inject the current resource object, and @ScriptVariable Style will inject the Style object. These objects are typically stored within the object, […]
AEM Sling Model Injectors Annotations Cheat Sheet Reference Guide
The Apache Sling Model enables injector specific annotations which aggregate the standard annotations for each of the available injector, which are: Script Bindings, Value Map, Resource Path, Child Resources, Request Attributes, OSGI Services, Self, and the Sling Object. Sure we can invoke injectors by the @inject, followed by the @source annotation (with an injector name) […]
AEM Sling Models Injectors Service Ranking
When working on an AEM project, Sling Models provides several custom Sling Models injectors to aid injection of Sling objects, Sling object values, OSGI services, etc… While using the injectors within in Sling Models, how do injectors invoke in order? Injectors are invoked in order, of their service ranking, from lowest to highest. If you […]
JUnit 4: AEM Sling Servlet Unit Test Example Using wcm.io AEM Mocks, Servlet by Resource Type
This article will demonstrate how to write AEM Unit tests for @SlingServletResourceTypes (OSGi DS 1.4 (R7) component property type annotations) using the Junit4 testing framework. With developers being more visual, the source code is posted below. Technologies here used are: AEM project archetype 19 (link) Mockito 2.27.0 (link) AEM Mocks JUnit 4 2.7.2 (link) This […]
JUnit 4: AEM Sling Models Unit Test Example Using wcm.io AEM Mocks
Creating new AEM components, we sometimes need backend logic to compute user requests with business logic. There are multiple ways of doing so, like using the Java-Use API or Javascript-Use API, but the most popular and best practice of writing business logic for an AEM component will be using Sling Models. This article will demonstrate […]