In this article we will adding our custom metadata in OOTB pageinfo Servlet. PageInfo servlet returns information about resources or page present in the respository. User is required to pass the path of the resource. The OOTB servlet is bound to the http://server:port/libs/wcm/core/content/pageinfo.json URL. In order to get the data of a page, you can simply send the request :-
http://localhost:4502/libs/wcm/core/content/pageinfo.json?path=/content/pageinfo/en
As a response you will get data in JSON format.
Important point is this JSON data about the resource is collected from various OOTB page info service provider implementation. Let’s take a look at some of those:-
1. Default Page Status Provider: Information about the page status, such as whether it is locked, whether the page is the payload of an active workflow, and which workflows are available for the page. Below is the data returned by this
"status": {
"path": "/content/geometrixx-outdoors/en",
"isLocked": false,
"lockOwner": "",
"canUnlock": false,
"isDesignable": true
},
2. Live Relationship Info Provider: Information regarding Multi Site Management (MSM), such as whether the page is part of a BluePrint, and whether it is a Live Copy. Below is the data returned by this
"msm": {
"msm:isLiveCopy": false,
"msm:isInBlueprint": true,
"msm:isSource": true
},
Complete list can be seen in your AEM at path /libs/wcm/foundation/components/page/cq:infoProviders
Now when you implement your own page info service provider, your custom metadata will be displayed along with the data given by other page info service provider. Follow the below steps to create your own service.
1. Create your own OSGI service, service will implement the com.day.cq.wcm.api.PageInfoProvider interface.
2. In the page component of your project, Add a node below the page component named cq:infoProviders of type nt:unstructured.
3. Below the cq:infoProviders node, add a node with any name of type nt:unstructured. Add the following property to this node:
Name: className
Type: String
Value: The PID of your PageInfoProvider service.
At the end it should be like below image
Once the above structure has been created, all you need to do is write a small service class with the same name as given above in className property.
package com.pageinfo.core.service;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageInfoProvider;
@Component(metatype = false)
@Properties({
@Property(name = "service.description", value = "Returns our custom metadata added")
})
@Service
public class CustomPageInfoProvider implements PageInfoProvider {
private final Logger logger = LoggerFactory.getLogger(getClass());
// Method which will be executed once servlet is called
public void updatePageInfo(SlingHttpServletRequest request,
JSONObject info, Resource resource) throws JSONException {
Page page = resource.adaptTo(Page.class);
JSONObject urlinfo = new JSONObject();
urlinfo.put("name", page.getName());
urlinfo.put("depth", page.getDepth());
urlinfo.put("navigationTitle", page.getNavigationTitle());
urlinfo.put("title", page.getPageTitle());
info.put("customPageData", urlinfo);
}
}
Once service is deployed in OSGI you can access data for your page using the URL
http://localhost:4502/libs/wcm/core/content/pageinfo.json?path=/content/pageinfo/en
In below response it can be see that we are getting our custom data along with other metadata.
It was tested in AEM 6.1, should work in 6.0 and 5.6.1 without any issue.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.