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
34 package info.magnolia.module.forum.frontend;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.security.SecurityUtil;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.link.LinkUtil;
40 import info.magnolia.module.admininterface.PageMVCHandler;
41 import info.magnolia.module.forum.ForumManager;
42
43 import java.io.IOException;
44
45 import javax.jcr.RepositoryException;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.lang.StringUtils;
50
51
52
53
54
55
56
57 public class ForumController extends PageMVCHandler {
58 private final ForumManager forumManager;
59
60
61 protected ForumController(String name, HttpServletRequest request, HttpServletResponse response, ForumManager forumManager) {
62 super(name, request, response);
63 this.forumManager = forumManager;
64 }
65
66 public ForumController(String name, HttpServletRequest request, HttpServletResponse response) {
67 this(name, request, response, ForumManager.Factory.getInstance());
68 }
69
70 @Override
71 public String getCommand() {
72 return mandatoryParam("command");
73 }
74
75
76
77
78 @Override
79 public String execute(String command) {
80
81
82 return super.execute(command);
83 }
84
85
86
87
88
89
90 @Override
91 public void renderHtml(final String view) throws IOException {
92 final String targetUrl;
93 if (view == null) {
94 targetUrl = request.getHeader("referer");
95 } else {
96 targetUrl = view;
97 }
98
99 mandatory(targetUrl, "referer or target view");
100 response.sendRedirect(targetUrl);
101 }
102
103 @Override
104 public void init() {
105
106 }
107
108
109
110
111 public String createThread() throws RepositoryException {
112 final String forumId = mandatoryParam("forumId");
113 final String threadTitle = mandatoryParam("threadTitle");
114 final String messageText = mandatoryParam("messageText");
115 Content newThread;
116
117 if (!SecurityUtil.isAnonymous()) {
118 final String userId = getUserId();
119 newThread = forumManager.createThread(forumId, threadTitle, messageText, userId, false);
120 } else {
121 final String anonymousUsername = mandatoryParam("anonymousUsername");
122 newThread = forumManager.createThread(forumId, threadTitle, messageText, anonymousUsername, true);
123 }
124
125
126
127 final String targetPageUUID = mandatoryParam("targetPage");
128 final String targetPage = LinkUtil.createAbsoluteLink("website", targetPageUUID);
129 return targetPage + "?threadId=" + newThread.getUUID() + "&forumId=" + forumId;
130 }
131
132 public String replyToThread() throws RepositoryException {
133 final String threadId = mandatoryParam("threadId");
134 final String messageTitle = param("messageTitle");
135 final String messageText = mandatoryParam("messageText");
136 final String inReplyTo = param("inReplyTo");
137
138 if (!SecurityUtil.isAnonymous()) {
139 final String userId = getUserId();
140 forumManager.replyToThread(threadId, inReplyTo, messageTitle, messageText, userId, false);
141 } else {
142 final String anonymousUsername = mandatoryParam("anonymousUsername");
143 forumManager.replyToThread(threadId, inReplyTo, messageTitle, messageText, anonymousUsername, true);
144 }
145
146 final String targetPageUUID = param("targetPage");
147 if (StringUtils.isNotEmpty(targetPageUUID)) {
148 final String targetPage = LinkUtil.createAbsoluteLink("website", targetPageUUID);
149 return targetPage + "?threadId=" + threadId;
150 } else {
151 return null;
152 }
153
154 }
155
156
157 protected String getUserId() {
158 return MgnlContext.getUser().getName();
159 }
160
161 private String param(String paramName) {
162 final String value = request.getParameter(paramName);
163 return StringUtils.stripToNull(value);
164 }
165
166 private String mandatoryParam(String paramName) {
167 final String value = param(paramName);
168 mandatory(value, paramName);
169 return value;
170 }
171
172 private void mandatory(String s, String desc) {
173 if (StringUtils.isEmpty(s)) {
174 throw new RuntimeException(desc + " is mandatory and is missing from the request.");
175 }
176 }
177 }