KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > velocity > VelocityDecoratorServlet


1 /*
2  * Title: VelocityDecoratorServlet
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.velocity;
11
12 import com.opensymphony.module.sitemesh.*;
13 import com.opensymphony.module.sitemesh.util.OutputConverter;
14
15 import org.apache.velocity.Template;
16 import org.apache.velocity.context.Context;
17 import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import java.io.StringWriter JavaDoc;
22
23 /**
24  * Servlet that allows Velocity templates to be used as decorators.
25  *
26  * @author <a HREF="mailto:joe@truemesh.com">Joe Walnes</a>
27  * @version $Revision: 1.8 $
28  */

29 public class VelocityDecoratorServlet extends VelocityViewServlet {
30     public Template handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Context context) throws Exception JavaDoc {
31         HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE);
32         String JavaDoc template;
33
34         context.put("base", request.getContextPath());
35
36         // For backwards compatability with apps that used the old VelocityDecoratorServlet
37
// that extended VelocityServlet instead of VelocityViewServlet
38
context.put("req", request);
39         context.put("res", response);
40
41         if (htmlPage == null) {
42             context.put("title", "Title?");
43             context.put("body", "<p>Body?</p>");
44             context.put("head", "<!-- head -->");
45             template = request.getServletPath();
46         }
47         else {
48             context.put("title", OutputConverter.convert(htmlPage.getTitle()));
49             {
50                 StringWriter JavaDoc buffer = new StringWriter JavaDoc();
51                 htmlPage.writeBody(OutputConverter.getWriter(buffer));
52                 context.put("body", buffer.toString());
53             }
54             {
55                 StringWriter JavaDoc buffer = new StringWriter JavaDoc();
56                 htmlPage.writeHead(OutputConverter.getWriter(buffer));
57                 context.put("head", buffer.toString());
58             }
59             context.put("page", htmlPage);
60             Factory factory = Factory.getInstance(new Config(getServletConfig()));
61             Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage);
62             template = decorator.getPage();
63         }
64
65         return getTemplate(template);
66     }
67 }
Popular Tags