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); }
/** * Utility method used when redirecting to a login page. * * @param request The servlet request object. * @param pageInfo The current WikiPageInfo object, which contains information needed for * rendering the final JSP page. * @param topic The topic to be redirected to. Valid examples are "Special:Admin", * "StartingPoints", etc. * @param messageObject A WikiMessage object to be displayed on the login page. * @return Returns a ModelAndView object corresponding to the login page display. * @throws Exception Thrown if any error occurs during processing. */ protected static ModelAndView viewLogin( HttpServletRequest request, WikiPageInfo pageInfo, String topic, WikiMessage messageObject) throws Exception { ModelAndView next = new ModelAndView("wiki"); pageInfo.reset(); String virtualWikiName = WikiUtil.getVirtualWikiFromURI(request); String target = request.getParameter("target"); if (!StringUtils.hasText(target)) { if (!StringUtils.hasText(topic)) { VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); topic = virtualWiki.getDefaultTopicName(); } target = topic; if (StringUtils.hasText(request.getQueryString())) { target += "?" + request.getQueryString(); } } next.addObject("target", target); pageInfo.setPageTitle(new WikiMessage("login.title")); pageInfo.setContentJsp(JSP_LOGIN); pageInfo.setSpecial(true); if (messageObject != null) { next.addObject("messageObject", messageObject); } return next; }
private void view( HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo) throws Exception { String topicName = WikiUtil.getTopicFromURI(request); if (StringUtils.isBlank(topicName)) { String virtualWikiName = pageInfo.getVirtualWikiName(); VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); topicName = virtualWiki.getRootTopicName(); } String virtualWiki = pageInfo.getVirtualWikiName(); if (StringUtils.isBlank(virtualWiki)) { virtualWiki = VirtualWiki.defaultVirtualWiki().getName(); } Topic topic = ServletUtil.initializeTopic(virtualWiki, topicName); if (topic.getTopicId() <= 0) { // topic does not exist, return 404 and display empty page response.setStatus(HttpServletResponse.SC_NOT_FOUND); WikiMessage wikiMessage = new WikiMessage("topic.notcreated"); // topic name is escaped from WikiUtil.getTopicFromURI, so do not double-escape wikiMessage.setParamsWithoutEscaping(new String[] {topicName}); next.addObject("notopic", wikiMessage); } WikiMessage pageTitle = new WikiMessage("topic.title", topicName); ServletUtil.viewTopic(request, next, pageInfo, pageTitle, topic, true, true); }
private void writeSiteInfo(Writer writer, String virtualWikiName) throws DataAccessException, IOException { VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); writer.append("\n<siteinfo>"); String sitename = virtualWiki.getSiteName(); writer.append('\n'); XMLUtil.buildTag(writer, "sitename", sitename, true); String base = this.retrieveBaseUrl(); writer.append('\n'); XMLUtil.buildTag(writer, "base", base, true); String generator = "JAMWiki " + WikiVersion.CURRENT_WIKI_VERSION; writer.append('\n'); XMLUtil.buildTag(writer, "generator", generator, true); /* Cannot have two titles differing only by case of first letter. Default behavior through 1.5, $wgCapitalLinks = true <enumeration value="first-letter" /> Complete title is case-sensitive. Behavior when $wgCapitalLinks = false <enumeration value="case-sensitive" /> Cannot have two titles differing only by case. Not yet implemented as of MediaWiki 1.5 <enumeration value="case-insensitive" /> */ writer.append('\n'); XMLUtil.buildTag(writer, "case", "case-sensitive", true); writer.append("\n<namespaces>"); Map<String, String> attributes = new HashMap<String, String>(); List<Namespace> namespaces = WikiBase.getDataHandler().lookupNamespaces(); for (Namespace namespace : namespaces) { attributes.put("key", Integer.toString(namespace.getId())); writer.append('\n'); XMLUtil.buildTag(writer, "namespace", namespace.getLabel(virtualWikiName), attributes, true); } writer.append("\n</namespaces>"); writer.append("\n</siteinfo>"); }
/** * Return the URL of the index page for the wiki. * * @throws DataAccessException Thrown if any error occurs while retrieving data. */ public static String getBaseUrl() throws DataAccessException { VirtualWiki virtualWiki = VirtualWiki.defaultVirtualWiki(); String url = Environment.getValue(Environment.PROP_SERVER_URL); url += LinkUtil.buildTopicUrl( WEBAPP_CONTEXT_PATH, virtualWiki.getName(), virtualWiki.getRootTopicName(), true); return url; }
/** * Return the URL of the index page for the wiki. * * @throws DataAccessException Thrown if any error occurs while retrieving data. */ private String retrieveBaseUrl() throws DataAccessException { VirtualWiki virtualWiki = VirtualWiki.defaultVirtualWiki(); String url = Environment.getValue(Environment.PROP_SERVER_URL); WikiLink wikiLink = LinkUtil.parseWikiLink( WikiUtil.WEBAPP_CONTEXT_PATH, virtualWiki.getName(), virtualWiki.getRootTopicName()); url += LinkUtil.buildTopicUrl(wikiLink); return url; }
/** * Set up images separately - one image is created in both virtual wikis, the second image is set * up in only the shared virtual wiki. */ private void setupImage(VirtualWiki virtualWiki, Topic topic) throws DataAccessException, IOException, WikiException { if (!topic.getName().toLowerCase().startsWith("image:")) { throw new IllegalArgumentException( "Cannot call JAMWikiUtilTest.setupImage for non-image topics"); } TopicVersion topicVersion = new TopicVersion( null, "127.0.0.1", null, topic.getTopicContent(), topic.getTopicContent().length()); topic.setTopicType(TopicType.IMAGE); topicVersion.setEditType(TopicVersion.EDIT_UPLOAD); // hard code image details - Image:Test Image.jpg will be created for both the "en" // and "test" virtual wikis, while Image:Test Image2.jpg will be created only for // the "test" virtual wiki. WikiFileVersion wikiFileVersion = new WikiFileVersion(); if (topic.getName().equals("Image:Test Image.jpg") && virtualWiki.getName().equals("en")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image.jpg", "/test_image.jpg", "image/jpeg", 61136, null); } else if (topic.getName().equals("Image:Test Image.jpg") && virtualWiki.getName().equals("test")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image_shared.jpg", "/test_image_shared.jpg", "image/jpeg", 61136, null); } else if (topic.getName().equals("Image:Test Image2.jpg") && virtualWiki.getName().equals("test")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image2_shared.jpg", "/test_image2_shared.jpg", "image/jpeg", 61136, null); } }
/** * Determine the URL for the default virtual wiki topic, not including the application server * context. */ public static String findDefaultVirtualWikiUrl(String virtualWikiName) { VirtualWiki virtualWiki = VirtualWiki.defaultVirtualWiki(); if (!StringUtils.isBlank(virtualWikiName)) { try { virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); } catch (DataAccessException e) { logger.warn("Unable to retrieve default topic for virtual wiki", e); } } return "/" + virtualWiki.getName() + "/" + virtualWiki.getRootTopicName(); }
/** * Refresh the current search index by re-visiting all topic pages. * * @throws Exception Thrown if any error occurs while re-indexing the Wiki. */ public void refreshIndex() throws Exception { List<VirtualWiki> allWikis = WikiBase.getDataHandler().getVirtualWikiList(); Topic topic; for (VirtualWiki virtualWiki : allWikis) { long start = System.currentTimeMillis(); int count = 0; IndexWriter writer = null; try { writer = this.retrieveIndexWriter(virtualWiki.getName(), true); List<String> topicNames = WikiBase.getDataHandler().getAllTopicNames(virtualWiki.getName(), false); // FIXME - parsing all documents will be intolerably slow with even a // moderately large Wiki for (String topicName : topicNames) { topic = WikiBase.getDataHandler().lookupTopic(virtualWiki.getName(), topicName, false); if (topic == null) { logger.info("Unable to rebuild search index for topic: " + topicName); continue; } // note: no delete is necessary since a new index is being created this.addToIndex(writer, topic); count++; } } catch (Exception ex) { logger.error("Failure while refreshing search index", ex); } finally { try { if (writer != null) { writer.optimize(); } } catch (Exception e) { logger.error("Exception during optimize", e); } try { if (writer != null) { writer.close(); } } catch (Exception e) { logger.error("Exception during close", e); } } if (logger.isInfoEnabled()) { logger.info( "Rebuilt search index for " + virtualWiki.getName() + " (" + count + " documents) in " + ((System.currentTimeMillis() - start) / 1000.000) + " seconds"); } } }
/** * Initialize a test HSQL database for JAMWiki including two virtual wikis and a default user * account. */ private void setupDatabase() throws Exception { WikiDatabase.setupDefaultDatabase(Environment.getInstance()); Locale locale = new Locale("en-US"); String username = "******"; String password = "******"; WikiUser wikiUser = new WikiUser(username); WikiBase.reset(locale, wikiUser, username, password); // set up a second "test" virtual wiki VirtualWiki virtualWiki = new VirtualWiki("test"); virtualWiki.setRootTopicName("StartingPoints"); WikiBase.getDataHandler().writeVirtualWiki(virtualWiki); WikiBase.getDataHandler().setupSpecialPages(locale, wikiUser, virtualWiki); }
// 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; } }
private static void setupSpecialPages(Locale locale, WikiUser user, Connection conn) throws Exception { Collection all = WikiBase.getDataHandler().getVirtualWikiList(conn); for (Iterator iterator = all.iterator(); iterator.hasNext(); ) { VirtualWiki virtualWiki = (VirtualWiki) iterator.next(); // create the default topics setupSpecialPage( locale, virtualWiki.getName(), WikiBase.SPECIAL_PAGE_STARTING_POINTS, user, false, conn); setupSpecialPage( locale, virtualWiki.getName(), WikiBase.SPECIAL_PAGE_LEFT_MENU, user, true, conn); setupSpecialPage( locale, virtualWiki.getName(), WikiBase.SPECIAL_PAGE_BOTTOM_AREA, user, true, conn); setupSpecialPage( locale, virtualWiki.getName(), WikiBase.SPECIAL_PAGE_STYLESHEET, user, true, conn); } }
/** * 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); }
/** * Given a virtual wiki name, return a <code>VirtualWiki</code> object. If there is no virtual * wiki available with the given name then the default virtual wiki is returned. * * @param virtualWikiName The name of the virtual wiki that is being retrieved. * @return A <code>VirtualWiki</code> object. If there is no virtual wiki available with the given * name then the default virtual wiki is returned. */ public static VirtualWiki retrieveVirtualWiki(String virtualWikiName) { VirtualWiki virtualWiki = null; if (virtualWikiName == null) { virtualWikiName = WikiBase.DEFAULT_VWIKI; } // FIXME - the check here for initialized properties is due to this // change being made late in a release cycle. Revisit in a future // release & clean this up. if (Environment.getBooleanValue(Environment.PROP_BASE_INITIALIZED)) { try { virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); } catch (Exception e) { } } if (virtualWiki == null) { logger.severe("No virtual wiki found for " + virtualWikiName); virtualWiki.setName(WikiBase.DEFAULT_VWIKI); virtualWiki.setDefaultTopicName(Environment.getValue(Environment.PROP_BASE_DEFAULT_TOPIC)); } return virtualWiki; }
private void addVirtualWiki(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception { WikiUser user = Utilities.currentUser(request); try { VirtualWiki virtualWiki = new VirtualWiki(); if (StringUtils.hasText(request.getParameter("virtualWikiId"))) { virtualWiki.setVirtualWikiId(new Integer(request.getParameter("virtualWikiId")).intValue()); } virtualWiki.setName(request.getParameter("name")); virtualWiki.setDefaultTopicName( Utilities.encodeForURL(request.getParameter("defaultTopicName"))); WikiBase.getDataHandler().writeVirtualWiki(virtualWiki, null); if (!StringUtils.hasText(request.getParameter("virtualWikiId"))) { WikiBase.getDataHandler().setupSpecialPages(request.getLocale(), user, virtualWiki, null); } next.addObject("message", new WikiMessage("admin.message.virtualwikiadded")); } catch (Exception e) { logger.severe("Failure while adding virtual wiki", e); next.addObject("message", new WikiMessage("admin.message.virtualwikifail", e.getMessage())); } view(request, next, pageInfo, null); }
/** Crate a test topic. */ protected Topic setupTopic(VirtualWiki virtualWiki, String topicName, String contents) throws DataAccessException, IOException, WikiException { if (virtualWiki == null) { virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki("en"); } Topic topic = new Topic(virtualWiki.getName(), topicName); topic.setTopicContent(contents); if (topicName.toLowerCase().startsWith("image:")) { this.setupImage(virtualWiki, topic); return topic; } this.setupTopic(topic); return topic; }
/** * Allow subclasses to modify the redirection message. * * @param request the request * @param response the response * @param url the URL to redirect to * @throws IOException in the event of any failure */ protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { String targetUrl = url; if ("/DEFAULT_VIRTUAL_WIKI".equals(url)) { // ugly, but a hard-coded constant seems to be the only way to // allow a dynamic url value String virtualWikiName = WikiUtil.getVirtualWikiFromURI(request); if (!StringUtils.hasText(virtualWikiName)) { virtualWikiName = WikiBase.DEFAULT_VWIKI; } String topicName = Environment.getValue(Environment.PROP_BASE_DEFAULT_TOPIC); try { VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); topicName = virtualWiki.getDefaultTopicName(); } catch (Exception e) { logger.warning("Unable to retrieve default topic for virtual wiki", e); } targetUrl = request.getContextPath() + "/" + virtualWikiName + "/" + topicName; } else if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) { String virtualWiki = WikiUtil.getVirtualWikiFromURI(request); targetUrl = request.getContextPath() + "/" + virtualWiki + url; } response.sendRedirect(response.encodeRedirectURL(targetUrl)); }
/** * This method ensures that the left menu, logo, and other required values have been loaded into * the session object. * * @param request The servlet request object. * @param next A ModelAndView object corresponding to the page being constructed. */ private static void buildLayout(HttpServletRequest request, ModelAndView next) { String virtualWikiName = WikiUtil.getVirtualWikiFromURI(request); if (virtualWikiName == null) { logger.severe("No virtual wiki available for page request " + request.getRequestURI()); virtualWikiName = WikiBase.DEFAULT_VWIKI; } VirtualWiki virtualWiki = retrieveVirtualWiki(virtualWikiName); // build the layout contents String leftMenu = ServletUtil.cachedContent( request.getContextPath(), request.getLocale(), virtualWikiName, WikiBase.SPECIAL_PAGE_LEFT_MENU, true); next.addObject("leftMenu", leftMenu); next.addObject("defaultTopic", virtualWiki.getDefaultTopicName()); next.addObject("virtualWiki", virtualWiki.getName()); next.addObject("logo", Environment.getValue(Environment.PROP_BASE_LOGO_IMAGE)); String bottomArea = ServletUtil.cachedContent( request.getContextPath(), request.getLocale(), virtualWiki.getName(), WikiBase.SPECIAL_PAGE_BOTTOM_AREA, true); next.addObject("bottomArea", bottomArea); next.addObject(ServletUtil.PARAMETER_VIRTUAL_WIKI, virtualWiki.getName()); Integer cssRevision = new Integer(0); try { cssRevision = WikiBase.getDataHandler() .lookupTopic(virtualWiki.getName(), WikiBase.SPECIAL_PAGE_STYLESHEET, false, null) .getCurrentVersionId(); } catch (Exception e) { } next.addObject("cssRevision", cssRevision); }
private static void setupDefaultVirtualWiki(Connection conn) throws Exception { VirtualWiki virtualWiki = new VirtualWiki(); virtualWiki.setName(WikiBase.DEFAULT_VWIKI); virtualWiki.setDefaultTopicName(Environment.getValue(Environment.PROP_BASE_DEFAULT_TOPIC)); WikiBase.getDataHandler().writeVirtualWiki(virtualWiki, conn); }