KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > FlavorServlet


1 package org.roller.presentation.velocity;
2 import org.apache.commons.lang.StringUtils;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.velocity.Template;
6 import org.apache.velocity.context.Context;
7 import org.apache.velocity.servlet.VelocityServlet;
8 import org.roller.RollerException;
9 import org.roller.presentation.RollerRequest;
10 import java.io.IOException JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import javax.servlet.jsp.JspFactory JavaDoc;
15 import javax.servlet.jsp.PageContext JavaDoc;
16 /////////////////////////////////////////////////////////////////////////////
17
/**
18   * <p>Responsible for rendering RSS feeds and other "flavors" of output for a
19   * weblog.</p>
20   *
21   * <p>If Servlet is mapped to <code>/rss</code> and user has defined
22   * an RSS override page (i.e. a page named "_rss"), then that Velocity
23   * template will be used for rendering.</p>
24   *
25   * <p>If there is a request parameter named "flavor", then the Velocity
26   * template specified by that parameter will be used for rendering. For
27   * example if the flavor is "rss092" then the template at classpath
28   * "/flavors/rss092.vm" will be used for rendering.</p>
29   *
30   * <p>Otherwise, the template /flavors/rss.vm" will be used for rendering.</p>
31   *
32   * <p>Assumes that If-Modified-Since has already been handled.</p>
33   *
34   * @author David M Johnson
35   *
36   * @web.servlet name="RssServlet"
37   * @web.servlet-mapping url-pattern="/rss/*"
38   * @web.servlet-mapping url-pattern="/flavor/*"
39   */

40 public class FlavorServlet extends VelocityServlet
41 {
42     static final long serialVersionUID = -2720532269434186051L;
43     
44     private static Log mLogger = LogFactory.getFactory()
45                                            .getInstance(RollerRequest.class);
46     public Template handleRequest(HttpServletRequest JavaDoc request,
47                                   HttpServletResponse JavaDoc response, Context ctx)
48     {
49         RollerRequest rreq = null;
50         try
51         {
52             rreq = RollerRequest.getRollerRequest(request,getServletContext());
53             
54             // This is an ugly hack to fix the following bug:
55
// ROL-547: "Site wide RSS feed is your own if you are logged in"
56
String JavaDoc[] pathInfo = StringUtils.split(rreq.getPathInfo(),"/");
57             if (pathInfo.length < 1)
58             {
59                 // If website not specified in URL, set it to null
60
rreq.setWebsite(null);
61             }
62         }
63         catch (RollerException e)
64         {
65             // An error initializing the request is considered to be a 404
66
if (mLogger.isDebugEnabled())
67             {
68                 mLogger.debug("RollerRequest threw Exception", e);
69             }
70             try
71             {
72                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
73             }
74             catch (IOException JavaDoc e1)
75             {
76                 if (mLogger.isDebugEnabled())
77                 {
78                     mLogger.debug("IOException sending error", e);
79                 }
80             }
81             return null;
82         }
83         try
84         {
85             // Needed to init request attributes, etc.
86
PageContext JavaDoc pageContext =
87                 JspFactory.getDefaultFactory().getPageContext(
88                 this, request, response, "", true, 8192, true);
89             rreq.setPageContext(pageContext);
90             ContextLoader.setupContext(ctx, rreq, response);
91
92             final String JavaDoc useTemplate;
93             PageModel pageModel = (PageModel)ctx.get("pageModel");
94             if ( request.getServletPath().endsWith("rss")
95                  && pageModel.getPageByName("_rss") != null )
96             {
97                 // If the request specified the "/rss" mapping and the
98
// user has defined an RSS override page, we will use that.
99
useTemplate = pageModel.getPageByName("_rss").getId();
100             }
101             else if (request.getParameter("flavor") != null)
102             {
103                 // If request specifies a "flavor" then use that.
104
String JavaDoc flavor = request.getParameter("flavor");
105                 useTemplate = "/flavors/" + flavor + ".vm";
106             }
107             else
108             {
109                 // Fall through to default RSS page template.
110
useTemplate = "/flavors/rss.vm";
111             }
112             return getTemplate(useTemplate);
113         }
114         catch (Exception JavaDoc e)
115         {
116             mLogger.error("ERROR in RssServlet", e);
117         }
118         return null;
119     }
120
121     //------------------------------------------------------------------------
122
/**
123      * Handle error in Velocity processing.
124      */

125     protected void error( HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res,
126         Exception JavaDoc e) throws ServletException JavaDoc, IOException JavaDoc
127     {
128         mLogger.warn("ERROR in FlavorServlet",e);
129     }
130 }
131
132
Popular Tags