251c78432bf2f99628b86f33cde1ea03bc829c53
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetReplyAjaxPage.java
1 /*
2  * Sone - GetReplyAjaxPage.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 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
22
23 import com.google.common.base.Optional;
24
25 import net.pterodactylus.sone.data.PostReply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.web.WebInterface;
28 import net.pterodactylus.sone.web.page.FreenetRequest;
29 import net.pterodactylus.util.io.Closer;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
32 import net.pterodactylus.util.template.TemplateException;
33
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.node.ObjectNode;
36
37 /**
38  * This AJAX page returns the details of a reply.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class GetReplyAjaxPage extends JsonPage {
43
44         /** The template to render. */
45         private final Template replyTemplate;
46
47         /**
48          * Creates a new “get reply” page.
49          *
50          * @param webInterface
51          *            The Sone web interface
52          * @param replyTemplate
53          *            The template to render
54          */
55         public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
56                 super("getReply.ajax", webInterface);
57                 this.replyTemplate = replyTemplate;
58         }
59
60         //
61         // JSONPAGE METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         protected JsonReturnObject createJsonObject(FreenetRequest request) {
69                 String replyId = request.getHttpRequest().getParam("reply");
70                 Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
71                 if (!reply.isPresent()) {
72                         return createErrorJsonObject("invalid-reply-id");
73                 }
74                 return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext())));
75         }
76
77         /**
78          * {@inheritDoc}
79          */
80         @Override
81         protected boolean needsFormPassword() {
82                 return false;
83         }
84
85         //
86         // PRIVATE METHODS
87         //
88
89         /**
90          * Creates a JSON representation of the given reply.
91          *
92          * @param request
93          *            The request being processed
94          * @param reply
95          *            The reply to convert
96          * @param currentSone
97          *            The currently logged in Sone (to store in the template)
98          * @return The JSON representation of the reply
99          */
100         private JsonNode createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
101                 ObjectNode jsonReply = new ObjectNode(instance);
102                 jsonReply.put("id", reply.getId());
103                 jsonReply.put("postId", reply.getPostId());
104                 jsonReply.put("soneId", reply.getSone().getId());
105                 jsonReply.put("time", reply.getTime());
106                 StringWriter stringWriter = new StringWriter();
107                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
108                 templateContext.set("core", webInterface.getCore());
109                 templateContext.set("request", request);
110                 templateContext.set("reply", reply);
111                 templateContext.set("currentSone", currentSone);
112                 try {
113                         replyTemplate.render(templateContext, stringWriter);
114                 } catch (TemplateException te1) {
115                         /* TODO - shouldn’t happen. */
116                 } finally {
117                         Closer.close(stringWriter);
118                 }
119                 return jsonReply.put("html", stringWriter.toString());
120         }
121
122 }