public Object handlePostLike(Request request, Response response) { String permalink = request.queryParams("permalink"); String commentOrdinalStr = request.queryParams("comment_ordinal"); // look up the post in question int ordinal = Integer.parseInt(commentOrdinalStr); // TODO: check return or have checkSession throw String username = sessionDAO.findUserNameBySessionId(request.cookie("session")); Document post = blogPostDAO.findByPermalink(permalink); // if post not found, redirect to post not found error if (post == null) { response.redirect("/post_not_found"); } else { blogPostDAO.likePost(permalink, ordinal); response.redirect("/post/" + permalink); } SimpleHash root = new SimpleHash(); return templateEngine.render(new ModelAndView(root, "entry_template.ftl")); }
/** * @param request The request object providing information about the HTTP request * @param response The response object providing functionality for modifying the response * @return The content to be set in the response * @throws Exception implementation can choose to throw exception */ public String handlePostNewPost(Request request, Response response) throws Exception { String title = StringEscapeUtils.escapeHtml4(request.queryParams("subject")); String post = StringEscapeUtils.escapeHtml4(request.queryParams("body")); String tags = StringEscapeUtils.escapeHtml4(request.queryParams("tags")); String username = sessionDAO.findUserNameBySessionId(request.cookie("session")); if (username == null) { response.redirect("/login"); // only logged in users can post to blog return templateEngine.render(new ModelAndView(null, "redirect.ftl")); } else if (title.equals("") || post.equals("")) { SimpleHash root = new SimpleHash(); // redisplay page with errors root.put("errors", "post must contain a title and blog entry."); root.put("subject", title); root.put("username", username); root.put("tags", tags); root.put("body", post); return templateEngine.render(new ModelAndView(root, "newpost_template.ftl")); } else { // extract tags List<String> tagsArray = extractTags(tags); // substitute some <p> for the paragraph breaks post = post.replaceAll("\\r?\\n", "<p>"); String permalink = blogPostDAO.addPost(title, post, tagsArray, username); // now redirect to the blog permalink response.redirect("/post/" + permalink); return templateEngine.render(new ModelAndView(null, "redirect.ftl")); } }
/** * This is where we would normally load up the blog data but this week, we just display a * placeholder. * * @param request The request object providing information about the HTTP request * @param response The response object providing functionality for modifying the response * @return The content to be set in the response * @throws Exception implementation can choose to throw exception */ public String handleGetRoot(Request request, Response response) throws Exception { SimpleHash root = new SimpleHash(); String username = sessionDAO.findUserNameBySessionId(request.cookie("session")); List<Document> posts = blogPostDAO.findByDateDescending(10); root.put("myposts", posts); if (username != null) { root.put("username", username); } return templateEngine.render(new ModelAndView(root, "blog_template.ftl")); }
/** * @param request The request object providing information about the HTTP request * @param response The response object providing functionality for modifying the response * @return The content to be set in the response * @throws Exception implementation can choose to throw exception */ public String handleGetNewPost(Request request, Response response) throws Exception { // get cookie String username = sessionDAO.findUserNameBySessionId(request.cookie("session")); if (username == null) { // looks like a bad request. user is not logged in response.redirect("/login"); return templateEngine.render(new ModelAndView(null, "redirect.ftl")); } else { SimpleHash root = new SimpleHash(); root.put("username", username); return templateEngine.render(new ModelAndView(root, "newpost_template.ftl")); } }
/** * @param request The request object providing information about the HTTP request * @param response The response object providing functionality for modifying the response * @return The content to be set in the response * @throws Exception implementation can choose to throw exception */ public String handleGetByTag(Request request, Response response) throws Exception { String username = sessionDAO.findUserNameBySessionId(request.cookie("session")); SimpleHash root = new SimpleHash(); String tag = StringEscapeUtils.escapeHtml4(request.params(":thetag")); List<Document> posts = blogPostDAO.findByTagDateDescending(tag); root.put("myposts", posts); if (username != null) { root.put("username", username); } return templateEngine.render(new ModelAndView(root, "blog_template.ftl")); }