Merge branch 'release-0.6.4'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonPage.java
1 /*
2  * Sone - JsonPage.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 net.pterodactylus.sone.data.Sone;
21 import net.pterodactylus.sone.web.WebInterface;
22 import net.pterodactylus.sone.web.page.Page;
23 import net.pterodactylus.util.json.JsonObject;
24 import net.pterodactylus.util.json.JsonUtils;
25 import freenet.clients.http.SessionManager.Session;
26 import freenet.clients.http.ToadletContext;
27
28 /**
29  * A JSON page is a specialized {@link Page} that will always return a JSON
30  * object to the browser, e.g. for use with AJAX or other scripting frameworks.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public abstract class JsonPage implements Page {
35
36         /** The path of the page. */
37         private final String path;
38
39         /** The Sone web interface. */
40         protected final WebInterface webInterface;
41
42         /**
43          * Creates a new JSON page at the given path.
44          *
45          * @param path
46          *            The path of the page
47          * @param webInterface
48          *            The Sone web interface
49          */
50         public JsonPage(String path, WebInterface webInterface) {
51                 this.path = path;
52                 this.webInterface = webInterface;
53         }
54
55         //
56         // ACCESSORS
57         //
58
59         /**
60          * Returns the current session, creating a new session if there is no
61          * current session.
62          *
63          * @param toadletContenxt
64          *            The toadlet context
65          * @return The current session, or {@code null} if there is no current
66          *         session
67          */
68         protected Session getCurrentSession(ToadletContext toadletContenxt) {
69                 return webInterface.getCurrentSession(toadletContenxt);
70         }
71
72         /**
73          * Returns the current session, creating a new session if there is no
74          * current session and {@code create} is {@code true}.
75          *
76          * @param toadletContenxt
77          *            The toadlet context
78          * @param create
79          *            {@code true} to create a new session if there is no current
80          *            session, {@code false} otherwise
81          * @return The current session, or {@code null} if there is no current
82          *         session
83          */
84         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
85                 return webInterface.getCurrentSession(toadletContenxt, create);
86         }
87
88         /**
89          * Returns the currently logged in Sone.
90          *
91          * @param toadletContext
92          *            The toadlet context
93          * @return The currently logged in Sone, or {@code null} if no Sone is
94          *         currently logged in
95          */
96         protected Sone getCurrentSone(ToadletContext toadletContext) {
97                 return webInterface.getCurrentSone(toadletContext);
98         }
99
100         /**
101          * Returns the currently logged in Sone.
102          *
103          * @param toadletContext
104          *            The toadlet context
105          * @param create
106          *            {@code true} to create a new session if no session exists,
107          *            {@code false} to not create a new session
108          * @return The currently logged in Sone, or {@code null} if no Sone is
109          *         currently logged in
110          */
111         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
112                 return webInterface.getCurrentSone(toadletContext, create);
113         }
114
115         //
116         // METHODS FOR SUBCLASSES TO OVERRIDE
117         //
118
119         /**
120          * This method is called to create the JSON object that is returned back to
121          * the browser.
122          *
123          * @param request
124          *            The request to handle
125          * @return The created JSON object
126          */
127         protected abstract JsonObject createJsonObject(Request request);
128
129         /**
130          * Returns whether this command needs the form password for authentication
131          * and to prevent abuse.
132          *
133          * @return {@code true} if the form password (given as “formPassword”) is
134          *         required, {@code false} otherwise
135          */
136         protected boolean needsFormPassword() {
137                 return true;
138         }
139
140         /**
141          * Returns whether this page requires the user to be logged in.
142          *
143          * @return {@code true} if the user needs to be logged in to use this page,
144          *         {@code false} otherwise
145          */
146         protected boolean requiresLogin() {
147                 return true;
148         }
149
150         //
151         // PROTECTED METHODS
152         //
153
154         /**
155          * Creates a success reply.
156          *
157          * @return A reply signaling success
158          */
159         protected JsonObject createSuccessJsonObject() {
160                 return new JsonObject().put("success", true);
161         }
162
163         /**
164          * Creates an error reply.
165          *
166          * @param error
167          *            The error that has occured
168          * @return The JSON object, signalling failure and the error code
169          */
170         protected JsonObject createErrorJsonObject(String error) {
171                 return new JsonObject().put("success", false).put("error", error);
172         }
173
174         //
175         // PAGE METHODS
176         //
177
178         /**
179          * {@inheritDoc}
180          */
181         @Override
182         public String getPath() {
183                 return path;
184         }
185
186         /**
187          * {@inheritDoc}
188          */
189         @Override
190         public Response handleRequest(Request request) {
191                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
192                         return new Response(403, "Forbidden", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
193                 }
194                 if (needsFormPassword()) {
195                         String formPassword = request.getHttpRequest().getParam("formPassword");
196                         if (!webInterface.getFormPassword().equals(formPassword)) {
197                                 return new Response(403, "Forbidden", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
198                         }
199                 }
200                 if (requiresLogin()) {
201                         if (getCurrentSone(request.getToadletContext(), false) == null) {
202                                 return new Response(403, "Forbidden", "application/json", JsonUtils.format(createErrorJsonObject("auth-required")));
203                         }
204                 }
205                 JsonObject jsonObject = createJsonObject(request);
206                 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));
207         }
208
209 }