savecommentaction.java

### File Source: savecommentaction.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.action.SaveCommentAction__static_init
              *
              *  Postconditions:
              *    init'ed(log)
              */
         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
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.action.SaveCommentAction()
              */
        34  import javax.servlet.ServletException;
        35  import javax.servlet.ServletRequest;
        36  import javax.servlet.http.HttpServletRequest;
        37  import javax.servlet.http.HttpServletResponse;
        38
        39  import net.sourceforge.pebble.Constants;
        40  import net.sourceforge.pebble.api.confirmation.CommentConfirmationStrategy;
        41  import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
        42  import net.sourceforge.pebble.domain.Blog;
        43  import net.sourceforge.pebble.domain.BlogEntry;
        44  import net.sourceforge.pebble.domain.BlogService;
        45  import net.sourceforge.pebble.domain.BlogServiceException;
        46  import net.sourceforge.pebble.domain.Comment;
        47  import net.sourceforge.pebble.util.I18n;
        48  import net.sourceforge.pebble.web.security.RequireSecurityToken;
        49  import net.sourceforge.pebble.web.validation.ValidationContext;
        50  import net.sourceforge.pebble.web.view.NotFoundView;
        51  import net.sourceforge.pebble.web.view.View;
        52  import net.sourceforge.pebble.web.view.impl.CommentConfirmationView;
        53  import net.sourceforge.pebble.web.view.impl.CommentFormView;
        54  import net.sourceforge.pebble.web.view.impl.ConfirmCommentView;
        55
        56  import org.apache.commons.logging.Log;
        57  import org.apache.commons.logging.LogFactory;
        58
        59  /**
        60   * Adds a comment to an existing blog entry.
        61   *
        62   * @author    Simon Brown
        63   */
        64  @RequireSecurityToken
        65  public class SaveCommentAction extends AbstractCommentAction {
        66
        67    /** the log used by this class */
        68    private static Log log = LogFactory.getLog(SaveCommentAction.class);
        69
        70    /**
        71     * Peforms the processing associated with this action.
        72     *
        73     * @param request  the HttpServletRequest instance
        74     * @param response the HttpServletResponse instance
        75     * @return the name of the next view
        76     */
        77    public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
                 /*
        P/P       *  Method: View process(HttpServletRequest, HttpServletResponse)
                  *
                  *  Preconditions:
                  *    request != null
                  *    this.model != null
                  *    this.model.data != null
                  *    (soft) log != null
                  *    (soft) net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
                  *
                  *  Presumptions:
                  *    java.util.HashMap:get(...)@63 != null
                  *    javax.servlet.http.HttpServletRequest:getSession(...)@118 != null
                  *    javax.servlet.http.HttpServletRequest:getSession(...)@126 != null
                  *    javax.servlet.http.HttpServletRequest:getSession(...)@134 != null
                  *    net.sourceforge.pebble.domain.Blog:getCommentConfirmationStrategy(...)@123 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{&new CommentConfirmationView(process#9),&new ConfirmCommentView(process#8),&new CommentFormView(process#7),&new CommentConfirmationView(process#5),&new NotFoundView(process#4)}
                  *    new CommentConfirmationView(process#5) num objects <= 1
                  *    new CommentConfirmationView(process#9) num objects <= 1
                  *    new CommentFormView(process#7) num objects <= 1
                  *    new ConfirmCommentView(process#8) num objects <= 1
                  *    new NotFoundView(process#4) num objects <= 1
                  *
                  *  Test Vectors:
                  *    java.lang.String:equalsIgnoreCase(...)@120: {1}, {0}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@84: Addr_Set{null}, Inverse{null}
                  *    net.sourceforge.pebble.api.confirmation.CommentConfirmationStrategy:confirmationRequired(...)@128: {0}, {1}
                  *    net.sourceforge.pebble.domain.BlogEntry:isCommentsEnabled(...)@97: {1}, {0}
                  *    net.sourceforge.pebble.domain.BlogService:getBlogEntry(...)@88: Inverse{null}, Addr_Set{null}
                  */
        78      Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
        79      BlogEntry blogEntry;
        80      Comment comment;
        81
        82      String entry = request.getParameter("entry");
        83      String rememberMe = request.getParameter("rememberMe");
        84      String submitType = request.getParameter("submit");
        85
        86      BlogService service = new BlogService();
        87      try {
        88        blogEntry = service.getBlogEntry(blog, entry);
        89      } catch (BlogServiceException e) {
        90        throw new ServletException(e);
        91      }
        92      if (blogEntry == null) {
        93        // just send back a 404 - this is probably somebody looking for a way
        94        // to send comment spam ;-)
        95        log.info("ignoring saveComment with no related blog entry (spam) from " + request.getRemoteAddr());
        96        return new NotFoundView();
        97      } else if (!blogEntry.isCommentsEnabled()) {
        98        return new CommentConfirmationView();
        99      }
       100
       101      comment = createComment(request, blogEntry);
       102      ValidationContext context = validateComment(comment);
       103
       104      // are we previewing or adding the comment?
       105      String previewButton = I18n.getMessage(blog, "comment.previewButton");
       106
       107      ContentDecoratorContext decoratorContext = new ContentDecoratorContext();
       108      decoratorContext.setView(ContentDecoratorContext.DETAIL_VIEW);
       109      decoratorContext.setMedia(ContentDecoratorContext.HTML_PAGE);
       110
       111      Comment decoratedComment = (Comment)comment.clone();
       112      blog.getContentDecoratorChain().decorate(decoratorContext, decoratedComment);
       113      getModel().put("decoratedComment", decoratedComment);
       114      getModel().put("undecoratedComment", comment);
       115      getModel().put("rememberMe", rememberMe);
       116      getModel().put(Constants.BLOG_ENTRY_KEY, blogEntry);
       117      getModel().put(Constants.COMMENT_KEY, comment);
       118      ((ServletRequest) request.getSession()).setAttribute("rememberMe", request.getParameter("rememberMe"));
       119
       120      if (submitType == null || submitType.equalsIgnoreCase(previewButton) || context.hasErrors()) {
       121        return new CommentFormView();
       122      } else {
       123        CommentConfirmationStrategy strategy = blog.getCommentConfirmationStrategy();
       124
       125        Comment clonedComment = (Comment)comment.clone();
       126        ((ServletRequest) request.getSession()).setAttribute(Constants.COMMENT_KEY, comment);
       127
       128        if (strategy.confirmationRequired(clonedComment)) {
       129          strategy.setupConfirmation(request);
       130          return new ConfirmCommentView();
       131        } else {
       132          try {
       133            saveComment(request, response, blogEntry, comment);
       134            ((LogFactory) request.getSession()).removeAttribute(Constants.COMMENT_KEY);
       135            return new CommentConfirmationView();
       136          } catch (BlogServiceException be) {
       137            log.error(be.getMessage(), be);
       138            throw new ServletException(be);
       139          }
       140        }
       141      }
       142    }
       143  }

SofCheck Inspector Build Version : 2.22510

savecommentaction.java | 2010-Jul-19 17:34:36
savecommentaction.class | 2010-Jul-19 20:23:38