Make Sone returned by a Sone provider optional.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetPostAjaxPage.java
1 /*
2  * Sone - GetPostAjaxPage.java - Copyright © 2010–2013 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 com.google.common.base.Optional;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.web.WebInterface;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.io.Closer;
29 import net.pterodactylus.util.json.JsonObject;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
32 import net.pterodactylus.util.template.TemplateException;
33
34 /**
35  * This AJAX handler retrieves information and rendered representation of a
36  * {@link Post}.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class GetPostAjaxPage extends JsonPage {
41
42         /** The template to render for posts. */
43         private final Template postTemplate;
44
45         /**
46          * Creates a new “get post” AJAX handler.
47          *
48          * @param webInterface
49          *            The Sone web interface
50          * @param postTemplate
51          *            The template to render for posts
52          */
53         public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) {
54                 super("getPost.ajax", webInterface);
55                 this.postTemplate = postTemplate;
56         }
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         protected JsonObject createJsonObject(FreenetRequest request) {
63                 String postId = request.getHttpRequest().getParam("post");
64                 Optional<Post> post = webInterface.getCore().getPost(postId);
65                 if (!post.isPresent()) {
66                         return createErrorJsonObject("invalid-post-id");
67                 }
68                 return createSuccessJsonObject().put("post", createJsonPost(request, post.get(), getCurrentSone(request.getToadletContext())));
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         protected boolean needsFormPassword() {
76                 return false;
77         }
78
79         //
80         // PRIVATE METHODS
81         //
82
83         /**
84          * Creates a JSON object from the given post. The JSON object will only
85          * contain the ID of the post, its time, and its rendered HTML code.
86          *
87          * @param request
88          *            The request being processed
89          * @param post
90          *            The post to create a JSON object from
91          * @param currentSone
92          *            The currently logged in Sone (to store in the template)
93          * @return The JSON representation of the post
94          */
95         private JsonObject createJsonPost(FreenetRequest request, Post post, Sone currentSone) {
96                 JsonObject jsonPost = new JsonObject();
97                 jsonPost.put("id", post.getId());
98                 jsonPost.put("sone", post.getSone().getId());
99                 jsonPost.put("recipient", post.getRecipientId().orNull());
100                 jsonPost.put("time", post.getTime());
101                 StringWriter stringWriter = new StringWriter();
102                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
103                 templateContext.set("core", webInterface.getCore());
104                 templateContext.set("request", request);
105                 templateContext.set("post", post);
106                 templateContext.set("currentSone", currentSone);
107                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
108                 try {
109                         postTemplate.render(templateContext, stringWriter);
110                 } catch (TemplateException te1) {
111                         /* TODO - shouldn’t happen. */
112                 } finally {
113                         Closer.close(stringWriter);
114                 }
115                 jsonPost.put("html", stringWriter.toString());
116                 return jsonPost;
117         }
118
119 }