NIHVIVO-3270 Instead of storing request-specific URLs in the FreemarkerConfiguration (where they become global), create a "urls" variable in the body map that includes both request-specific and context-general values, and that masks that "urls" variable in the config.

This commit is contained in:
j2blake 2012-03-06 21:39:24 +00:00
parent d54cdb2a82
commit ca844e91a2

View file

@ -338,42 +338,38 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
return appBean.getThemeDir().replaceAll("/$", ""); return appBean.getThemeDir().replaceAll("/$", "");
} }
/** /**
* Define the request-specific URLs that are accessible to the templates. * Define the request-specific URLs that are accessible to the templates.
* @param VitroRequest vreq * Merge it with the context-specific URLs from the configuration, because
*/ * this map will mask that one.
private void setRequestUrls(VitroRequest vreq) { */
private Map<String, Object> buildRequestUrls(VitroRequest vreq) {
Map<String, Object> requestUrls = new HashMap<String, Object>();
FreemarkerConfiguration config = (FreemarkerConfiguration)vreq.getAttribute("freemarkerConfig"); FreemarkerConfiguration config = (FreemarkerConfiguration)vreq.getAttribute("freemarkerConfig");
TemplateModel urlModel = config.getSharedVariable("urls"); TemplateModel urlModel = config.getSharedVariable("urls");
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> urls = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(urlModel); Map<String, Object> configUrls = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(urlModel);
requestUrls.putAll(configUrls);
// This is request-specific because email can be configured // This is request-specific because email can be configured
// and de-configured in the application interface. // and de-configured in the application interface.
if (FreemarkerEmailFactory.isConfigured(vreq)) { if (FreemarkerEmailFactory.isConfigured(vreq)) {
urls.put("contact", UrlBuilder.getUrl(Route.CONTACT)); requestUrls.put("contact", UrlBuilder.getUrl(Route.CONTACT));
} else {
urls.remove("contact"); // clear value from a previous request
} }
urls.put("currentPage", getCurrentPageUrl(vreq)); requestUrls.put("currentPage", getCurrentPageUrl(vreq));
urls.put("referringPage", getReferringPageUrl(vreq)); requestUrls.put("referringPage", getReferringPageUrl(vreq));
if (PolicyHelper.isAuthorizedForActions(vreq, SimplePermission.EDIT_OWN_ACCOUNT.ACTIONS)) { if (PolicyHelper.isAuthorizedForActions(vreq, SimplePermission.EDIT_OWN_ACCOUNT.ACTIONS)) {
urls.put("myAccount", UrlBuilder.getUrl("/accounts/myAccount")); requestUrls.put("myAccount", UrlBuilder.getUrl("/accounts/myAccount"));
} else {
urls.remove("myAccount"); // clear value from a previous request
} }
config.setSharedVariable("urls", urls);
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
log.error(e, e); log.error(e, e);
} }
return requestUrls;
} }
private String getCurrentPageUrl(HttpServletRequest request) { private String getCurrentPageUrl(HttpServletRequest request) {
@ -424,7 +420,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
// This may be overridden by the body data model received from the subcontroller. // This may be overridden by the body data model received from the subcontroller.
map.put("title", getTitle(vreq.getAppBean().getApplicationName(), vreq)); map.put("title", getTitle(vreq.getAppBean().getApplicationName(), vreq));
setRequestUrls(vreq); map.put("urls", buildRequestUrls(vreq));
map.put("menu", getDisplayModelMenu(vreq)); map.put("menu", getDisplayModelMenu(vreq));