2 * Sone - JsonPage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
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;
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.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public abstract class JsonPage implements Page {
36 /** The path of the page. */
37 private final String path;
39 /** The Sone web interface. */
40 protected final WebInterface webInterface;
43 * Creates a new JSON page at the given path.
46 * The path of the page
48 * The Sone web interface
50 public JsonPage(String path, WebInterface webInterface) {
52 this.webInterface = webInterface;
60 * Returns the current session, creating a new session if there is no
63 * @param toadletContenxt
65 * @return The current session, or {@code null} if there is no current
68 protected Session getCurrentSession(ToadletContext toadletContenxt) {
69 return webInterface.getCurrentSession(toadletContenxt);
73 * Returns the current session, creating a new session if there is no
74 * current session and {@code create} is {@code true}.
76 * @param toadletContenxt
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
84 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
85 return webInterface.getCurrentSession(toadletContenxt, create);
89 * Returns the currently logged in Sone.
91 * @param toadletContext
93 * @return The currently logged in Sone, or {@code null} if no Sone is
96 protected Sone getCurrentSone(ToadletContext toadletContext) {
97 return webInterface.getCurrentSone(toadletContext);
101 * Returns the currently logged in Sone.
103 * @param toadletContext
104 * The toadlet context
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
111 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
112 return webInterface.getCurrentSone(toadletContext, create);
116 // METHODS FOR SUBCLASSES TO OVERRIDE
120 * This method is called to create the JSON object that is returned back to
124 * The request to handle
125 * @return The created JSON object
127 protected abstract JsonObject createJsonObject(Request request);
130 * Returns whether this command needs the form password for authentication
131 * and to prevent abuse.
133 * @return {@code true} if the form password (given as “formPassword”) is
134 * required, {@code false} otherwise
136 protected boolean needsFormPassword() {
141 * Returns whether this page requires the user to be logged in.
143 * @return {@code true} if the user needs to be logged in to use this page,
144 * {@code false} otherwise
146 protected boolean requiresLogin() {
155 * Creates a success reply.
157 * @return A reply signaling success
159 protected JsonObject createSuccessJsonObject() {
160 return new JsonObject().put("success", true);
164 * Creates an error reply.
167 * The error that has occured
168 * @return The JSON object, signalling failure and the error code
170 protected JsonObject createErrorJsonObject(String error) {
171 return new JsonObject().put("success", false).put("error", error);
182 public String getPath() {
190 public Response handleRequest(Request request) {
191 if (needsFormPassword()) {
192 String formPassword = request.getHttpRequest().getParam("formPassword");
193 if (!webInterface.getFormPassword().equals(formPassword)) {
194 return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
197 if (requiresLogin()) {
198 if (getCurrentSone(request.getToadletContext(), false) == null) {
199 return new Response(401, "Not authorized", "application/json", JsonUtils.format(createErrorJsonObject("auth-required")));
202 JsonObject jsonObject = createJsonObject(request);
203 return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));