KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > www > BlogBrowserServlet


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: BlogBrowserServlet.java,v 1.6 2007/02/20 02:01:38 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.blog.www;
23
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.opensubsystems.blog.logic.BlogController;
35 import org.opensubsystems.core.error.OSSException;
36 import org.opensubsystems.core.logic.ControllerManager;
37 import org.opensubsystems.core.util.Log;
38 import org.opensubsystems.core.www.WebUIServlet;
39 import org.opensubsystems.core.www.WebUtils;
40
41 /**
42  * Servlet responsible for browsing blogs and their entries as if they were
43  * displayed using a static website consisting of pages with .html extension.
44  * his servlet contains read-only functionality so it can be safely used, when
45  * user shouldn't be allowed to modify the data.
46  *
47  * @version $Id: BlogBrowserServlet.java,v 1.6 2007/02/20 02:01:38 bastafidli Exp $
48  * @author Miro Halas
49  * @code.reviewer Miro Halas
50  * @code.reviewed Initial revision
51  */

52 public class BlogBrowserServlet extends WebUIServlet
53 {
54    // Configuration settings ///////////////////////////////////////////////////
55

56    /**
57     * Name of the property for page which is the main entry point to the blog
58     * functionality.
59     */

60    public static final String JavaDoc BLOGBROWSER_BLOG_INDEX_PAGE = "blogbrowser.blog.index.page";
61
62    /**
63     * Name of the property for page to view single blog.
64     */

65    public static final String JavaDoc BLOGBROWSER_BLOG_VIEWER_PAGE = "blogbrowser.blog.viewer.page";
66
67    /**
68     * Name of the property for page to view single blog entry.
69     */

70    public static final String JavaDoc BLOGBROWSER_BLOGENTRY_VIEWER_PAGE
71                                  = "blogbrowser.entry.viewer.page";
72
73    // Constants ////////////////////////////////////////////////////////////////
74

75    /**
76     * Constant for number of forms recognized by this servlet
77     */

78    public static final int FORM_COUNT_BLOGBROWSER = FORM_COUNT_WEBUI;
79
80    // Cached values ////////////////////////////////////////////////////////////
81

82    /**
83     * Logger for this class
84     */

85    private static Logger JavaDoc s_logger = Log.getInstance(BlogBrowserServlet.class);
86
87    // Attributes ///////////////////////////////////////////////////////////////
88

89    /**
90     * Generated serial version id for this class.
91     */

92    private static final long serialVersionUID = 3131866602964809055L;
93
94    // Public methods ///////////////////////////////////////////////////////////
95

96    /**
97     * {@inheritDoc}
98     */

99    public void init(
100       ServletConfig JavaDoc scConfig
101    ) throws ServletException JavaDoc
102    {
103       super.init(scConfig);
104
105       // Load UI pages for this blog
106
// The main entry point to the blog functionality
107
cacheUIPath(scConfig, BLOGBROWSER_BLOG_INDEX_PAGE,
108                   "Path to main index page is not set in property "
109                   + BLOGBROWSER_BLOG_INDEX_PAGE);
110       
111       // Page to view single blog
112
cacheUIPath(scConfig, BLOGBROWSER_BLOG_VIEWER_PAGE,
113                   "Path to blog viewer page is not set in property "
114                   + BLOGBROWSER_BLOG_INDEX_PAGE);
115
116       // Page to view single blog entry
117
cacheUIPath(scConfig, BLOGBROWSER_BLOGENTRY_VIEWER_PAGE,
118                   "Path to entry viewer page is not set in property "
119                   + BLOGBROWSER_BLOGENTRY_VIEWER_PAGE);
120    }
121
122    /**
123     * {@inheritDoc}
124     */

125    protected void doGet(
126       HttpServletRequest JavaDoc hsrqRequest,
127       HttpServletResponse JavaDoc hsrpResponse
128    ) throws ServletException JavaDoc,
129             IOException JavaDoc
130    {
131       if (WebUtils.isIndexPage(hsrqRequest))
132       {
133          if (WebUtils.isMainIndexPage(hsrqRequest))
134          {
135             // Create page displaying all existing blogs
136
createMainIndexPage(hsrqRequest, hsrpResponse);
137          }
138          else
139          {
140             // Create page displaying one blog
141
createIndexPage(hsrqRequest, hsrpResponse);
142          }
143       }
144       else
145       {
146          if (WebUtils.isStaticWebPage(hsrqRequest))
147          {
148             // Create page displaying one entry of a blogs
149
createBlogEntryPage(hsrqRequest, hsrpResponse);
150          }
151          else
152          {
153             hsrpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
154          }
155       }
156    }
157
158    /**
159     * {@inheritDoc}
160     */

161    public String JavaDoc getServletInfo(
162    )
163    {
164       return this.getClass().getName();
165    }
166    
167    // Helper methods ///////////////////////////////////////////////////////////
168

169    /**
170     * Create main index page of all blogs.
171     *
172     * @param hsrqRequest - the servlet request
173     * @param hsrpResponse - the servlet response
174     * @throws ServletException - an error while serving request
175     * @throws IOException - an error while writing response
176     */

177    protected void createMainIndexPage(
178       HttpServletRequest JavaDoc hsrqRequest,
179       HttpServletResponse JavaDoc hsrpResponse
180    ) throws IOException JavaDoc,
181             ServletException JavaDoc
182    {
183       s_logger.entering(this.getClass().getName(), "createMainIndexPage");
184
185       try
186       {
187          List JavaDoc lstBlogs;
188
189          lstBlogs = getController().getAll();
190          if (lstBlogs != null)
191          {
192             hsrqRequest.setAttribute("blogs", lstBlogs);
193          }
194          hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest));
195          displayUI(BLOGBROWSER_BLOG_INDEX_PAGE, hsrqRequest, hsrpResponse);
196       }
197       catch (Exception JavaDoc eExc)
198       {
199          s_logger.log(Level.WARNING,
200                       "An error has occured while retrieving list of blogs.",
201                       eExc);
202          messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(),
203                         getNavigator(hsrqRequest).getRootURL(),
204                         eExc.getCause());
205       }
206       finally
207       {
208          s_logger.exiting(this.getClass().getName(), "createMainIndexPage");
209       }
210    }
211
212    /**
213     * Create index page of all entries in the blog.
214     *
215     * @param hsrqRequest - the servlet request
216     * @param hsrpResponse - the servlet response
217     * @throws ServletException - an error while serving request
218     * @throws IOException - an error while writing response
219     */

220    protected void createIndexPage(
221       HttpServletRequest JavaDoc hsrqRequest,
222       HttpServletResponse JavaDoc hsrpResponse
223    ) throws IOException JavaDoc,
224             ServletException JavaDoc
225    {
226       s_logger.entering(this.getClass().getName(), "createIndexPage");
227
228       try
229       {
230          Object JavaDoc objBlogIdentification;
231          Object JavaDoc[] arObjects;
232          BlogNavigator navigator;
233
234          navigator = getNavigator(hsrqRequest);
235          objBlogIdentification = navigator.getBlogIdentification(hsrqRequest);
236          arObjects = getController().getWithEntries((String JavaDoc)objBlogIdentification);
237                   
238          if ((arObjects != null) && (arObjects[0] != null))
239          {
240             hsrqRequest.setAttribute("blog", arObjects[0]);
241             // It is ok to do not have any entries
242
if (arObjects[1] != null)
243             {
244                hsrqRequest.setAttribute("blogentries", arObjects[1]);
245             }
246             hsrqRequest.setAttribute("blognavigator", navigator);
247             displayUI(BLOGBROWSER_BLOG_VIEWER_PAGE, hsrqRequest, hsrpResponse);
248          }
249          else
250          {
251             // We have found either the blog or the entry so tell the user
252
// about it
253
hsrpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
254          }
255       }
256       catch (Exception JavaDoc eExc)
257       {
258          s_logger.log(Level.WARNING, "An error has occured while retrieving blog.",
259                       eExc);
260          messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(),
261                         getNavigator(hsrqRequest).getRootURL(),
262                         eExc.getCause());
263       }
264       finally
265       {
266          s_logger.exiting(this.getClass().getName(), "createIndexPage");
267       }
268    }
269
270    /**
271     * Create blog page for the path requested by user
272     *
273     * @param hsrqRequest - the servlet request
274     * @param hsrpResponse - the servlet response
275     * @throws ServletException - an error while serving request
276     * @throws IOException - an error while writing response
277     */

278    protected void createBlogEntryPage(
279       HttpServletRequest JavaDoc hsrqRequest,
280       HttpServletResponse JavaDoc hsrpResponse
281    ) throws IOException JavaDoc,
282             ServletException JavaDoc
283    {
284       s_logger.entering(this.getClass().getName(), "createBlogEntryPage");
285
286       try
287       {
288          BlogEntryIdentification entryIdentification;
289          Object JavaDoc[] arObjects = null;
290          BlogNavigator navigator;
291          
292          navigator = getNavigator(hsrqRequest);
293          entryIdentification = navigator.getBlogEntryIdentification(hsrqRequest);
294          if (entryIdentification != null)
295          {
296             arObjects = getController().getWithEntry(
297                            (String JavaDoc)entryIdentification.getBlogIdentification(),
298                            entryIdentification.getBlogEntryIdentification());
299          }
300          
301          if ((arObjects != null) && (arObjects[0] != null)
302             && (arObjects[1] != null))
303          {
304             hsrqRequest.setAttribute("blog", arObjects[0]);
305             hsrqRequest.setAttribute("blogentry", arObjects[1]);
306             hsrqRequest.setAttribute("blognavigator", navigator);
307             displayUI(BLOGBROWSER_BLOGENTRY_VIEWER_PAGE, hsrqRequest, hsrpResponse);
308          }
309          else
310          {
311             // We have not found either the blog or the entry so tell the user
312
// about it
313
hsrpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
314          }
315       }
316       catch (Exception JavaDoc eExc)
317       {
318          s_logger.log(Level.WARNING,
319                       "An error has occured while retrieving blog entry.",
320                       eExc);
321          messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(),
322                         getNavigator(hsrqRequest).getRootURL(), eExc.getCause());
323       }
324       finally
325       {
326          s_logger.exiting(this.getClass().getName(), "createBlogEntryPage");
327       }
328    }
329
330    // Helper methods ///////////////////////////////////////////////////////////
331

332    /**
333     * Get instance of navigator object suitable for processing current request.
334     *
335     * @param hsrqRequest - current request
336     * @return BlogNavigator - navigator for current request
337     */

338    protected BlogNavigator getNavigator(
339       HttpServletRequest JavaDoc hsrqRequest
340    )
341    {
342       return new BlogNavigator(hsrqRequest);
343    }
344
345    /**
346     * Get controller to invoke business logic.
347     *
348     * @return BlogController
349     * @throws OSSException - an error has occured
350     */

351    protected BlogController getController(
352    ) throws OSSException
353    {
354       BlogController controller;
355       
356       controller = (BlogController)ControllerManager.getInstance(
357                                                         BlogController.class);
358       
359       return controller;
360    }
361 }
362
Popular Tags