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