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;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.web.WebInterface;
25 import net.pterodactylus.sone.web.page.FreenetPage;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import net.pterodactylus.util.json.JsonObject;
28 import net.pterodactylus.util.json.JsonUtils;
29 import net.pterodactylus.util.web.Page;
30 import net.pterodactylus.util.web.Response;
31 import freenet.clients.http.ToadletContext;
32 import freenet.clients.http.SessionManager.Session;
35 * A JSON page is a specialized {@link Page} that will always return a JSON
36 * object to the browser, e.g. for use with AJAX or other scripting frameworks.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public abstract class JsonPage implements FreenetPage {
42 /** The path of the page. */
43 private final String path;
45 /** The Sone web interface. */
46 protected final WebInterface webInterface;
49 * Creates a new JSON page at the given path.
52 * The path of the page
54 * The Sone web interface
56 public JsonPage(String path, WebInterface webInterface) {
58 this.webInterface = webInterface;
66 * Returns the current session, creating a new session if there is no
69 * @param toadletContenxt
71 * @return The current session, or {@code null} if there is no current
74 protected Session getCurrentSession(ToadletContext toadletContenxt) {
75 return webInterface.getCurrentSession(toadletContenxt);
79 * Returns the current session, creating a new session if there is no
80 * current session and {@code create} is {@code true}.
82 * @param toadletContenxt
85 * {@code true} to create a new session if there is no current
86 * session, {@code false} otherwise
87 * @return The current session, or {@code null} if there is no current
90 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
91 return webInterface.getCurrentSession(toadletContenxt, create);
95 * Returns the currently logged in Sone.
97 * @param toadletContext
99 * @return The currently logged in Sone, or {@code null} if no Sone is
100 * currently logged in
102 protected Sone getCurrentSone(ToadletContext toadletContext) {
103 return webInterface.getCurrentSone(toadletContext);
107 * Returns the currently logged in Sone.
109 * @param toadletContext
110 * The toadlet context
112 * {@code true} to create a new session if no session exists,
113 * {@code false} to not create a new session
114 * @return The currently logged in Sone, or {@code null} if no Sone is
115 * currently logged in
117 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
118 return webInterface.getCurrentSone(toadletContext, create);
122 // METHODS FOR SUBCLASSES TO OVERRIDE
126 * This method is called to create the JSON object that is returned back to
130 * The request to handle
131 * @return The created JSON object
133 protected abstract JsonObject createJsonObject(FreenetRequest request);
136 * Returns whether this command needs the form password for authentication
137 * and to prevent abuse.
139 * @return {@code true} if the form password (given as “formPassword”) is
140 * required, {@code false} otherwise
142 protected boolean needsFormPassword() {
147 * Returns whether this page requires the user to be logged in.
149 * @return {@code true} if the user needs to be logged in to use this page,
150 * {@code false} otherwise
152 protected boolean requiresLogin() {
161 * Creates a success reply.
163 * @return A reply signaling success
165 protected JsonObject createSuccessJsonObject() {
166 return new JsonObject().put("success", true);
170 * Creates an error reply.
173 * The error that has occured
174 * @return The JSON object, signalling failure and the error code
176 protected JsonObject createErrorJsonObject(String error) {
177 return new JsonObject().put("success", false).put("error", error);
188 public String getPath() {
196 public boolean isPrefixPage() {
204 public Response handleRequest(FreenetRequest request, Response response) throws IOException {
205 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
206 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
208 if (needsFormPassword()) {
209 String formPassword = request.getHttpRequest().getParam("formPassword");
210 if (!webInterface.getFormPassword().equals(formPassword)) {
211 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
214 if (requiresLogin()) {
215 if (getCurrentSone(request.getToadletContext(), false) == null) {
216 return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
219 JsonObject jsonObject = createJsonObject(request);
220 return response.setStatusCode(200).setStatusText("OK").setContentType("application/json").write(JsonUtils.format(jsonObject));
227 public boolean isLinkExcepted(URI link) {