KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.jsp.JspFactory JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.roller.RollerException;
34 import org.apache.roller.config.RollerConfig;
35 import org.apache.roller.config.RollerRuntimeConfig;
36 import org.apache.roller.pojos.Template;
37 import org.apache.roller.pojos.Theme;
38 import org.apache.roller.pojos.WebsiteData;
39 import org.apache.roller.ui.core.RollerContext;
40 import org.apache.roller.util.cache.CachedContent;
41 import org.apache.roller.ui.rendering.Renderer;
42 import org.apache.roller.ui.rendering.RendererManager;
43 import org.apache.roller.ui.rendering.model.ModelLoader;
44 import org.apache.roller.ui.rendering.util.WeblogPreviewRequest;
45
46
47 /**
48  * Responsible for rendering weblog page previews.
49  *
50  * This servlet is used as part of the authoring interface to provide previews
51  * of what a weblog will look like with a given theme. It is not available
52  * outside of the authoring interface.
53  *
54  * @web.servlet name="PreviewServlet" load-on-startup="9"
55  * @web.servlet-mapping url-pattern="/roller-ui/authoring/preview/*"
56  */

57 public class PreviewServlet extends HttpServlet JavaDoc {
58     
59     private static Log log = LogFactory.getLog(PreviewServlet.class);
60     
61     
62     /**
63      * Init method for this servlet
64      */

65     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
66         
67         super.init(servletConfig);
68         
69         log.info("Initializing PreviewServlet");
70     }
71     
72     
73     /**
74      * Handle GET requests for weblog pages.
75      */

76     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77             throws ServletException JavaDoc, IOException JavaDoc {
78         
79         log.debug("Entering");
80         
81         WebsiteData weblog = null;
82         
83         WeblogPreviewRequest previewRequest = null;
84         try {
85             previewRequest = new WeblogPreviewRequest(request);
86             
87             // lookup weblog specified by preview request
88
weblog = previewRequest.getWeblog();
89             if(weblog == null) {
90                 throw new RollerException("unable to lookup weblog: "+
91                         previewRequest.getWeblogHandle());
92             }
93         } catch (Exception JavaDoc e) {
94             // some kind of error parsing the request or getting weblog
95
log.error("error creating preview request", e);
96             response.sendError(HttpServletResponse.SC_NOT_FOUND);
97             return;
98         }
99         
100         // try getting the preview theme
101
log.debug("preview theme = "+previewRequest.getThemeName());
102         Theme previewTheme = previewRequest.getTheme();
103         
104         // construct a temporary Website object for this request
105
// and set the EditorTheme to our previewTheme
106
WebsiteData tmpWebsite = new WebsiteData();
107         tmpWebsite.setData(weblog);
108         if(previewTheme != null && previewTheme.isEnabled()) {
109             tmpWebsite.setEditorTheme(previewTheme.getName());
110         } else if(Theme.CUSTOM.equals(previewRequest.getThemeName())) {
111             tmpWebsite.setEditorTheme(Theme.CUSTOM);
112         }
113         
114         // we've got to set the weblog in our previewRequest because that's
115
// the object that gets referenced during rendering operations
116
previewRequest.setWeblog(tmpWebsite);
117         
118         
119         Template page = null;
120         try {
121             // we just want to show the default view
122
page = tmpWebsite.getDefaultPage();
123             
124             if(page == null) {
125                 throw new RollerException("No default page for weblog: "+tmpWebsite.getHandle());
126             }
127         } catch(RollerException re) {
128             // couldn't get page
129
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
130             log.error("Error getting default page for preview", re);
131             return;
132         }
133         
134         log.debug("preview page found, dealing with it");
135         
136         // set the content type
137
String JavaDoc pageLink = previewRequest.getWeblogPageName();
138         String JavaDoc mimeType = RollerContext.getServletContext().getMimeType(pageLink);
139         String JavaDoc contentType = "text/html; charset=utf-8";
140         if(mimeType != null) {
141             // we found a match ... set the content type
142
contentType = mimeType+"; charset=utf-8";
143         } else if ("_css".equals(previewRequest.getWeblogPageName())) {
144             // TODO: store content-type for each page so this hack is unnecessary
145
contentType = "text/css; charset=utf-8";
146         }
147         
148         // looks like we need to render content
149
Map JavaDoc model = new HashMap JavaDoc();
150         try {
151             PageContext JavaDoc pageContext = JspFactory.getDefaultFactory().getPageContext(
152                     this, request, response,"", true, 8192, true);
153             
154             // special hack for menu tag
155
request.setAttribute("pageRequest", previewRequest);
156             
157             // populate the rendering model
158
Map JavaDoc initData = new HashMap JavaDoc();
159             initData.put("request", request);
160             initData.put("pageRequest", previewRequest);
161             initData.put("weblogRequest", previewRequest);
162             initData.put("pageContext", pageContext);
163             
164             // Load models for pages
165
String JavaDoc pageModels = RollerConfig.getProperty("rendering.pageModels");
166             ModelLoader.loadModels(pageModels, model, initData, true);
167             
168             // Load special models for site-wide blog
169
if(RollerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
170                 String JavaDoc siteModels = RollerConfig.getProperty("rendering.siteModels");
171                 ModelLoader.loadModels(siteModels, model, initData, true);
172             }
173
174             // Load weblog custom models
175
ModelLoader.loadCustomModels(weblog, model, initData);
176             
177             // ick, gotta load pre-3.0 model stuff as well :(
178
ModelLoader.loadOldModels(model, request, response, pageContext, previewRequest);
179             
180         } catch (RollerException ex) {
181             log.error("ERROR loading model for page", ex);
182             
183             if(!response.isCommitted()) response.reset();
184             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
185             return;
186         }
187         
188         
189         // lookup Renderer we are going to use
190
Renderer renderer = null;
191         try {
192             log.debug("Looking up renderer");
193             renderer = RendererManager.getRenderer(page);
194         } catch(Exception JavaDoc e) {
195             // nobody wants to render my content :(
196
log.error("Couldn't find renderer for page "+page.getId(), e);
197             
198             if(!response.isCommitted()) response.reset();
199             response.sendError(HttpServletResponse.SC_NOT_FOUND);
200             return;
201         }
202         
203         // render content. use default size of about 24K for a standard page
204
CachedContent rendererOutput = new CachedContent(24567);
205         try {
206             log.debug("Doing rendering");
207             renderer.render(model, rendererOutput.getCachedWriter());
208             
209             // flush rendered output and close
210
rendererOutput.flush();
211             rendererOutput.close();
212         } catch(Exception JavaDoc e) {
213             // bummer, error during rendering
214
log.error("Error during rendering for page "+page.getId(), e);
215             
216             if(!response.isCommitted()) response.reset();
217             response.sendError(HttpServletResponse.SC_NOT_FOUND);
218             return;
219         }
220         
221         
222         // post rendering process
223

224         // flush rendered content to response
225
log.debug("Flushing response output");
226         response.setContentType(contentType);
227         response.setContentLength(rendererOutput.getContent().length);
228         response.getOutputStream().write(rendererOutput.getContent());
229         
230         log.debug("Exiting");
231     }
232     
233 }
234
Popular Tags