1 package org.apache.velocity.servlet; 2 3 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.io.OutputStreamWriter ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.UnsupportedEncodingException ; 26 27 import java.util.Properties ; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletOutputStream ; 33 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.apache.velocity.Template; 39 import org.apache.velocity.runtime.RuntimeConstants; 40 import org.apache.velocity.runtime.RuntimeSingleton; 41 import org.apache.velocity.io.VelocityWriter; 42 import org.apache.velocity.util.SimplePool; 43 44 import org.apache.velocity.context.Context; 45 import org.apache.velocity.VelocityContext; 46 47 import org.apache.velocity.app.Velocity; 48 49 import org.apache.velocity.exception.ResourceNotFoundException; 50 import org.apache.velocity.exception.ParseErrorException; 51 import org.apache.velocity.exception.MethodInvocationException; 52 53 93 public abstract class VelocityServlet extends HttpServlet 94 { 95 98 public static final String REQUEST = "req"; 99 100 103 public static final String RESPONSE = "res"; 104 105 108 public static final String CONTENT_TYPE = "default.contentType"; 109 110 113 public static final String DEFAULT_CONTENT_TYPE = "text/html"; 114 115 116 119 public static final String DEFAULT_OUTPUT_ENCODING = "ISO-8859-1"; 120 121 125 private static String defaultContentType; 126 127 131 protected static final String INIT_PROPS_KEY = 132 "org.apache.velocity.properties"; 133 134 138 private static final String OLD_INIT_PROPS_KEY = "properties"; 139 140 143 144 private static SimplePool writerPool = new SimplePool(40); 145 146 154 public void init( ServletConfig config ) 155 throws ServletException 156 { 157 super.init( config ); 158 159 162 initVelocity( config ); 163 164 167 defaultContentType = RuntimeSingleton.getString(CONTENT_TYPE, 168 DEFAULT_CONTENT_TYPE); 169 } 170 171 180 protected void initVelocity( ServletConfig config ) 181 throws ServletException 182 { 183 try 184 { 185 190 191 Properties props = loadConfiguration( config ); 192 193 Velocity.init( props ); 194 } 195 catch( Exception e ) 196 { 197 throw new ServletException ("Error initializing Velocity: " + e, e); 198 } 199 } 200 201 249 protected Properties loadConfiguration(ServletConfig config) 250 throws IOException , FileNotFoundException 251 { 252 String propsFile = config.getInitParameter(INIT_PROPS_KEY); 257 if (propsFile == null || propsFile.length() == 0) 258 { 259 ServletContext sc = config.getServletContext(); 260 propsFile = config.getInitParameter(OLD_INIT_PROPS_KEY); 261 if (propsFile == null || propsFile.length() == 0) 262 { 263 propsFile = sc.getInitParameter(INIT_PROPS_KEY); 264 if (propsFile == null || propsFile.length() == 0) 265 { 266 propsFile = sc.getInitParameter(OLD_INIT_PROPS_KEY); 267 if (propsFile != null && propsFile.length() > 0) 268 { 269 sc.log("Use of the properties initialization " + 270 "parameter '" + OLD_INIT_PROPS_KEY + "' has " + 271 "been deprecated by '" + INIT_PROPS_KEY + '\''); 272 } 273 } 274 } 275 else 276 { 277 sc.log("Use of the properties initialization parameter '" + 278 OLD_INIT_PROPS_KEY + "' has been deprecated by '" + 279 INIT_PROPS_KEY + '\''); 280 } 281 } 282 283 292 293 Properties p = new Properties (); 294 295 if ( propsFile != null ) 296 { 297 String realPath = getServletContext().getRealPath(propsFile); 298 299 if ( realPath != null ) 300 { 301 propsFile = realPath; 302 } 303 304 p.load( new FileInputStream (propsFile) ); 305 } 306 307 return p; 308 } 309 310 314 public void doGet( HttpServletRequest request, HttpServletResponse response ) 315 throws ServletException , IOException 316 { 317 doRequest(request, response); 318 } 319 320 324 public void doPost( HttpServletRequest request, HttpServletResponse response ) 325 throws ServletException , IOException 326 { 327 doRequest(request, response); 328 } 329 330 336 protected void doRequest(HttpServletRequest request, HttpServletResponse response ) 337 throws ServletException , IOException 338 { 339 Context context = null; 340 try 341 { 342 345 346 context = createContext( request, response ); 347 348 351 352 setContentType( request, response ); 353 354 357 358 Template template = handleRequest( request, response, context ); 359 362 363 if ( template == null ) 364 { 365 return; 366 } 367 368 371 372 mergeTemplate( template, context, response ); 373 } 374 catch (Exception e) 375 { 376 380 381 error( request, response, e); 382 } 383 finally 384 { 385 388 389 requestCleanup( request, response, context ); 390 } 391 } 392 393 403 protected void requestCleanup( HttpServletRequest request, HttpServletResponse response, Context context ) 404 { 405 return; 406 } 407 408 416 protected void mergeTemplate( Template template, Context context, HttpServletResponse response ) 417 throws ResourceNotFoundException, ParseErrorException, 418 MethodInvocationException, IOException , UnsupportedEncodingException , Exception 419 { 420 ServletOutputStream output = response.getOutputStream(); 421 VelocityWriter vw = null; 422 String encoding = response.getCharacterEncoding(); 424 425 try 426 { 427 vw = (VelocityWriter) writerPool.get(); 428 429 if (vw == null) 430 { 431 vw = new VelocityWriter(new OutputStreamWriter (output, 432 encoding), 433 4 * 1024, true); 434 } 435 else 436 { 437 vw.recycle(new OutputStreamWriter (output, encoding)); 438 } 439 440 template.merge(context, vw); 441 } 442 finally 443 { 444 try 445 { 446 if (vw != null) 447 { 448 453 vw.flush(); 454 455 460 vw.recycle(null); 461 462 writerPool.put(vw); 463 } 464 } 465 catch (Exception e) 466 { 467 } 469 } 470 } 471 472 481 protected void setContentType(HttpServletRequest request, 482 HttpServletResponse response) 483 { 484 String contentType = defaultContentType; 485 int index = contentType.lastIndexOf(';') + 1; 486 if (index <= 0 || (index < contentType.length() && 487 contentType.indexOf("charset", index) == -1)) 488 { 489 String encoding = chooseCharacterEncoding(request); 491 if (!DEFAULT_OUTPUT_ENCODING.equalsIgnoreCase(encoding)) 494 { 495 contentType += "; charset=" + encoding; 496 } 497 } 498 response.setContentType(contentType); 499 } 502 503 514 protected String chooseCharacterEncoding(HttpServletRequest request) 515 { 516 return RuntimeSingleton.getString(RuntimeConstants.OUTPUT_ENCODING, 517 DEFAULT_OUTPUT_ENCODING); 518 } 519 520 533 protected Context createContext(HttpServletRequest request, HttpServletResponse response ) 534 { 535 538 539 VelocityContext context = new VelocityContext(); 540 541 546 547 context.put( REQUEST, request ); 548 context.put( RESPONSE, response ); 549 550 return context; 551 } 552 553 565 public Template getTemplate( String name ) 566 throws ResourceNotFoundException, ParseErrorException, Exception 567 { 568 return RuntimeSingleton.getTemplate(name); 569 } 570 571 588 public Template getTemplate( String name, String encoding ) 589 throws ResourceNotFoundException, ParseErrorException, Exception 590 { 591 return RuntimeSingleton.getTemplate( name, encoding ); 592 } 593 594 611 protected Template handleRequest( HttpServletRequest request, HttpServletResponse response, Context ctx ) 612 throws Exception 613 { 614 617 618 Template t = handleRequest( ctx ); 619 620 625 626 if (t == null) 627 { 628 throw new Exception ("handleRequest(Context) returned null - no template selected!" ); 629 } 630 631 return t; 632 } 633 634 649 protected Template handleRequest( Context ctx ) 650 throws Exception 651 { 652 throw new Exception ("You must override VelocityServlet.handleRequest( Context) " 653 + " or VelocityServlet.handleRequest( HttpServletRequest, " 654 + " HttpServletResponse, Context)" ); 655 } 656 657 666 protected void error( HttpServletRequest request, HttpServletResponse response, Exception cause ) 667 throws ServletException , IOException 668 { 669 StringBuffer html = new StringBuffer (); 670 html.append("<html>"); 671 html.append("<title>Error</title>"); 672 html.append("<body bgcolor=\"#ffffff\">"); 673 html.append("<h2>VelocityServlet: Error processing the template</h2>"); 674 html.append("<pre>"); 675 String why = cause.getMessage(); 676 if (why != null && why.trim().length() > 0) 677 { 678 html.append(why); 679 html.append("<br>"); 680 } 681 682 StringWriter sw = new StringWriter (); 683 cause.printStackTrace( new PrintWriter ( sw ) ); 684 685 html.append( sw.toString() ); 686 html.append("</pre>"); 687 html.append("</body>"); 688 html.append("</html>"); 689 response.getOutputStream().print( html.toString() ); 690 } 691 } 692 | Popular Tags |