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.authentication.WikiUserAuth;
025: import org.jamwiki.model.Role;
026: import org.jamwiki.model.Topic;
027: import org.jamwiki.model.TopicVersion;
028: import org.jamwiki.utils.Utilities;
029: import org.jamwiki.utils.WikiLogger;
030: import org.jamwiki.utils.WikiUtil;
031: import org.springframework.web.servlet.ModelAndView;
032:
033: /**
034: * Used to handle moving a topic to a new name.
035: */
036: public class MoveServlet extends JAMWikiServlet {
037:
038: private static final WikiLogger logger = WikiLogger
039: .getLogger(MoveServlet.class.getName());
040: protected static final String JSP_MOVE = "move.jsp";
041:
042: /**
043: *
044: */
045: protected ModelAndView handleJAMWikiRequest(
046: HttpServletRequest request, HttpServletResponse response,
047: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
048: WikiUserAuth user = ServletUtil.currentUser();
049: if (!user.hasRole(Role.ROLE_MOVE)) {
050: WikiMessage messageObject = new WikiMessage(
051: "login.message.move");
052: return ServletUtil.viewLogin(request, pageInfo, WikiUtil
053: .getTopicFromURI(request), messageObject);
054: }
055: if (request.getParameter("move") == null) {
056: view(request, next, pageInfo);
057: } else {
058: move(request, next, pageInfo);
059: }
060: return next;
061: }
062:
063: /**
064: *
065: */
066: private void move(HttpServletRequest request, ModelAndView next,
067: WikiPageInfo pageInfo) throws Exception {
068: String topicName = WikiUtil.getTopicFromRequest(request);
069: if (StringUtils.isBlank(topicName)) {
070: throw new WikiException(new WikiMessage(
071: "common.exception.notopic"));
072: }
073: WikiMessage pageTitle = new WikiMessage("move.title", topicName);
074: pageInfo.setPageTitle(pageTitle);
075: pageInfo.setTopicName(topicName);
076: String moveDestination = request
077: .getParameter("moveDestination");
078: if (!movePage(request, next, pageInfo, topicName,
079: moveDestination)) {
080: return;
081: }
082: if (!StringUtils.isBlank(request
083: .getParameter("moveCommentsPage"))) {
084: String moveCommentsPage = Utilities.decodeFromRequest(
085: request.getParameter("moveCommentsPage"), true);
086: String commentsDestination = WikiUtil
087: .extractCommentsLink(moveDestination);
088: if (WikiUtil.isCommentsPage(moveCommentsPage)
089: && !moveCommentsPage.equals(topicName)
090: && !commentsDestination.equals(moveDestination)) {
091: if (!movePage(request, next, pageInfo,
092: moveCommentsPage, commentsDestination)) {
093: return;
094: }
095: }
096: }
097: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
098: ServletUtil.redirect(next, virtualWiki, moveDestination);
099: }
100:
101: /**
102: *
103: */
104: private boolean movePage(HttpServletRequest request,
105: ModelAndView next, WikiPageInfo pageInfo, String moveFrom,
106: String moveDestination) throws Exception {
107: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
108: Topic fromTopic = WikiBase.getDataHandler().lookupTopic(
109: virtualWiki, moveFrom, false, null);
110: if (fromTopic == null) {
111: throw new WikiException(new WikiMessage(
112: "common.exception.notopic"));
113: }
114: if (StringUtils.isBlank(moveDestination)) {
115: pageInfo.setContentJsp(JSP_MOVE);
116: next.addObject("messageObject", new WikiMessage(
117: "move.exception.nodestination"));
118: return false;
119: }
120: WikiUserAuth user = ServletUtil.currentUser();
121: if (!ServletUtil.isMoveable(virtualWiki, moveFrom, user)) {
122: pageInfo.setContentJsp(JSP_MOVE);
123: next.addObject("messageObject", new WikiMessage(
124: "move.exception.permission", moveFrom));
125: return false;
126: }
127: if (!WikiBase.getDataHandler().canMoveTopic(fromTopic,
128: moveDestination)) {
129: pageInfo.setContentJsp(JSP_MOVE);
130: next.addObject("messageObject",
131: new WikiMessage("move.exception.destinationexists",
132: moveDestination));
133: next.addObject("moveDestination", moveDestination);
134: next.addObject("moveComment", request
135: .getParameter("moveComment"));
136: return false;
137: }
138: String moveComment = Utilities.formatMessage(
139: "move.editcomment", request.getLocale(), new String[] {
140: moveFrom, moveDestination });
141: if (!StringUtils.isBlank(request.getParameter("moveComment"))) {
142: moveComment += " (" + request.getParameter("moveComment")
143: + ")";
144: }
145: TopicVersion topicVersion = new TopicVersion(user, ServletUtil
146: .getIpAddress(request), moveComment, fromTopic
147: .getTopicContent());
148: topicVersion.setEditType(TopicVersion.EDIT_MOVE);
149: WikiBase.getDataHandler().moveTopic(fromTopic, topicVersion,
150: moveDestination, null);
151: return true;
152: }
153:
154: /**
155: *
156: */
157: private void view(HttpServletRequest request, ModelAndView next,
158: WikiPageInfo pageInfo) throws Exception {
159: String topicName = WikiUtil.getTopicFromRequest(request);
160: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
161: if (StringUtils.isBlank(topicName)) {
162: throw new WikiException(new WikiMessage(
163: "common.exception.notopic"));
164: }
165: Topic topic = WikiBase.getDataHandler().lookupTopic(
166: virtualWiki, topicName, false, null);
167: if (topic == null) {
168: throw new WikiException(new WikiMessage(
169: "common.exception.notopic"));
170: }
171: String commentsPage = WikiUtil.extractCommentsLink(topicName);
172: Topic commentsTopic = WikiBase.getDataHandler().lookupTopic(
173: virtualWiki, commentsPage, false, null);
174: if (commentsTopic != null) {
175: // add option to also move comments page
176: next.addObject("moveCommentsPage", commentsPage);
177: }
178: WikiMessage pageTitle = new WikiMessage("move.title", topicName);
179: pageInfo.setPageTitle(pageTitle);
180: pageInfo.setContentJsp(JSP_MOVE);
181: pageInfo.setTopicName(topicName);
182: }
183: }
|