Generating a Site Map with Presentation Contexts
Added 30 Jul 2008
As discussed in the introduction of this article, generating a site map from the presentation contexts would be quite easy if it was not for tree optimization. The root presentation context, DesktopPresentationContext, is easily retrieved from the current HttpServletRequest and then it is just a matter of walking the list of contexts. From the DesktopPresentationContext we can get the child contexts such as the BookPresentationContext and PagePresentationContext where a context exists for each book and page in the Portal.
Here is the method used to walk the presentation contexts recursively.
private void getSites(HttpServletRequest request,
HttpServletResponse response, BookPresentationContext context,
List sites) {
List children = context.getEntitledPagePresentationContexts();
for (int i = 0; i < children.size(); i++) {
PagePresentationContext child = (PagePresentationContext) children
.get(i);
List childSites = new ArrayList();
if (child instanceof BookPresentationContext) {
childSites = new ArrayList
With tree optimization turned on we can only get the presentation contexts for the slice of the tree that is currently active. As a result, we need to turn off the tree optimization for the single request where the site map needs to be generated. To handle this issue we will attach a backing file to the context site map portlet. Thus if tree optimization is on the backing file simply redirects the request while including _nfto=false in the redirected request. This ensures the next request will have tree optimization turned off and permits a full site map to be constructed.
The backing file does need to perform some checks prior to doing a redirect, the render method will check to see if a site map needs to be generated and if so it also checks if tree optimization is on. Another check is seeing if the site map is already cached, after all there is no point in redirecting the request with tree optimization off if all that is needed is to pull the site map from the cache.
Once tree optimization is turned off building the site map itself off of the presentation contexts is quite simple to implement. However this technique of redirecting does have a couple of potential drawbacks that need to be highlighted. If the site map portlet is sharing the page with other portlets the redirect could interfere with the proper functioning of other portlets. Also, if other portlets on the page require a significant amount of time to render the redirect could cause reduced performance in handling two requests instead of one.