Some refactoring in FreemarkerHttpServlet

This commit is contained in:
rjy7 2010-09-16 15:50:37 +00:00
parent 8547665029
commit d5664d93ec
4 changed files with 44 additions and 36 deletions

View file

@ -834,15 +834,6 @@
<url-pattern>/listObjectPropertyStatements</url-pattern> <url-pattern>/listObjectPropertyStatements</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet>
<servlet-name>EntityListController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.EntityListController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EntityListController</servlet-name>
<url-pattern>/entitylist</url-pattern>
</servlet-mapping>
<servlet> <servlet>
<servlet-name>IndividualListController</servlet-name> <servlet-name>IndividualListController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController</servlet-class> <servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController</servlet-class>

View file

@ -2,11 +2,11 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker; package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -33,8 +33,8 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
Configuration config = getConfig(vreq); Configuration config = getConfig(vreq);
// root is the map used to create the page shell - header, footer, menus, etc. // root is the map used to create the page shell - header, footer, menus, etc.
Map<String, Object> root = getSharedVariables(vreq); Map<String, Object> root = getSharedVariables(vreq, new HashMap<String, Object>());
setUpRoot(vreq, root); root.putAll(getRootValues(vreq));
request.setAttribute("ftl_identity", get("identity", root, config)); request.setAttribute("ftl_identity", get("identity", root, config));
request.setAttribute("ftl_menu", get("menu", root, config)); request.setAttribute("ftl_menu", get("menu", root, config));

View file

@ -82,16 +82,18 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return getConfigForTheme(themeDir); return getConfigForTheme(themeDir);
} }
@SuppressWarnings("unchecked")
protected Configuration getConfigForTheme(String themeDir) { protected Configuration getConfigForTheme(String themeDir) {
// The template loader is theme-specific because it specifies the theme template directory as a location to // The template loader is theme-specific because it specifies the theme template directory as a location to
// load templates from. Thus configurations are associated with themes rather than portals. // load templates from. Thus configurations are associated with themes rather than portals.
@SuppressWarnings("unchecked")
Map<String, Configuration> themeToConfigMap = (Map<String, Configuration>) (getServletContext().getAttribute("themeToConfigMap")); Map<String, Configuration> themeToConfigMap = (Map<String, Configuration>) (getServletContext().getAttribute("themeToConfigMap"));
if( themeToConfigMap == null ) { if( themeToConfigMap == null ) {
log.error("The templating system is not configured correctly. Make sure that you have the FreemarkerSetup context listener in your web.xml."); log.error("The templating system is not configured correctly. Make sure that you have the FreemarkerSetup context listener in your web.xml.");
return null; // RY should we throw an error here instead? // We'll end up with a blank page as well as errors in the log, which is probably fine.
// Doesn't seem like we should throw a checked exception in this case.
return null;
} else if (themeToConfigMap.containsKey(themeDir)) { } else if (themeToConfigMap.containsKey(themeDir)) {
return themeToConfigMap.get(themeDir); return themeToConfigMap.get(themeDir);
} else { } else {
@ -226,27 +228,24 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
protected void doTemplate(VitroRequest vreq, HttpServletResponse response, ResponseValues values) { protected void doTemplate(VitroRequest vreq, HttpServletResponse response, ResponseValues values) {
Configuration config = getConfig(vreq); Configuration config = getConfig(vreq);
Map<String, Object> bodyMap = values.getMap();
// We can't use shared variables in the Freemarker configuration to store anything // We can't use shared variables in the Freemarker configuration to store anything
// except theme-specific data, because multiple portals or apps might share the same theme. So instead // except theme-specific data, because multiple portals or apps might share the same theme. So instead
// just put the shared variables in both root and body. // just put the shared variables in both root and body.
Map<String, Object> sharedVariables = getSharedVariables(vreq); Map<String, Object> sharedVariables = getSharedVariables(vreq, bodyMap);
// root is the map used to create the page shell - header, footer, menus, etc. // root is the map used to create the page shell - header, footer, menus, etc.
Map<String, Object> root = new HashMap<String, Object>(sharedVariables); Map<String, Object> root = new HashMap<String, Object>(sharedVariables);
// body is the map used to create the page body // body is the map used to create the page body
Map<String, Object> body = new HashMap<String, Object>(sharedVariables); Map<String, Object> body = new HashMap<String, Object>(sharedVariables);
setUpRoot(vreq, root); root.putAll(getRootValues(vreq));
// Add the values that we got, and merge to the template. // Add the values that we got, and merge to the template.
body.putAll(values.getMap()); body.putAll(bodyMap);
root.put("body", mergeMapToTemplate(values.getTemplateName(), body, config)); root.put("body", mergeMapToTemplate(values.getTemplateName(), body, config));
// Subclass processing may have changed the title, so put the new value in the root map. (E.g., the title may
// include an individual's name, which is only discovered when processing the body.)
root.put("title", body.get("title"));
writePage(root, config, response); writePage(root, config, response);
} }
@ -278,7 +277,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
// We can't use shared variables in the Freemarker configuration to store anything // We can't use shared variables in the Freemarker configuration to store anything
// except theme-specific data, because multiple portals or apps might share the same theme. So instead // except theme-specific data, because multiple portals or apps might share the same theme. So instead
// we'll get all the shared variables here, and put them in both root and body maps. // we'll get all the shared variables here, and put them in both root and body maps.
protected Map<String, Object> getSharedVariables(VitroRequest vreq) { protected Map<String, Object> getSharedVariables(VitroRequest vreq, Map<String, Object> bodyMap) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
@ -289,7 +288,16 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
String siteName = portal.getAppName(); String siteName = portal.getAppName();
map.put("siteName", siteName); map.put("siteName", siteName);
map.put("title", getTitle(siteName));
// In some cases the title is determined during the subclass processRequest() method; e.g.,
// for an Individual profile page, the title should be the Individual's label. In that case,
// put that title in the sharedVariables map because it's needed by the page root map as well
// to generate the <title> element. Otherwise, use the getTitle() method to generate the title.
String title = (String) bodyMap.get("title");
if (StringUtils.isEmpty(title)) {
title = getTitle(siteName);
}
map.put("title", title);
String themeDir = getThemeDir(portal); String themeDir = getThemeDir(portal);
UrlBuilder urlBuilder = new UrlBuilder(portal); UrlBuilder urlBuilder = new UrlBuilder(portal);
@ -305,7 +313,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
map.put("scripts", getScriptList(themeDir)); map.put("scripts", getScriptList(themeDir));
map.put("headScripts", getScriptList(themeDir)); map.put("headScripts", getScriptList(themeDir));
addDirectives(map); map.putAll(getDirectives());
return map; return map;
} }
@ -369,18 +377,21 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
} }
// Add any Java directives the templates should have access to // Add any Java directives the templates should have access to
private void addDirectives(Map<String, Object> map) { private Map<String, Object> getDirectives() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("describe", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DescribeDirective()); map.put("describe", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DescribeDirective());
map.put("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpDirective()); map.put("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpDirective());
map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective()); map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective());
map.put("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective()); map.put("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective());
return map;
} }
// Add variables that should be available only to the page's root map, not to the body. // Add variables that should be available only to the page's root map, not to the body.
// RY This is protected instead of private so FreeMarkerComponentGenerator can access. // RY This is protected instead of private so FreeMarkerComponentGenerator can access.
// Once we don't need that (i.e., jsps have been eliminated) we can make it private. // Once we don't need that (i.e., jsps have been eliminated) we can make it private.
protected void setUpRoot(VitroRequest vreq, Map<String, Object> root) { protected Map<String, Object> getRootValues(VitroRequest vreq) {
Map<String, Object> root = new HashMap<String, Object>();
root.put("tabMenu", getTabMenu(vreq)); root.put("tabMenu", getTabMenu(vreq));
Portal portal = vreq.getPortal(); Portal portal = vreq.getPortal();
@ -389,7 +400,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
PortalWebUtil.populateSearchOptions(portal, appBean, vreq.getWebappDaoFactory().getPortalDao()); PortalWebUtil.populateSearchOptions(portal, appBean, vreq.getWebappDaoFactory().getPortalDao());
PortalWebUtil.populateNavigationChoices(portal, vreq, appBean, vreq.getWebappDaoFactory().getPortalDao()); PortalWebUtil.populateNavigationChoices(portal, vreq, appBean, vreq.getWebappDaoFactory().getPortalDao());
addLoginInfo(vreq, root); root.putAll(getLoginValues(vreq));
root.put("copyright", getCopyrightInfo(portal)); root.put("copyright", getCopyrightInfo(portal));
@ -406,6 +417,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
root.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage)); root.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage));
} }
return root;
} }
private TabMenu getTabMenu(VitroRequest vreq) { private TabMenu getTabMenu(VitroRequest vreq) {
@ -415,7 +427,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return new TabMenu(vreq, portalId); return new TabMenu(vreq, portalId);
} }
private final void addLoginInfo(VitroRequest vreq, Map<String, Object> root) { private final Map<String, Object> getLoginValues(VitroRequest vreq) {
String loginName = null; String loginName = null;
int securityLevel; int securityLevel;
@ -426,17 +438,22 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
loginName = loginBean.getLoginName(); loginName = loginBean.getLoginName();
securityLevel = Integer.parseInt(loginBean.getLoginRole()); securityLevel = Integer.parseInt(loginBean.getLoginRole());
} }
Map<String, Object> map = new HashMap<String, Object>();
if (loginName != null) { if (loginName != null) {
root.put("loginName", loginName); map.put("loginName", loginName);
securityLevel = Integer.parseInt(loginBean.getLoginRole()); securityLevel = Integer.parseInt(loginBean.getLoginRole());
if (securityLevel >= FILTER_SECURITY_LEVEL) { if (securityLevel >= FILTER_SECURITY_LEVEL) {
ApplicationBean appBean = vreq.getAppBean(); ApplicationBean appBean = vreq.getAppBean();
if (appBean.isFlag1Active()) { if (appBean.isFlag1Active()) {
root.put("showFlag1SearchField", true); map.put("showFlag1SearchField", true);
} }
} }
} }
return map;
} }
private final Map<String, Object> getCopyrightInfo(Portal portal) { private final Map<String, Object> getCopyrightInfo(Portal portal) {
@ -509,7 +526,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
new FreemarkerComponentGenerator(request); new FreemarkerComponentGenerator(request);
} }
public static interface ResponseValues { protected static interface ResponseValues {
// enum ResponseType { // enum ResponseType {
// TEMPLATE, REDIRECT, FORWARD, EXCEPTION // TEMPLATE, REDIRECT, FORWARD, EXCEPTION
// } // }

View file

@ -163,10 +163,10 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
private String doTemplate(VitroRequest vreq, TemplateResponseValues values) { private String doTemplate(VitroRequest vreq, TemplateResponseValues values) {
// Set it up like FreeMarkerHttpServlet.doGet() would do. // Set it up like FreeMarkerHttpServlet.doGet() would do.
Configuration config = getConfig(vreq); Configuration config = getConfig(vreq);
Map<String, Object> sharedVariables = getSharedVariables(vreq); Map<String, Object> sharedVariables = getSharedVariables(vreq, new HashMap<String, Object>());
Map<String, Object> root = new HashMap<String, Object>(sharedVariables); Map<String, Object> root = new HashMap<String, Object>(sharedVariables);
Map<String, Object> body = new HashMap<String, Object>(sharedVariables); Map<String, Object> body = new HashMap<String, Object>(sharedVariables);
setUpRoot(vreq, root); root.putAll(getRootValues(vreq));
// Add the values that we got, and merge to the template. // Add the values that we got, and merge to the template.
body.putAll(values.getMap()); body.putAll(values.getMap());