1 43 package net.jforum; 44 45 46 import javax.servlet.http.HttpServletResponse ; 47 48 import net.jforum.exceptions.TemplateNotFoundException; 49 import net.jforum.repository.Tpl; 50 import net.jforum.util.preferences.ConfigKeys; 51 import net.jforum.util.preferences.SystemGlobals; 52 import net.jforum.util.preferences.TemplateKeys; 53 import freemarker.template.SimpleHash; 54 import freemarker.template.Template; 55 56 64 public abstract class Command 65 { 66 private static Class [] NO_ARGS_CLASS = new Class [0]; 67 private static Object [] NO_ARGS_OBJECT = new Object [0]; 68 69 private boolean ignoreAction; 70 71 protected String templateName; 72 protected ActionServletRequest request; 73 protected HttpServletResponse response; 74 protected SimpleHash context; 75 76 protected void setTemplateName(String templateName) 77 { 78 this.templateName = Tpl.name(templateName); 79 } 80 81 protected void ignoreAction() 82 { 83 this.ignoreAction = true; 84 } 85 86 96 public abstract void list() throws Exception ; 97 98 104 public Template process(ActionServletRequest request, 105 HttpServletResponse response, 106 SimpleHash context) throws Exception 107 { 108 this.request = request; 109 this.response = response; 110 this.context = context; 111 112 String action = this.request.getAction(); 113 114 if (!this.ignoreAction) { 115 try { 116 this.getClass().getMethod(action, NO_ARGS_CLASS).invoke(this, NO_ARGS_OBJECT); 117 } 118 catch (NoSuchMethodException e) { 119 this.list(); 120 } 121 catch (Exception e) { 122 throw e; 123 } 124 } 125 126 if (JForumExecutionContext.getRedirectTo() != null) { 127 this.setTemplateName(TemplateKeys.EMPTY); 128 } 129 else if (request.getAttribute("template") != null) { 130 this.setTemplateName((String )request.getAttribute("template")); 131 } 132 133 if (JForumExecutionContext.isCustomContent()) { 134 return null; 135 } 136 137 if (this.templateName == null) { 138 throw new TemplateNotFoundException("Template for action " + action + " is not defined"); 139 } 140 141 return JForumExecutionContext.templateConfig().getTemplate( 142 new StringBuffer (SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR)). 143 append('/').append(this.templateName).toString()); 144 } 145 } 146 | Popular Tags |