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