KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > servlets > RSDServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.servlets;
20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.RollerException;
31 import org.apache.roller.config.RollerRuntimeConfig;
32 import org.apache.roller.pojos.StaticTemplate;
33 import org.apache.roller.pojos.Template;
34 import org.apache.roller.pojos.WebsiteData;
35 import org.apache.roller.ui.rendering.util.WeblogRequest;
36 import org.apache.roller.ui.rendering.util.ModDateHeaderUtil;
37 import org.apache.roller.ui.rendering.Renderer;
38 import org.apache.roller.ui.rendering.RendererManager;
39 import org.apache.roller.util.cache.CachedContent;
40
41
42 /**
43  * Generates simple rsd feed for a given weblog.
44  *
45  * This servlet supports 304 If-Modified-Since checking, but does not do any
46  * level of content caching.
47  *
48  * @web.servlet name="RSDServlet" load-on-startup="7"
49  * @web.servlet-mapping url-pattern="/roller-ui/rendering/rsd/*"
50  */

51 public class RSDServlet extends HttpServlet JavaDoc {
52     
53     private static Log log = LogFactory.getLog(RSDServlet.class);
54     
55     
56     /**
57      * Init method for this servlet
58      */

59     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
60         
61         super.init(servletConfig);
62         
63         log.info("Initializing RSDServlet");
64     }
65     
66     
67     /**
68      * Handle GET requests for weblog pages.
69      */

70     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71             throws ServletException JavaDoc, IOException JavaDoc {
72         
73         log.debug("Entering");
74         
75         WebsiteData weblog = null;
76         
77         WeblogRequest weblogRequest = null;
78         try {
79             weblogRequest = new WeblogRequest(request);
80             
81             // now make sure the specified weblog really exists
82
weblog = weblogRequest.getWeblog();
83             if(weblog == null) {
84                 throw new RollerException("Unable to lookup weblog: "+
85                         weblogRequest.getWeblogHandle());
86             }
87             
88         } catch(Exception JavaDoc e) {
89             // invalid rsd request format or weblog doesn't exist
90
log.debug("error creating weblog request", e);
91             response.sendError(HttpServletResponse.SC_NOT_FOUND);
92             return;
93         }
94         
95         
96
97         // Respond with 304 Not Modified if it is not modified.
98
long lastModified = System.currentTimeMillis();
99         if (weblog.getLastModified() != null) {
100             lastModified = weblog.getLastModified().getTime();
101         }
102         if (ModDateHeaderUtil.respondIfNotModified(request,response,lastModified)) {
103             return;
104         }
105
106         // set last-modified date
107
ModDateHeaderUtil.setLastModifiedHeader(response,lastModified);
108
109         // set the content type
110
response.setContentType("application/rsd+xml; charset=utf-8");
111         
112         // populate the model
113
HashMap JavaDoc model = new HashMap JavaDoc();
114         model.put("website", weblog);
115         model.put("absBaseURL", RollerRuntimeConfig.getAbsoluteContextURL());
116
117         
118         // lookup Renderer we are going to use
119
Renderer renderer = null;
120         try {
121             log.debug("Looking up renderer");
122             Template template = new StaticTemplate("templates/weblog/rsd.vm", null, "velocity");
123             renderer = RendererManager.getRenderer(template);
124         } catch(Exception JavaDoc e) {
125             // nobody wants to render my content :(
126
log.error("Couldn't find renderer for rsd template", e);
127             
128             if(!response.isCommitted()) response.reset();
129             response.sendError(HttpServletResponse.SC_NOT_FOUND);
130             return;
131         }
132         
133         // render content
134
CachedContent rendererOutput = new CachedContent(4096);
135         try {
136             log.debug("Doing rendering");
137             renderer.render(model, rendererOutput.getCachedWriter());
138             
139             // flush rendered output and close
140
rendererOutput.flush();
141             rendererOutput.close();
142         } catch(Exception JavaDoc e) {
143             // bummer, error during rendering
144
log.error("Error during rendering for rsd template", e);
145             
146             if(!response.isCommitted()) response.reset();
147             response.sendError(HttpServletResponse.SC_NOT_FOUND);
148             return;
149         }
150         
151         
152         // post rendering process
153

154         // flush rendered content to response
155
log.debug("Flushing response output");
156         response.setContentLength(rendererOutput.getContent().length);
157         response.getOutputStream().write(rendererOutput.getContent());
158         
159         log.debug("Exiting");
160     }
161     
162 }
163
Popular Tags