Coverage Report - net.sourceforge.pebble.web.action.SaveCommentAction
 
Classes in this File Line Coverage Branch Coverage Complexity
SaveCommentAction
81%
36/44
66%
8/12
16
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 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  
  * Adds a comment to an existing blog entry.
 60  
  *
 61  
  * @author    Simon Brown
 62  
  */
 63  
 @RequireSecurityToken
 64  20
 public class SaveCommentAction extends AbstractCommentAction {
 65  
 
 66  
   /** the log used by this class */
 67  4
   private static Log log = LogFactory.getLog(SaveCommentAction.class);
 68  
 
 69  
   /**
 70  
    * Peforms the processing associated with this action.
 71  
    *
 72  
    * @param request  the HttpServletRequest instance
 73  
    * @param response the HttpServletResponse instance
 74  
    * @return the name of the next view
 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  
       // just send back a 404 - this is probably somebody looking for a way
 93  
       // to send comment spam ;-)
 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  
     // are we previewing or adding the comment?
 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  
 }