NIHVIVO-1304 Make request and context accessible through the Environment object so directives can reference them.
This commit is contained in:
parent
555c787570
commit
e940ebc379
18 changed files with 188 additions and 131 deletions
|
@ -21,6 +21,7 @@ import javax.mail.internet.AddressException;
|
|||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -28,7 +29,6 @@ import org.apache.commons.logging.LogFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet.ResponseValues;
|
||||
import freemarker.template.Configuration;
|
||||
|
||||
public class ContactMailController extends FreemarkerHttpServlet {
|
||||
|
@ -171,13 +171,13 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
Configuration config = (Configuration) vreq.getAttribute("freemarkerConfig");
|
||||
String msgText = composeEmail(webusername, webuseremail, comments,
|
||||
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config);
|
||||
deliveryfrom, originalReferer, vreq.getRemoteAddr(), config, vreq);
|
||||
|
||||
// Write the email to a backup file
|
||||
try {
|
||||
FileWriter fw = new FileWriter(getServletContext().getRealPath(EMAIL_BACKUP_FILE_PATH),true);
|
||||
PrintWriter outFile = new PrintWriter(fw);
|
||||
writeBackupCopy(outFile, msgText, spamReason, config);
|
||||
writeBackupCopy(outFile, msgText, spamReason, config, vreq);
|
||||
|
||||
// Set the smtp host
|
||||
Properties props = System.getProperties();
|
||||
|
@ -238,7 +238,8 @@ 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, Configuration config,
|
||||
HttpServletRequest request) {
|
||||
|
||||
Map<String, Object> email = new HashMap<String, Object>();
|
||||
String template = TEMPLATE_EMAIL;
|
||||
|
@ -252,11 +253,11 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
email.put("referrer", UrlBuilder.urlDecode(originalReferer));
|
||||
}
|
||||
|
||||
return mergeMapToTemplate(template, email, config);
|
||||
return processTemplateToString(template, email, config, request);
|
||||
}
|
||||
|
||||
private void writeBackupCopy(PrintWriter outFile, String msgText,
|
||||
String spamReason, Configuration config) {
|
||||
String spamReason, Configuration config, HttpServletRequest request) {
|
||||
|
||||
Map<String, Object> backup = new HashMap<String, Object>();
|
||||
String template = TEMPLATE_BACKUP;
|
||||
|
@ -270,7 +271,7 @@ public class ContactMailController extends FreemarkerHttpServlet {
|
|||
|
||||
backup.put("msgText", msgText);
|
||||
|
||||
String backupText = mergeMapToTemplate(template, backup, config);
|
||||
String backupText = processTemplateToString(template, backup, config, request);
|
||||
outFile.print(backupText);
|
||||
outFile.flush();
|
||||
//outFile.close();
|
||||
|
|
|
@ -36,16 +36,16 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
|
|||
Map<String, Object> root = getSharedVariables(vreq, new HashMap<String, Object>());
|
||||
root.putAll(getPageTemplateValues(vreq));
|
||||
|
||||
request.setAttribute("ftl_identity", get("identity", root, config));
|
||||
request.setAttribute("ftl_menu", get("menu", root, config));
|
||||
request.setAttribute("ftl_search", get("search", root, config));
|
||||
request.setAttribute("ftl_footer", get("footer", root, config));
|
||||
request.setAttribute("ftl_googleAnalytics", get("googleAnalytics", root, config));
|
||||
request.setAttribute("ftl_identity", get("identity", root, config, vreq));
|
||||
request.setAttribute("ftl_menu", get("menu", root, config, vreq));
|
||||
request.setAttribute("ftl_search", get("search", root, config, vreq));
|
||||
request.setAttribute("ftl_footer", get("footer", root, config, vreq));
|
||||
request.setAttribute("ftl_googleAnalytics", get("googleAnalytics", root, config, vreq));
|
||||
}
|
||||
|
||||
private String get(String templateName, Map<String, Object> root, Configuration config) {
|
||||
private String get(String templateName, Map<String, Object> root, Configuration config, HttpServletRequest request) {
|
||||
templateName += ".ftl";
|
||||
return mergeToTemplate(templateName, root, config).toString();
|
||||
return processTemplate(templateName, root, config, request).toString();
|
||||
}
|
||||
|
||||
// RY We need the servlet context in getConfig(). For some reason using the method inherited from
|
||||
|
@ -57,7 +57,5 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
|
|||
protected static void setServletContext(ServletContext sc) {
|
||||
context = sc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public class FreemarkerConfigurationLoader {
|
|||
|
||||
private ServletContext context;
|
||||
|
||||
public FreemarkerConfigurationLoader( ServletContext context){
|
||||
public FreemarkerConfigurationLoader(ServletContext context){
|
||||
this.context = context;
|
||||
context.setAttribute("FreemarkerConfigurationLoader", this);
|
||||
}
|
||||
|
|
|
@ -4,11 +4,16 @@ package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
|
@ -18,37 +23,56 @@ public class FreemarkerHelper {
|
|||
private static final Log log = LogFactory.getLog(FreemarkerHelper.class);
|
||||
|
||||
private Configuration config = null;
|
||||
private HttpServletRequest request = null;
|
||||
private ServletContext context = null;
|
||||
|
||||
public FreemarkerHelper(Configuration config) {
|
||||
public FreemarkerHelper(Configuration config, HttpServletRequest request, ServletContext context) {
|
||||
this.config = config;
|
||||
this.request = request;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public StringWriter mergeToTemplate(String templateName, Map<String, Object> map) {
|
||||
public StringWriter processTemplate(String templateName, Map<String, Object> map) {
|
||||
|
||||
Template template = getTemplate(templateName);
|
||||
StringWriter sw = new StringWriter();
|
||||
processTemplate(template, map, sw);
|
||||
return sw;
|
||||
}
|
||||
|
||||
public void processTemplate(Template template, Map<String, Object> map, Writer writer) {
|
||||
|
||||
try {
|
||||
Environment env = template.createProcessingEnvironment(map, writer);
|
||||
// Add request and servlet context as custom attributes of the environment, so they
|
||||
// can be used in directives.
|
||||
env.setCustomAttribute("request", request);
|
||||
env.setCustomAttribute("context", context);
|
||||
env.process();
|
||||
} catch (TemplateException e) {
|
||||
log.error("Template Exception creating processing environment", e);
|
||||
} catch (IOException e) {
|
||||
log.error("IOException creating processing environment", e);
|
||||
}
|
||||
}
|
||||
|
||||
// In fact, we can put StringWriter objects directly into the data model, so perhaps we should eliminate the processTemplateToString() methods.
|
||||
public String processTemplateToString(String templateName, Map<String, Object> map) {
|
||||
return processTemplate(templateName, map).toString();
|
||||
}
|
||||
|
||||
// public String processTemplateToString(String templateName, Map<String, Object> map) {
|
||||
// return processTemplate(templateName, map).toString();
|
||||
// }
|
||||
|
||||
private Template getTemplate(String templateName) {
|
||||
Template template = null;
|
||||
try {
|
||||
template = config.getTemplate(templateName);
|
||||
} catch (IOException e) {
|
||||
log.error("Cannot get template " + templateName);
|
||||
}
|
||||
StringWriter sw = new StringWriter();
|
||||
if (template != null) {
|
||||
try {
|
||||
template.process(map, sw);
|
||||
} catch (TemplateException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sw;
|
||||
}
|
||||
|
||||
|
||||
public String mergeMapToTemplate(String templateName, Map<String, Object> map) {
|
||||
return mergeToTemplate(templateName, map).toString();
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -124,27 +124,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
if (statusCode > 0) {
|
||||
response.setStatus(statusCode);
|
||||
}
|
||||
|
||||
// switch (values.getType()) {
|
||||
// case TEMPLATE:
|
||||
// doTemplate(vreq, response, values);
|
||||
// break;
|
||||
// case REDIRECT:
|
||||
// doRedirect(vreq, response, values);
|
||||
// break;
|
||||
// case FORWARD:
|
||||
// doForward(vreq, response, values);
|
||||
// break;
|
||||
// case EXCEPTION:
|
||||
// doException(vreq, response, values);
|
||||
// break;
|
||||
// }
|
||||
|
||||
// RY Discuss with Jim - doing this instead of the switch allows us to get rid of the
|
||||
// type field. We could also cast the values to the appropriate type: e.g.,
|
||||
// doException(vreq, response, (ExceptionResponseValues) values
|
||||
// then method signature is doException(VitroRequest vreq, HttpServletResponse response, ExceptionResponseValues values)
|
||||
// which seems to make more sense
|
||||
|
||||
if (values instanceof ExceptionResponseValues) {
|
||||
doException(vreq, response, values);
|
||||
} else if (values instanceof TemplateResponseValues) {
|
||||
|
@ -157,11 +137,9 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
doRdf(vreq, response, values);
|
||||
}
|
||||
} catch (ServletException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
log.error("ServletException in doResponse()", e);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
log.error("IOException in doResponse()", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -178,7 +156,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
|
||||
// root is the map used to create the page shell - header, footer, menus, etc.
|
||||
Map<String, Object> root = new HashMap<String, Object>(sharedVariables);
|
||||
//AllHttpScopesHashModel root = new AllHttpScopesHashModel(null, null, vreq);
|
||||
|
||||
root.putAll(getPageTemplateValues(vreq));
|
||||
// Tell the template and any directives it uses that we're processing a page template.
|
||||
|
@ -196,7 +173,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
body.putAll(bodyMap);
|
||||
// Tell the template and any directives it uses that we're processing a body template.
|
||||
body.put("templateType", "body");
|
||||
bodyString = mergeMapToTemplate(bodyTemplate, body, config);
|
||||
bodyString = processTemplateToString(bodyTemplate, body, config, vreq);
|
||||
} else {
|
||||
// The subcontroller has not defined a body template. All markup for the page
|
||||
// is specified in the main page template.
|
||||
|
@ -204,7 +181,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
}
|
||||
root.put("body", bodyString);
|
||||
|
||||
writePage(root, config, response);
|
||||
writePage(root, config, vreq, response);
|
||||
}
|
||||
|
||||
protected void doRedirect(HttpServletRequest request, HttpServletResponse response, ResponseValues values)
|
||||
|
@ -361,6 +338,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
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("url", new edu.cornell.mannlib.vitro.webapp.web.directives.UrlDirective());
|
||||
map.put("widget", new edu.cornell.mannlib.vitro.webapp.web.directives.widgets.BaseWidgetDirective());
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@ -453,29 +431,33 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
return siteName;
|
||||
}
|
||||
|
||||
protected StringWriter mergeToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||
FreemarkerHelper helper = new FreemarkerHelper(config);
|
||||
return helper.mergeToTemplate(templateName, map);
|
||||
protected StringWriter processTemplate(String templateName, Map<String, Object> map, Configuration config,
|
||||
HttpServletRequest request) {
|
||||
FreemarkerHelper helper = new FreemarkerHelper(config, request, getServletContext());
|
||||
return helper.processTemplate(templateName, map);
|
||||
}
|
||||
|
||||
protected StringWriter mergeToTemplate(ResponseValues values, Configuration config) {
|
||||
return mergeToTemplate(values.getTemplateName(), values.getMap(), config);
|
||||
}
|
||||
|
||||
protected String mergeMapToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||
return mergeToTemplate(templateName, map, config).toString();
|
||||
protected StringWriter processTemplate(ResponseValues values, Configuration config, HttpServletRequest request) {
|
||||
return processTemplate(values.getTemplateName(), values.getMap(), config, request);
|
||||
}
|
||||
|
||||
protected String mergeResponseValuesToTemplate(ResponseValues values, Configuration config) {
|
||||
return mergeMapToTemplate(values.getTemplateName(), values.getMap(), config);
|
||||
// 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,
|
||||
HttpServletRequest request) {
|
||||
return processTemplate(templateName, map, config, request).toString();
|
||||
}
|
||||
|
||||
protected String processTemplateToString(ResponseValues values, Configuration config, HttpServletRequest request) {
|
||||
return processTemplate(values, config, request).toString();
|
||||
}
|
||||
|
||||
protected void writePage(Map<String, Object> root, Configuration config, HttpServletResponse response) {
|
||||
writeTemplate(getPageTemplateName(), root, config, response);
|
||||
protected void writePage(Map<String, Object> root, Configuration config, HttpServletRequest request, HttpServletResponse response) {
|
||||
writeTemplate(getPageTemplateName(), root, config, request, response);
|
||||
}
|
||||
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map, Configuration config, HttpServletResponse response) {
|
||||
StringWriter sw = mergeToTemplate(templateName, map, config);
|
||||
protected void writeTemplate(String templateName, Map<String, Object> map, Configuration config,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
StringWriter sw = processTemplate(templateName, map, config, request);
|
||||
write(sw, response);
|
||||
}
|
||||
|
||||
|
|
|
@ -97,16 +97,6 @@ public class TestController extends FreemarkerHttpServlet {
|
|||
return "Test";
|
||||
}
|
||||
|
||||
protected String getBody(VitroRequest vreq, Map<String, Object> body, Configuration config) {
|
||||
|
||||
|
||||
|
||||
// Create the template to see the examples live.
|
||||
String bodyTemplate = "test.ftl";
|
||||
return mergeMapToTemplate(bodyTemplate, body, config);
|
||||
|
||||
}
|
||||
|
||||
private void getBerries(Map<String, Object> body) {
|
||||
body.put("berries", "strawberries, raspberries, blueberries");
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase {
|
|||
|
||||
// Add the values that we got, and merge to the template.
|
||||
body.putAll(values.getMap());
|
||||
return mergeMapToTemplate(values.getTemplateName(), body, config);
|
||||
return processTemplateToString(values.getTemplateName(), body, config, vreq);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -82,7 +82,7 @@ public class AutocompleteController extends FreemarkerHttpServlet{
|
|||
if( vreq.getWebappDaoFactory() == null
|
||||
|| vreq.getWebappDaoFactory().getIndividualDao() == null ){
|
||||
log.error("makeUsableBeans() could not get IndividualDao ");
|
||||
doSearchError(map, config, response);
|
||||
doSearchError(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
IndividualDao iDao = vreq.getWebappDaoFactory().getIndividualDao();
|
||||
|
@ -98,7 +98,7 @@ public class AutocompleteController extends FreemarkerHttpServlet{
|
|||
log.debug("query for '" + qtxt +"' is " + query.toString());
|
||||
|
||||
if (query == null ) {
|
||||
doNoQuery(map, config, response);
|
||||
doNoQuery(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -115,20 +115,20 @@ public class AutocompleteController extends FreemarkerHttpServlet{
|
|||
topDocs = searcherForRequest.search(query,null,maxHitSize);
|
||||
}catch (Exception ex){
|
||||
log.error(ex);
|
||||
doFailedSearch(map, config, response);
|
||||
doFailedSearch(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if( topDocs == null || topDocs.scoreDocs == null){
|
||||
log.error("topDocs for a search was null");
|
||||
doFailedSearch(map, config, response);
|
||||
doFailedSearch(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
int hitsLength = topDocs.scoreDocs.length;
|
||||
if ( hitsLength < 1 ){
|
||||
doFailedSearch(map, config, response);
|
||||
doFailedSearch(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
log.debug("found "+hitsLength+" hits");
|
||||
|
@ -152,11 +152,11 @@ public class AutocompleteController extends FreemarkerHttpServlet{
|
|||
|
||||
Collections.sort(results);
|
||||
map.put("results", results);
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, response);
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, request, response);
|
||||
|
||||
} catch (Throwable e) {
|
||||
log.error("AutocompleteController(): " + e);
|
||||
doSearchError(map, config, response);
|
||||
doSearchError(map, config, request, response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -403,21 +403,19 @@ public class AutocompleteController extends FreemarkerHttpServlet{
|
|||
qp.setDefaultOperator(QueryParser.AND_OPERATOR);
|
||||
return qp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void doNoQuery(Map<String, Object> map, Configuration config, HttpServletResponse response) {
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, response);
|
||||
private void doNoQuery(Map<String, Object> map, Configuration config, HttpServletRequest request, HttpServletResponse response) {
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, request, response);
|
||||
}
|
||||
|
||||
private void doFailedSearch(Map<String, Object> map, Configuration config, HttpServletResponse response) {
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, response);
|
||||
private void doFailedSearch(Map<String, Object> map, Configuration config, HttpServletRequest request, HttpServletResponse response) {
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, request, response);
|
||||
}
|
||||
|
||||
private void doSearchError(Map<String, Object> map, Configuration config, HttpServletResponse response) {
|
||||
private void doSearchError(Map<String, Object> map, Configuration config, HttpServletRequest request, HttpServletResponse response) {
|
||||
// For now, we are not sending an error message back to the client because with the default autocomplete configuration it
|
||||
// chokes.
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, response);
|
||||
writeTemplate(TEMPLATE_DEFAULT, map, config, request, response);
|
||||
}
|
||||
|
||||
public static final int MAX_QUERY_LENGTH = 500;
|
||||
|
|
|
@ -5,10 +5,14 @@ package edu.cornell.mannlib.vitro.webapp.web.directives;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateDirectiveModel;
|
||||
|
||||
|
@ -16,13 +20,13 @@ public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveMod
|
|||
|
||||
private static final Log log = LogFactory.getLog(BaseTemplateDirectiveModel.class);
|
||||
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment environment) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
map.put("name", name);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, environment);
|
||||
}
|
||||
|
||||
protected String getDirectiveName() {
|
||||
|
@ -34,8 +38,18 @@ public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveMod
|
|||
return directiveName;
|
||||
}
|
||||
|
||||
protected String mergeToHelpTemplate(Map<String, Object> map, Configuration config) {
|
||||
return new FreemarkerHelper(config).mergeMapToTemplate("help-directive.ftl", map);
|
||||
protected String mergeToHelpTemplate(Map<String, Object> map, Environment environment) {
|
||||
FreemarkerHelper helper = getFreemarkerHelper(environment);
|
||||
return helper.processTemplateToString("help-directive.ftl", map);
|
||||
}
|
||||
|
||||
public static FreemarkerHelper getFreemarkerHelper(Environment env) {
|
||||
Configuration config = env.getConfiguration();
|
||||
// In a directive, custom attributes for request and context are available in the Environment.
|
||||
// They are put there when the enclosing template is processed.
|
||||
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
|
||||
ServletContext context = (ServletContext) env.getCustomAttribute("context");
|
||||
return new FreemarkerHelper(config, request, context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ public class UrlDirective extends BaseTemplateDirectiveModel {
|
|||
out.write(url);
|
||||
}
|
||||
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
|
@ -78,7 +78,7 @@ public class UrlDirective extends BaseTemplateDirectiveModel {
|
|||
examples.add("<@" + name + " path=\"/images/dummyImages/person.thumbnail.jpg\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -96,7 +96,7 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
}
|
||||
|
||||
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
|
@ -114,7 +114,7 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
|
|||
examples.add("<@" + name + " var=\"stylesheets\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
private List<Method> getPublicMethods(Class<?> cls) {
|
||||
|
|
|
@ -57,7 +57,7 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
|
|||
for (String var : varNames) {
|
||||
Object value = dm.get(var);
|
||||
if (value instanceof BaseTemplateDirectiveModel) {
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(config);
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(env);
|
||||
directives.add(help);
|
||||
} else {
|
||||
models.add(helper.getVariableDump(var));
|
||||
|
@ -80,7 +80,7 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
|
@ -94,7 +94,7 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
|
|||
examples.add("<@" + name + " />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ public class DumpDirective extends BaseTemplateDirectiveModel {
|
|||
}
|
||||
|
||||
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
|
@ -79,7 +79,7 @@ public class DumpDirective extends BaseTemplateDirectiveModel {
|
|||
examples.add("<@" + name + " var=\"urls\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.TemplateBooleanModel;
|
||||
|
@ -35,7 +36,8 @@ public class DumpHelper {
|
|||
|
||||
public String getVariableDump(String varName) {
|
||||
Map<String, Object> map = getVariableDumpData(varName);
|
||||
return new FreemarkerHelper(environment.getConfiguration()).mergeMapToTemplate("dump-var.ftl", map);
|
||||
FreemarkerHelper helper = BaseTemplateDirectiveModel.getFreemarkerHelper(environment);
|
||||
return helper.processTemplateToString("dump-var.ftl", map);
|
||||
}
|
||||
|
||||
public Map<String, Object> getVariableDumpData(String varName) {
|
||||
|
@ -104,8 +106,8 @@ public class DumpHelper {
|
|||
|
||||
public void writeDump(String templateName, Map<String, Object> map, String modelName) {
|
||||
|
||||
FreemarkerHelper helper = new FreemarkerHelper(environment.getConfiguration());
|
||||
String output = helper.mergeMapToTemplate(templateName, map);
|
||||
FreemarkerHelper helper = BaseTemplateDirectiveModel.getFreemarkerHelper(environment);
|
||||
String output = helper.processTemplateToString(templateName, map);
|
||||
Writer out = environment.getOut();
|
||||
try {
|
||||
out.write(output);
|
||||
|
|
|
@ -59,7 +59,7 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
|
|||
Configuration config = env.getConfiguration();
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(config);
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(env);
|
||||
map.put("help", help);
|
||||
|
||||
try {
|
||||
|
@ -74,7 +74,7 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
|
|||
}
|
||||
|
||||
|
||||
public String help(Configuration config) {
|
||||
public String help(Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
|
@ -90,7 +90,7 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
|
|||
examples.add("<@" + name + " directive=\"dump\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, config);
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.directives.widgets;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.ext.servlet.AllHttpScopesHashModel;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateModel;
|
||||
import freemarker.template.TemplateModelException;
|
||||
|
||||
public class BaseWidgetDirective extends BaseTemplateDirectiveModel {
|
||||
|
||||
private static final Log log = LogFactory.getLog(BaseWidgetDirective.class);
|
||||
|
||||
@Override
|
||||
public void execute(Environment env, Map params, TemplateModel[] loopVars,
|
||||
TemplateDirectiveBody body) throws TemplateException, IOException {
|
||||
|
||||
if (loopVars.length != 0) {
|
||||
throw new TemplateModelException(
|
||||
"The dump directive doesn't allow loop variables.");
|
||||
}
|
||||
if (body != null) {
|
||||
throw new TemplateModelException(
|
||||
"The dump directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
Object o = params.get("name");
|
||||
if ( !(o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"Value of parameter 'name' must be a string.");
|
||||
}
|
||||
|
||||
AllHttpScopesHashModel dataModel = (AllHttpScopesHashModel)(env.getDataModel());
|
||||
|
||||
System.out.println("In widget " + o.toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -12,7 +12,7 @@
|
|||
<p><strong>Comments:</strong> ${comments}</p>
|
||||
</#if>
|
||||
|
||||
<h6>Parameters:</h6>
|
||||
<p><strong>Parameters:</strong></p>
|
||||
<#if params?? && params?keys?has_content>
|
||||
<ul>
|
||||
<#list params?keys as param>
|
||||
|
@ -22,8 +22,9 @@
|
|||
<#else>
|
||||
<p>none</p>
|
||||
</#if>
|
||||
<br />
|
||||
|
||||
<h6>Examples:</h6>
|
||||
<p><strong>Examples:</strong></p>
|
||||
<#if examples??>
|
||||
<ul>
|
||||
<#list examples as example>
|
||||
|
|
|
@ -46,14 +46,12 @@
|
|||
|
||||
<p><strong>Berries: </strong>${berries}</p>
|
||||
|
||||
|
||||
<@dump var="now" />
|
||||
<@dump var="urls" />
|
||||
<@dump var="fruit" />
|
||||
<@dump var="trueStatement" />
|
||||
<@dump var="zoo1" />
|
||||
|
||||
|
||||
${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")}
|
||||
${scripts.addFromTheme("/js/jstest.js")}
|
||||
${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
|
||||
|
@ -67,5 +65,5 @@ ${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
|
|||
|
||||
<@describe var="scripts" />
|
||||
|
||||
|
||||
<@describe var="headScripts" />
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue