1 16 17 package org.apache.velocity.tools.view.servlet; 18 19 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 import java.io.UnsupportedEncodingException ; 27 28 import org.apache.velocity.Template; 29 import org.apache.velocity.app.Velocity; 30 import org.apache.velocity.context.Context; 31 import org.apache.velocity.exception.MethodInvocationException; 32 import org.apache.velocity.exception.ParseErrorException; 33 import org.apache.velocity.exception.ResourceNotFoundException; 34 import org.apache.velocity.runtime.RuntimeSingleton; 35 import org.apache.velocity.tools.view.servlet.VelocityViewServlet; 36 37 38 49 50 public class VelocityLayoutServlet extends VelocityViewServlet 51 { 52 53 57 public static final String PROPERTY_ERROR_TEMPLATE = 58 "tools.view.servlet.error.template"; 59 60 64 public static final String PROPERTY_LAYOUT_DIR = 65 "tools.view.servlet.layout.directory"; 66 67 71 public static final String PROPERTY_DEFAULT_LAYOUT = 72 "tools.view.servlet.layout.default.template"; 73 74 75 78 public static String DEFAULT_ERROR_TEMPLATE = "Error.vm"; 79 80 83 public static String DEFAULT_LAYOUT_DIR = "layout/"; 84 85 88 public static String DEFAULT_DEFAULT_LAYOUT = "Default.vm"; 89 90 91 96 102 public static String KEY_SCREEN_CONTENT = "screen_content"; 103 104 108 public static String KEY_LAYOUT = "layout"; 109 110 111 115 public static String KEY_ERROR_CAUSE = "error_cause"; 116 117 121 public static String KEY_ERROR_STACKTRACE = "stack_trace"; 122 123 130 public static String KEY_ERROR_INVOCATION_EXCEPTION = "invocation_exception"; 131 132 133 private String errorTemplate; 134 private String layoutDir; 135 private String defaultLayout; 136 137 138 144 public void init(ServletConfig config) throws ServletException 145 { 146 super.init(config); 148 149 errorTemplate = 151 RuntimeSingleton.getString(PROPERTY_ERROR_TEMPLATE, DEFAULT_ERROR_TEMPLATE); 152 layoutDir = 153 RuntimeSingleton.getString(PROPERTY_LAYOUT_DIR, DEFAULT_LAYOUT_DIR); 154 defaultLayout = 155 RuntimeSingleton.getString(PROPERTY_DEFAULT_LAYOUT, DEFAULT_DEFAULT_LAYOUT); 156 157 if (!layoutDir.endsWith("/")) 159 { 160 layoutDir += '/'; 161 } 162 163 Velocity.info("VelocityLayoutServlet: Error screen is '"+errorTemplate+"'"); 165 Velocity.info("VelocityLayoutServlet: Layout directory is '"+layoutDir+"'"); 166 Velocity.info("VelocityLayoutServlet: Default layout template is '"+defaultLayout+"'"); 167 168 defaultLayout = layoutDir + defaultLayout; 170 } 171 172 173 181 protected Context createContext(HttpServletRequest request, 182 HttpServletResponse response) 183 { 184 185 Context ctx = super.createContext(request, response); 186 187 String layout = request.getParameter(KEY_LAYOUT); 190 if (layout != null) 191 { 192 ctx.put(KEY_LAYOUT, layout); 194 } 195 return ctx; 196 } 197 198 199 203 protected void mergeTemplate(Template template, 204 Context context, 205 HttpServletResponse response) 206 throws ResourceNotFoundException, ParseErrorException, 207 MethodInvocationException, IOException , 208 UnsupportedEncodingException , Exception 209 { 210 StringWriter sw = new StringWriter (); 215 template.merge(context, sw); 216 context.put(KEY_SCREEN_CONTENT, sw.toString()); 218 219 Object obj = context.get(KEY_LAYOUT); 225 String layout = (obj == null) ? null : obj.toString(); 226 if (layout == null) 227 { 228 layout = defaultLayout; 230 } 231 else 232 { 233 layout = layoutDir + layout; 235 } 236 237 try 238 { 239 template = getTemplate(layout); 241 } 242 catch (Exception e) 243 { 244 Velocity.error("VelocityLayoutServlet: Can't load layout \"" + 245 layout + "\": " + e); 246 247 if (!layout.equals(defaultLayout)) 249 { 250 template = getTemplate(defaultLayout); 253 } 254 } 255 256 super.mergeTemplate(template, context, response); 258 } 259 260 261 264 protected void error(HttpServletRequest request, 265 HttpServletResponse response, 266 Exception e) 267 throws ServletException 268 { 269 try 270 { 271 Context ctx = createContext(request, response); 273 274 Throwable cause = e; 275 276 if (cause instanceof MethodInvocationException) 278 { 279 ctx.put(KEY_ERROR_INVOCATION_EXCEPTION, e); 281 cause = ((MethodInvocationException)e).getWrappedThrowable(); 283 } 284 285 ctx.put(KEY_ERROR_CAUSE, cause); 287 288 StringWriter sw = new StringWriter (); 290 cause.printStackTrace(new java.io.PrintWriter (sw)); 291 ctx.put(KEY_ERROR_STACKTRACE, sw.toString()); 292 293 Template et = getTemplate(errorTemplate); 295 mergeTemplate(et, ctx, response); 296 297 } 298 catch (Exception e2) 299 { 300 Velocity.error("VelocityLayoutServlet: " + 302 " Error during error template rendering - " + e2); 303 super.error(request, response, e); 305 } 306 } 307 308 309 } 310 | Popular Tags |