log.debug("Doing comment posting for entry = "+entry.getPermalink());
// collect input from request params and construct new comment object
// fields: name, email, url, content, notify
// TODO: data validation on collected comment data
WeblogEntryComment comment = new WeblogEntryComment();
comment.setName(commentRequest.getName());
comment.setEmail(commentRequest.getEmail());
comment.setUrl(commentRequest.getUrl());
comment.setContent(commentRequest.getContent());
comment.setNotify(new Boolean(commentRequest.isNotify()));
comment.setWeblogEntry(entry);
comment.setRemoteHost(request.getRemoteHost());
comment.setPostTime(new Timestamp(System.currentTimeMillis()));
// set comment content-type depending on if html is allowed
if(WebloggerRuntimeConfig.getBooleanProperty("users.comments.htmlenabled")) {
comment.setContentType("text/html");
} else {
comment.setContentType("text/plain");
}
// set whatever comment plugins are configured
comment.setPlugins(WebloggerRuntimeConfig.getProperty("users.comments.plugins"));
WeblogEntryCommentForm cf = new WeblogEntryCommentForm();
cf.setData(comment);
if (preview) {
cf.setPreview(comment);
}
I18nMessages messageUtils = I18nMessages.getMessages(commentRequest.getLocaleInstance());
// check if comments are allowed for this entry
// this checks site-wide settings, weblog settings, and entry settings
if(!entry.getCommentsStillAllowed() || !entry.isPublished()) {
error = messageUtils.getString("comments.disabled");
// if this is a real comment post then authenticate request
} else if(!preview && !this.authenticator.authenticate(request)) {
error = messageUtils.getString("error.commentAuthFailed");
log.debug("Comment failed authentication");
}
// bail now if we have already found an error
if(error != null) {
cf.setError(error);
request.setAttribute("commentForm", cf);
RequestDispatcher dispatcher = request.getRequestDispatcher(dispatch_url);
dispatcher.forward(request, response);
return;
}
int validationScore = commentValidationManager.validateComment(comment, messages);
log.debug("Comment Validation score: " + validationScore);
if (!preview) {
if (validationScore == 100 && weblog.getCommentModerationRequired()) {
// Valid comments go into moderation if required
comment.setStatus(WeblogEntryComment.PENDING);
message = messageUtils.getString("commentServlet.submittedToModerator");
} else if (validationScore == 100) {
// else they're approved
comment.setStatus(WeblogEntryComment.APPROVED);
message = messageUtils.getString("commentServlet.commentAccepted");
} else {
// Invalid comments are marked as spam
log.debug("Comment marked as spam");
comment.setStatus(WeblogEntryComment.SPAM);
error = messageUtils.getString("commentServlet.commentMarkedAsSpam");
// add specific error messages if they exist
if(messages.getErrorCount() > 0) {
Iterator errors = messages.getErrors();
RollerMessage errorKey = null;
StringBuffer buf = new StringBuffer();
buf.append("<ul>");
while(errors.hasNext()) {
errorKey = (RollerMessage)errors.next();
buf.append("<li>");
if(errorKey.getArgs() != null) {
buf.append(messageUtils.getString(errorKey.getKey(), errorKey.getArgs()));
} else {
buf.append(messageUtils.getString(errorKey.getKey()));
}
buf.append("</li>");
}
buf.append("</ul>");
error += buf.toString();
}
}
try {
if(!WeblogEntryComment.SPAM.equals(comment.getStatus()) ||
!WebloggerRuntimeConfig.getBooleanProperty("comments.ignoreSpam.enabled")) {
WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
mgr.saveComment(comment);
WebloggerFactory.getWeblogger().flush();