1 package net.sourceforge.blogentis.modules.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.Date;
26
27 import net.sourceforge.blogentis.om.Blog;
28 import net.sourceforge.blogentis.om.Comment;
29 import net.sourceforge.blogentis.om.Post;
30 import net.sourceforge.blogentis.plugins.BlogPluginService;
31 import net.sourceforge.blogentis.plugins.base.ICommentExtensionPoint;
32 import net.sourceforge.blogentis.turbine.BlogParameterParser;
33 import net.sourceforge.blogentis.turbine.BlogRunData;
34 import net.sourceforge.blogentis.utils.BlogPropertyCache;
35 import net.sourceforge.blogentis.utils.MappedConfiguration;
36 import net.sourceforge.blogentis.utils.StringUtils;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.turbine.modules.actions.VelocityAction;
41 import org.apache.turbine.util.RunData;
42 import org.apache.velocity.context.Context;
43
44 public class CommentAdd
45 extends VelocityAction {
46 private static Log log = LogFactory.getLog(CommentAdd.class);
47
48 public void doAdd(RunData data, Context context)
49 throws Exception {
50 BlogParameterParser bpp = (BlogParameterParser)(data.getParameters());
51 Post post = bpp.getPost();
52
53 Comment c = new Comment();
54 c.setPostedTime(new Date());
55 c.setPost(post);
56 c.setName(StringUtils.escapeHTML(bpp.getString("author",
57 "Anonymous Coward")));
58 c.setText(bpp.getString("comment", ""));
59 c.setUrl(StringUtils.escapeHTML(bpp.getString("url", "")));
60 if (c.getText().length() == 0)
61 c.setText(null);
62 if (c.getUrl().length() == 0)
63 c.setUrl(null);
64 if (c.getText() == null || c.getText().equals("")) {
65 data.setMessage("Please don't leave the comment field empty.");
66 return;
67 }
68
69 ICommentExtensionPoint cext = (ICommentExtensionPoint)BlogPluginService
70 .locateExtensionPoint(bpp.getBlog(), ICommentExtensionPoint.class);
71 if (cext == null || cext.commentCreated((BlogRunData)data, c))
72 c.save();
73
74 if (bpp.getBoolean("save", false)) {
75 int age = 60 * 60 * 24 * 365;
76 data.getCookies().set("bauth", c.getName(), age);
77 data.getCookies().set("burl", c.getUrl(), age);
78 }
79 }
80
81 protected boolean isAuthorized(RunData data)
82 throws Exception {
83 BlogParameterParser bpp = (BlogParameterParser)(data.getParameters());
84 Blog blog = bpp.getBlog();
85 if (blog == null)
86 return false;
87 Post post = bpp.getPost();
88 if (post == null || post.getBlogId() != blog.getBlogId())
89 return false;
90
91 return new MappedConfiguration(post).getBoolean("commentsDisabled",
92 false)
93 || BlogPropertyCache.getInstance().getBlogProperties(blog)
94 .isAllowComments();
95 }
96
97 public void doPerform(RunData data, Context context)
98 throws Exception {
99 log.debug("Unknown action requested");
100 data.setMessage("Unknown action requested");
101 }
102 }