Add AJAX handler to get a rendered post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 25 Nov 2010 11:18:24 +0000 (12:18 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 25 Nov 2010 11:18:24 +0000 (12:18 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java [new file with mode: 0644]

index 67a4fdb..d22e169 100644 (file)
@@ -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 (file)
index 0000000..b0d8ec5
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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;
+       }
+
+}