8be816da6d59fd337dc0fac7494da3e235554fe5
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetPostAjaxPage.java
1 /*
2  * Sone - GetPostAjaxPage.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
20 import java.io.StringWriter;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.web.WebInterface;
25 import net.pterodactylus.sone.web.page.FreenetRequest;
26 import net.pterodactylus.util.io.Closer;
27 import net.pterodactylus.util.json.JsonObject;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.template.TemplateException;
31
32 /**
33  * This AJAX handler retrieves information and rendered representation of a
34  * {@link Post}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class GetPostAjaxPage extends JsonPage {
39
40         /** The template to render for posts. */
41         private final Template postTemplate;
42
43         /**
44          * Creates a new “get post” AJAX handler.
45          *
46          * @param webInterface
47          *            The Sone web interface
48          * @param postTemplate
49          *            The template to render for posts
50          */
51         public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) {
52                 super("getPost.ajax", webInterface);
53                 this.postTemplate = postTemplate;
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
60         protected JsonObject createJsonObject(FreenetRequest request) {
61                 String postId = request.getHttpRequest().getParam("post");
62                 Post post = webInterface.getCore().getPost(postId, false);
63                 if (post == null) {
64                         return createErrorJsonObject("invalid-post-id");
65                 }
66                 return createSuccessJsonObject().put("post", createJsonPost(request, post, getCurrentSone(request.getToadletContext())));
67         }
68
69         /**
70          * {@inheritDoc}
71          */
72         @Override
73         protected boolean needsFormPassword() {
74                 return false;
75         }
76
77         //
78         // PRIVATE METHODS
79         //
80
81         /**
82          * Creates a JSON object from the given post. The JSON object will only
83          * contain the ID of the post, its time, and its rendered HTML code.
84          *
85          * @param request
86          *            The request being processed
87          * @param post
88          *            The post to create a JSON object from
89          * @param currentSone
90          *            The currently logged in Sone (to store in the template)
91          * @return The JSON representation of the post
92          */
93         private JsonObject createJsonPost(FreenetRequest request, Post post, Sone currentSone) {
94                 JsonObject jsonPost = new JsonObject();
95                 jsonPost.put("id", post.getId());
96                 jsonPost.put("sone", post.getSone().getId());
97                 jsonPost.put("recipient", (post.getRecipient() == null) ? null : post.getRecipient().getId());
98                 jsonPost.put("time", post.getTime());
99                 StringWriter stringWriter = new StringWriter();
100                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
101                 templateContext.set("core", webInterface.getCore());
102                 templateContext.set("request", request);
103                 templateContext.set("post", post);
104                 templateContext.set("currentSone", currentSone);
105                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
106                 try {
107                         postTemplate.render(templateContext, stringWriter);
108                 } catch (TemplateException te1) {
109                         /* TODO - shouldn’t happen. */
110                 } finally {
111                         Closer.close(stringWriter);
112                 }
113                 jsonPost.put("html", stringWriter.toString());
114                 return jsonPost;
115         }
116
117 }