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 java.io.IOException;
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.web.WebInterface;
24 import net.pterodactylus.sone.web.page.FreenetRequest;
25 import net.pterodactylus.util.json.JsonObject;
26 import net.pterodactylus.util.json.JsonUtils;
27 import net.pterodactylus.util.web.Page;
28 import net.pterodactylus.util.web.Response;
29 import freenet.clients.http.SessionManager.Session;
30 import freenet.clients.http.ToadletContext;
33 * A JSON page is a specialized {@link Page} that will always return a JSON
34 * object to the browser, e.g. for use with AJAX or other scripting frameworks.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public abstract class JsonPage implements Page<FreenetRequest> {
40 /** The path of the page. */
41 private final String path;
43 /** The Sone web interface. */
44 protected final WebInterface webInterface;
47 * Creates a new JSON page at the given path.
50 * The path of the page
52 * The Sone web interface
54 public JsonPage(String path, WebInterface webInterface) {
56 this.webInterface = webInterface;
64 * Returns the current session, creating a new session if there is no
67 * @param toadletContenxt
69 * @return The current session, or {@code null} if there is no current
72 protected Session getCurrentSession(ToadletContext toadletContenxt) {
73 return webInterface.getCurrentSession(toadletContenxt);
77 * Returns the current session, creating a new session if there is no
78 * current session and {@code create} is {@code true}.
80 * @param toadletContenxt
83 * {@code true} to create a new session if there is no current
84 * session, {@code false} otherwise
85 * @return The current session, or {@code null} if there is no current
88 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
89 return webInterface.getCurrentSession(toadletContenxt, create);
93 * Returns the currently logged in Sone.
95 * @param toadletContext
97 * @return The currently logged in Sone, or {@code null} if no Sone is
100 protected Sone getCurrentSone(ToadletContext toadletContext) {
101 return webInterface.getCurrentSone(toadletContext);
105 * Returns the currently logged in Sone.
107 * @param toadletContext
108 * The toadlet context
110 * {@code true} to create a new session if no session exists,
111 * {@code false} to not create a new session
112 * @return The currently logged in Sone, or {@code null} if no Sone is
113 * currently logged in
115 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
116 return webInterface.getCurrentSone(toadletContext, create);
120 // METHODS FOR SUBCLASSES TO OVERRIDE
124 * This method is called to create the JSON object that is returned back to
128 * The request to handle
129 * @return The created JSON object
131 protected abstract JsonObject createJsonObject(FreenetRequest request);
134 * Returns whether this command needs the form password for authentication
135 * and to prevent abuse.
137 * @return {@code true} if the form password (given as “formPassword”) is
138 * required, {@code false} otherwise
140 protected boolean needsFormPassword() {
145 * Returns whether this page requires the user to be logged in.
147 * @return {@code true} if the user needs to be logged in to use this page,
148 * {@code false} otherwise
150 protected boolean requiresLogin() {
159 * Creates a success reply.
161 * @return A reply signaling success
163 protected JsonObject createSuccessJsonObject() {
164 return new JsonObject().put("success", true);
168 * Creates an error reply.
171 * The error that has occured
172 * @return The JSON object, signalling failure and the error code
174 protected JsonObject createErrorJsonObject(String error) {
175 return new JsonObject().put("success", false).put("error", error);
186 public String getPath() {
194 public boolean isPrefixPage() {
202 public Response handleRequest(FreenetRequest request, Response response) throws IOException {
203 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
204 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
206 if (needsFormPassword()) {
207 String formPassword = request.getHttpRequest().getParam("formPassword");
208 if (!webInterface.getFormPassword().equals(formPassword)) {
209 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
212 if (requiresLogin()) {
213 if (getCurrentSone(request.getToadletContext(), false) == null) {
214 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
217 JsonObject jsonObject = createJsonObject(request);
218 return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(JsonUtils.format(jsonObject));