{
// save access exceptions
SecurityException accessException = null;
// valid SiteView required from session profile locators
SiteView view = getSiteView();
if (view != null)
{
// default request to root folder if not specified
if (requestPath == null)
{
requestPath = Folder.PATH_SEPARATOR;
}
// log page request
if (log.isDebugEnabled())
{
log.debug("Request page: request path=" + requestPath);
}
// lookup request path in view for viewable page or folder
// nodes; note: directly requested pages/folders may be hidden
// or not viewable
Node requestNode = null;
try
{
// try page or folder request url
requestNode = view.getNodeProxy(requestPath, null, false, false);
}
catch (NodeNotFoundException nnfe)
{
// if request path ends with default page, strip from
// request url to retry for folder default
if (requestPath.endsWith(Folder.PATH_SEPARATOR + Folder.FALLBACK_DEFAULT_PAGE))
{
// retry folder request url
requestPath = requestPath.substring(0, requestPath.length() - Folder.FALLBACK_DEFAULT_PAGE.length());
requestNode = view.getNodeProxy(requestPath, null, true, false);
}
else
{
// rethrow original exception
throw nnfe;
}
}
// invoke default page logic to determine folder page
if (requestNode instanceof Folder)
{
Folder requestFolder = (Folder)requestNode;
// support subfolders specified as default pages;
// find highest subfolder with a default page that
// specifies a default folder, (not a default page).
try
{
String defaultFolderName = requestFolder.getDefaultPage();
if (defaultFolderName != null)
{
// do not follow broken default folders
Folder defaultRequestFolder = requestFolder;
// follow default folders to parent folders
while ((defaultRequestFolder != null) && (defaultFolderName != null) &&
defaultFolderName.equals(".."))
{
defaultRequestFolder = (Folder)defaultRequestFolder.getParent();
if (defaultRequestFolder != null)
{
defaultFolderName = defaultRequestFolder.getDefaultPage();
}
else
{
defaultFolderName = null;
}
}
// follow default folders to subfolders
while ((defaultRequestFolder != null) && (defaultFolderName != null) &&
!defaultFolderName.endsWith(Page.DOCUMENT_TYPE) && !defaultFolderName.equals(".."))
{
defaultRequestFolder = defaultRequestFolder.getFolder(defaultFolderName);
defaultFolderName = defaultRequestFolder.getDefaultPage();
}
// use default request folder
if (defaultRequestFolder != null)
{
requestFolder = defaultRequestFolder;
}
}
}
catch (NodeException ne)
{
}
catch (NodeNotFoundException nnfe)
{
}
catch (SecurityException se)
{
requestFolder = null;
accessException = se;
}
// only request folders with pages can be
// selected by request; otherwise, fall back to
// parent folders assuming that immediate parents
// will have the most appropriate default page
NodeSet requestFolderPages = null;
if (requestFolder != null)
{
try
{
requestFolderPages = requestFolder.getPages();
while (((requestFolderPages == null) || requestFolderPages.isEmpty()) && (requestFolder.getParent() != null))
{
requestFolder = (Folder)requestFolder.getParent();
requestFolderPages = requestFolder.getPages();
}
}
catch (NodeException ne)
{
requestFolderPages = null;
}
catch (SecurityException se)
{
requestFolderPages = null;
accessException = se;
}
}
if ((requestFolder != null) && (requestFolderPages != null) && !requestFolderPages.isEmpty())
{
Page requestPage = null;
// attempt to lookup last visited page by folder proxy
// path, (proxies are hashed by their path), contains
// test must be performed since identical paths may
// occur in multiple site views
if (useHistory)
{
requestPage = (Page)getFolderPageHistory().get(requestFolder);
if ((requestPage != null) && requestFolderPages.contains(requestPage))
{
// log selected request page
if (log.isDebugEnabled())
{
log.debug("Selected folder historical page: path=" + view.getManagedPage(requestPage).getPath());
}
return requestPage;
}
}
// get default page for folder proxy if more than one
// page is available to choose from
if (requestFolderPages.size() > 1)
{
String defaultPageName = requestFolder.getDefaultPage();
if (defaultPageName == null)
{
// use fallback default if default page
// not explicitly specified
defaultPageName = Folder.FALLBACK_DEFAULT_PAGE;
}
try
{
// save last visited non-hidden page for folder proxy
// path, (proxies are hashed by their path), and
// return default page
requestPage = requestFolder.getPage(defaultPageName);
if (!requestPage.isHidden())
{
getFolderPageHistory().put(requestFolder, requestPage);
}
// log selected request page
if (log.isDebugEnabled())
{
log.debug("Selected folder default page: path=" + view.getManagedPage(requestPage).getPath());
}
return requestPage;
}
catch (NodeException ne)
{
}
catch (NodeNotFoundException nnfe)
{
}
catch (SecurityException se)
{
accessException = se;
}
}
// default page not available, select first page
// proxy in request folder; save last visited
// non-hidden page for folder proxy path, (proxies
// are hashed by their path), and return default page
requestPage = (Page)requestFolderPages.iterator().next();
if (!requestPage.isHidden())
{
getFolderPageHistory().put(requestFolder, requestPage);
}
// log selected request page
if (log.isDebugEnabled())
{
log.debug("Selected first folder page, path=" + view.getManagedPage(requestPage).getPath());
}
return requestPage;
}
}
else if (requestNode instanceof Page)
{
Page requestPage = (Page)requestNode;
// save last visited non-hidden page for folder proxy
// path, (proxies are hashed by their path), and
// return matched page
Folder requestFolder = (Folder)requestPage.getParent();
if (!requestPage.isHidden())
{
getFolderPageHistory().put(requestFolder, requestPage);
}
// log selected request page
if (log.isDebugEnabled())
{
log.debug("Selected page, path=" + view.getManagedPage(requestPage).getPath());
}
return requestPage;
}
}