1 package org.roller.presentation.velocity; 2 3 import java.io.IOException ; 4 import java.net.MalformedURLException ; 5 import java.util.Date ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.ResourceBundle ; 9 import java.util.Set ; 10 import java.util.TreeSet ; 11 12 import javax.mail.MessagingException ; 13 import javax.mail.Session ; 14 import javax.naming.InitialContext ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.jsp.JspFactory ; 20 import javax.servlet.jsp.PageContext ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.struts.util.RequestUtils; 25 import org.apache.velocity.Template; 26 import org.apache.velocity.context.Context; 27 import org.roller.RollerException; 28 import org.roller.config.RollerConfig; 29 import org.roller.config.RollerRuntimeConfig; 30 import org.roller.model.IndexManager; 31 import org.roller.model.Roller; 32 import org.roller.model.UserManager; 33 import org.roller.model.WeblogManager; 34 import org.roller.pojos.CommentData; 35 import org.roller.pojos.PageData; 36 import org.roller.pojos.UserData; 37 import org.roller.pojos.WeblogEntryData; 38 import org.roller.pojos.WebsiteData; 39 import org.roller.presentation.LanguageUtil; 40 import org.roller.presentation.RollerContext; 41 import org.roller.presentation.RollerRequest; 42 import org.roller.presentation.RollerSession; 43 import org.roller.presentation.pagecache.PageCacheFilter; 44 import org.roller.presentation.weblog.formbeans.CommentFormEx; 45 import org.roller.util.CommentSpamChecker; 46 import org.roller.util.MailUtil; 47 import org.roller.util.StringUtils; 48 49 77 public class CommentServlet extends PageServlet 78 { 79 private static final String COMMENT_SPAM_MSG = 80 "Your comment has been recognized as " 81 + "<a HREF='http://www.jayallen.org/projects/mt-blacklist/'>" 82 + "Comment Spam</a> and rejected."; 83 private transient ResourceBundle bundle = 84 ResourceBundle.getBundle("ApplicationResources"); 85 private static Log mLogger = 86 LogFactory.getFactory().getInstance(CommentServlet.class); 87 88 93 public Template handleRequest( HttpServletRequest request, 94 HttpServletResponse response, 95 Context ctx ) throws Exception 96 { 97 Template template = null; 98 if (request.getParameter("popup") == null) 99 { 100 template = super.handleRequest(request, response, ctx); 102 } 103 else 104 { 105 PageContext pageContext = 106 JspFactory.getDefaultFactory().getPageContext( 107 this, request, response,"", true, 8192, true); 108 RollerRequest rreq= RollerRequest.getRollerRequest(pageContext); 109 UserManager userMgr = rreq.getRoller().getUserManager(); 110 WebsiteData website = rreq.getWebsite(); 111 112 PageData page = null; 114 Exception pageException = null; 115 try 116 { 117 page = userMgr.getPageByName(website, "_popupcomments"); 119 } 120 catch(Exception e ) 121 { 122 pageException = e; 123 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 124 } 125 if (pageException != null) 126 { 127 mLogger.error("EXCEPTION: in RollerServlet", pageException); 128 request.setAttribute("DisplayException", pageException); 129 } 130 if (page == null) 132 { 133 page = new PageData("/popupcomments.vm", website, "Comments", 134 "Comments", "dummy_link", "dummy_template", new Date ()); 135 } 136 rreq.setPage(page); 137 template = prepareForPageExecution(ctx, rreq, response, page); 138 } 139 return template; 140 } 141 142 146 public void doPost( 147 HttpServletRequest request, HttpServletResponse response) 148 throws IOException , ServletException 149 { 150 if (request.getParameter("method") != null 151 && request.getParameter("method").equals("preview")) 152 { 153 doPreviewPost(request, response); 154 return; 155 } 156 157 RollerRequest rreq = RollerRequest.getRollerRequest(request); 158 HttpSession session = request.getSession(); 159 try 160 { 161 WeblogEntryData wd = rreq.getWeblogEntry(); 163 if (wd == null || wd.getId() == null) 164 { 165 throw new RollerException( 166 "Unable to find WeblogEntry for " 167 + request.getParameter(RollerRequest.WEBLOGENTRYID_KEY)); 168 } 169 if ( !wd.getWebsite().getAllowComments().booleanValue() 170 || !wd.getCommentsStillAllowed()) 171 { 172 throw new RollerException("ERROR comments not allowed"); 173 } 174 175 request.setAttribute("blogEntry", wd); 176 177 UserData user = wd.getWebsite().getUser(); 179 180 request.setAttribute(RollerRequest.OWNING_USER, user); 182 183 WeblogManager mgr = rreq.getRoller().getWeblogManager(); 185 CommentFormEx cf = new CommentFormEx(); 186 CommentData cd = new CommentData(); 187 RequestUtils.populate(cf, request); 188 cf.copyTo(cd, request.getLocale()); 189 cd.setWeblogEntry(wd); 190 cd.setRemoteHost(request.getRemoteHost()); 191 cd.setPostTime(new java.sql.Timestamp (System.currentTimeMillis())); 192 193 if (!testCommentSpam(cd, request)) 194 { 195 if (RollerContext.getCommentAuthenticator().authenticate(cd, request)) 196 { 197 cd.save(); 198 rreq.getRoller().commit(); 199 reindexEntry(rreq.getRoller(), wd); 200 201 PageCacheFilter.removeFromCache(request, user); 203 204 List comments = mgr.getComments(wd.getId()); 206 request.setAttribute("blogComments", comments); 207 208 sendEmailNotification(request, rreq, wd, cd, user,comments); 210 211 super.doPost(request, response); 212 return; 213 } 214 else 215 { 216 request.getSession().setAttribute( 217 RollerSession.ERROR_MESSAGE, 218 bundle.getString("error.commentAuthFailed")); 219 } 220 } 221 doPreviewPost(request, response); 222 } 223 catch (Exception e) 224 { 225 mLogger.error("ERROR posting comment", e); 226 session.setAttribute(RollerSession.ERROR_MESSAGE, e.getMessage()); 228 } 229 } 230 231 236 public void doPreviewPost( 237 HttpServletRequest request, HttpServletResponse response) 238 throws IOException , ServletException 239 { 240 RollerRequest rreq = RollerRequest.getRollerRequest(request); 241 try 242 { 243 WeblogEntryData wd = rreq.getWeblogEntry(); 244 if (wd == null || wd.getId() == null) 245 { 246 throw new RollerException( 247 "Unable to find WeblogEntry for " 248 + request.getParameter(RollerRequest.WEBLOGENTRYID_KEY)); 249 } 250 request.setAttribute("blogEntry", wd); 251 252 request.setAttribute(RollerRequest.OWNING_USER, wd.getWebsite().getUser()); 254 255 CommentFormEx cf = new CommentFormEx(); 256 RequestUtils.populate(cf, request); 257 cf.setWeblogEntry(wd); 258 cf.setPostTime(new java.sql.Timestamp (System.currentTimeMillis())); 259 request.setAttribute("commentForm", cf); 260 request.setAttribute("previewComments","dummy"); 261 } 262 catch (Exception e) 263 { 264 mLogger.error(e); 266 } 267 super.doPost(request, response); 268 } 269 270 274 private void reindexEntry(Roller roller, WeblogEntryData entry) throws RollerException 275 { 276 IndexManager manager = roller.getIndexManager(); 277 278 manager.removeEntryIndexOperation(entry); 280 281 if (entry.getPublishEntry() == Boolean.TRUE) 283 { 284 manager.addEntryIndexOperation(entry); 285 } 286 } 287 288 294 private boolean testCommentSpam(CommentData cd, HttpServletRequest req) 295 { 296 boolean ret = false; 297 CommentSpamChecker checker = new CommentSpamChecker(); 298 checker.testComment(cd); 299 if (cd.getSpam().booleanValue()) 300 { 301 HttpSession session = req.getSession(); 302 session.setAttribute( 303 RollerSession.ERROR_MESSAGE, COMMENT_SPAM_MSG); 304 ret = true; 305 } 306 return ret; 307 } 308 309 311 313 316 private static final String EMAIL_ADDR_REGEXP = "^.*@.*[.].{2,}$"; 317 318 private static final String SEPARATE_OWNER_MSG_PARAM = CommentServlet.class.getName() + ".separateOwnerMessage"; 321 private static final String HIDE_COMMENTER_ADDRESSES_PARAM = CommentServlet.class.getName() + ".hideCommenterAddresses"; 323 324 325 328 private void sendEmailNotification( 329 HttpServletRequest request, 330 RollerRequest rreq, 331 WeblogEntryData wd, 332 CommentData cd, 333 UserData user, 334 List comments) throws MalformedURLException 335 { 336 RollerContext rc = RollerContext.getRollerContext(request); 337 ResourceBundle resources = ResourceBundle.getBundle( 338 "ApplicationResources",LanguageUtil.getViewLocale(request)); 339 UserManager userMgr = null; 340 WebsiteData site = null; 341 try 342 { 343 userMgr = RollerContext.getRoller(request).getUserManager(); 344 site = userMgr.getWebsite(user.getUserName()); 345 } 346 catch (RollerException re) 347 { 348 re.printStackTrace(); 349 mLogger.error( 350 "Couldn't get UserManager from RollerContext", re.getRootCause()); 351 } 352 353 boolean notify = RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify"); 355 if (notify && site.getEmailComments().booleanValue()) 356 { 357 boolean separateMessages = 359 RollerConfig.getBooleanProperty("comment.notification.separateOwnerMessage"); 360 boolean hideCommenterAddrs = 361 RollerConfig.getBooleanProperty("comment.notification.hideCommenterAddresses"); 362 363 367 String from = 368 (StringUtils.isEmpty(site.getEmailFromAddress())) 369 ? user.getEmailAddress() 370 : site.getEmailFromAddress(); 371 372 375 Set subscribers = new TreeSet (); 377 for (Iterator it = comments.iterator(); it.hasNext();) 378 { 379 CommentData comment = (CommentData) it.next(); 380 if (!StringUtils.isEmpty(comment.getEmail())) 381 { 382 if (comment.getNotify().booleanValue()) 385 { 386 if (comment.getEmail().matches(EMAIL_ADDR_REGEXP)) 388 { 389 subscribers.add(comment.getEmail()); 390 } 391 } 392 else 393 { 394 subscribers.remove(comment.getEmail()); 396 } 397 } 398 } 399 400 String [] commenterAddrs = (String [])subscribers.toArray(new String [0]); 402 403 407 StringBuffer msg = new StringBuffer (); 409 StringBuffer ownermsg = new StringBuffer (); 410 boolean escapeHtml = RollerRuntimeConfig.getBooleanProperty("users.comments.escapehtml"); 411 412 if (!escapeHtml) 413 { 414 msg.append("<html><body style=\"background: white; "); 415 msg.append(" color: black; font-size: 12px\">"); 416 } 417 418 if (!StringUtils.isEmpty(cd.getName())) 419 { 420 msg.append(cd.getName() + " " 421 + resources.getString("email.comment.wrote")+": "); 422 } 423 else 424 { 425 msg.append(resources.getString("email.comment.anonymous")+": "); 426 } 427 428 msg.append((escapeHtml) ? "\n\n" : "<br /><br />"); 429 msg.append(cd.getContent()); 430 msg.append((escapeHtml) ? "\n\n----\n" 431 : "<br /><br /><hr /><span style=\"font-size: 11px\">"); 432 msg.append(resources.getString("email.comment.respond") + ": "); 433 msg.append((escapeHtml) ? "\n" : "<br />"); 434 435 String rootURL = rc.getAbsoluteContextUrl(request); 436 if (rootURL == null || rootURL.trim().length()==0) 437 { 438 rootURL = RequestUtils.serverURL(request) + request.getContextPath(); 439 } 440 441 443 StringBuffer commentURL = new StringBuffer (rootURL); 444 commentURL.append("/comments/"); 445 commentURL.append(rreq.getUser().getUserName()); 446 447 PageData page = rreq.getPage(); 448 if (page == null) 449 { 450 commentURL.append("?entry="); 451 } 452 else 453 { 454 commentURL.append("/").append(page.getLink()).append("/"); 455 } 456 457 commentURL.append(wd.getAnchor()); 458 459 if (escapeHtml) 460 { 461 msg.append(commentURL.toString()); 462 } 463 else 464 { 465 msg.append("<a HREF=\""+commentURL+"\">"+commentURL+"</a></span>"); 466 } 467 468 ownermsg.append(msg); 469 470 ownermsg.append((escapeHtml) ? "\n\n----\n" : 472 "<br /><br /><hr /><span style=\"font-size: 11px\">"); 473 ownermsg.append("Link to comment management page:"); 474 ownermsg.append((escapeHtml) ? "\n" : "<br />"); 475 476 StringBuffer deleteURL = new StringBuffer (rootURL); 477 deleteURL.append("/editor/weblog.do?method=edit&entryid="+wd.getId()); 478 479 if (escapeHtml) 480 { 481 ownermsg.append(deleteURL.toString()); 482 } 483 else 484 { 485 ownermsg.append( 486 "<a HREF=\"" + deleteURL + "\">" + deleteURL + "</a></span>"); 487 msg.append("</Body></html>"); 488 ownermsg.append("</Body></html>"); 489 } 490 491 String subject = null; 492 if ((subscribers.size() > 1) || 493 (StringUtils.equals(cd.getEmail(), user.getEmailAddress()))) 494 { 495 subject= "RE: "+resources.getString("email.comment.title")+": "; 496 } 497 else 498 { 499 subject = resources.getString("email.comment.title") + ": "; 500 } 501 subject += wd.getTitle(); 502 503 try 506 { 507 javax.naming.Context ctx = (javax.naming.Context ) 508 new InitialContext ().lookup("java:comp/env"); 509 Session session = (Session )ctx.lookup("mail/Session"); 510 boolean isHtml = !escapeHtml; 511 if (separateMessages) 512 { 513 sendMessage(session, from, 515 new String []{user.getEmailAddress()}, null, null, subject, ownermsg.toString(), isHtml); 516 if (commenterAddrs.length > 0) 517 { 518 String [] to = hideCommenterAddrs ? null : commenterAddrs; 520 String [] bcc = hideCommenterAddrs ? commenterAddrs : null; 521 sendMessage(session, from, to, null, bcc, subject, msg.toString(), isHtml); 522 523 } 524 } 525 else 526 { 527 String [] cc = hideCommenterAddrs ? null : commenterAddrs; 529 String [] bcc = hideCommenterAddrs ? commenterAddrs : null; 530 sendMessage(session, from, new String []{user.getEmailAddress()}, cc, bcc, subject, 531 ownermsg.toString(), isHtml); 532 } 533 } 534 catch (javax.naming.NamingException ne) 535 { 536 mLogger.error("Unable to lookup mail session. Check configuration. NamingException: " + ne.getMessage()); 537 } 538 catch (Exception e) 539 { 540 mLogger.warn("Exception sending comment mail: " + e.getMessage()); 541 if (mLogger.isDebugEnabled()) 543 { 544 mLogger.debug(e); 545 } 546 } 547 548 } } 550 551 private void sendMessage(Session session, String from, String [] to, String [] cc, String [] bcc, String subject, 553 String msg, boolean isHtml) throws MessagingException 554 { 555 if (isHtml) 556 MailUtil.sendHTMLMessage(session, from, to, cc, bcc, subject, msg); 557 else 558 MailUtil.sendTextMessage(session, from, to, cc, bcc, subject, msg); 559 } 560 561 568 } 569 570 | Popular Tags |