001: /*
002: * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
003: *
004: * Project: OpenChronicle
005: *
006: * $Id: BlogBrowserServlet.java,v 1.4 2007/02/20 03:50:56 bastafidli Exp $
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; version 2 of the License.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.opensubsystems.blog.www;
023:
024: import java.io.IOException;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.servlet.ServletConfig;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.opensubsystems.blog.logic.BlogController;
034: import org.opensubsystems.core.error.OSSException;
035: import org.opensubsystems.core.logic.ControllerManager;
036: import org.opensubsystems.core.util.Log;
037: import org.opensubsystems.core.www.WebUIServlet;
038: import org.opensubsystems.core.www.WebUtils;
039:
040: /**
041: * Servlet responsible for browsing blogs and their entries as if they were
042: * displayed using a static website consisting of pages with .html extension.
043: * his servlet contains read-only functionality so it can be safely used, when
044: * user shouldn't be allowed to modify the data.
045: *
046: * @version $Id: BlogBrowserServlet.java,v 1.4 2007/02/20 03:50:56 bastafidli Exp $
047: * @author Miro Halas
048: * @code.reviewer Miro Halas
049: * @code.reviewed Initial revision
050: */
051: public class BlogBrowserServlet extends WebUIServlet {
052: // Configuration settings ///////////////////////////////////////////////////
053:
054: /**
055: * Name of the property for page which is the main entry point to the blog
056: * functionality.
057: */
058: public static final String BLOGBROWSER_BLOG_INDEX_PAGE = "blogbrowser.blog.index.page";
059:
060: /**
061: * Name of the property for page to view single blog.
062: */
063: public static final String BLOGBROWSER_BLOG_VIEWER_PAGE = "blogbrowser.blog.viewer.page";
064:
065: /**
066: * Name of the property for page to view single blog entry.
067: */
068: public static final String BLOGBROWSER_BLOGENTRY_VIEWER_PAGE = "blogbrowser.entry.viewer.page";
069:
070: // Constants ////////////////////////////////////////////////////////////////
071:
072: /**
073: * Constant for number of forms recognized by this servlet
074: */
075: public static final int FORM_COUNT_BLOGBROWSER = FORM_COUNT_WEBUI;
076:
077: // Cached values ////////////////////////////////////////////////////////////
078:
079: /**
080: * Logger for this class
081: */
082: private static Logger s_logger = Log
083: .getInstance(BlogBrowserServlet.class);
084:
085: // Attributes ///////////////////////////////////////////////////////////////
086:
087: /**
088: * Generated serial version id for this class.
089: */
090: private static final long serialVersionUID = 3131866602964809055L;
091:
092: // Public methods ///////////////////////////////////////////////////////////
093:
094: /**
095: * {@inheritDoc}
096: */
097: public void init(ServletConfig scConfig) throws ServletException {
098: super .init(scConfig);
099:
100: // Load UI pages for this blog
101: // The main entry point to the blog functionality
102: cacheUIPath(scConfig, BLOGBROWSER_BLOG_INDEX_PAGE,
103: "Path to main index page is not set in property "
104: + BLOGBROWSER_BLOG_INDEX_PAGE);
105:
106: // Page to view single blog
107: cacheUIPath(scConfig, BLOGBROWSER_BLOG_VIEWER_PAGE,
108: "Path to blog viewer page is not set in property "
109: + BLOGBROWSER_BLOG_INDEX_PAGE);
110:
111: // Page to view single blog entry
112: cacheUIPath(scConfig, BLOGBROWSER_BLOGENTRY_VIEWER_PAGE,
113: "Path to entry viewer page is not set in property "
114: + BLOGBROWSER_BLOGENTRY_VIEWER_PAGE);
115: }
116:
117: /**
118: * {@inheritDoc}
119: */
120: protected void doGet(HttpServletRequest hsrqRequest,
121: HttpServletResponse hsrpResponse) throws ServletException,
122: IOException {
123: BlogNavigator navigator;
124: int iPageNumber;
125:
126: navigator = getNavigator(hsrqRequest);
127: iPageNumber = navigator.isIndexPage();
128: if (iPageNumber > 0) {
129: if (navigator.isBlogIndexPage()) {
130: // Create page displaying all existing blogs
131: createMainIndexPage(hsrqRequest, hsrpResponse,
132: navigator, iPageNumber);
133: } else {
134: // Create page displaying one blog
135: createIndexPage(hsrqRequest, hsrpResponse, navigator,
136: iPageNumber);
137: }
138: } else {
139: if (WebUtils.isStaticWebPage(hsrqRequest)) {
140: // Create page displaying one entry of a blogs
141: createBlogEntryPage(hsrqRequest, hsrpResponse,
142: navigator);
143: } else {
144: hsrpResponse
145: .sendError(HttpServletResponse.SC_NOT_FOUND);
146: }
147: }
148: }
149:
150: /**
151: * {@inheritDoc}
152: */
153: public String getServletInfo() {
154: return this .getClass().getName();
155: }
156:
157: // Helper methods ///////////////////////////////////////////////////////////
158:
159: /**
160: * Create main index page of all blogs.
161: *
162: * @param hsrqRequest - the servlet request
163: * @param hsrpResponse - the servlet response
164: * @param navigator - navigator object used to parse and generate all
165: * application links
166: * @param iPageNumber - page number of the blog index page to display
167: * @throws ServletException - an error while serving request
168: * @throws IOException - an error while writing response
169: */
170: protected void createMainIndexPage(HttpServletRequest hsrqRequest,
171: HttpServletResponse hsrpResponse, BlogNavigator navigator,
172: int iPageNumber) throws IOException, ServletException {
173: s_logger.entering(this .getClass().getName(),
174: "createMainIndexPage");
175:
176: try {
177: Object[] arObjects;
178:
179: arObjects = getController().getPage(iPageNumber);
180: // It is ok to do not have any blogs
181: if (arObjects[0] != null) {
182: hsrqRequest.setAttribute("blogs", arObjects[0]);
183: }
184: if (arObjects[1] != null) {
185: hsrqRequest.setAttribute("listoptions", arObjects[1]);
186: }
187: hsrqRequest.setAttribute("blognavigator", navigator);
188: displayUI(BLOGBROWSER_BLOG_INDEX_PAGE, hsrqRequest,
189: hsrpResponse);
190: } catch (Exception eExc) {
191: s_logger
192: .log(
193: Level.WARNING,
194: "An error has occured while retrieving list of blogs.",
195: eExc);
196: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
197: .getMessage(), getNavigator(hsrqRequest)
198: .getRootURL(), eExc.getCause());
199: } finally {
200: s_logger.exiting(this .getClass().getName(),
201: "createMainIndexPage");
202: }
203: }
204:
205: /**
206: * Create index page of all entries in the blog.
207: *
208: * @param hsrqRequest - the servlet request
209: * @param hsrpResponse - the servlet response
210: * @param navigator - navigator object used to parse and generate all
211: * application links
212: * @param iPageNumber - page number of the blog index page to display
213: * @throws ServletException - an error while serving request
214: * @throws IOException - an error while writing response
215: */
216: protected void createIndexPage(HttpServletRequest hsrqRequest,
217: HttpServletResponse hsrpResponse, BlogNavigator navigator,
218: int iPageNumber) throws IOException, ServletException {
219: s_logger.entering(this .getClass().getName(), "createIndexPage");
220:
221: try {
222: Object objBlogIdentification;
223: Object[] arObjects;
224:
225: objBlogIdentification = navigator
226: .getBlogIdentification(hsrqRequest);
227: arObjects = getController().getWithEntries(
228: objBlogIdentification.toString(), iPageNumber);
229:
230: if ((arObjects != null) && (arObjects[0] != null)) {
231: hsrqRequest.setAttribute("blog", arObjects[0]);
232: // It is ok to do not have any entries
233: if (arObjects[1] != null) {
234: hsrqRequest.setAttribute("blogentries",
235: arObjects[1]);
236: }
237: if (arObjects[2] != null) {
238: hsrqRequest.setAttribute("listoptions",
239: arObjects[2]);
240: }
241: hsrqRequest.setAttribute("blognavigator", navigator);
242: displayUI(BLOGBROWSER_BLOG_VIEWER_PAGE, hsrqRequest,
243: hsrpResponse);
244: } else {
245: // We have found either the blog or the entry so tell the user
246: // about it
247: hsrpResponse
248: .sendError(HttpServletResponse.SC_NOT_FOUND);
249: }
250: } catch (Exception eExc) {
251: s_logger
252: .log(
253: Level.WARNING,
254: "An error has occured while retrieving blog.",
255: eExc);
256: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
257: .getMessage(), getNavigator(hsrqRequest)
258: .getRootURL(), eExc.getCause());
259: } finally {
260: s_logger.exiting(this .getClass().getName(),
261: "createIndexPage");
262: }
263: }
264:
265: /**
266: * Create blog page for the path requested by user
267: *
268: * @param hsrqRequest - the servlet request
269: * @param hsrpResponse - the servlet response
270: * @param navigator - navigator object used to parse and generate all
271: * application links
272: * @throws ServletException - an error while serving request
273: * @throws IOException - an error while writing response
274: */
275: protected void createBlogEntryPage(HttpServletRequest hsrqRequest,
276: HttpServletResponse hsrpResponse, BlogNavigator navigator)
277: throws IOException, ServletException {
278: s_logger.entering(this .getClass().getName(),
279: "createBlogEntryPage");
280:
281: try {
282: BlogEntryIdentification entryIdentification;
283: Object[] arObjects = null;
284:
285: entryIdentification = navigator
286: .getBlogEntryIdentification(hsrqRequest);
287: if (entryIdentification != null) {
288: Object objBlogIdentification;
289:
290: objBlogIdentification = entryIdentification
291: .getBlogIdentification();
292: if (objBlogIdentification instanceof Integer) {
293: arObjects = getController().getWithEntry(
294: ((Integer) objBlogIdentification)
295: .intValue(),
296: entryIdentification
297: .getBlogEntryIdentification());
298: } else {
299: arObjects = getController().getWithEntry(
300: objBlogIdentification.toString(),
301: entryIdentification
302: .getBlogEntryIdentification());
303: }
304: }
305:
306: if ((arObjects != null) && (arObjects[0] != null)
307: && (arObjects[1] != null)) {
308: hsrqRequest.setAttribute("blog", arObjects[0]);
309: hsrqRequest.setAttribute("blogentry", arObjects[1]);
310: hsrqRequest.setAttribute("blognavigator", navigator);
311: displayUI(BLOGBROWSER_BLOGENTRY_VIEWER_PAGE,
312: hsrqRequest, hsrpResponse);
313: } else {
314: // We have not found either the blog or the entry so tell the user
315: // about it
316: hsrpResponse
317: .sendError(HttpServletResponse.SC_NOT_FOUND);
318: }
319: } catch (Exception eExc) {
320: s_logger
321: .log(
322: Level.WARNING,
323: "An error has occured while retrieving blog entry.",
324: eExc);
325: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
326: .getMessage(), getNavigator(hsrqRequest)
327: .getRootURL(), eExc.getCause());
328: } finally {
329: s_logger.exiting(this .getClass().getName(),
330: "createBlogEntryPage");
331: }
332: }
333:
334: // Helper methods ///////////////////////////////////////////////////////////
335:
336: /**
337: * Get instance of navigator object suitable for processing current request.
338: *
339: * @param hsrqRequest - current request
340: * @return BlogNavigator - navigator for current request
341: */
342: protected BlogNavigator getNavigator(HttpServletRequest hsrqRequest) {
343: BlogNavigator navigator;
344: String strUrl = WebUtils.getRequestURLWithoutQuery(hsrqRequest);
345:
346: if (strUrl.endsWith(DynamicBlogNavigator.DYNAMIC_POST_WEB_PAGE)) {
347: navigator = new DynamicBlogNavigator(hsrqRequest);
348: } else {
349: navigator = new BlogNavigator(hsrqRequest);
350: }
351:
352: return navigator;
353: }
354:
355: /**
356: * Get controller to invoke business logic.
357: *
358: * @return BlogController
359: * @throws OSSException - an error has occured
360: */
361: protected BlogController getController() throws OSSException {
362: BlogController controller;
363:
364: controller = (BlogController) ControllerManager
365: .getInstance(BlogController.class);
366:
367: return controller;
368: }
369: }
|