KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > servlet > BlogServlet


1 /*
2  * $Id: BlogServlet.java,v 1.17 2005/01/17 21:36:33 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.servlet;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import com.j2biz.blogunity.IConstants;
40 import com.j2biz.blogunity.dao.BlogDAO;
41 import com.j2biz.blogunity.dao.CalendarEntryDAO;
42 import com.j2biz.blogunity.dao.CommentDAO;
43 import com.j2biz.blogunity.dao.EntryDAO;
44 import com.j2biz.blogunity.dao.RefererDAO;
45 import com.j2biz.blogunity.dao.VisitedPageDAO;
46 import com.j2biz.blogunity.exception.BlogunityException;
47 import com.j2biz.blogunity.i18n.I18N;
48 import com.j2biz.blogunity.i18n.I18NStatusFactory;
49 import com.j2biz.blogunity.pojo.Blog;
50 import com.j2biz.blogunity.web.IActionResult;
51 import com.j2biz.blogunity.web.actions.AbstractAction;
52 import com.j2biz.blogunity.web.actions.blog.AbstractFeedAction;
53 import com.j2biz.blogunity.web.actions.blog.AbstractResourceAction;
54 import com.j2biz.blogunity.web.actions.blog.AtomFeedAction;
55 import com.j2biz.blogunity.web.actions.blog.CategoryAction;
56 import com.j2biz.blogunity.web.actions.blog.DayAction;
57 import com.j2biz.blogunity.web.actions.blog.EntryAction;
58 import com.j2biz.blogunity.web.actions.blog.ErrorAction;
59 import com.j2biz.blogunity.web.actions.blog.FeedsAction;
60 import com.j2biz.blogunity.web.actions.blog.FileResourceAction;
61 import com.j2biz.blogunity.web.actions.blog.FrontpageAction;
62 import com.j2biz.blogunity.web.actions.blog.ImageResourceAction;
63 import com.j2biz.blogunity.web.actions.blog.MonthAction;
64 import com.j2biz.blogunity.web.actions.blog.RssFeedAction;
65 import com.j2biz.blogunity.web.actions.blog.SearchAction;
66 import com.j2biz.blogunity.web.actions.blog.ThemeResourceAction;
67 import com.j2biz.blogunity.web.actions.blog.TrackBackAction;
68 import com.j2biz.blogunity.web.actions.blog.YearAction;
69
70 public class BlogServlet extends HttpServlet JavaDoc {
71
72     /**
73      * Comment for <code>serialVersionUID</code>
74      */

75     private static final long serialVersionUID = 4050481205600467252L;
76
77     private static final Log log = LogFactory.getLog(BlogServlet.class);
78
79     private static final String JavaDoc BLOGS_MAPPING = "/blogs";
80
81     private String JavaDoc blogname = null;
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
87      * javax.servlet.http.HttpServletResponse)
88      */

89     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
90             throws ServletException JavaDoc, IOException JavaDoc {
91         doPost(request, response);
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
98      * javax.servlet.http.HttpServletResponse)
99      */

100     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
101             throws ServletException JavaDoc, IOException JavaDoc {
102
103         try {
104
105             // first find name of the requested blog
106
blogname = getBlogname(request);
107             if (log.isDebugEnabled()) {
108                 log.debug("Blog '" + blogname + "' is requested...");
109             }
110
111             // find requested blog-object in database and set it to request if
112
// exists
113
BlogDAO dao = new BlogDAO();
114             Blog blog = dao.getBlogByUrlName(blogname);
115             if (blog == null) {
116                 BlogunityException e = new BlogunityException(I18NStatusFactory.create(
117                         I18N.ERRORS.BLOG_NOT_FOUND, blogname));
118                 throw new ServletException JavaDoc(e);
119             }
120
121             request.setAttribute("blog", blog);
122
123             // now create action-instance
124
String JavaDoc url = getRequestedUrl(request, blogname);
125
126             AbstractAction action = null;
127
128             if (url.matches(IConstants.PATTERNS.FRONTPAGE_PATTERN)) {
129                 if (log.isDebugEnabled()) {
130                     log.debug("Frontpage requested!");
131                 }
132
133                 // create frontpage-action
134
action = new FrontpageAction(blog);
135
136             } else if (url.matches(IConstants.PATTERNS.YEAR_PATTERN)) {
137                 if (log.isDebugEnabled()) {
138                     log.debug("Year requested!");
139                 }
140
141                 // get year from url
142
String JavaDoc year = url.substring(1);
143                 if (year.endsWith("/")) year = year.substring(0, year.length() - 1);
144
145                 action = new YearAction(blog, year);
146
147             } else if (url.matches(IConstants.PATTERNS.MONTH_PATTERN)) {
148                 if (log.isDebugEnabled()) {
149                     log.debug("Month requested!");
150                 }
151
152                 // get year and month from url
153
url = url.substring(1);
154                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
155
156                 String JavaDoc[] _array = url.split("/");
157
158                 action = new MonthAction(blog, _array[0], _array[1]);
159
160             } else if (url.matches(IConstants.PATTERNS.DAY_PATTERN)) {
161                 if (log.isDebugEnabled()) {
162                     log.debug("Day requested!");
163                 }
164
165                 // get year, month and day from url
166
url = url.substring(1);
167                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
168
169                 String JavaDoc[] _array = url.split("/");
170
171                 action = new DayAction(blog, _array[0], _array[1], _array[2]);
172
173             } else if (url.matches(IConstants.PATTERNS.ENTRY_PATTERN)) {
174                 if (log.isDebugEnabled()) {
175                     log.debug("Entry requested!");
176                 }
177
178                 // get year, month, day and entry from url
179
url = url.substring(1);
180                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
181
182                 String JavaDoc[] _array = url.split("/");
183
184                 action = new EntryAction(blog, _array[0], _array[1], _array[2], _array[3]);
185
186             } else if (url.matches(IConstants.PATTERNS.CATEGORY_PATTERN)) {
187                 if (log.isDebugEnabled()) {
188                     log.debug("Category requested!");
189                 }
190
191                 // get categoryId from url
192
url = url.substring(1);
193                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
194                 String JavaDoc[] _array = url.split("/");
195                 action = new CategoryAction(blog, _array[1]);
196
197             } else if (url.matches(IConstants.PATTERNS.FEEDS_PATTERN)) {
198                 if (log.isDebugEnabled()) {
199                     log.debug("Feeds requested!");
200                 }
201
202                 // create action for "all feeds"-page
203
action = new FeedsAction(blog);
204
205             } else if (url.matches(IConstants.PATTERNS.ATOM_03_PATTERN)) {
206                 if (log.isDebugEnabled()) {
207                     log.debug("Atom-Feed requested!");
208                 }
209
210                 // get categoryId from url
211
url = url.substring(1);
212                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
213                 String JavaDoc[] _array = url.split("/");
214
215                 if (_array.length == 3) action = new AtomFeedAction(blog, _array[2]);
216                 else
217                     action = new AtomFeedAction(blog, null);
218
219             } else if (url.matches(IConstants.PATTERNS.RSS_20_PATTERN)) {
220                 if (log.isDebugEnabled()) {
221                     log.debug("Rss-Feed requested!");
222                 }
223
224                 // get categoryId from url
225
url = url.substring(1);
226                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
227                 String JavaDoc[] _array = url.split("/");
228
229                 if (_array.length == 3) action = new RssFeedAction(blog, _array[2]);
230                 else
231                     action = new RssFeedAction(blog, null);
232
233             } else if (url.matches(IConstants.PATTERNS.TRACKBACK_PATTERN)) {
234                 if (log.isDebugEnabled()) {
235                     log.debug("Trackback create requested!");
236                 }
237
238                 // getentryId from url
239
url = url.substring(1);
240                 if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
241                 String JavaDoc[] _array = url.split("/");
242                 action = new TrackBackAction(blog, _array[1]);
243             } else if (url.matches(IConstants.PATTERNS.SEARCH_PATTERN)) {
244                 if (log.isDebugEnabled()) {
245                     log.debug("Search requested!");
246                 }
247
248                 action = new SearchAction(blog);
249
250             } else if (url.matches(IConstants.PATTERNS.THEME_FILE_PATTERN)) {
251                 if (log.isDebugEnabled()) {
252                     log.debug("Themefile requested!");
253                 }
254
255                 // get resource
256
if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
257                 String JavaDoc resource = StringUtils.substringAfter(url, "/theme/");
258                 action = new ThemeResourceAction(blog, resource);
259
260             } else if (url.matches(IConstants.PATTERNS.IMAGE_FILE_PATTERN)) {
261                 if (log.isDebugEnabled()) {
262                     log.debug("Image requested!");
263                 }
264
265                 // get resource
266
if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
267                 String JavaDoc resource = StringUtils.substringAfter(url, "/images/");
268                 action = new ImageResourceAction(blog, resource);
269
270             } else if (url.matches(IConstants.PATTERNS.ATTACHMENT_FILE_PATTERN)) {
271                 if (log.isDebugEnabled()) {
272                     log.debug("Attachment-file requested!");
273                 }
274
275                 // get resource
276
if (url.endsWith("/")) url = url.substring(0, url.length() - 1);
277                 String JavaDoc resource = StringUtils.substringAfter(url, "/files/");
278
279                 action = new FileResourceAction(blog, resource);
280
281             } else {
282                 if (log.isDebugEnabled()) {
283                     log.debug("Unknown pattern requested. Return blog's frontpage!");
284                 }
285
286                 action = new FrontpageAction(blog);
287             }
288
289             if (action != null) {
290
291                 String JavaDoc actionName = "/blogs/" + blogname;
292                 action.init(request, response, actionName);
293                 IActionResult result = action.execute(request, response);
294
295                 if (!(action instanceof AbstractFeedAction || action instanceof AbstractResourceAction)) {
296                     // don't set this attributes, if blog' feeds are requested
297
request.setAttribute("archives", (new CalendarEntryDAO()).getArchives(blog));
298                     request.setAttribute("recentEntries", (new EntryDAO())
299                             .getRecentBlogEntries(blog));
300                     request.setAttribute("recentComments", (new CommentDAO())
301                             .getRecentBlogComments(blog));
302                     request.setAttribute("referers", (new RefererDAO()).getTodayReferers(blog));
303                     request.setAttribute("visitedPages", (new VisitedPageDAO())
304                             .getTodayVisitedPages(blog));
305                 }
306
307                 if (result != null) {
308
309                     request.setAttribute(IConstants.Request.CURRENT_THEME_FILE, result);
310
311                     if (result.getType() == IActionResult.FORWARD) {
312                         request.getRequestDispatcher(result.getPath()).forward(request, response);
313                     } else if (result.getType() == IActionResult.INCLUDE) {
314                         request.getRequestDispatcher(result.getPath()).include(request, response);
315                     } else if (result.getType() == IActionResult.REDIRECT) {
316                         response.sendRedirect(request.getContextPath() + result.getPath());
317                     } else {
318                         throw new BlogunityException(I18NStatusFactory
319                                 .create(I18N.ERRORS.ACTION_FORWARD_FAILED));
320                     }
321
322                 } else {
323                     if (log.isDebugEnabled()) {
324                         log.debug("Execution of the action='" + action.getClass()
325                                 + "' returned result: NULL! do nothing...");
326                     }
327                 }
328
329             }
330
331         } catch (BlogunityException e) {
332             // we do not throw exceptions here, we just call blog's specific
333
// error-page
334
try {
335
336                 IActionResult forward = new ErrorAction(blogname, e).execute(request, response);
337                 request.setAttribute(IConstants.Request.CURRENT_THEME_FILE, forward);
338                 request.getRequestDispatcher(forward.getPath()).forward(request, response);
339
340             } catch (BlogunityException e1) {
341                 throw new ServletException JavaDoc(e);
342             }
343
344         }
345
346     }
347
348     /**
349      * @param request
350      * @return
351      */

352     private String JavaDoc getRequestedUrl(HttpServletRequest JavaDoc request, String JavaDoc blogname) {
353
354         String JavaDoc requestUri = request.getRequestURI();
355         return requestUri.substring(requestUri.indexOf(blogname) + blogname.length());
356
357     }
358
359     private String JavaDoc getBlogname(HttpServletRequest JavaDoc request) throws BlogunityException {
360
361         String JavaDoc requestUri = request.getRequestURI();
362         requestUri = requestUri.substring(request.getContextPath().length()
363                 + BLOGS_MAPPING.length(), requestUri.length());
364         if (requestUri.length() == 0) {
365             requestUri = "/";
366         }
367         int indx = requestUri.indexOf("/", 1);
368         if (indx == -1) {
369             indx = requestUri.length();
370         }
371
372         // get blogname here
373
String JavaDoc blogName = requestUri.substring(1, indx);
374
375         if (StringUtils.isEmpty(blogName)) {
376
377         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.BLOG_NOT_FOUND,
378                 new String JavaDoc[]{blogName}));
379
380         }
381
382         return blogName;
383     }
384
385 }
Popular Tags