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