Ejemplo n.º 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);
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
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;
 }
Ejemplo n.º 7
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);
 }
Ejemplo n.º 8
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);
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
0
 protected static boolean isTopic(HttpServletRequest request, String value) {
   try {
     String topic = JAMWikiServlet.getTopicFromURI(request);
     if (!StringUtils.hasText(topic)) {
       return false;
     }
     if (value != null && topic.equals(value)) {
       return true;
     }
   } catch (Exception e) {
   }
   return false;
 }
Ejemplo n.º 11
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;
   }
 }
Ejemplo n.º 12
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);
 }
Ejemplo n.º 13
0
 protected void loadDefaults(
     HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) {
   // load cached top area, nav bar, etc.
   try {
     this.buildLayout(request, next);
   } catch (Exception e) {
     logger.error("Unable to build default page layout", e);
   }
   // add link to user page and comments page
   WikiUser user = Utilities.currentUser(request);
   if (user != null) {
     next.addObject("userpage", WikiBase.NAMESPACE_USER + user.getLogin());
     next.addObject("usercomments", WikiBase.NAMESPACE_USER_COMMENTS + user.getLogin());
     next.addObject("adminUser", new Boolean(user.getAdmin()));
   }
   if (!this.pageInfo.getSpecial()) {
     // FIXME - this is really ugly
     String article = this.pageInfo.getTopicName();
     String comments = WikiBase.NAMESPACE_COMMENTS + article;
     if (article != null && article.startsWith(WikiBase.NAMESPACE_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_COMMENTS.length();
       article = article.substring(pos);
       comments = WikiBase.NAMESPACE_COMMENTS + article;
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_SPECIAL)) {
       int pos = WikiBase.NAMESPACE_SPECIAL.length();
       article = article.substring(pos);
       comments = WikiBase.NAMESPACE_COMMENTS + article;
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_USER_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_USER_COMMENTS.length();
       comments = article;
       article = WikiBase.NAMESPACE_USER + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_USER)) {
       int pos = WikiBase.NAMESPACE_USER.length();
       comments = WikiBase.NAMESPACE_USER_COMMENTS + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_IMAGE_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_IMAGE_COMMENTS.length();
       comments = article;
       article = WikiBase.NAMESPACE_IMAGE + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_IMAGE)) {
       int pos = WikiBase.NAMESPACE_IMAGE.length();
       comments = WikiBase.NAMESPACE_IMAGE_COMMENTS + article.substring(pos);
     }
     next.addObject("article", article);
     next.addObject("comments", comments);
     String editLink = "Special:Edit?topic=" + Utilities.encodeURL(this.pageInfo.getTopicName());
     if (StringUtils.hasText(request.getParameter("topicVersionId"))) {
       editLink += "&topicVersionId=" + request.getParameter("topicVersionId");
     }
     next.addObject("edit", editLink);
   }
   next.addObject(JAMWikiServlet.PARAMETER_TOPIC, this.pageInfo.getTopicName());
   if (!StringUtils.hasText(this.pageInfo.getTopicName())) {
     try {
       next.addObject(JAMWikiServlet.PARAMETER_TOPIC, JAMWikiServlet.getTopicFromURI(request));
     } catch (Exception e) {
       logger.error("Unable to load topic value in JAMWikiServlet", e);
     }
   }
   next.addObject(JAMWikiServlet.PARAMETER_ADMIN, new Boolean(this.pageInfo.getAdmin()));
   next.addObject(JAMWikiServlet.PARAMETER_SPECIAL, new Boolean(this.pageInfo.getSpecial()));
   next.addObject(JAMWikiServlet.PARAMETER_TITLE, this.pageInfo.getPageTitle());
   next.addObject(JAMWikiServlet.PARAMETER_ACTION, this.pageInfo.getPageAction());
   // reset pageInfo object - seems not to reset with each servlet call
   this.pageInfo = new WikiPageInfo();
 }
Ejemplo n.º 14
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;
   }
 }
Ejemplo n.º 15
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);
 }