abstractcommentaction.java

### File Source: abstractcommentaction.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.action.AbstractCommentAction__static_init
              *
              *  Postconditions:
              *    init'ed(log)
              */
         1  package net.sourceforge.pebble.web.action;
         2
         3  import java.io.UnsupportedEncodingException;
         4  import java.net.URLDecoder;
         5  import java.net.URLEncoder;
         6
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.action.AbstractCommentAction()
              */
         7  import javax.servlet.http.Cookie;
         8  import javax.servlet.http.HttpServletRequest;
         9  import javax.servlet.http.HttpServletResponse;
        10
        11  import net.sourceforge.pebble.domain.Blog;
        12  import net.sourceforge.pebble.domain.BlogEntry;
        13  import net.sourceforge.pebble.domain.BlogService;
        14  import net.sourceforge.pebble.domain.BlogServiceException;
        15  import net.sourceforge.pebble.domain.Comment;
        16  import net.sourceforge.pebble.security.PebbleUserDetails;
        17  import net.sourceforge.pebble.util.CookieUtils;
        18  import net.sourceforge.pebble.util.MailUtils;
        19  import net.sourceforge.pebble.util.SecurityUtils;
        20  import net.sourceforge.pebble.util.StringUtils;
        21  import net.sourceforge.pebble.web.validation.ValidationContext;
        22
        23  import org.apache.commons.logging.Log;
        24  import org.apache.commons.logging.LogFactory;
        25
        26  /**
        27   * Adds a comment to an existing blog entry.
        28   *
        29   * @author    Simon Brown
        30   */
        31  public abstract class AbstractCommentAction extends Action {
        32
        33    private static final Log log = LogFactory.getLog(AbstractCommentAction.class);
        34
        35    protected Comment createComment(HttpServletRequest request, BlogEntry blogEntry) {
                 /*
        P/P       *  Method: Comment createComment(HttpServletRequest, BlogEntry)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *    request != null
                  *    (soft) net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.domain.BlogEntry:createComment(...)@43 != null
                  *    net.sourceforge.pebble.domain.BlogEntry:getBlog(...)@54 != null
                  *
                  *  Postconditions:
                  *    (soft) return_value != null
                  *
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@51: {1}, {0}
                  *    java.lang.String:length(...)@62: {0}, {1..232-1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@61: Addr_Set{null}, Inverse{null}
                  *    net.sourceforge.pebble.domain.BlogEntry:getComment(...)@64: Addr_Set{null}, Inverse{null}
                  *    user.website: Addr_Set{null}, Inverse{null}
                  */
        36      String author = StringUtils.transformHTML(request.getParameter("author"));
        37      String email = request.getParameter("email");
        38      String website = request.getParameter("website");
        39      String ipAddress = request.getRemoteAddr();
        40      String title = StringUtils.transformHTML(request.getParameter("title"));
        41      String body = request.getParameter("commentBody");
        42
        43      Comment comment = blogEntry.createComment(title, body, author, email, website, ipAddress);
        44
        45      // if the user is authenticated, overwrite the author information
        46      if (SecurityUtils.isUserAuthenticated()) {
        47        PebbleUserDetails user = SecurityUtils.getUserDetails();
        48        if (user != null) {
        49          comment.setAuthor(user.getName());
        50          comment.setEmail(user.getEmailAddress());
        51          if (user.getWebsite() != null && !user.getWebsite().equals("")) {
        52            comment.setWebsite(user.getWebsite());
        53          } else {
        54            comment.setWebsite(blogEntry.getBlog().getUrl() + "authors/" + user.getUsername() + "/");
        55          }
        56          comment.setAuthenticated(true);
        57        }
        58      }
        59
        60      // are we replying to an existing comment?
        61      String parentCommentId = request.getParameter("comment");
        62      if (parentCommentId != null && parentCommentId.length() > 0) {
        63        long parent = Long.parseLong(parentCommentId);
        64        Comment parentComment = blogEntry.getComment(parent);
        65        if (parentComment != null) {
        66          comment.setParent(parentComment);
        67        }
        68      }
        69
        70      return comment;
        71    }
        72
        73    protected Comment createBlankComment(Blog blog, BlogEntry blogEntry, HttpServletRequest request) {
                 /*
        P/P       *  Method: Comment createBlankComment(Blog, BlogEntry, HttpServletRequest)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *    request != null
                  *    (soft) blog != null
                  *    (soft) net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
                  *
                  *  Presumptions:
                  *    javax.servlet.http.HttpServletRequest:getCookies(...).length@103 <= 232-1
                  *    javax.servlet.http.HttpServletRequest:getCookies(...).length@108 <= 232-1
                  *    javax.servlet.http.HttpServletRequest:getCookies(...).length@94 <= 232-1
                  *    javax.servlet.http.HttpServletRequest:getCookies(...).length@98 <= 232-1
                  *    javax.servlet.http.HttpServletRequest:getCookies(...)[...]@103 != null
                  *    ...
                  *
                  *  Postconditions:
                  *    (soft) return_value != null
                  *
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@84: {1}, {0}
                  *    java.lang.String:length(...)@120: {0}, {1..232-1}
                  *    javax.servlet.http.HttpServletRequest:getParameter(...)@119: Addr_Set{null}, Inverse{null}
                  *    net.sourceforge.pebble.domain.BlogEntry:getComment(...)@122: Addr_Set{null}, Inverse{null}
                  *    user.website: Addr_Set{null}, Inverse{null}
                  */
        74      Comment comment = blogEntry.createComment("", "", "", "", "", request.getRemoteAddr());
        75
        76      // populate the author, email and website from one of :
        77      // - the logged in user details
        78      // - the "remember me" cookie
        79      if (SecurityUtils.isUserAuthenticated()) {
        80        PebbleUserDetails user = SecurityUtils.getUserDetails();
        81        if (user != null) {
        82          comment.setAuthor(user.getName());
        83          comment.setEmail(user.getEmailAddress());
        84          if (user.getWebsite() != null && !user.getWebsite().equals("")) {
        85            comment.setWebsite(user.getWebsite());
        86          } else {
        87            comment.setWebsite(blogEntry.getBlog().getUrl() + "authors/" + user.getUsername() + "/");
        88          }
        89          comment.setAuthenticated(true);
        90        }
        91      } else {
        92        try {
        93          // is "remember me" set?
        94          Cookie rememberMe = CookieUtils.getCookie(request.getCookies(), "rememberMe");
        95          if (rememberMe != null) {
        96            // remember me has been checked and we're not already previewing a comment
        97            // so create a new comment as this will populate the author/email/website
        98            Cookie author = CookieUtils.getCookie(request.getCookies(), "rememberMe.author");
        99            if (author != null) {
       100              comment.setAuthor(URLDecoder.decode(author.getValue(), blog.getCharacterEncoding()));
       101            }
       102
       103            Cookie email = CookieUtils.getCookie(request.getCookies(), "rememberMe.email");
       104            if (email != null) {
       105              comment.setEmail(URLDecoder.decode(email.getValue(), blog.getCharacterEncoding()));
       106            }
       107
       108            Cookie website = CookieUtils.getCookie(request.getCookies(), "rememberMe.website");
       109            if (website != null) {
       110              comment.setWebsite(URLDecoder.decode(website.getValue(), blog.getCharacterEncoding()));
       111            }
       112          }
       113        } catch (UnsupportedEncodingException e) {
       114          log.error("Exception encountered", e);
       115        }
       116      }
       117
       118      // are we replying to an existing comment?
       119      String parentCommentId = request.getParameter("comment");
       120      if (parentCommentId != null && parentCommentId.length() > 0) {
       121        long parent = Long.parseLong(parentCommentId);
       122        Comment parentComment = blogEntry.getComment(parent);
       123        if (parentComment != null) {
       124          comment.setParent(parentComment);
       125          comment.setTitle(parentComment.getTitle());
       126        }
       127      }
       128
       129      return comment;
       130    }
       131
               /*
        P/P     *  Method: ValidationContext validateComment(Comment)
                *
                *  Preconditions:
                *    this.model != null
                *    this.model.data != null
                *    (soft) comment != null
                *
                *  Postconditions:
                *    return_value == &new ValidationContext(validateComment#1)
                *    new ArrayList(ValidationContext#1) num objects == 1
                *    new ValidationContext(validateComment#1) num objects == 1
                *    return_value.errors == &new ArrayList(ValidationContext#1)
                */
       132    protected ValidationContext validateComment(Comment comment) {
       133      ValidationContext context = new ValidationContext();
       134      try {
       135        MailUtils.validate(comment.getEmail(), context);
       136      } catch (NoClassDefFoundError e) {
       137        // most likely: JavaMail is not in classpath
       138        // ignore, when we can not send email we must not validate address
       139        // this might lead to problems when mail is activated later without this
       140        // address being validated... Discussion started on mailing list, Oct-25 2008
       141      }
       142      getModel().put("validationContext", context);
       143      return context;
       144    }
       145
       146    protected void saveComment(HttpServletRequest request, HttpServletResponse response, BlogEntry blogEntry, Comment comment) throws BlogServiceException {
       147      Blog blog = blogEntry.getBlog();
       148      blogEntry.addComment(comment);
       149
       150      BlogService service = new BlogService();
       151      service.putBlogEntry(blogEntry);
       152
       153      // remember me functionality
                 /*
        P/P       *  Method: void saveComment(HttpServletRequest, HttpServletResponse, BlogEntry, Comment)
                  *    saveComment fails for all possible inputs
                  */
       154      String rememberMe = (String)request.getSession().getAttribute("rememberMe");
       155      if (rememberMe != null && rememberMe.equals("true")) {
       156        CookieUtils.addCookie(response, "rememberMe", "true", CookieUtils.ONE_MONTH);
       157        CookieUtils.addCookie(response, "rememberMe.author", encode(comment.getAuthor(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH);
       158        CookieUtils.addCookie(response, "rememberMe.email", encode(comment.getEmail(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH);
       159        CookieUtils.addCookie(response, "rememberMe.website", encode(comment.getWebsite(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH);
       160      } else {
       161        CookieUtils.removeCookie(response, "rememberMe");
       162        CookieUtils.removeCookie(response, "rememberMe.author");
       163        CookieUtils.removeCookie(response, "rememberMe.email");
       164        CookieUtils.removeCookie(response, "rememberMe.website");
       165      }
       166    }
       167
               /*
        P/P     *  Method: String encode(String, String)
                *
                *  Presumptions:
                *    org.apache.commons.logging.LogFactory:getLog(...)@33 != null
                *
                *  Postconditions:
                *    init'ed(return_value)
                *
                *  Test Vectors:
                *    s: Inverse{null}, Addr_Set{null}
                */
       168    private String encode(String s, String characterEncoding) {
       169      if (s == null) {
       170        return "";
       171      } else {
       172        try {
       173          return URLEncoder.encode(s, characterEncoding);
       174        } catch (UnsupportedEncodingException e) {
       175          log.error("Exception encountered", e);
       176          return "";
       177        }
       178      }
       179    }
       180
       181  }

SofCheck Inspector Build Version : 2.22510

abstractcommentaction.java | 2010-Jun-25 19:40:34
abstractcommentaction.class | 2010-Jul-19 20:23:38