1 4 package org.openedit.blog.modules; 5 6 import java.io.StringWriter ; 7 import java.io.Writer ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.openedit.blog.archive.BlogArchive; 15 import org.openedit.repository.filesystem.StringItem; 16 17 import com.openedit.OpenEditException; 18 import com.openedit.WebPageRequest; 19 import com.openedit.blog.Blog; 20 import com.openedit.blog.BlogCommentNotification; 21 import com.openedit.blog.BlogEntry; 22 import com.openedit.blog.Comment; 23 import com.openedit.blog.Question; 24 import com.openedit.modules.BaseModule; 25 import com.openedit.modules.html.EditorSession; 26 import com.openedit.page.Page; 27 import com.openedit.users.User; 28 import com.openedit.users.filesystem.FileSystemUser; 29 import com.sun.syndication.feed.synd.SyndFeed; 30 import com.sun.syndication.feed.synd.SyndFeedImpl; 31 import com.sun.syndication.io.SyndFeedOutput; 32 33 37 public class BlogModule extends BaseModule 38 { 39 protected Map fieldBlogs; 40 private static final Log log = LogFactory.getLog(BlogModule.class); 41 protected BlogCommentNotification fieldCommentNotification; 42 43 public Blog getBlog(WebPageRequest req ) throws OpenEditException 44 { 45 Page inPath = req.getPage(); 46 String home = inPath.get("bloghome"); 47 48 if( home == null) 49 { 50 Blog blog = (Blog)req.getPageValue("blog"); if( blog != null) 52 { 53 return blog; 54 } 55 throw new OpenEditException("Need to define bloghome page property"); 56 } 57 Blog blog = getBlog(home); 58 req.putPageValue("blog",blog); 59 req.putPageValue("bloghome",blog.getBlogHome()); 60 return blog; 61 } 62 63 public Blog getBlog(String home) throws OpenEditException 64 { 65 if( home.endsWith("/")) 66 { 67 home = home.substring(0,home.length()-1); 68 } 69 Blog blog = (Blog)getBlogs().get(home); 70 boolean changed = false; 71 if ( blog == null) 72 { 73 changed = true; 74 } 75 else 76 { 77 changed = blog.hasChanged(); 79 } 80 81 if (changed) 82 { 83 blog = new Blog(); 84 blog.setBlogHome(home); 85 BlogArchive archive = getArchive(home + "/index.html"); 86 archive.loadBlog(blog); 87 getBlogs().put(home,blog); 88 } 89 return blog; 90 } 91 92 public void getRandomQuestion(WebPageRequest inReq) throws Exception 93 { 94 Question q = (Question)inReq.getSessionValue("question"); 95 if( q == null) 96 { 97 Blog blog = getBlog( inReq ); 98 q = blog.getRandomQuestion(); 99 inReq.putSessionValue("question", q); 100 } 101 } 102 103 104 public void writeBlogSettings(WebPageRequest inReq) throws Exception 105 { 106 Blog blog = getBlog( inReq ); 107 Page settings = getPageManager().getPage(blog.getBlogHome() + "/blogsettings.xml"); 108 110 String parameter = inReq.getRequestParameter("blogtitle"); 111 blog.setTitle(parameter); 112 parameter = inReq.getRequestParameter("bloghostname"); 113 blog.setHostName(parameter); 114 parameter = inReq.getRequestParameter("blogauthor"); 115 blog.setAuthor(parameter); 116 parameter = inReq.getRequestParameter("blogdescription"); 117 blog.setDescription(parameter); 118 parameter = inReq.getRequestParameter("bloganonymous"); 119 if (parameter == null) 120 { 121 parameter = "false"; 122 } 123 blog.setAllowAnonymous("true".equals(parameter)); 124 parameter = inReq.getRequestParameter("blogautopublishcomments"); 125 if (parameter == null) 126 { 127 parameter = "false"; 128 } 129 blog.setAutoPublishingComments("true".equals(parameter)); 130 131 StringWriter writer = new StringWriter (); 132 try 133 { 134 new BlogArchive().saveBlog(blog,writer, settings.getCharacterEncoding()); 135 } 136 finally 137 { 138 writer.close(); 139 } 140 141 StringItem item = new StringItem( settings.getPath(), writer.toString(), settings.getCharacterEncoding() ); 143 item.setMessage( "Edited blog settings via admin interface" ); 144 item.setAuthor( inReq.getUser().getUserName() ); 145 settings.setContentItem(item); 146 getPageManager().putPage(settings); 147 } 148 149 public void generateFeed(WebPageRequest req) throws Exception 150 { 151 SyndFeed feed = new SyndFeedImpl(); 152 String feedtype = req.getPage().get("feedtype"); 153 if ( feedtype == null) 154 { 155 feedtype = "atom_0.3"; 156 } 157 feed.setFeedType(feedtype); 158 159 Blog blog = getBlog(req); 160 161 feed.setTitle(blog.getTitle()); 162 feed.setLink(blog.getHostName()); 163 feed.setDescription(blog.getDescription()); 164 165 List recent = blog.getRecentEntries(); 166 feed.setEntries(recent); 167 168 Writer writer = new StringWriter (); 169 SyndFeedOutput output = new SyndFeedOutput(); 170 output.output(feed,writer); 171 writer.close(); 172 req.putPageValue("feedresults",writer.toString()); 173 } 174 175 public Map getBlogs() 176 { 177 if ( fieldBlogs == null) 178 { 179 fieldBlogs = new HashMap (); 180 } 181 return fieldBlogs; 182 } 183 public void setBlogs(Map inBlogs) 184 { 185 fieldBlogs = inBlogs; 186 } 187 190 public BlogEntry getEntry(WebPageRequest inReq) throws OpenEditException 191 { 192 Blog blog = getBlog(inReq); 193 String entryId = inReq.getRequestParameter("entryId"); 194 BlogEntry entry = blog.getEntry(entryId); 195 inReq.putPageValue("entry",entry); 196 return entry; 197 } 198 public void editEntry(WebPageRequest inReq) throws Exception 199 { 200 Blog blog = getBlog(inReq); 201 String entryId = inReq.getRequestParameter("entryId"); 202 BlogEntry entry = blog.getEntry(entryId); 203 inReq.putPageValue("blog",blog); 204 inReq.putSessionValue("entry",entry); 205 inReq.setRequestParameter("editPath",entry.getPath()); 206 inReq.setRequestParameter("originalPath",blog.getBlogHome()); 207 208 } 209 210 public void loadPermalink(WebPageRequest inReq) throws Exception 211 { 212 Blog blog = getBlog(inReq); 214 String path = inReq.getPath(); 215 216 BlogEntry entry = blog.getEntry(path); 218 if ( entry != null) 219 { 220 inReq.putPageValue("entry",entry); 221 } 222 else 223 { 224 log.error("Null entry" + path); 225 } 226 } 227 public void loadAdminLink(WebPageRequest inReq) throws Exception 238 { 239 String path = inReq.getPath(); 241 Blog blog = getBlog(inReq); 242 path = "admin/" + path; 243 BlogEntry entry = blog.getEntry(path); 244 inReq.putPageValue("entry",entry); 245 } 246 249 public void addNewEntry(WebPageRequest inReq) throws Exception 250 { 251 Blog blog = getBlog(inReq); 252 BlogEntry entry = blog.createNewEntry(inReq.getUser()); 253 254 inReq.putPageValue("blog",blog); 255 inReq.putSessionValue("entry",entry); 256 257 inReq.setRequestParameter("editPath",entry.getPath()); 258 inReq.setRequestParameter("originalURL",blog.getBlogHome()); 259 260 } 261 262 public void saveEntry(WebPageRequest inReq) throws Exception 263 { 264 Blog blog = getBlog(inReq); 265 if ( !blog.canEdit(inReq.getUser()) ) 266 { 267 throw new OpenEditException("User cannot edit blog"); 268 } 269 270 BlogEntry entry = (BlogEntry)inReq.getSessionValue("entry"); 271 if ( entry == null) 272 { 273 entry = blog.createNewEntry(inReq.getUser()); 274 } 275 276 278 String content = inReq.getRequestParameter("content"); 279 if ( content != null) 280 { 281 EditorSession session = new EditorSession(); 282 content = session.stripBody(content); 283 289 297 298 } 299 String author = inReq.getRequestParameter("author"); 300 entry.setAuthor( author); 301 302 String title = inReq.getRequestParameter("title"); 303 entry.setTitle(title); 304 entry.setDescription(content); 305 306 entry.setVisible(blog.isAutoPublishEntries()); 307 308 BlogArchive archive = getArchive(inReq.getPath()); 309 310 archive.saveEntry(blog,entry); 311 archive.getEntryArchive().saveLinks(blog); 312 } 313 public void removeEntry(WebPageRequest inReq) throws Exception 314 { 315 Blog blog = getBlog(inReq); 316 317 if ( !blog.canEdit(inReq.getUser()) ) 318 { 319 throw new OpenEditException("User cannot edit blog"); 320 } 321 322 String entryId = inReq.getRequestParameter("entryId"); 323 BlogEntry entry = blog.getEntry(entryId); 324 325 blog.removeEntry(entry); 326 BlogArchive archive = getArchive(blog.getBlogHome()); 327 archive.getEntryArchive().saveLinks(blog); 328 } 329 protected BlogArchive getArchive(String inPath) throws OpenEditException 330 { 331 Page path = getPageManager().getPage(inPath); 332 return getArchive(path); 333 } 334 protected BlogArchive getArchive(Page inPath) throws OpenEditException 335 { 336 String name = inPath.getProperty("blogarchivename"); 338 if ( name == null) 339 { 340 name = "BlogArchive"; 341 } 342 BlogArchive archive = (BlogArchive)getBeanFactory().getBean(name); 343 return archive; 344 } 345 348 public void addNewComment(WebPageRequest inReq) throws Exception 349 { 350 String content = inReq.getRequestParameter("content"); 351 String entryId = inReq.getRequestParameter("entryId"); 352 if( content == null || entryId == null) 353 { 354 return; 355 } 356 Blog blog = getBlog(inReq); 357 if ( checkQuestion(inReq) ) 358 { 359 BlogEntry entry = blog.getEntry(entryId); 360 361 String author = inReq.getRequestParameter("username"); 362 User user = getUserManager().getUser(author); 363 if( user == null) 364 { 365 if( "anonymous".equals( author ) && blog.getAllowAnonymous() ) 366 { 367 user = new FileSystemUser(); 368 user.setUserName(author); 369 } 370 else 371 { 372 throw new OpenEditException("Anonymous user not allowed"); 373 } 374 } 375 Comment comment = blog.createNewComment(user, content); 376 entry.addComment(comment); 377 BlogArchive archive = getArchive(inReq.getPage()); 378 archive.getEntryArchive().getCommentArchive().saveComments(blog, entry); 379 getCommentNotification().commentAdded(inReq, blog, entry,comment); 380 } 381 } 383 384 public boolean checkQuestion(WebPageRequest inReq) throws OpenEditException 385 { 386 if( inReq.getSessionValue("validperson") != null ) 387 { 388 return true; 389 } 390 391 String answer = inReq.getRequestParameter("answerid"); 392 Blog blog = getBlog(inReq); 393 Question q = (Question)inReq.getSessionValue("question"); 394 if ( !q.checkAnswer( answer) ) 395 { 396 inReq.redirect(blog.getBlogHome() + "/questionerror.html"); 397 return false; 398 } 399 inReq.putSessionValue("validperson", "true"); 400 return true; 401 } 402 public void removeComment(WebPageRequest inReq) throws Exception 403 { 404 Blog blog = getBlog(inReq); 405 String entryId = inReq.getRequestParameter("entryId"); 406 BlogEntry entry = blog.getEntry(entryId); 407 String commentId = inReq.getRequestParameter("commentId"); 408 Comment comment = entry.getComment(commentId); 409 User user = (User)inReq.getSessionValue("user"); 410 411 if (comment.canEdit(user)) 412 { 413 entry.removeComment(comment); 414 BlogArchive archive = getArchive(inReq.getPage()); 415 archive.getEntryArchive().getCommentArchive().saveComments(blog, entry); 416 } 417 redirectToOrig(inReq); 418 } 419 public void changeCommentVisibility(WebPageRequest inReq) throws Exception 420 { 421 String entryId = inReq.getRequestParameter("entryId"); 422 if( entryId != null) 423 { 424 Blog blog = getBlog(inReq); 425 String commentId = inReq.getRequestParameter("commentId"); 426 BlogEntry entry = blog.getEntry(entryId); 427 Comment comment = entry.getComment(commentId); 428 if ( comment != null ) 429 { 430 if(comment.canEdit(inReq.getUser()) || blog.canEdit(inReq.getUser() )) 431 { 432 comment.setVisible(!comment.isVisible()); 433 BlogArchive archive = getArchive(inReq.getPage()); 434 archive.getEntryArchive().getCommentArchive().saveComments(blog, entry); 435 } 436 } 437 redirectToOrig(inReq); 438 } 439 } 440 private void redirectToOrig(WebPageRequest inReq) 441 { 442 String origURL = inReq.getRequestParameter("origURL"); 443 if (origURL != null && origURL.length() > 0) 444 { 445 inReq.redirect(origURL); 446 } 447 } 448 public void login(WebPageRequest inReq) throws Exception 449 { 450 String username = inReq.getRequestParameter("username"); 451 User user = getUserManager().getUser(username); 452 String password = inReq.getRequestParameter("password"); 453 if( getUserManager().authenticate(user , password) ) 454 { 455 inReq.putSessionValue("user",user); 456 } 457 } 458 494 public void changeEntryVisibility(WebPageRequest inReq) throws Exception 495 { 496 Blog blog = getBlog(inReq); 497 498 if ( !blog.canEdit(inReq.getUser()) ) 499 { 500 throw new OpenEditException("User cannot edit blog"); 501 } 502 503 String entryId = inReq.getRequestParameter("entryId"); 504 BlogEntry entry = blog.getEntry(entryId); 505 506 entry.setVisible(!entry.isVisible()); 507 BlogArchive archive = getArchive(inReq.getPage()); 508 archive.saveEntry(blog, entry); 509 } 510 511 public BlogCommentNotification getCommentNotification() 512 { 513 return fieldCommentNotification; 514 } 515 516 public void setCommentNotification(BlogCommentNotification inCommentNotification) 517 { 518 fieldCommentNotification = inCommentNotification; 519 } 520 } 521 | Popular Tags |