001: package vqwiki.servlets;
002:
003: import vqwiki.Environment;
004: import vqwiki.PseudoTopicHandler;
005: import vqwiki.Topic;
006: import vqwiki.WikiBase;
007: import vqwiki.WikiException;
008: import vqwiki.utils.JSPUtils;
009: import vqwiki.utils.Utilities;
010:
011: import javax.servlet.ServletException;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014: import java.io.BufferedReader;
015: import java.io.IOException;
016: import java.io.StringReader;
017: import java.util.Collection;
018: import java.util.ResourceBundle;
019:
020: /**
021: * @author garethc
022: * Date: Jan 8, 2003
023: */
024: public class EditServlet extends VQWikiServlet {
025:
026: /**
027: *
028: */
029: protected void doGet(HttpServletRequest request,
030: HttpServletResponse response) throws ServletException,
031: IOException {
032: request.getSession().setMaxInactiveInterval(
033: 60 * Environment.getInstance().getEditTimeOut());
034: ResourceBundle messages = ResourceBundle.getBundle(
035: "ApplicationResources", request.getLocale());
036: String topic = request.getParameter("topic");
037: if (topic == null) {
038: if (request.getAttribute("topic") != null) {
039: topic = (String) request.getAttribute("topic");
040: }
041: }
042: if (PseudoTopicHandler.getInstance().isPseudoTopic(topic)) {
043: error(request, response, new WikiServletException(topic
044: + " "
045: + messages.getString("edit.exception.pseudotopic")));
046: return;
047: }
048: String virtualWiki = (String) request
049: .getAttribute("virtual-wiki");
050: try {
051: if (virtualWiki == null) {
052: virtualWiki = Utilities.extractVirtualWiki(request);
053: }
054: Topic t = new Topic(topic);
055: if (t.isReadOnlyTopic(virtualWiki)) {
056: error(request, response, new WikiServletException(
057: WikiException.READ_ONLY));
058: return;
059: }
060: if (WikiBase.getInstance().isAdminOnlyTopic(
061: request.getLocale(), virtualWiki, topic)) {
062: if (!Utilities.isAdmin(request)) {
063: request.setAttribute("title", Utilities.resource(
064: "login.title", request.getLocale()));
065: String rootPath = JSPUtils.createLocalRootPath(
066: request, virtualWiki);
067: StringBuffer buffer = new StringBuffer();
068: buffer.append(rootPath);
069: buffer.append("Wiki?" + topic);
070: request.setAttribute("redirect", buffer.toString());
071: dispatch("/jsp/login.jsp", request, response);
072: return;
073: }
074: }
075: } catch (Exception e) {
076: error(request, response, e);
077: return;
078: }
079: if (topic == null || topic.equals("")) {
080: error(request, response, new WikiServletException(messages
081: .getString("edit.exception.notopic")));
082: return;
083: }
084: WikiBase base = null;
085: try {
086: base = WikiBase.getInstance();
087: String key = request.getSession().getId();
088: if (!base.lockTopic(virtualWiki, topic, key)) {
089: error(request, response, new WikiServletException(
090: WikiException.TOPIC_LOCKED));
091: return;
092: }
093: } catch (Exception e) {
094: error(request, response, e);
095: return;
096: }
097: String contents = null;
098: String preview = null;
099: Collection templateNames = null;
100: try {
101: if (request.getAttribute(WikiServlet.ACTION_PREVIEW) != null) {
102: request.removeAttribute(WikiServlet.ACTION_PREVIEW);
103: contents = (String) request.getParameter("contents");
104: if (request.getParameter("convertTabs") != null) {
105: contents = Utilities.convertTabs(contents);
106: }
107: } else {
108: contents = base.readRaw(virtualWiki, topic);
109: }
110: templateNames = base.getTemplates(virtualWiki);
111: preview = base.cook(new BufferedReader(new StringReader(
112: contents)), virtualWiki);
113: } catch (Exception e) {
114: error(request, response, e);
115: return;
116: }
117: StringBuffer buffer = new StringBuffer();
118: buffer.append(messages.getString("edit"));
119: buffer.append(" ");
120: buffer.append(topic);
121: request.setAttribute("title", buffer.toString());
122: request.setAttribute("templateNames", templateNames);
123: request.setAttribute("contents", contents);
124: request.setAttribute("preview", preview);
125: request.setAttribute("topic", topic);
126: dispatch("/jsp/edit.jsp", request, response);
127: }
128:
129: /**
130: *
131: */
132: protected void doPost(HttpServletRequest httpServletRequest,
133: HttpServletResponse httpServletResponse)
134: throws ServletException, IOException {
135: this.doGet(httpServletRequest, httpServletResponse);
136: }
137: }
|