In this session, we will learn about how to access resources using bundles deployed in the Felix console. After all, for Sling, everything is a resource. We will update the bundle we developed with a method that accesses a node and prints its title. Though it sounds very simple, we will learn about a very important interface ResourceResolverFactory, that helps us to get ResourveResolver. ResourceResolver gets the Resource object that provides the functionalities to manipulate the nodes and its properties. We will also learn about the adptTo() method that helps to translate an object into a different object type.
So, let us start:
- Add the following method in HelloService interface:
public String getPageTitle();
- Add the following ResourceResolveFactory reference:@Reference ResourceResolverFactory resolverFactory;
- Implement the getPageTitle() method.
- Create a hashmap object with a string & object combination as shown below:
Map<String, Object> param = new HashMap<String, Object>();
- Put ResourceResolverFactory.SUBSERVICE and the service associated to the HashMap object.
This is a new security layer added in AEM on 6.1 version. It means that the resolveResolver object we create is used by a readService process that we would be created later.
- Now create a reference to the ResourseResolver.
- Create resourceResolver object as shown below:
resourceResolver = resolverFactory.getServiceResourceResolver(param);
As I mentioned earlier, for Sling, everything is a resource. Generally, you use Sling APIs for doing any content manipulation. Getting a ResourceResolver object is imperative for accessing the Nodes in Oak, and later doing various operations, such as Create, Read, Update, and Delete (CRUD). You can compare it to creating a connection object in Java to a database.
- Now create a resource object using the getResource() method with a path to the company page as argument.
Resource pageResource = resourceResolver.getResource("/content/aem-company");
- Translate the resource into a Page object using the adaptTo() class.
Page myPage = pageResource.adaptTo(Page.class);
Note that the method takes the class name to which we want to translate the resource as an argument. We do this operation to avail various APIs associated with various objects, such as Node, Page, and so on. In this case, we use the getTitle() method in the Page object. This can throw a null or Login Failed exception, and that’s the reason we added a try catch block.
- Use the following Page method to get the resource title.
String title = myPage.getTitle();
Update the bundle service component
Update the bundleservice page, so that we can test if the bundle returns the value.
- Login to CRXDE LIte.
- Update the bundleservice page.
Create a service user mapper
- Login to System console.
- In OSGI Configuration, search for Apache Sling Service User Mapper Service.
- Create a new service mapping as shown below:
- The first part is the bundle name. Second part is the service name that we used in the method. (Step: 5) Third part is the system user who can access the service.
We are yet to create the system user. Let us do that.
Create a system user and provide access rights
- Login to CRX Explorer.
- Select User Administration > Create System User.
- Enter user ID.
- Select Yes for creation.
- Provide access to the user to avail the paths.
- Search for the user. (myUser)
- Provide full access to the content path.
- Then, Save the user.
Test the component
- Access the page we created.
Note that it displays AEM Company, the title of the node.