From cc44d978793786438495d308185b3193a2cff062 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 25 Nov 2010 12:18:24 +0100 Subject: [PATCH] Add AJAX handler to get a rendered post. --- .../net/pterodactylus/sone/web/WebInterface.java | 2 + .../sone/web/ajax/GetPostAjaxPage.java | 104 +++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 67a4fdb..d22e169 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -55,6 +55,7 @@ import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage; import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage; import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage; import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage; +import net.pterodactylus.sone.web.ajax.GetPostAjaxPage; import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage; import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage; import net.pterodactylus.sone.web.ajax.GetTranslationPage; @@ -358,6 +359,7 @@ public class WebInterface implements CoreListener { pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this))); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java new file mode 100644 index 0000000..b0d8ec5 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java @@ -0,0 +1,104 @@ +/* + * Sone - GetPostAjaxPage.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.ajax; + +import java.io.StringWriter; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.json.JsonObject; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateException; + +/** + * This AJAX handler retrieves information and rendered representation of a + * {@link Post}. + * + * @author David ‘Bombe’ Roden + */ +public class GetPostAjaxPage extends JsonPage { + + /** The template to render for posts. */ + private final Template postTemplate; + + /** + * Creates a new “get post” AJAX handler. + * + * @param webInterface + * The Sone web interface + * @param postTemplate + * The template to render for posts + */ + public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) { + super("ajax/getPost.ajax", webInterface); + this.postTemplate = postTemplate; + } + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(Request request) { + String postId = request.getHttpRequest().getParam("post"); + Post post = webInterface.getCore().getPost(postId, false); + if (post == null) { + return createErrorJsonObject("invalid-post-id"); + } + postTemplate.set("currentSone", getCurrentSone(request.getToadletContext())); + return createSuccessJsonObject().put("post", createJsonPost(post)); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean needsFormPassword() { + return false; + } + + // + // PRIVATE METHODS + // + + /** + * Creates a JSON object from the given post. The JSON object will only + * contain the ID of the post, its time, and its rendered HTML code. + * + * @param post + * The post to create a JSON object from + * @return The JSON representation of the post + */ + private JsonObject createJsonPost(Post post) { + JsonObject jsonPost = new JsonObject(); + jsonPost.put("id", post.getId()); + jsonPost.put("time", post.getTime()); + postTemplate.set("post", post); + StringWriter stringWriter = new StringWriter(); + try { + postTemplate.render(stringWriter); + } catch (TemplateException te1) { + /* TODO - shouldn’t happen. */ + } finally { + Closer.close(stringWriter); + } + jsonPost.put("html", stringWriter.toString()); + return jsonPost; + } + +} -- 2.7.4