001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020: import org.apache.commons.lang.StringUtils;
021: import org.jamwiki.WikiBase;
022: import org.jamwiki.WikiException;
023: import org.jamwiki.WikiMessage;
024: import org.jamwiki.model.Topic;
025: import org.jamwiki.model.TopicVersion;
026: import org.jamwiki.model.WikiUser;
027: import org.jamwiki.utils.Utilities;
028: import org.jamwiki.utils.WikiLogger;
029: import org.jamwiki.utils.WikiUtil;
030: import org.springframework.web.servlet.ModelAndView;
031:
032: /**
033: * Used to provide capability for deleting, undeleting, and protecting topics.
034: */
035: public class ManageServlet extends JAMWikiServlet {
036:
037: private static final WikiLogger logger = WikiLogger
038: .getLogger(ManageServlet.class.getName());
039: protected static final String JSP_ADMIN_MANAGE = "admin-manage.jsp";
040:
041: /**
042: *
043: */
044: protected ModelAndView handleJAMWikiRequest(
045: HttpServletRequest request, HttpServletResponse response,
046: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
047: if (!StringUtils.isBlank(request.getParameter("delete"))) {
048: delete(request, next, pageInfo);
049: } else if (!StringUtils.isBlank(request
050: .getParameter("undelete"))) {
051: undelete(request, next, pageInfo);
052: } else if (!StringUtils.isBlank(request
053: .getParameter("permissions"))) {
054: permissions(request, next, pageInfo);
055: } else {
056: view(request, next, pageInfo);
057: }
058: return next;
059: }
060:
061: /**
062: *
063: */
064: private void delete(HttpServletRequest request, ModelAndView next,
065: WikiPageInfo pageInfo) throws Exception {
066: String topicName = WikiUtil.getTopicFromRequest(request);
067: if (topicName == null) {
068: throw new WikiException(new WikiMessage(
069: "common.exception.notopic"));
070: }
071: deletePage(request, next, pageInfo, topicName);
072: if (!StringUtils.isBlank(request
073: .getParameter("manageCommentsPage"))) {
074: String manageCommentsPage = Utilities.decodeFromRequest(
075: request.getParameter("manageCommentsPage"), true);
076: if (WikiUtil.isCommentsPage(manageCommentsPage)
077: && !manageCommentsPage.equals(topicName)) {
078: deletePage(request, next, pageInfo, manageCommentsPage);
079: }
080: }
081: next.addObject("message", new WikiMessage(
082: "manage.message.updated", topicName));
083: view(request, next, pageInfo);
084: }
085:
086: /**
087: *
088: */
089: private void deletePage(HttpServletRequest request,
090: ModelAndView next, WikiPageInfo pageInfo, String topicName)
091: throws Exception {
092: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
093: Topic topic = WikiBase.getDataHandler().lookupTopic(
094: virtualWiki, topicName, true, null);
095: if (topic.getDeleted()) {
096: logger
097: .warning("Attempt to delete a topic that is already deleted: "
098: + virtualWiki + " / " + topicName);
099: return;
100: }
101: String contents = "";
102: topic.setTopicContent(contents);
103: WikiUser user = ServletUtil.currentUser();
104: TopicVersion topicVersion = new TopicVersion(user, ServletUtil
105: .getIpAddress(request), request
106: .getParameter("deleteComment"), contents);
107: topicVersion.setEditType(TopicVersion.EDIT_DELETE);
108: WikiBase.getDataHandler().deleteTopic(topic, topicVersion,
109: true, null);
110: }
111:
112: /**
113: *
114: */
115: private void permissions(HttpServletRequest request,
116: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
117: String topicName = WikiUtil.getTopicFromRequest(request);
118: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
119: if (topicName == null) {
120: throw new WikiException(new WikiMessage(
121: "common.exception.notopic"));
122: }
123: Topic topic = WikiBase.getDataHandler().lookupTopic(
124: virtualWiki, topicName, false, null);
125: if (topic == null) {
126: throw new WikiException(new WikiMessage(
127: "common.exception.notopic"));
128: }
129: topic.setReadOnly(request.getParameter("readOnly") != null);
130: topic.setAdminOnly(request.getParameter("adminOnly") != null);
131: WikiUser user = ServletUtil.currentUser();
132: TopicVersion topicVersion = new TopicVersion(user, ServletUtil
133: .getIpAddress(request), Utilities.formatMessage(
134: "manage.message.permissions", request.getLocale()),
135: topic.getTopicContent());
136: topicVersion.setEditType(TopicVersion.EDIT_PERMISSION);
137: WikiBase.getDataHandler().writeTopic(topic, topicVersion, null,
138: null, true, null);
139: next.addObject("message", new WikiMessage(
140: "manage.message.updated", topicName));
141: view(request, next, pageInfo);
142: }
143:
144: /**
145: *
146: */
147: private void undelete(HttpServletRequest request,
148: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
149: String topicName = WikiUtil.getTopicFromRequest(request);
150: if (topicName == null) {
151: throw new WikiException(new WikiMessage(
152: "common.exception.notopic"));
153: }
154: undeletePage(request, next, pageInfo, topicName);
155: if (!StringUtils.isBlank(request
156: .getParameter("manageCommentsPage"))) {
157: String manageCommentsPage = Utilities.decodeFromRequest(
158: request.getParameter("manageCommentsPage"), true);
159: if (WikiUtil.isCommentsPage(manageCommentsPage)
160: && !manageCommentsPage.equals(topicName)) {
161: undeletePage(request, next, pageInfo,
162: manageCommentsPage);
163: }
164: }
165: next.addObject("message", new WikiMessage(
166: "manage.message.updated", topicName));
167: view(request, next, pageInfo);
168: }
169:
170: /**
171: *
172: */
173: private void undeletePage(HttpServletRequest request,
174: ModelAndView next, WikiPageInfo pageInfo, String topicName)
175: throws Exception {
176: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
177: Topic topic = WikiBase.getDataHandler().lookupTopic(
178: virtualWiki, topicName, true, null);
179: if (!topic.getDeleted()) {
180: logger
181: .warning("Attempt to undelete a topic that is not deleted: "
182: + virtualWiki + " / " + topicName);
183: return;
184: }
185: TopicVersion previousVersion = WikiBase.getDataHandler()
186: .lookupTopicVersion(
187: topic.getCurrentVersionId().intValue(), null);
188: while (previousVersion != null
189: && previousVersion.getPreviousTopicVersionId() != null
190: && previousVersion.getEditType() == TopicVersion.EDIT_DELETE) {
191: // loop back to find the last non-delete edit
192: previousVersion = WikiBase.getDataHandler()
193: .lookupTopicVersion(
194: previousVersion.getPreviousTopicVersionId()
195: .intValue(), null);
196: }
197: String contents = previousVersion.getVersionContent();
198: topic.setTopicContent(contents);
199: WikiUser user = ServletUtil.currentUser();
200: TopicVersion topicVersion = new TopicVersion(user, ServletUtil
201: .getIpAddress(request), request
202: .getParameter("undeleteComment"), contents);
203: topicVersion.setEditType(TopicVersion.EDIT_UNDELETE);
204: WikiBase.getDataHandler().undeleteTopic(topic, topicVersion,
205: true, null);
206: }
207:
208: /**
209: *
210: */
211: private void view(HttpServletRequest request, ModelAndView next,
212: WikiPageInfo pageInfo) throws Exception {
213: String topicName = WikiUtil.getTopicFromRequest(request);
214: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
215: Topic topic = WikiBase.getDataHandler().lookupTopic(
216: virtualWiki, topicName, true, null);
217: if (topic == null) {
218: throw new WikiException(new WikiMessage(
219: "common.exception.notopic"));
220: }
221: String commentsPage = WikiUtil.extractCommentsLink(topicName);
222: if (!topicName.equals(commentsPage)) {
223: Topic commentsTopic = WikiBase.getDataHandler()
224: .lookupTopic(virtualWiki, commentsPage, true, null);
225: if (commentsTopic != null
226: && commentsTopic.getDeleted() == topic.getDeleted()) {
227: // add option to also move comments page
228: next.addObject("manageCommentsPage", commentsPage);
229: }
230: }
231: next.addObject("readOnly", new Boolean(topic.getReadOnly()));
232: next.addObject("adminOnly", new Boolean(topic.getAdminOnly()));
233: next.addObject("deleted", new Boolean(
234: topic.getDeleteDate() != null));
235: pageInfo.setTopicName(topicName);
236: pageInfo.setContentJsp(JSP_ADMIN_MANAGE);
237: pageInfo
238: .setPageTitle(new WikiMessage("manage.title", topicName));
239: }
240: }
|