NIHVIVO-3729 Don't fetch the FreemarkerConfiguration only to pass it through 6 levels of calls. Instead, get it from the FreemarkerConfigurationLoader when you need it.
This commit is contained in:
parent
981ac675c9
commit
91b2f03780
7 changed files with 61 additions and 86 deletions
|
@ -69,22 +69,14 @@ public abstract class VitroAjaxController extends HttpServlet {
|
|||
return Actions.AUTHORIZED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current Freemarker Configuration so the controller can process
|
||||
* its data through a template.
|
||||
*/
|
||||
protected final Configuration getFreemarkerConfiguration(VitroRequest vreq) {
|
||||
return FreemarkerConfigurationLoader.getConfig(vreq);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process data through a Freemarker template and output the result.
|
||||
*/
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map,
|
||||
Configuration config, HttpServletRequest request, HttpServletResponse response) {
|
||||
Template template = null;
|
||||
VitroRequest vreq, HttpServletResponse response) {
|
||||
Configuration config = FreemarkerConfigurationLoader.getConfig(vreq);
|
||||
try {
|
||||
template = config.getTemplate(templateName);
|
||||
Template template = config.getTemplate(templateName);
|
||||
PrintWriter out = response.getWriter();
|
||||
template.process(map, out);
|
||||
} catch (Exception e) {
|
||||
|
@ -92,7 +84,8 @@ public abstract class VitroAjaxController extends HttpServlet {
|
|||
}
|
||||
}
|
||||
|
||||
protected void doError(HttpServletResponse response, String errorMsg, int httpstatus){
|
||||
protected void doError(HttpServletResponse response, String errorMsg,
|
||||
int httpstatus) {
|
||||
response.setStatus(httpstatus);
|
||||
try {
|
||||
response.getWriter().write(errorMsg);
|
||||
|
|
|
@ -32,7 +32,6 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessing
|
|||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
||||
import edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailFactory;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
public class ContactMailController extends FreemarkerHttpServlet {
|
||||
private static final Log log = LogFactory
|
||||
|
@ -112,15 +111,14 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
String originalReferer = getOriginalRefererFromSession(vreq);
|
||||
|
||||
Configuration config = FreemarkerConfigurationLoader.getConfig(vreq);
|
||||
String msgText = composeEmail(webusername, webuseremail, comments,
|
||||
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq);
|
||||
deliveryfrom, originalReferer, vreq.getRemoteAddr(), vreq);
|
||||
|
||||
try {
|
||||
// Write the message to the journal file
|
||||
FileWriter fw = new FileWriter(locateTheJournalFile(vreq),true);
|
||||
PrintWriter outFile = new PrintWriter(fw);
|
||||
writeBackupCopy(outFile, msgText, config, vreq);
|
||||
writeBackupCopy(outFile, msgText, vreq);
|
||||
|
||||
try {
|
||||
// Send the message
|
||||
|
@ -222,7 +220,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
private String composeEmail(String webusername, String webuseremail,
|
||||
String comments, String deliveryfrom,
|
||||
String originalReferer, String ipAddr, Configuration config,
|
||||
String originalReferer, String ipAddr,
|
||||
HttpServletRequest request) {
|
||||
|
||||
Map<String, Object> email = new HashMap<String, Object>();
|
||||
|
@ -238,7 +236,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
}
|
||||
|
||||
try {
|
||||
return processTemplateToString(template, email, config, request);
|
||||
return processTemplateToString(template, email, request);
|
||||
} catch (TemplateProcessingException e) {
|
||||
log.error("Error processing email text through template: " + e.getMessage(), e);
|
||||
return null;
|
||||
|
@ -246,7 +244,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
}
|
||||
|
||||
private void writeBackupCopy(PrintWriter outFile, String msgText,
|
||||
Configuration config, HttpServletRequest request) {
|
||||
HttpServletRequest request) {
|
||||
|
||||
Map<String, Object> backup = new HashMap<String, Object>();
|
||||
String template = TEMPLATE_BACKUP;
|
||||
|
@ -256,7 +254,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
backup.put("msgText", msgText);
|
||||
|
||||
try {
|
||||
String backupText = processTemplateToString(template, backup, config, request);
|
||||
String backupText = processTemplateToString(template, backup, request);
|
||||
outFile.print(backupText);
|
||||
outFile.flush();
|
||||
//outFile.close();
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
@ -14,7 +13,6 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
/**
|
||||
* TEMPORARY for transition from JSP to FreeMarker. Once transition
|
||||
|
@ -34,34 +32,33 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
|
|||
|
||||
// Mimic what FreemarkerHttpServlet does for a new request
|
||||
VitroRequest vreq = new VitroRequest(request);
|
||||
FreemarkerConfiguration config = getConfig(vreq);
|
||||
Map<String, Object> map = getPageTemplateValues(vreq);
|
||||
|
||||
request.setAttribute("ftl_head", getHead("head", map, config, vreq));
|
||||
request.setAttribute("ftl_identity", get("identity", map, config, vreq));
|
||||
request.setAttribute("ftl_menu", get("menu", map, config, vreq));
|
||||
request.setAttribute("ftl_search", get("search", map, config, vreq));
|
||||
request.setAttribute("ftl_footer", get("footer", map, config, vreq));
|
||||
request.setAttribute("ftl_googleAnalytics", get("googleAnalytics", map, config, vreq));
|
||||
request.setAttribute("ftl_head", getHead("head", map, vreq));
|
||||
request.setAttribute("ftl_identity", get("identity", map, vreq));
|
||||
request.setAttribute("ftl_menu", get("menu", map, vreq));
|
||||
request.setAttribute("ftl_search", get("search", map, vreq));
|
||||
request.setAttribute("ftl_footer", get("footer", map, vreq));
|
||||
request.setAttribute("ftl_googleAnalytics", get("googleAnalytics", map, vreq));
|
||||
}
|
||||
|
||||
private String get(String templateName, Map<String, Object> root, Configuration config, HttpServletRequest request) {
|
||||
private String get(String templateName, Map<String, Object> root, HttpServletRequest request) {
|
||||
templateName += ".ftl";
|
||||
try {
|
||||
return processTemplate(templateName, root, config, request).toString();
|
||||
return processTemplate(templateName, root, request).toString();
|
||||
} catch (TemplateProcessingException e) {
|
||||
log.error("Error processing template " + templateName + ": " + e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getHead(String templateName, Map<String, Object> root, Configuration config, HttpServletRequest request) {
|
||||
private String getHead(String templateName, Map<String, Object> root, HttpServletRequest request) {
|
||||
// The Freemarker head template displays the page title in the <title> tag. Get the value out of the request.
|
||||
String title = (String) request.getAttribute("title");
|
||||
if (!StringUtils.isEmpty(title)) {
|
||||
root.put("title", title);
|
||||
}
|
||||
return get(templateName, root, config, request);
|
||||
return get(templateName, root, request);
|
||||
}
|
||||
|
||||
// RY We need the servlet context in getConfig(). For some reason using the method inherited from
|
||||
|
|
|
@ -41,7 +41,6 @@ import edu.cornell.mannlib.vitro.webapp.web.templatemodels.Tags;
|
|||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.User;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MainMenu;
|
||||
import freemarker.ext.beans.BeansWrapper;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.DefaultObjectWrapper;
|
||||
import freemarker.template.TemplateModel;
|
||||
import freemarker.template.TemplateModelException;
|
||||
|
@ -100,8 +99,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
return;
|
||||
}
|
||||
|
||||
FreemarkerConfiguration config = getConfig(vreq);
|
||||
|
||||
responseValues = processRequest(vreq);
|
||||
doResponse(vreq, response, responseValues);
|
||||
|
||||
|
@ -195,10 +192,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
doGet(request, response);
|
||||
}
|
||||
|
||||
protected FreemarkerConfiguration getConfig(VitroRequest vreq) {
|
||||
return FreemarkerConfigurationLoader.getConfig(vreq);
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, a page requires authorization for no actions.
|
||||
* Subclasses that require authorization to process their page will override
|
||||
|
@ -248,8 +241,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
protected void doTemplate(VitroRequest vreq, HttpServletResponse response,
|
||||
ResponseValues values) throws TemplateProcessingException {
|
||||
|
||||
Configuration config = getConfig(vreq);
|
||||
|
||||
Map<String, Object> templateDataModel = new HashMap<String, Object>();
|
||||
templateDataModel.putAll(getPageTemplateValues(vreq));
|
||||
|
||||
|
@ -264,7 +255,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
if (bodyTemplate != null) {
|
||||
// Tell the template and any directives it uses that we're processing a body template.
|
||||
templateDataModel.put("templateType", BODY_TEMPLATE_TYPE);
|
||||
bodyString = processTemplateToString(bodyTemplate, templateDataModel, config, vreq);
|
||||
bodyString = processTemplateToString(bodyTemplate, templateDataModel, vreq);
|
||||
} else {
|
||||
// The subcontroller has not defined a body template. All markup for the page
|
||||
// is specified in the main page template.
|
||||
|
@ -275,7 +266,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
// Tell the template and any directives it uses that we're processing a page template.
|
||||
templateDataModel.put("templateType", PAGE_TEMPLATE_TYPE);
|
||||
|
||||
writePage(templateDataModel, config, vreq, response, values.getStatusCode(), values);
|
||||
writePage(templateDataModel, vreq, response, values.getStatusCode(), values);
|
||||
}
|
||||
|
||||
protected void doRedirect(HttpServletRequest request, HttpServletResponse response, ResponseValues values)
|
||||
|
@ -484,29 +475,29 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
return siteName;
|
||||
}
|
||||
|
||||
protected StringWriter processTemplate(String templateName, Map<String, Object> map, Configuration config,
|
||||
protected StringWriter processTemplate(String templateName, Map<String, Object> map,
|
||||
HttpServletRequest request) throws TemplateProcessingException {
|
||||
TemplateProcessingHelper helper = new TemplateProcessingHelper(config, request, getServletContext());
|
||||
TemplateProcessingHelper helper = new TemplateProcessingHelper(request, getServletContext());
|
||||
return helper.processTemplate(templateName, map);
|
||||
}
|
||||
|
||||
protected StringWriter processTemplate(ResponseValues values, Configuration config,
|
||||
protected StringWriter processTemplate(ResponseValues values,
|
||||
HttpServletRequest request) throws TemplateProcessingException {
|
||||
return processTemplate(values.getTemplateName(), values.getMap(), config, request);
|
||||
return processTemplate(values.getTemplateName(), values.getMap(), request);
|
||||
}
|
||||
|
||||
// In fact, we can put StringWriter objects directly into the data model, so perhaps we should eliminate the processTemplateToString() methods.
|
||||
protected String processTemplateToString(String templateName, Map<String, Object> map, Configuration config,
|
||||
protected String processTemplateToString(String templateName, Map<String, Object> map,
|
||||
HttpServletRequest request) throws TemplateProcessingException {
|
||||
return processTemplate(templateName, map, config, request).toString();
|
||||
return processTemplate(templateName, map, request).toString();
|
||||
}
|
||||
|
||||
protected String processTemplateToString(ResponseValues values, Configuration config,
|
||||
protected String processTemplateToString(ResponseValues values,
|
||||
HttpServletRequest request) throws TemplateProcessingException {
|
||||
return processTemplate(values, config, request).toString();
|
||||
return processTemplate(values, request).toString();
|
||||
}
|
||||
|
||||
protected void writePage(Map<String, Object> root, Configuration config, HttpServletRequest request,
|
||||
protected void writePage(Map<String, Object> root, HttpServletRequest request,
|
||||
HttpServletResponse response, int statusCode, ResponseValues rv) throws TemplateProcessingException {
|
||||
|
||||
// For an error page, use the standard page template rather than a special one, in case that is the source
|
||||
|
@ -514,17 +505,17 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
// less likely.
|
||||
String pageTemplateName =
|
||||
rv instanceof ExceptionResponseValues ? Template.PAGE_DEFAULT.toString() : getPageTemplateName();
|
||||
writeTemplate(pageTemplateName, root, config, request, response, statusCode);
|
||||
writeTemplate(pageTemplateName, root, request, response, statusCode);
|
||||
}
|
||||
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map, Configuration config,
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map,
|
||||
HttpServletRequest request, HttpServletResponse response) throws TemplateProcessingException {
|
||||
writeTemplate(templateName, map, config, request, response, 0);
|
||||
writeTemplate(templateName, map, request, response, 0);
|
||||
}
|
||||
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map, Configuration config,
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map,
|
||||
HttpServletRequest request, HttpServletResponse response, int statusCode) throws TemplateProcessingException {
|
||||
StringWriter sw = processTemplate(templateName, map, config, request);
|
||||
StringWriter sw = processTemplate(templateName, map, request);
|
||||
write(sw, response, statusCode);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
|
@ -27,8 +28,8 @@ public class TemplateProcessingHelper {
|
|||
private HttpServletRequest request = null;
|
||||
private ServletContext context = null;
|
||||
|
||||
public TemplateProcessingHelper(Configuration config, HttpServletRequest request, ServletContext context) {
|
||||
this.config = config;
|
||||
public TemplateProcessingHelper(HttpServletRequest request, ServletContext context) {
|
||||
this.config = FreemarkerConfigurationLoader.getConfig(new VitroRequest(request));
|
||||
this.request = request;
|
||||
this.context = context;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServ
|
|||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper.TemplateProcessingException;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.login.LoginProcessBean.State;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
/**
|
||||
* A temporary means of displaying the Login templates within the SiteAdmin
|
||||
|
@ -164,13 +163,11 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
|
|||
*/
|
||||
private String doTemplate(VitroRequest vreq, TemplateResponseValues values) throws TemplateProcessingException {
|
||||
// Set it up like FreeMarkerHttpServlet.doGet() would do.
|
||||
Configuration config = getConfig(vreq);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.putAll(getPageTemplateValues(vreq));
|
||||
map.putAll(values.getMap());
|
||||
|
||||
return processTemplateToString(values.getTemplateName(), map, config, vreq);
|
||||
return processTemplateToString(values.getTemplateName(), map, vreq);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -51,7 +51,6 @@ import edu.cornell.mannlib.vitro.webapp.search.beans.VitroQueryFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.LinkTemplateModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.searchresult.IndividualSearchResult;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
/**
|
||||
* Paged search controller that uses Solr
|
||||
|
@ -104,12 +103,11 @@ public class PagedSearchController extends FreemarkerHttpServlet {
|
|||
super.doGet(vreq,response);
|
||||
}else{
|
||||
try {
|
||||
Configuration config = getConfig(vreq);
|
||||
ResponseValues rvalues = processRequest(vreq);
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("text/xml;charset=UTF-8");
|
||||
writeTemplate(rvalues.getTemplateName(), rvalues.getMap(), config, request, response);
|
||||
writeTemplate(rvalues.getTemplateName(), rvalues.getMap(), request, response);
|
||||
} catch (Exception e) {
|
||||
log.error(e, e);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue