| 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 | |
|
| 33 | |
package net.sourceforge.pebble.web.action; |
| 34 | |
|
| 35 | |
import java.io.UnsupportedEncodingException; |
| 36 | |
import java.net.URLDecoder; |
| 37 | |
import java.net.URLEncoder; |
| 38 | |
|
| 39 | |
import javax.servlet.http.Cookie; |
| 40 | |
import javax.servlet.http.HttpServletRequest; |
| 41 | |
import javax.servlet.http.HttpServletResponse; |
| 42 | |
|
| 43 | |
import net.sourceforge.pebble.domain.Blog; |
| 44 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 45 | |
import net.sourceforge.pebble.domain.BlogService; |
| 46 | |
import net.sourceforge.pebble.domain.BlogServiceException; |
| 47 | |
import net.sourceforge.pebble.domain.Comment; |
| 48 | |
import net.sourceforge.pebble.security.PebbleUserDetails; |
| 49 | |
import net.sourceforge.pebble.util.CookieUtils; |
| 50 | |
import net.sourceforge.pebble.util.MailUtils; |
| 51 | |
import net.sourceforge.pebble.util.SecurityUtils; |
| 52 | |
import net.sourceforge.pebble.util.StringUtils; |
| 53 | |
import net.sourceforge.pebble.web.validation.ValidationContext; |
| 54 | |
|
| 55 | |
import org.apache.commons.logging.Log; |
| 56 | |
import org.apache.commons.logging.LogFactory; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | 40 | public abstract class AbstractCommentAction extends Action { |
| 64 | |
|
| 65 | 4 | private static final Log log = LogFactory.getLog(AbstractCommentAction.class); |
| 66 | |
|
| 67 | |
protected Comment createComment(HttpServletRequest request, BlogEntry blogEntry) { |
| 68 | 16 | String author = StringUtils.transformHTML(request.getParameter("author")); |
| 69 | 16 | String email = request.getParameter("email"); |
| 70 | 16 | String website = request.getParameter("website"); |
| 71 | 16 | String avatar = request.getParameter("avatar"); |
| 72 | 16 | String ipAddress = request.getRemoteAddr(); |
| 73 | 16 | String title = StringUtils.transformHTML(request.getParameter("title")); |
| 74 | 16 | String body = request.getParameter("commentBody"); |
| 75 | |
|
| 76 | 16 | Comment comment = blogEntry.createComment(title, body, author, email, website, avatar, ipAddress); |
| 77 | |
|
| 78 | |
|
| 79 | 16 | if (SecurityUtils.isUserAuthenticated()) { |
| 80 | 16 | PebbleUserDetails user = SecurityUtils.getUserDetails(); |
| 81 | 16 | if (user != null) { |
| 82 | 0 | comment.setAuthor(user.getName()); |
| 83 | 0 | comment.setEmail(user.getEmailAddress()); |
| 84 | 0 | if (user.getWebsite() != null && !user.getWebsite().equals("")) { |
| 85 | 0 | comment.setWebsite(user.getWebsite()); |
| 86 | |
} else { |
| 87 | 0 | comment.setWebsite(blogEntry.getBlog().getUrl() + "authors/" + user.getUsername() + "/"); |
| 88 | |
} |
| 89 | 0 | comment.setAuthenticated(true); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | 16 | String parentCommentId = request.getParameter("comment"); |
| 95 | 16 | if (parentCommentId != null && parentCommentId.length() > 0) { |
| 96 | 8 | long parent = Long.parseLong(parentCommentId); |
| 97 | 8 | Comment parentComment = blogEntry.getComment(parent); |
| 98 | 8 | if (parentComment != null) { |
| 99 | 4 | comment.setParent(parentComment); |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | 16 | return comment; |
| 104 | |
} |
| 105 | |
|
| 106 | |
protected Comment createBlankComment(Blog blog, BlogEntry blogEntry, HttpServletRequest request) { |
| 107 | 8 | Comment comment = blogEntry.createComment("", "", "", "", "", "", request.getRemoteAddr()); |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 8 | if (SecurityUtils.isUserAuthenticated()) { |
| 113 | 4 | PebbleUserDetails user = SecurityUtils.getUserDetails(); |
| 114 | 4 | if (user != null) { |
| 115 | 0 | comment.setAuthor(user.getName()); |
| 116 | 0 | comment.setEmail(user.getEmailAddress()); |
| 117 | 0 | if (user.getWebsite() != null && !user.getWebsite().equals("")) { |
| 118 | 0 | comment.setWebsite(user.getWebsite()); |
| 119 | |
} else { |
| 120 | 0 | comment.setWebsite(blogEntry.getBlog().getUrl() + "authors/" + user.getUsername() + "/"); |
| 121 | |
} |
| 122 | 0 | comment.setAuthenticated(true); |
| 123 | |
} |
| 124 | 4 | } else { |
| 125 | |
try { |
| 126 | |
|
| 127 | 4 | Cookie rememberMe = CookieUtils.getCookie(request.getCookies(), "rememberMe"); |
| 128 | 4 | if (rememberMe != null) { |
| 129 | |
|
| 130 | |
|
| 131 | 0 | Cookie author = CookieUtils.getCookie(request.getCookies(), "rememberMe.author"); |
| 132 | 0 | if (author != null) { |
| 133 | 0 | comment.setAuthor(URLDecoder.decode(author.getValue(), blog.getCharacterEncoding())); |
| 134 | |
} |
| 135 | |
|
| 136 | 0 | Cookie email = CookieUtils.getCookie(request.getCookies(), "rememberMe.email"); |
| 137 | 0 | if (email != null) { |
| 138 | 0 | comment.setEmail(URLDecoder.decode(email.getValue(), blog.getCharacterEncoding())); |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | Cookie website = CookieUtils.getCookie(request.getCookies(), "rememberMe.website"); |
| 142 | 0 | if (website != null) { |
| 143 | 0 | comment.setWebsite(URLDecoder.decode(website.getValue(), blog.getCharacterEncoding())); |
| 144 | |
} |
| 145 | |
} |
| 146 | 0 | } catch (UnsupportedEncodingException e) { |
| 147 | 0 | log.error("Exception encountered", e); |
| 148 | 4 | } |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | 8 | String parentCommentId = request.getParameter("comment"); |
| 153 | 8 | if (parentCommentId != null && parentCommentId.length() > 0) { |
| 154 | 0 | long parent = Long.parseLong(parentCommentId); |
| 155 | 0 | Comment parentComment = blogEntry.getComment(parent); |
| 156 | 0 | if (parentComment != null) { |
| 157 | 0 | comment.setParent(parentComment); |
| 158 | 0 | comment.setTitle(parentComment.getTitle()); |
| 159 | |
} |
| 160 | |
} |
| 161 | |
|
| 162 | 8 | return comment; |
| 163 | |
} |
| 164 | |
|
| 165 | |
protected ValidationContext validateComment(Comment comment) { |
| 166 | 16 | ValidationContext context = new ValidationContext(); |
| 167 | |
try { |
| 168 | 16 | MailUtils.validate(comment.getEmail(), context); |
| 169 | 0 | } catch (NoClassDefFoundError e) { |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | 16 | } |
| 175 | 16 | getModel().put("validationContext", context); |
| 176 | 16 | return context; |
| 177 | |
} |
| 178 | |
|
| 179 | |
protected void saveComment(HttpServletRequest request, HttpServletResponse response, BlogEntry blogEntry, Comment comment) throws BlogServiceException { |
| 180 | 12 | Blog blog = blogEntry.getBlog(); |
| 181 | 12 | blogEntry.addComment(comment); |
| 182 | |
|
| 183 | 12 | BlogService service = new BlogService(); |
| 184 | 12 | service.putBlogEntry(blogEntry); |
| 185 | |
|
| 186 | |
|
| 187 | 12 | String rememberMe = (String)request.getSession().getAttribute("rememberMe"); |
| 188 | 12 | if (rememberMe != null && rememberMe.equals("true")) { |
| 189 | 0 | CookieUtils.addCookie(response, "rememberMe", "true", CookieUtils.ONE_MONTH); |
| 190 | 0 | CookieUtils.addCookie(response, "rememberMe.author", encode(comment.getAuthor(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH); |
| 191 | 0 | CookieUtils.addCookie(response, "rememberMe.email", encode(comment.getEmail(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH); |
| 192 | 0 | CookieUtils.addCookie(response, "rememberMe.website", encode(comment.getWebsite(), blog.getCharacterEncoding()), CookieUtils.ONE_MONTH); |
| 193 | |
} else { |
| 194 | 12 | CookieUtils.removeCookie(response, "rememberMe"); |
| 195 | 12 | CookieUtils.removeCookie(response, "rememberMe.author"); |
| 196 | 12 | CookieUtils.removeCookie(response, "rememberMe.email"); |
| 197 | 12 | CookieUtils.removeCookie(response, "rememberMe.website"); |
| 198 | |
} |
| 199 | 12 | } |
| 200 | |
|
| 201 | |
private String encode(String s, String characterEncoding) { |
| 202 | 0 | if (s == null) { |
| 203 | 0 | return ""; |
| 204 | |
} else { |
| 205 | |
try { |
| 206 | 0 | return URLEncoder.encode(s, characterEncoding); |
| 207 | 0 | } catch (UnsupportedEncodingException e) { |
| 208 | 0 | log.error("Exception encountered", e); |
| 209 | 0 | return ""; |
| 210 | |
} |
| 211 | |
} |
| 212 | |
} |
| 213 | |
|
| 214 | |
} |