| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.web.action; |
| 33 | |
|
| 34 | |
import javax.servlet.ServletException; |
| 35 | |
import javax.servlet.http.HttpServletRequest; |
| 36 | |
import javax.servlet.http.HttpServletResponse; |
| 37 | |
|
| 38 | |
import net.sourceforge.pebble.Constants; |
| 39 | |
import net.sourceforge.pebble.api.confirmation.CommentConfirmationStrategy; |
| 40 | |
import net.sourceforge.pebble.api.decorator.ContentDecoratorContext; |
| 41 | |
import net.sourceforge.pebble.domain.Blog; |
| 42 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 43 | |
import net.sourceforge.pebble.domain.BlogService; |
| 44 | |
import net.sourceforge.pebble.domain.BlogServiceException; |
| 45 | |
import net.sourceforge.pebble.domain.Comment; |
| 46 | |
import net.sourceforge.pebble.util.I18n; |
| 47 | |
import net.sourceforge.pebble.web.security.RequireSecurityToken; |
| 48 | |
import net.sourceforge.pebble.web.validation.ValidationContext; |
| 49 | |
import net.sourceforge.pebble.web.view.NotFoundView; |
| 50 | |
import net.sourceforge.pebble.web.view.View; |
| 51 | |
import net.sourceforge.pebble.web.view.impl.CommentConfirmationView; |
| 52 | |
import net.sourceforge.pebble.web.view.impl.CommentFormView; |
| 53 | |
import net.sourceforge.pebble.web.view.impl.ConfirmCommentView; |
| 54 | |
|
| 55 | |
import org.apache.commons.logging.Log; |
| 56 | |
import org.apache.commons.logging.LogFactory; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
@RequireSecurityToken |
| 64 | 20 | public class SaveCommentAction extends AbstractCommentAction { |
| 65 | |
|
| 66 | |
|
| 67 | 4 | private static Log log = LogFactory.getLog(SaveCommentAction.class); |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
| 77 | 20 | Blog blog = (Blog)getModel().get(Constants.BLOG_KEY); |
| 78 | |
BlogEntry blogEntry; |
| 79 | |
Comment comment; |
| 80 | |
|
| 81 | 20 | String entry = request.getParameter("entry"); |
| 82 | 20 | String rememberMe = request.getParameter("rememberMe"); |
| 83 | 20 | String submitType = request.getParameter("submit"); |
| 84 | |
|
| 85 | 20 | BlogService service = new BlogService(); |
| 86 | |
try { |
| 87 | 20 | blogEntry = service.getBlogEntry(blog, entry); |
| 88 | 0 | } catch (BlogServiceException e) { |
| 89 | 0 | throw new ServletException(e); |
| 90 | 20 | } |
| 91 | 20 | if (blogEntry == null) { |
| 92 | |
|
| 93 | |
|
| 94 | 0 | log.info("ignoring saveComment with no related blog entry (spam) from " + request.getRemoteAddr()); |
| 95 | 0 | return new NotFoundView(); |
| 96 | 20 | } else if (!blogEntry.isCommentsEnabled()) { |
| 97 | 4 | return new CommentConfirmationView(); |
| 98 | |
} |
| 99 | |
|
| 100 | 16 | comment = createComment(request, blogEntry); |
| 101 | 16 | ValidationContext context = validateComment(comment); |
| 102 | |
|
| 103 | |
|
| 104 | 16 | String previewButton = I18n.getMessage(blog, "comment.previewButton"); |
| 105 | |
|
| 106 | 16 | ContentDecoratorContext decoratorContext = new ContentDecoratorContext(); |
| 107 | 16 | decoratorContext.setView(ContentDecoratorContext.DETAIL_VIEW); |
| 108 | 16 | decoratorContext.setMedia(ContentDecoratorContext.HTML_PAGE); |
| 109 | |
|
| 110 | 16 | Comment decoratedComment = (Comment)comment.clone(); |
| 111 | 16 | blog.getContentDecoratorChain().decorate(decoratorContext, decoratedComment); |
| 112 | 16 | getModel().put("decoratedComment", decoratedComment); |
| 113 | 16 | getModel().put("undecoratedComment", comment); |
| 114 | 16 | getModel().put("rememberMe", rememberMe); |
| 115 | 16 | getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry); |
| 116 | 16 | getModel().put(Constants.COMMENT_KEY, comment); |
| 117 | 16 | request.getSession().setAttribute("rememberMe", request.getParameter("rememberMe")); |
| 118 | |
|
| 119 | 16 | if (submitType == null || submitType.equalsIgnoreCase(previewButton) || context.hasErrors()) { |
| 120 | 0 | return new CommentFormView(); |
| 121 | |
} else { |
| 122 | 16 | CommentConfirmationStrategy strategy = blog.getCommentConfirmationStrategy(); |
| 123 | |
|
| 124 | 16 | Comment clonedComment = (Comment)comment.clone(); |
| 125 | 16 | request.getSession().setAttribute(Constants.COMMENT_KEY, comment); |
| 126 | |
|
| 127 | 16 | if (strategy.confirmationRequired(clonedComment)) { |
| 128 | 4 | strategy.setupConfirmation(request); |
| 129 | 4 | return new ConfirmCommentView(); |
| 130 | |
} else { |
| 131 | |
try { |
| 132 | 12 | saveComment(request, response, blogEntry, comment); |
| 133 | 12 | request.getSession().removeAttribute(Constants.COMMENT_KEY); |
| 134 | 12 | return new CommentConfirmationView(); |
| 135 | 0 | } catch (BlogServiceException be) { |
| 136 | 0 | log.error(be.getMessage(), be); |
| 137 | 0 | throw new ServletException(be); |
| 138 | |
} |
| 139 | |
} |
| 140 | |
} |
| 141 | |
} |
| 142 | |
} |