001: /*
002: * Copyright 2002 Gareth Cronin
003: * This software is subject to the GNU Public Licence
004: */
005: package vqwiki.servlets;
006:
007: import java.io.IOException;
008: import java.util.Collection;
009: import java.util.ResourceBundle;
010:
011: import javax.servlet.RequestDispatcher;
012: import javax.servlet.ServletException;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015:
016: import vqwiki.Change;
017: import vqwiki.ChangeLog;
018: import vqwiki.SearchEngine;
019: import vqwiki.Topic;
020: import vqwiki.WikiBase;
021: import vqwiki.file.FileChangeLog;
022: import vqwiki.utils.JSPUtils;
023: import vqwiki.utils.Utilities;
024: import org.apache.log4j.Logger;
025:
026: public class SaveTopicServlet extends VQWikiServlet {
027:
028: private static final Logger logger = Logger
029: .getLogger(SaveTopicServlet.class);
030:
031: /**
032: *
033: */
034: protected void doPost(HttpServletRequest request,
035: HttpServletResponse response) throws ServletException,
036: IOException {
037: logger.debug("Request for save");
038: String virtualWiki = null;
039: String user = request.getRemoteAddr();
040: logger.debug("user: " + user);
041: if (Utilities.getUserFromRequest(request) != null) {
042: user = Utilities.getUserFromRequest(request);
043: }
044: String topic = request.getParameter("topic");
045: logger.debug("Saving topic: " + topic);
046: if (topic == null) {
047: WikiServletException err = new WikiServletException(
048: "Topic must be specified");
049: error(request, response, err);
050: return;
051: }
052: WikiBase base = null;
053: try {
054: base = WikiBase.getInstance();
055: virtualWiki = (String) request.getAttribute("virtual-wiki");
056: logger.debug("vwiki is " + virtualWiki);
057: } catch (Exception err) {
058: error(request, response, new WikiServletException(err
059: .getMessage()));
060: return;
061: }
062: ResourceBundle messages = ResourceBundle.getBundle(
063: "ApplicationResources", request.getLocale());
064: String append = messages.getString("edit.action.append");
065: if (append == null)
066: append = "append";
067: // First try the above templatelist.
068: String templatevalue = request.getParameter("templateabove");
069: String notemplate = Utilities.resource("edit.notemplate",
070: request.getLocale());
071: if (templatevalue != null) {
072: // if it is not set try the below templatelist.
073: if (templatevalue.equals(notemplate)) {
074: templatevalue = request.getParameter("templatebelow");
075: }
076: }
077: if ((templatevalue != null)
078: && append.equals(request.getParameter("action"))) {
079: logger.debug("appending template contents from template: "
080: + templatevalue);
081: if (!templatevalue.equals(notemplate)) {
082: String templateContents = null;
083: Collection templateNames = null;
084: try {
085: templateContents = base.getTemplate(virtualWiki,
086: templatevalue);
087: templateNames = base.getTemplates(virtualWiki);
088: } catch (Exception e) {
089: error(request, response, e);
090: return;
091: }
092: StringBuffer buffer = new StringBuffer();
093: buffer.append(request.getParameter("contents"));
094: buffer.append(templateContents);
095: request.setAttribute("contents", buffer.toString());
096: String title = Utilities.resource("edit", request
097: .getLocale())
098: + " " + topic;
099: request.setAttribute("title", title);
100: request.setAttribute("templateNames", templateNames);
101: request.setAttribute("topic", topic);
102: dispatch("/jsp/edit.jsp", request, response);
103: return;
104: } else {
105: Collection templateNames = null;
106: try {
107: templateNames = base.getTemplates(virtualWiki);
108: } catch (Exception e) {
109: error(request, response, e);
110: return;
111: }
112: StringBuffer buffer = new StringBuffer();
113: buffer.append(request.getParameter("contents"));
114: request.setAttribute("contents", buffer.toString());
115: String title = Utilities.resource("edit", request
116: .getLocale())
117: + " " + topic;
118: request.setAttribute("title", title);
119: request.setAttribute("templateNames", templateNames);
120: request.setAttribute("topic", topic);
121: dispatch("/jsp/edit.jsp", request, response);
122: return;
123: }
124: }
125: try {
126: Topic t = new Topic(topic);
127: if (t.isReadOnlyTopic(virtualWiki)) {
128: throw new WikiServletException(
129: WikiServletException.READ_ONLY);
130: }
131: String key = request.getSession().getId();
132: if (!base.holdsLock(virtualWiki, topic, key)) {
133: logger.debug("No lock for " + virtualWiki + "/" + topic
134: + "/" + key);
135: error(request, response, new WikiServletException(
136: WikiServletException.LOCK_TIMEOUT));
137: return;
138: }
139: String contents = request.getParameter("contents");
140: if (contents == null) {
141: error(request, response, new WikiServletException(
142: "Contents must be supplied"));
143: return;
144: }
145: base.write(virtualWiki, contents, request
146: .getParameter("convertTabs") != null, topic, user);
147: if (request.getParameter("minorEdit") == null) {
148: Change change = new Change();
149: change.setTopic(topic);
150: change.setUser(user);
151: change.setTime(new java.util.Date());
152: change.setVirtualWiki(virtualWiki);
153: ChangeLog cl = WikiBase.getInstance()
154: .getChangeLogInstance();
155: cl.logChange(change, request);
156: }
157: SearchEngine sedb = WikiBase.getInstance()
158: .getSearchEngineInstance();
159: sedb.indexText(virtualWiki, topic, request
160: .getParameter("contents"));
161: } catch (Exception err) {
162: logger.debug("Exception saving: " + err.toString());
163: if (err instanceof java.io.InvalidClassException) {
164: try {
165: ((FileChangeLog) WikiBase.getInstance()
166: .getChangeLogInstance())
167: .deleteChangeTableFile(virtualWiki);
168: } catch (Exception ignore) {
169: }
170: request
171: .setAttribute(
172: "exception",
173: new WikiServletException(
174: "Due to a change to the RecentChanges mechanism, "
175: + "this upgrade is incompatible with previous recent changes, the offending file (wikihome/recent.hashtable) should now have been deleted, "
176: + "please restart the app server"));
177: } else {
178: request.setAttribute("exception",
179: new WikiServletException(err.toString()));
180: }
181: err.printStackTrace();
182: RequestDispatcher dispatch = request
183: .getRequestDispatcher("/jsp/servlet-error.jsp");
184: dispatch.forward(request, response);
185: return;
186: }
187: try {
188: base.unlockTopic(virtualWiki, topic);
189: } catch (Exception err) {
190: request.setAttribute("exception", new WikiServletException(
191: err.toString()));
192: err.printStackTrace();
193: RequestDispatcher dispatch = request
194: .getRequestDispatcher("/jsp/servlet-error.jsp");
195: dispatch.forward(request, response);
196: return;
197: }
198: String next = null;
199: response.setLocale(request.getLocale());
200: next = JSPUtils.createRedirectURL(request, "Wiki?"
201: + JSPUtils.encodeURL(topic));
202: logger.debug("Creating redirect: " + next);
203: logger.debug("Redirect URL: "
204: + response.encodeRedirectURL(next));
205: response.sendRedirect(response.encodeRedirectURL(next));
206: }
207:
208: /**
209: *
210: */
211: protected void doGet(HttpServletRequest request,
212: HttpServletResponse response) throws ServletException,
213: IOException {
214: response.sendError(HttpServletResponse.SC_BAD_REQUEST,
215: "GET requests are not serviced in saves");
216: }
217: }
|