Beispiel #1
0
 private static void buildLayout(HttpServletRequest request, ModelAndView next) throws Exception {
   String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request);
   if (virtualWikiName == null) {
     logger.error("No virtual wiki available for page request " + request.getRequestURI());
     virtualWikiName = WikiBase.DEFAULT_VWIKI;
   }
   VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName);
   if (virtualWiki == null) {
     logger.error("No virtual wiki found for " + virtualWikiName);
     virtualWikiName = WikiBase.DEFAULT_VWIKI;
     virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName);
   }
   String topic = JAMWikiServlet.getTopicFromRequest(request);
   if (topic == null) {
     topic = JAMWikiServlet.getTopicFromURI(request);
   }
   next.addObject(PARAMETER_TOPIC, topic);
   // build the layout contents
   String leftMenu =
       JAMWikiServlet.getCachedContent(
           request, virtualWikiName, WikiBase.SPECIAL_PAGE_LEFT_MENU, true);
   next.addObject("leftMenu", leftMenu);
   next.addObject("defaultTopic", virtualWiki.getDefaultTopicName());
   next.addObject("logo", Environment.getValue(Environment.PROP_BASE_LOGO_IMAGE));
   String bottomArea =
       JAMWikiServlet.getCachedContent(
           request, virtualWikiName, WikiBase.SPECIAL_PAGE_BOTTOM_AREA, true);
   next.addObject("bottomArea", bottomArea);
   next.addObject(PARAMETER_VIRTUAL_WIKI, virtualWikiName);
 }
Beispiel #2
0
 private void history(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   pageInfo.setAction(WikiPageInfo.ACTION_HISTORY);
   pageInfo.setTopicName(topicName);
   pageInfo.setPageTitle(new WikiMessage("history.title", topicName));
   Vector changes = WikiBase.getHandler().getRecentChanges(virtualWiki, topicName, true);
   next.addObject("changes", changes);
 }
Beispiel #3
0
 /**
  * Action used when viewing a topic.
  *
  * @param request The servlet request object.
  * @param next The Spring ModelAndView object.
  * @param topicName The topic being viewed. This value must be a valid topic that can be loaded as
  *     a org.jamwiki.model.Topic object.
  */
 protected void viewTopic(HttpServletRequest request, ModelAndView next, String topicName)
     throws Exception {
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   if (!StringUtils.hasText(virtualWiki)) {
     virtualWiki = WikiBase.DEFAULT_VWIKI;
   }
   Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName);
   WikiMessage pageTitle = new WikiMessage("topic.title", topicName);
   viewTopic(request, next, pageTitle, topic, true);
 }
Beispiel #4
0
 private void jumpTo(HttpServletRequest request, HttpServletResponse response, ModelAndView next)
     throws Exception {
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String text = request.getParameter("text");
   // FIXME - if topic doesn't exist, should probably go to an edit page
   // or else give an error message
   // FIXME - need a better way to do redirects
   String redirectURL = LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWiki, text);
   redirect(redirectURL, response);
 }
Beispiel #5
0
 private void preview(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String contents = (String) request.getParameter("contents");
   Topic previewTopic = new Topic();
   previewTopic.setName(topicName);
   previewTopic.setTopicContent(contents);
   previewTopic.setVirtualWiki(virtualWiki);
   pageInfo.setAction(WikiPageInfo.ACTION_EDIT_PREVIEW);
   next.addObject("contents", contents);
   viewTopic(request, next, pageInfo, null, previewTopic, false);
 }
Beispiel #6
0
 private void resolve(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   TopicVersion version = WikiBase.getHandler().lookupLastTopicVersion(virtualWiki, topicName);
   String contents1 = version.getVersionContent();
   String contents2 = request.getParameter("contents");
   next.addObject("lastTopicVersionId", new Integer(version.getTopicVersionId()));
   next.addObject("contents", contents1);
   next.addObject("contentsResolve", contents2);
   Vector diffs = DiffUtil.diff(contents1, contents2);
   next.addObject("diffs", diffs);
   loadEdit(request, next, pageInfo, virtualWiki, topicName, false);
   pageInfo.setAction(WikiPageInfo.ACTION_EDIT_RESOLVE);
 }
Beispiel #7
0
 // FIXME - shouldn't need to pass in response
 private boolean register(
     HttpServletRequest request,
     HttpServletResponse response,
     ModelAndView next,
     WikiPageInfo pageInfo)
     throws Exception {
   pageInfo.setSpecial(true);
   pageInfo.setAction(WikiPageInfo.ACTION_REGISTER);
   pageInfo.setPageTitle(new WikiMessage("register.title"));
   String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request);
   WikiUser user = new WikiUser();
   String userIdString = request.getParameter("userId");
   if (StringUtils.hasText(userIdString)) {
     int userId = new Integer(userIdString).intValue();
     if (userId > 0) user = WikiBase.getHandler().lookupWikiUser(userId);
   }
   user.setLogin(request.getParameter("login"));
   user.setDisplayName(request.getParameter("displayName"));
   user.setEmail(request.getParameter("email"));
   String newPassword = request.getParameter("newPassword");
   if (StringUtils.hasText(newPassword)) {
     user.setEncodedPassword(Encryption.encrypt(newPassword));
   }
   // FIXME - need to distinguish between add & update
   user.setCreateIpAddress(request.getRemoteAddr());
   user.setLastLoginIpAddress(request.getRemoteAddr());
   next.addObject("newuser", user);
   Vector errors = validate(request, user);
   if (errors.size() > 0) {
     next.addObject("errors", errors);
     String oldPassword = request.getParameter("oldPassword");
     String confirmPassword = request.getParameter("confirmPassword");
     if (oldPassword != null) next.addObject("oldPassword", oldPassword);
     if (newPassword != null) next.addObject("newPassword", newPassword);
     if (confirmPassword != null) next.addObject("confirmPassword", confirmPassword);
     return false;
   } else {
     WikiBase.getHandler().writeWikiUser(user);
     request.getSession().setAttribute(JAMWikiServlet.PARAMETER_USER, user);
     VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName);
     String topic = virtualWiki.getDefaultTopicName();
     String redirect =
         LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWikiName, topic);
     // FIXME - can a redirect be done with Spring?
     redirect(redirect, response);
     return true;
   }
 }
Beispiel #8
0
 private void viewVersion(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   // display an older version
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   int topicVersionId = Integer.parseInt(request.getParameter("topicVersionId"));
   TopicVersion topicVersion =
       WikiBase.getHandler().lookupTopicVersion(virtualWiki, topicName, topicVersionId);
   if (topicVersion == null) {
     throw new WikiException(new WikiMessage("common.exception.notopic"));
   }
   Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName);
   topic.setTopicContent(topicVersion.getVersionContent());
   String versionDate = DateFormat.getDateTimeInstance().format(topicVersion.getEditDate());
   WikiMessage pageTitle = new WikiMessage("topic.title", topicName + " @" + versionDate);
   viewTopic(request, next, pageInfo, pageTitle, topic, false, false);
 }
Beispiel #9
0
 private ModelAndView loginRequired(HttpServletRequest request) throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   if (!StringUtils.hasText(topicName) || !StringUtils.hasText(virtualWiki)) {
     return null;
   }
   if (Environment.getBooleanValue(Environment.PROP_TOPIC_FORCE_USERNAME)
       && Utilities.currentUser(request) == null) {
     WikiMessage errorMessage = new WikiMessage("edit.exception.login");
     return viewLogin(request, JAMWikiServlet.getTopicFromURI(request), errorMessage);
   }
   Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName);
   if (topic != null && topic.getAdminOnly() && !Utilities.isAdmin(request)) {
     WikiMessage errorMessage = new WikiMessage("edit.exception.loginadmin", topicName);
     return viewLogin(request, JAMWikiServlet.getTopicFromURI(request), errorMessage);
   }
   return null;
 }
Beispiel #10
0
 /**
  * Action used when redirecting to a login page.
  *
  * @param request The servlet request object.
  * @param next The Spring ModelAndView object.
  * @param topic The topic to be redirected to. Valid examples are "Special:Admin",
  *     "StartingPoints", etc.
  */
 protected void viewLogin(HttpServletRequest request, ModelAndView next, String topic)
     throws Exception {
   this.pageInfo = new WikiPageInfo();
   String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request);
   String redirect = request.getParameter("redirect");
   if (!StringUtils.hasText(redirect)) {
     if (!StringUtils.hasText(topic)) {
       VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName);
       topic = virtualWiki.getDefaultTopicName();
     }
     redirect =
         LinkUtil.buildInternalLinkUrl(
             request.getContextPath(), virtualWikiName, topic, null, request.getQueryString());
   }
   next.addObject("redirect", redirect);
   this.pageInfo.setPageTitle(new WikiMessage("login.title"));
   this.pageInfo.setPageAction(JAMWikiServlet.ACTION_LOGIN);
   this.pageInfo.setSpecial(true);
 }
Beispiel #11
0
 private void edit(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   Topic topic = loadTopic(virtualWiki, topicName);
   // topic name might be updated by loadTopic
   topicName = topic.getName();
   int lastTopicVersionId = retrieveLastTopicVersionId(request, virtualWiki, topicName);
   next.addObject("lastTopicVersionId", new Integer(lastTopicVersionId));
   loadEdit(request, next, pageInfo, virtualWiki, topicName, true);
   String contents = null;
   if (isPreview(request)) {
     preview(request, next, pageInfo);
     return;
   }
   pageInfo.setAction(WikiPageInfo.ACTION_EDIT);
   if (StringUtils.hasText(request.getParameter("topicVersionId"))) {
     // editing an older version
     int topicVersionId = Integer.parseInt(request.getParameter("topicVersionId"));
     TopicVersion topicVersion =
         WikiBase.getHandler().lookupTopicVersion(topicName, topicVersionId);
     if (topicVersion == null) {
       throw new WikiException(new WikiMessage("common.exception.notopic"));
     }
     contents = topicVersion.getVersionContent();
     if (lastTopicVersionId != topicVersionId) {
       next.addObject("topicVersionId", new Integer(topicVersionId));
     }
   } else if (StringUtils.hasText(request.getParameter("section"))) {
     // editing a section of a topic
     int section = (new Integer(request.getParameter("section"))).intValue();
     ParserDocument parserDocument =
         Utilities.parseSlice(request, virtualWiki, topicName, section);
     contents = parserDocument.getContent();
   } else {
     // editing a full new or existing topic
     contents = (topic == null) ? "" : topic.getTopicContent();
   }
   next.addObject("contents", contents);
 }
Beispiel #12
0
 private void search(HttpServletRequest request, HttpServletResponse response, ModelAndView next)
     throws Exception {
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   try {
     String searchField = request.getParameter("text");
     if (request.getParameter("text") == null) {
       this.pageInfo.setPageTitle("Special:Search");
     } else {
       String message =
           Utilities.getMessage("searchresult.title", request.getLocale(), searchField);
       this.pageInfo.setPageTitle(message);
     }
     // forward back to the search page if the request is blank or null
     if (!StringUtils.hasText(searchField)) {
       this.pageInfo.setPageAction(JAMWikiServlet.ACTION_SEARCH);
       this.pageInfo.setSpecial(true);
       return;
     }
     // grab search engine instance and find
     boolean fuzzy = false;
     if (request.getParameter("fuzzy") != null) fuzzy = true;
     SearchEngine sedb = WikiBase.getSearchEngineInstance();
     Collection results = sedb.findMultiple(virtualWiki, searchField, fuzzy);
     StringBuffer contents = new StringBuffer();
     if (results != null && results.size() > 0) {
       Iterator it = results.iterator();
       while (it.hasNext()) {
         SearchResultEntry result = (SearchResultEntry) it.next();
         contents.append("<p>");
         contents.append("<div class=\"searchresult\">");
         contents.append("<a href=\"");
         contents.append(
             LinkUtil.buildInternalLinkUrl(
                 request.getContextPath(), virtualWiki, result.getTopic()));
         if (result.getFoundWord().length() > 0) {
           contents.append("?highlight=");
           contents.append(Utilities.encodeURL(result.getFoundWord()));
         }
         contents.append("\">" + result.getTopic() + "</a>");
         contents.append("</div>");
         if (result.getTextBefore().length() > 0
             || result.getTextAfter().length() > 0
             || result.getFoundWord().length() > 0) {
           contents.append("<br />");
           contents.append(result.getTextBefore());
           contents.append("<a style=\"background:yellow\" href=\"");
           contents.append(
               LinkUtil.buildInternalLinkUrl(
                   request.getContextPath(), virtualWiki, result.getTopic()));
           contents.append("?highlight=");
           contents.append(Utilities.encodeURL(result.getFoundWord()));
           contents.append("\">");
           contents.append(result.getFoundWord());
           contents.append("</a> ");
           contents.append(result.getTextAfter());
         }
         contents.append("</p>");
       }
     } else {
       next.addObject("text", searchField);
       contents.append("<p>");
       String message =
           Utilities.getMessage("searchresult.notfound", request.getLocale(), searchField);
       contents.append(message);
       contents.append("</p>");
     }
     next.addObject("results", contents.toString());
     next.addObject("titlelink", "Special:Search");
     this.pageInfo.setPageAction(JAMWikiServlet.ACTION_SEARCH_RESULTS);
     this.pageInfo.setSpecial(true);
     return;
   } catch (Exception e) {
     logger.error(e);
     throw e;
   }
 }
Beispiel #13
0
 private void save(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   Topic topic = loadTopic(virtualWiki, topicName);
   TopicVersion lastTopicVersion =
       WikiBase.getHandler().lookupLastTopicVersion(virtualWiki, topicName);
   if (lastTopicVersion != null
       && lastTopicVersion.getTopicVersionId()
           != retrieveLastTopicVersionId(request, virtualWiki, topicName)) {
     // someone else has edited the topic more recently
     resolve(request, next, pageInfo);
     return;
   }
   String contents = request.getParameter("contents");
   if (StringUtils.hasText(request.getParameter("section"))) {
     // load section of topic
     int section = (new Integer(request.getParameter("section"))).intValue();
     ParserDocument parserDocument =
         Utilities.parseSplice(request, virtualWiki, topicName, section, contents);
     contents = parserDocument.getContent();
   }
   if (contents == null) {
     logger.warning("The topic " + topicName + " has no content");
     throw new WikiException(new WikiMessage("edit.exception.nocontent", topicName));
   }
   if (lastTopicVersion != null && lastTopicVersion.getVersionContent().equals(contents)) {
     viewTopic(request, next, pageInfo, topicName);
     return;
   }
   // parse for signatures and other syntax that should not be saved in raw form
   WikiUser user = Utilities.currentUser(request);
   ParserInput parserInput = new ParserInput();
   parserInput.setContext(request.getContextPath());
   parserInput.setLocale(request.getLocale());
   parserInput.setWikiUser(user);
   parserInput.setTopicName(topicName);
   parserInput.setUserIpAddress(request.getRemoteAddr());
   parserInput.setVirtualWiki(virtualWiki);
   ParserDocument parserDocument = Utilities.parseSave(parserInput, contents);
   contents = parserDocument.getContent();
   topic.setTopicContent(contents);
   if (StringUtils.hasText(parserDocument.getRedirect())) {
     // set up a redirect
     topic.setRedirectTo(parserDocument.getRedirect());
     topic.setTopicType(Topic.TYPE_REDIRECT);
   } else if (topic.getTopicType() == Topic.TYPE_REDIRECT) {
     // no longer a redirect
     topic.setRedirectTo(null);
     topic.setTopicType(Topic.TYPE_ARTICLE);
   }
   TopicVersion topicVersion =
       new TopicVersion(
           user, request.getRemoteAddr(), request.getParameter("editComment"), contents);
   if (request.getParameter("minorEdit") != null) {
     topicVersion.setEditType(TopicVersion.EDIT_MINOR);
   }
   WikiBase.getHandler().writeTopic(topic, topicVersion, parserDocument);
   // a save request has been made
   JAMWikiServlet.removeCachedContents();
   viewTopic(request, next, pageInfo, topicName);
 }