Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart9.png 23% of files have more coverage
43   142   9   43
8   79   0,21   1
1     9  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  SaveCommentAction       Line # 64 43 0% 9 8 84,6% 0.84615386
 
  (10)
 
1    /*
2    * Copyright (c) 2003-2006, 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.controller.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    public class SaveCommentAction extends AbstractCommentAction {
65   
66    /** the log used by this class */
67    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  10 toggle public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
77  10 Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
78  10 BlogEntry blogEntry;
79  10 Comment comment;
80   
81  10 String entry = request.getParameter("entry");
82  10 String rememberMe = request.getParameter("rememberMe");
83  10 String submitType = request.getParameter("submit");
84   
85  10 BlogService service = new BlogService();
86  10 try {
87  10 blogEntry = service.getBlogEntry(blog, entry);
88    } catch (BlogServiceException e) {
89  0 throw new ServletException(e);
90    }
91  10 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  10 } else if (!blogEntry.isCommentsEnabled()) {
97  2 return new CommentConfirmationView();
98    }
99   
100  8 comment = createComment(request, blogEntry);
101  8 ValidationContext context = validateComment(comment);
102   
103    // are we previewing or adding the comment?
104  8 String previewButton = I18n.getMessage(blog, "comment.previewButton");
105   
106  8 ContentDecoratorContext decoratorContext = new ContentDecoratorContext();
107  8 decoratorContext.setView(ContentDecoratorContext.DETAIL_VIEW);
108  8 decoratorContext.setMedia(ContentDecoratorContext.HTML_PAGE);
109   
110  8 Comment decoratedComment = (Comment)comment.clone();
111  8 blog.getContentDecoratorChain().decorate(decoratorContext, decoratedComment);
112  8 getModel().put("decoratedComment", decoratedComment);
113  8 getModel().put("undecoratedComment", comment);
114  8 getModel().put("rememberMe", rememberMe);
115  8 getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
116  8 getModel().put(Constants.COMMENT_KEY, comment);
117  8 request.getSession().setAttribute("rememberMe", request.getParameter("rememberMe"));
118   
119  8 if (submitType == null || submitType.equalsIgnoreCase(previewButton) || context.hasErrors()) {
120  0 return new CommentFormView();
121    } else {
122  8 CommentConfirmationStrategy strategy = blog.getCommentConfirmationStrategy();
123   
124  8 Comment clonedComment = (Comment)comment.clone();
125  8 request.getSession().setAttribute(Constants.COMMENT_KEY, comment);
126   
127  8 if (strategy.confirmationRequired(clonedComment)) {
128  2 strategy.setupConfirmation(request);
129  2 return new ConfirmCommentView();
130    } else {
131  6 try {
132  6 saveComment(request, response, blogEntry, comment);
133  6 request.getSession().removeAttribute(Constants.COMMENT_KEY);
134  6 return new CommentConfirmationView();
135    } catch (BlogServiceException be) {
136  0 log.error(be.getMessage(), be);
137  0 throw new ServletException(be);
138    }
139    }
140    }
141    }
142    }