1 25 26 package com.j2biz.blogunity.web.servlet; 27 28 import java.io.IOException ; 29 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 { 71 72 75 private static final long serialVersionUID = 4050481205600467252L; 76 77 private static final Log log = LogFactory.getLog(BlogServlet.class); 78 79 private static final String BLOGS_MAPPING = "/blogs"; 80 81 private String blogname = null; 82 83 89 protected void doGet(HttpServletRequest request, HttpServletResponse response) 90 throws ServletException , IOException { 91 doPost(request, response); 92 } 93 94 100 protected void doPost(HttpServletRequest request, HttpServletResponse response) 101 throws ServletException , IOException { 102 103 try { 104 105 blogname = getBlogname(request); 107 if (log.isDebugEnabled()) { 108 log.debug("Blog '" + blogname + "' is requested..."); 109 } 110 111 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 (e); 119 } 120 121 request.setAttribute("blog", blog); 122 123 String 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 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 String 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 url = url.substring(1); 154 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 155 156 String [] _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 url = url.substring(1); 167 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 168 169 String [] _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 url = url.substring(1); 180 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 181 182 String [] _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 url = url.substring(1); 193 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 194 String [] _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 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 url = url.substring(1); 212 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 213 String [] _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 url = url.substring(1); 226 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 227 String [] _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 url = url.substring(1); 240 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 241 String [] _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 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 257 String 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 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 267 String 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 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 277 String 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 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 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 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 (e); 342 } 343 344 } 345 346 } 347 348 352 private String getRequestedUrl(HttpServletRequest request, String blogname) { 353 354 String requestUri = request.getRequestURI(); 355 return requestUri.substring(requestUri.indexOf(blogname) + blogname.length()); 356 357 } 358 359 private String getBlogname(HttpServletRequest request) throws BlogunityException { 360 361 String 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 String 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 []{blogName})); 379 380 } 381 382 return blogName; 383 } 384 385 } | Popular Tags |