Generating a Site Map with the View API
Added 30 Jul 2008
This approach discusses the use of the View APIs to build a site map. Unlike the presentation context option, this option has no issues with tree optimization. However it does have a different issue, namely that the View APIs are not entitled. This means that if we use the View APIs as is the site map would show all books and pages including those that are entitled and the user cannot access. The solution we present here corrects this oversight by using the entitlements API to calculate the entitlement for each view artifact correctly.
The first thing that the code does is to get an instance of the DesktopView class that represents the current desktop, here is the code for that.
PortalCustomizationManager manager = PortalBeanManager
.getInstance().getPortalCustomizationManager();
CustomizationContext cc = new CustomizationContext(request);
DesktopView view = manager.getDesktopView(cc, appContext
.getWebAppName(),
new PortalPath(appContext.getPortalPath()),
Once we have the DesktopView it is an easy matter to get the root BookView and then start building the tree. The BookView interface declares a method called getNavigableViews() and this is what we use to get the children of the BookView.
Once we have a specific View object, be it BookView or PageView, we need to check if is entitled and this is where things get complicated. For better or worse the documentation around the Portal security framework is pretty weak in terms of explaining how things work but the key class is the Authorization class.
On the positive side, if you look at the javadoc for that class you can see that it has a method called isAccess allowed which fits our need nicely. On the negative side, it takes an object called a P13nResource object which is turn built from a string called a ResourceID. The negative is what these ResourceID strings consist of is not documented as far as I know, however a little digging around shows that they are a concatenated String with each element separated by the constant EntitlementConstants.RESOURCE_ID_DELIMITER.
In the first attempt at getting this going, the class HierarchyRoleResource which extends P13nResource, was used. Creating one of these instances is quite straightforward and can be done as follows:
new DesktopPath(appContext.getDesktopPath()));
String resourceId = resourceType
+ EntitlementConstants.RESOURCE_ID_DELIMITER
+ appContext.getPortalPath()
+ EntitlementConstants.RESOURCE_ID_DELIMITER
+ appContext.getDesktopPath()
+ EntitlementConstants.RESOURCE_ID_DELIMITER
+ view.getDefinitionLabel();
P13nResource resource = new HierarchyRoleResource(SecurityHelper
.getApplicationName(), appContext.getWebAppName(), resourceId,
"view");
Now you may be wondering, how did we figure out how to build the resourceID variable? Fortunately there is an easy way in Portal to find out the Resource IDs needed and that is the propagation tool. The propagation tool needs to output entitlements in order to be able to create them in a new environment. By reviewing the propagation inventory, which is simply a collection of XML files, we can extract the format for the Resource ID.
To get a sample ID, simply create an entitlement in the Portal Admin Tool (PAT) and then create a propagation inventory. Open the inventory zip file and then open the toc_0.zip contained within that. Finally open the toc.xml file in an editor and search for '__Entitlement'. You should see something like this: