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