Fix logging of exception.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetReplyAjaxPage.java
1 /*
2  * Sone - GetReplyAjaxPage.java - Copyright © 2010–2012 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.PostReply;
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 page returns the details of a reply.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class GetReplyAjaxPage extends JsonPage {
38
39         /** The template to render. */
40         private final Template replyTemplate;
41
42         /**
43          * Creates a new “get reply” page.
44          *
45          * @param webInterface
46          *            The Sone web interface
47          * @param replyTemplate
48          *            The template to render
49          */
50         public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
51                 super("getReply.ajax", webInterface);
52                 this.replyTemplate = replyTemplate;
53         }
54
55         //
56         // JSONPAGE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected JsonObject createJsonObject(FreenetRequest request) {
64                 String replyId = request.getHttpRequest().getParam("reply");
65                 PostReply reply = webInterface.getCore().getReply(replyId);
66                 if ((reply == null) || (reply.getSone() == null)) {
67                         return createErrorJsonObject("invalid-reply-id");
68                 }
69                 return createSuccessJsonObject().put("reply", createJsonReply(request, reply, getCurrentSone(request.getToadletContext())));
70         }
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         protected boolean needsFormPassword() {
77                 return false;
78         }
79
80         //
81         // PRIVATE METHODS
82         //
83
84         /**
85          * Creates a JSON representation of the given reply.
86          *
87          * @param request
88          *            The request being processed
89          * @param reply
90          *            The reply to convert
91          * @param currentSone
92          *            The currently logged in Sone (to store in the template)
93          * @return The JSON representation of the reply
94          */
95         private JsonObject createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
96                 JsonObject jsonReply = new JsonObject();
97                 jsonReply.put("id", reply.getId());
98                 jsonReply.put("postId", reply.getPost().getId());
99                 jsonReply.put("soneId", reply.getSone().getId());
100                 jsonReply.put("time", reply.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("reply", reply);
106                 templateContext.set("currentSone", currentSone);
107                 try {
108                         replyTemplate.render(templateContext, stringWriter);
109                 } catch (TemplateException te1) {
110                         /* TODO - shouldn’t happen. */
111                 } finally {
112                         Closer.close(stringWriter);
113                 }
114                 return jsonReply.put("html", stringWriter.toString());
115         }
116
117 }